From db87a1e570344968c600ef9936bb68af9d044bd0 Mon Sep 17 00:00:00 2001 From: LunaticWyrm <65457004+LunaticWyrm467@users.noreply.github.com> Date: Fri, 19 Jan 2024 13:45:17 +0000 Subject: [PATCH] Readied project for Vec3/Vec4 integration --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/vector/mod.rs | 15 --------------- src/vector/v2d.rs | 34 +++++++++++++++++++++++++--------- tests/integration_test.rs | 6 +++++- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff37a3b..342590e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,7 +28,7 @@ dependencies = [ [[package]] name = "swift_vec" -version = "0.1.2" +version = "0.1.3" dependencies = [ "approx", "num-traits", diff --git a/Cargo.toml b/Cargo.toml index 09351e6..08fd649 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "swift_vec" -version = "0.1.2" +version = "0.1.3" edition = "2021" exclude = [ ".github/*", diff --git a/src/vector/mod.rs b/src/vector/mod.rs index c9c19ba..03a2c5e 100644 --- a/src/vector/mod.rs +++ b/src/vector/mod.rs @@ -157,14 +157,6 @@ pub trait IntVector, V: IntVector, A>: Vector pub trait FloatVector, A>: SignedVector { //=====// Trigonometry //=====// - /// Initializes a vector from an angle in radians. - fn from_angle(angle: T) -> V; - - /// Calculates the angle of a vector in respect to the positive x-axis. - /// # Returns - /// The angle of the vector in radians. - fn angle(&self) -> T; - /// Calculates the angle to another vector. /// # Returns /// The angle in radians. @@ -172,13 +164,6 @@ pub trait FloatVector, A>: SignedVector< -(self.cross(&other)).atan2(self.dot(&other)) } - /// Calculates the angle between the line connecting the two positions and the x-axis. - /// # Returns - /// The angle in radians. - fn angle_between(&self, other: &V) -> T { - (other.to_owned() - self.identity().to_owned()).angle() - } - /// Rotates this vector by a given angle in radians. fn rotated(&self, angle: T) -> V; diff --git a/src/vector/v2d.rs b/src/vector/v2d.rs index 98effa0..d5da75d 100644 --- a/src/vector/v2d.rs +++ b/src/vector/v2d.rs @@ -32,7 +32,7 @@ use crate::scalar::{ Scalar, SignedScalar }; Implementation */ -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Axis2 { None, X, @@ -134,13 +134,7 @@ impl > IntVector, Axis2> for Vec2 { } impl FloatVector, Axis2> for Vec2 { - fn from_angle(angle: T) -> Vec2 { - Vec2(angle.cos(), angle.sin()) - } - - fn angle(&self) -> T { - self.y().atan2(self.x()) - } + fn rotated(&self, angle: T) -> Vec2 { @@ -310,7 +304,7 @@ impl Vec2 { /// Calculates the aspect ratio of this vector. pub fn aspect_ratio(&self) -> T { self.x() / self.y() - } + } } impl Vec2 { @@ -326,6 +320,28 @@ impl Vec2 { } } +impl Vec2 { + + /// Initializes a vector from an angle in radians. + pub fn from_angle(angle: T) -> Vec2 { + Vec2(angle.cos(), angle.sin()) + } + + /// Calculates the angle of a vector in respect to the positive x-axis. + /// # Returns + /// The angle of the vector in radians. + pub fn angle(&self) -> T { + self.y().atan2(self.x()) + } + + /// Calculates the angle between the line connecting the two positions `self` and `other` and the x-axis. + /// # Returns + /// The angle in radians. + pub fn angle_between(&self, other: &Vec2) -> T { + (other - self).angle() + } +} + /* Global Operations diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 409c026..469ba8d 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -106,8 +106,12 @@ fn vec2_global() { _ => println!("Other") } - let argmax_axis: Axis2 = Vec2(1, 0).argmax(); + let test_vec: Vec2 = Vec2(1, 0); + let argmax_axis: Axis2 = test_vec.argmax(); + let argmax_val: i32 = test_vec.get(argmax_axis); + assert_eq!(argmax_axis, Axis2::X); + assert_eq!(argmax_val, 1); // Vectors support all primitive numerical types. let vec_i32: Vec2 = Vec2::ones_like();