impl dot clamp distance in Rust Vector

This commit is contained in:
shenjack 2023-03-30 00:37:20 +08:00
parent 391f3df7b8
commit 7f3277fb5f
2 changed files with 55 additions and 12 deletions

View File

@ -35,8 +35,10 @@ pub mod vector {
fn floordiv(&self, other: &Self) -> Self;
fn abs(&self) -> f64;
fn neg(&self) -> Self;
fn dot(&self, other: &Self) -> f64;
fn round(&self, ndigits: Option<i64>) -> Self;
fn radd(&self, other: &Self) -> Self;
fn clamp(&self, min: f64, max: f64) -> Self;
fn distance(&self, other: &Self) -> f64;
}
impl Add for Vector2 {
@ -172,6 +174,10 @@ pub mod vector {
Self::new(-self.x, -self.y)
}
fn dot(&self, other: &Self) -> f64 {
self.x * other.x + self.y * other.y
}
fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits {
Some(ndigits) => {
@ -185,8 +191,12 @@ pub mod vector {
}
}
fn radd(&self, other: &Self) -> Self {
Self::new(self.x + other.x, self.y + other.y)
fn clamp(&self, min: f64, max: f64) -> Self {
Self::new(self.x.clamp(min, max), self.y.clamp(min, max))
}
fn distance(&self, other: &Self) -> f64 {
(self - other).abs()
}
}
@ -212,6 +222,10 @@ pub mod vector {
Self::new(-self.x, -self.y, -self.z)
}
fn dot(&self, other: &Self) -> f64 {
self.x * other.x + self.y * other.y + self.z * other.z
}
fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits {
Some(ndigits) => {
@ -226,8 +240,12 @@ pub mod vector {
}
}
fn radd(&self, other: &Self) -> Self {
Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
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))
}
fn distance(&self, other: &Self) -> f64 {
(self - other).abs()
}
}
@ -254,6 +272,10 @@ pub mod vector {
Self::new(-self.x, -self.y, -self.z, -self.w)
}
fn dot(&self, other: &Self) -> f64 {
self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
}
fn round(&self, ndigits: Option<i64>) -> Self {
match ndigits {
Some(ndigits) => {
@ -274,13 +296,12 @@ pub mod vector {
}
}
fn radd(&self, other: &Self) -> Self {
Self::new(
self.x + other.x,
self.y + other.y,
self.z + other.z,
self.w + other.w,
)
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))
}
fn distance(&self, other: &Self) -> f64 {
(self - other).abs()
}
}

View File

@ -13,26 +13,31 @@ pub mod python_class {
use crate::math::matrix::{Matrix3, Matrix4};
use crate::math::vector::{Vector2, Vector3, Vector4, VectorTrait};
#[derive(Clone)]
#[pyclass(name = "Vector2_rs")]
pub struct PyVector2 {
pub data: Vector2,
}
#[derive(Clone)]
#[pyclass(name = "Vector3_rs")]
pub struct PyVector3 {
pub data: Vector3,
}
#[derive(Clone)]
#[pyclass(name = "Vector4_rs")]
pub struct PyVector4 {
pub data: Vector4,
}
#[derive(Clone)]
#[pyclass(name = "Matrix3_rs")]
pub struct PyMatrix3 {
pub data: Matrix3,
}
#[derive(Clone)]
#[pyclass(name = "Matrix4_rs")]
pub struct PyMatrix4 {
pub data: Matrix4,
@ -72,6 +77,23 @@ pub mod python_class {
};
}
fn __radd__(&self, other: &PyAny) -> Self {
return if other.is_instance_of::<PyVector2>() {
Self {
data: self.data + other.extract::<PyVector2>().unwrap().data,
}
} else {
// if other == 0
if other.is_none() {
self.clone()
} else {
Self {
data: self.data + other.extract::<f64>().unwrap(),
}
}
}
}
fn __sub__(&self, other: &Self) -> Self {
return Self {
data: self.data - other.data,