126 lines
2.3 KiB
Rust
126 lines
2.3 KiB
Rust
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
|
|
|
use crate::Vec2;
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Point {
|
|
pub x: usize,
|
|
pub y: usize,
|
|
}
|
|
|
|
impl Point {
|
|
/// The point (0, 0).
|
|
pub const ZERO: Point = Point::new(0, 0);
|
|
|
|
/// The point at the origin; (0, 0).
|
|
pub const ORIGIN: Point = Point::new(0, 0);
|
|
|
|
/// Create a new `Point` with the provided `x` and `y` coordinates.
|
|
#[inline]
|
|
pub const fn new(x: usize, y: usize) -> Self {
|
|
Point { x, y }
|
|
}
|
|
|
|
/// Convert this point into a `Vec2`.
|
|
#[inline]
|
|
pub const fn to_vec2(self) -> Vec2 {
|
|
Vec2::new(self.x, self.y)
|
|
}
|
|
}
|
|
|
|
impl From<(usize, usize)> for Point {
|
|
#[inline]
|
|
fn from(v: (usize, usize)) -> Point {
|
|
Point { x: v.0, y: v.1 }
|
|
}
|
|
}
|
|
|
|
impl From<Point> for (usize, usize) {
|
|
#[inline]
|
|
fn from(v: Point) -> (usize, usize) {
|
|
(v.x, v.y)
|
|
}
|
|
}
|
|
|
|
impl Add<Vec2> for Point {
|
|
type Output = Point;
|
|
|
|
#[inline]
|
|
fn add(self, other: Vec2) -> Self {
|
|
Point::new(self.x + other.x, self.y + other.y)
|
|
}
|
|
}
|
|
|
|
impl AddAssign<Vec2> for Point {
|
|
#[inline]
|
|
fn add_assign(&mut self, other: Vec2) {
|
|
*self = Point::new(self.x + other.x, self.y + other.y)
|
|
}
|
|
}
|
|
|
|
impl Sub<Vec2> for Point {
|
|
type Output = Point;
|
|
|
|
#[inline]
|
|
fn sub(self, other: Vec2) -> Self {
|
|
Point::new(self.x - other.x, self.y - other.y)
|
|
}
|
|
}
|
|
|
|
impl SubAssign<Vec2> for Point {
|
|
#[inline]
|
|
fn sub_assign(&mut self, other: Vec2) {
|
|
*self = Point::new(self.x - other.x, self.y - other.y)
|
|
}
|
|
}
|
|
|
|
impl Add<(usize, usize)> for Point {
|
|
type Output = Point;
|
|
|
|
#[inline]
|
|
fn add(self, (x, y): (usize, usize)) -> Self {
|
|
Point::new(self.x + x, self.y + y)
|
|
}
|
|
}
|
|
|
|
impl Add for Point {
|
|
type Output = Point;
|
|
|
|
#[inline]
|
|
fn add(self, Self { x, y }: Self) -> Self {
|
|
Self::new(self.x + x, self.y + y)
|
|
}
|
|
}
|
|
|
|
impl AddAssign<(usize, usize)> for Point {
|
|
#[inline]
|
|
fn add_assign(&mut self, (x, y): (usize, usize)) {
|
|
*self = Point::new(self.x + x, self.y + y)
|
|
}
|
|
}
|
|
|
|
impl Sub<(usize, usize)> for Point {
|
|
type Output = Point;
|
|
|
|
#[inline]
|
|
fn sub(self, (x, y): (usize, usize)) -> Self {
|
|
Point::new(self.x - x, self.y - y)
|
|
}
|
|
}
|
|
|
|
impl SubAssign<(usize, usize)> for Point {
|
|
#[inline]
|
|
fn sub_assign(&mut self, (x, y): (usize, usize)) {
|
|
*self = Point::new(self.x - x, self.y - y)
|
|
}
|
|
}
|
|
|
|
impl Sub<Point> for Point {
|
|
type Output = Vec2;
|
|
|
|
#[inline]
|
|
fn sub(self, other: Point) -> Vec2 {
|
|
Vec2::new(self.x - other.x, self.y - other.y)
|
|
}
|
|
}
|