common/rust: add more functionality to Vec2
This commit is contained in:
parent
7896aafcd7
commit
d627f1d00f
1 changed files with 35 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
|||
use std::{
|
||||
ops::{Add, AddAssign, Sub, SubAssign},
|
||||
fmt,
|
||||
ops::{Add, AddAssign, Mul, Sub, SubAssign},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
|
@ -22,6 +23,17 @@ impl Vec2 {
|
|||
y: f(self.y),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn len(self) -> f64 {
|
||||
(f64::from(self.x).powi(2) + f64::from(self.y).powi(2)).sqrt()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Vec2 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", (self.x, self.y))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vec2 {
|
||||
|
@ -60,6 +72,28 @@ impl SubAssign for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Mul<i32> for Vec2 {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, n: i32) -> Self::Output {
|
||||
Vec2 {
|
||||
x: self.x * n,
|
||||
y: self.y * n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vec2> for i32 {
|
||||
type Output = Vec2;
|
||||
|
||||
fn mul(self, Vec2 { x, y }: Vec2) -> Self::Output {
|
||||
Vec2 {
|
||||
x: x * self,
|
||||
y: y * self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(i32, i32)> for Vec2 {
|
||||
fn from((x, y): (i32, i32)) -> Self {
|
||||
Self { x, y }
|
||||
|
|
Loading…
Reference in a new issue