impl __radd__

This commit is contained in:
shenjack 2023-03-30 00:51:52 +08:00
parent 7f3277fb5f
commit 373eec25c4
2 changed files with 59 additions and 8 deletions

View File

@ -39,6 +39,9 @@ pub mod vector {
fn round(&self, ndigits: Option<i64>) -> Self; fn round(&self, ndigits: Option<i64>) -> Self;
fn clamp(&self, min: f64, max: f64) -> Self; fn clamp(&self, min: f64, max: f64) -> Self;
fn distance(&self, other: &Self) -> f64; fn distance(&self, other: &Self) -> f64;
fn normalize(&self) -> Self;
// from
fn from_same(len: f64) -> Self;
} }
impl Add for Vector2 { impl Add for Vector2 {
@ -196,7 +199,20 @@ pub mod vector {
} }
fn distance(&self, other: &Self) -> f64 { fn distance(&self, other: &Self) -> f64 {
(self - other).abs() (*self - *other).abs()
}
fn normalize(&self) -> Self {
let d = self.abs();
return if d != 0.0 {
Self::new(self.x / d, self.y / d)
} else {
self.clone()
};
}
fn from_same(len: f64) -> Self {
Self::new(len, len)
} }
} }
@ -241,11 +257,28 @@ pub mod vector {
} }
fn clamp(&self, min: f64, max: f64) -> Self { fn clamp(&self, min: f64, max: f64) -> Self {
Self::new(self.x.clamp(min, max), self.y.clamp(min, max), self.z.clamp(min, max)) Self::new(
self.x.clamp(min, max),
self.y.clamp(min, max),
self.z.clamp(min, max),
)
} }
fn distance(&self, other: &Self) -> f64 { fn distance(&self, other: &Self) -> f64 {
(self - other).abs() (*self - *other).abs()
}
fn normalize(&self) -> Self {
let d = self.abs();
return if d != 0.0 {
Self::new(self.x / d, self.y / d, self.z / d)
} else {
self.clone()
};
}
fn from_same(len: f64) -> Self {
Self::new(len, len, len)
} }
} }
@ -297,11 +330,29 @@ pub mod vector {
} }
fn clamp(&self, min: f64, max: f64) -> Self { fn clamp(&self, min: f64, max: f64) -> Self {
Self::new(self.x.clamp(min, max), self.y.clamp(min, max), self.z.clamp(min, max), self.w.clamp(min, max)) Self::new(
self.x.clamp(min, max),
self.y.clamp(min, max),
self.z.clamp(min, max),
self.w.clamp(min, max),
)
} }
fn distance(&self, other: &Self) -> f64 { fn distance(&self, other: &Self) -> f64 {
(self - other).abs() (*self - *other).abs()
}
fn normalize(&self) -> Self {
let d = self.abs();
return if d != 0.0 {
Self::new(self.x / d, self.y / d, self.z / d, self.w / d)
} else {
self.clone()
};
}
fn from_same(len: f64) -> Self {
Self::new(len, len, len, len)
} }
} }

View File

@ -78,7 +78,7 @@ pub mod python_class {
} }
fn __radd__(&self, other: &PyAny) -> Self { fn __radd__(&self, other: &PyAny) -> Self {
return if other.is_instance_of::<PyVector2>() { return if other.is_instance_of::<PyVector2>().unwrap() {
Self { Self {
data: self.data + other.extract::<PyVector2>().unwrap().data, data: self.data + other.extract::<PyVector2>().unwrap().data,
} }
@ -88,10 +88,10 @@ pub mod python_class {
self.clone() self.clone()
} else { } else {
Self { Self {
data: self.data + other.extract::<f64>().unwrap(), data: self.data + Vector2::from_same(other.extract::<f64>().unwrap()),
} }
} }
} };
} }
fn __sub__(&self, other: &Self) -> Self { fn __sub__(&self, other: &Self) -> Self {