Skip to content

Commit

Permalink
Readied project for Vec3/Vec4 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaticWyrm467 committed Jan 19, 2024
1 parent d2e5864 commit db87a1e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swift_vec"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
exclude = [
".github/*",
Expand Down
15 changes: 0 additions & 15 deletions src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,13 @@ pub trait IntVector<T: IntScalar<T>, V: IntVector<T, V, A>, A>: Vector<T, V, A>
pub trait FloatVector<T: FloatScalar, V: FloatVector<T, V, A>, A>: SignedVector<T, V, A> {

//=====// 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.
fn angle_to(&self, other: &V) -> T {
-(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;

Expand Down
34 changes: 25 additions & 9 deletions src/vector/v2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,13 +134,7 @@ impl <T: IntScalar<T>> IntVector<T, Vec2<T>, Axis2> for Vec2<T> {
}

impl <T: FloatScalar> FloatVector<T, Vec2<T>, Axis2> for Vec2<T> {
fn from_angle(angle: T) -> Vec2<T> {
Vec2(angle.cos(), angle.sin())
}

fn angle(&self) -> T {
self.y().atan2(self.x())
}


fn rotated(&self, angle: T) -> Vec2<T> {

Expand Down Expand Up @@ -310,7 +304,7 @@ impl <T: Scalar> Vec2<T> {
/// Calculates the aspect ratio of this vector.
pub fn aspect_ratio(&self) -> T {
self.x() / self.y()
}
}
}

impl <T: SignedScalar> Vec2<T> {
Expand All @@ -326,6 +320,28 @@ impl <T: SignedScalar> Vec2<T> {
}
}

impl <T: FloatScalar> Vec2<T> {

/// Initializes a vector from an angle in radians.
pub fn from_angle(angle: T) -> Vec2<T> {
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>) -> T {
(other - self).angle()
}
}


/*
Global Operations
Expand Down
6 changes: 5 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ fn vec2_global() {
_ => println!("Other")
}

let argmax_axis: Axis2 = Vec2(1, 0).argmax();
let test_vec: Vec2<i32> = 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<i32> = Vec2::ones_like();
Expand Down

0 comments on commit db87a1e

Please sign in to comment.