You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.5 KiB
82 lines
1.5 KiB
use std::fmt::{self, Display, Formatter};
|
|
use std::ops::{Add, Mul, Sub, SubAssign};
|
|
|
|
/// Just a vector
|
|
#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
|
|
pub struct Vector2 {
|
|
pub x: f32,
|
|
pub y: f32,
|
|
}
|
|
|
|
impl Display for Vector2 {
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
write!(f, "[{}, {}]", self.x, self.y)
|
|
}
|
|
}
|
|
|
|
impl Vector2 {
|
|
pub fn new(x: f32, y: f32) -> Self {
|
|
Self { x, y }
|
|
}
|
|
|
|
pub fn abs(self) -> Self {
|
|
Self {
|
|
x: self.x.abs(),
|
|
y: self.y.abs(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Sub for Vector2 {
|
|
type Output = Vector2;
|
|
|
|
fn sub(self, o: Vector2) -> Self::Output {
|
|
Vector2 {
|
|
x: self.x - o.x,
|
|
y: self.y - o.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SubAssign for Vector2 {
|
|
fn sub_assign(&mut self, o: Self) {
|
|
*self = Self {
|
|
x: self.x - o.x,
|
|
y: self.y - o.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Add for Vector2 {
|
|
type Output = Vector2;
|
|
|
|
fn add(self, o: Vector2) -> Self::Output {
|
|
Vector2 {
|
|
x: self.x + o.x,
|
|
y: self.y + o.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Mul for Vector2 {
|
|
type Output = Vector2;
|
|
|
|
fn mul(self, o: Vector2) -> Self::Output {
|
|
Vector2 {
|
|
x: self.x * o.x,
|
|
y: self.y * o.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Vector2> for mint::Point2<f32> {
|
|
fn from(v: Vector2) -> Self {
|
|
[v.x, v.y].into()
|
|
}
|
|
}
|
|
|
|
impl From<mint::Point2<f32>> for Vector2 {
|
|
fn from(v: mint::Point2<f32>) -> Self {
|
|
Self { x: v.x, y: v.y }
|
|
}
|
|
}
|
|
|