Skip to content

Add component-wise ceiling division of 2d and 3d vectors #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[cfg(feature = "mint")]
use mint;
use num_traits::{Float, NumCast, Signed};
use num_traits::{Float, NumCast, PrimInt, Signed};
#[cfg(feature = "serde")]
use serde;

Expand Down Expand Up @@ -138,6 +138,11 @@ impl<T: Default, U> Default for Vector2D<T, U> {
}
}

#[inline]
fn ceiling_div<T: PrimInt>(a: T, b: T) -> T {
(a + b - T::one()) / b
}

impl<T, U> Vector2D<T, U> {
/// Constructor, setting all components to zero.
#[inline]
Expand Down Expand Up @@ -262,6 +267,15 @@ impl<T, U> Vector2D<T, U> {
{
vec2(self.x / other.x, self.y / other.y)
}

/// Returns the component-wise ceiling division of the two vectors.
#[inline]
pub fn component_ceiling_div(self, other: Self) -> Self
where
T: PrimInt + Div<Output = T>,
{
vec2(ceiling_div(self.x, other.x), ceiling_div(self.y, other.y))
}
}

impl<T: Copy, U> Vector2D<T, U> {
Expand Down Expand Up @@ -1107,6 +1121,19 @@ impl<T: Copy, U> Vector3D<T, U> {
vec3(self.x / other.x, self.y / other.y, self.z / other.z)
}

/// Returns the component-wise ceiling division of the two vectors.
#[inline]
pub fn component_ceiling_div(self, other: Self) -> Self
where
T: PrimInt + Div<Output = T>,
{
vec3(
ceiling_div(self.x, other.x),
ceiling_div(self.y, other.y),
ceiling_div(self.z, other.z),
)
}

/// Cast this vector into a point.
///
/// Equivalent to adding this vector to the origin.
Expand Down Expand Up @@ -2014,6 +2041,18 @@ mod vector2d {
assert_eq!(result, Vec2::new(15.0, 25.0));
}

#[test]
pub fn test_component_ceiling_div() {
type Vec2 = default::Vector2D<u32>;

let p1: Vec2 = vec2(3, 8);
let p2: Vec2 = vec2(2, 3);

let result = p1.component_ceiling_div(p2);

assert_eq!(result, Vec2::new(2, 3));
}

#[test]
pub fn test_dot() {
let p1: Vec2 = vec2(2.0, 7.0);
Expand Down Expand Up @@ -2252,6 +2291,18 @@ mod vector3d {
assert_eq!(p1 + &p2, vec3(5.0, 7.0, 9.0));
}

#[test]
pub fn test_component_ceiling_div() {
type Vec3 = default::Vector3D<u32>;

let p1: Vec3 = vec3(3, 8, 13);
let p2: Vec3 = vec3(2, 3, 4);

let result = p1.component_ceiling_div(p2);

assert_eq!(result, Vec3::new(2, 3, 4));
}

#[test]
pub fn test_sum() {
let vecs = [
Expand Down