From 373eec25c49886cfee05ad164d6eba4bdecab003 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 30 Mar 2023 00:51:52 +0800 Subject: [PATCH] impl __radd__ --- libs/pyglet_rs/src/src/math.rs | 61 +++++++++++++++++++++++++++++--- libs/pyglet_rs/src/src/pymath.rs | 6 ++-- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/libs/pyglet_rs/src/src/math.rs b/libs/pyglet_rs/src/src/math.rs index f121ff6..09b6554 100644 --- a/libs/pyglet_rs/src/src/math.rs +++ b/libs/pyglet_rs/src/src/math.rs @@ -39,6 +39,9 @@ pub mod vector { fn round(&self, ndigits: Option) -> Self; fn clamp(&self, min: f64, max: f64) -> Self; fn distance(&self, other: &Self) -> f64; + fn normalize(&self) -> Self; + // from + fn from_same(len: f64) -> Self; } impl Add for Vector2 { @@ -196,7 +199,20 @@ pub mod vector { } 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 { - 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 { - (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 { - 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 { - (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) } } diff --git a/libs/pyglet_rs/src/src/pymath.rs b/libs/pyglet_rs/src/src/pymath.rs index bb68950..4ca3e55 100644 --- a/libs/pyglet_rs/src/src/pymath.rs +++ b/libs/pyglet_rs/src/src/pymath.rs @@ -78,7 +78,7 @@ pub mod python_class { } fn __radd__(&self, other: &PyAny) -> Self { - return if other.is_instance_of::() { + return if other.is_instance_of::().unwrap() { Self { data: self.data + other.extract::().unwrap().data, } @@ -88,10 +88,10 @@ pub mod python_class { self.clone() } else { Self { - data: self.data + other.extract::().unwrap(), + data: self.data + Vector2::from_same(other.extract::().unwrap()), } } - } + }; } fn __sub__(&self, other: &Self) -> Self {