Skip to content

Commit b3671c5

Browse files
author
Alexandre Kirszenberg
committed
Add component-wise ceiling division of 2d and 3d vectors
1 parent e1383e0 commit b3671c5

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/vector.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::marker::PhantomData;
2727
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
2828
#[cfg(feature = "mint")]
2929
use mint;
30-
use num_traits::{Float, NumCast, Signed};
30+
use num_traits::{Float, NumCast, PrimInt, Signed};
3131
#[cfg(feature = "serde")]
3232
use serde;
3333

@@ -138,6 +138,11 @@ impl<T: Default, U> Default for Vector2D<T, U> {
138138
}
139139
}
140140

141+
#[inline]
142+
fn ceiling_div<T: PrimInt>(a: T, b: T) -> T {
143+
(a + b - T::one()) / b
144+
}
145+
141146
impl<T, U> Vector2D<T, U> {
142147
/// Constructor, setting all components to zero.
143148
#[inline]
@@ -253,6 +258,15 @@ impl<T, U> Vector2D<T, U> {
253258
{
254259
vec2(self.x * other.x, self.y * other.y)
255260
}
261+
262+
/// Returns the component-wise ceiling division of the two vectors.
263+
#[inline]
264+
pub fn component_ceiling_div(self, other: Self) -> Self
265+
where
266+
T: PrimInt + Div<Output = T>,
267+
{
268+
vec2(ceiling_div(self.x, other.x), ceiling_div(self.y, other.y))
269+
}
256270
}
257271

258272
impl<T: Copy, U> Vector2D<T, U> {
@@ -1089,6 +1103,19 @@ impl<T: Copy, U> Vector3D<T, U> {
10891103
vec3(self.x * other.x, self.y * other.y, self.z * other.z)
10901104
}
10911105

1106+
/// Returns the component-wise ceiling division of the two vectors.
1107+
#[inline]
1108+
pub fn component_ceiling_div(self, other: Self) -> Self
1109+
where
1110+
T: PrimInt + Div<Output = T>,
1111+
{
1112+
vec3(
1113+
ceiling_div(self.x, other.x),
1114+
ceiling_div(self.y, other.y),
1115+
ceiling_div(self.z, other.z),
1116+
)
1117+
}
1118+
10921119
/// Cast this vector into a point.
10931120
///
10941121
/// Equivalent to adding this vector to the origin.
@@ -1996,6 +2023,18 @@ mod vector2d {
19962023
assert_eq!(result, Vec2::new(15.0, 25.0));
19972024
}
19982025

2026+
#[test]
2027+
pub fn test_component_ceiling_div() {
2028+
type Vec2 = default::Vector2D<u32>;
2029+
2030+
let p1: Vec2 = vec2(3, 8);
2031+
let p2: Vec2 = vec2(2, 3);
2032+
2033+
let result = p1.component_ceiling_div(p2);
2034+
2035+
assert_eq!(result, Vec2::new(2, 3));
2036+
}
2037+
19992038
#[test]
20002039
pub fn test_dot() {
20012040
let p1: Vec2 = vec2(2.0, 7.0);
@@ -2234,6 +2273,18 @@ mod vector3d {
22342273
assert_eq!(p1 + &p2, vec3(5.0, 7.0, 9.0));
22352274
}
22362275

2276+
#[test]
2277+
pub fn test_component_ceiling_div() {
2278+
type Vec3 = default::Vector3D<u32>;
2279+
2280+
let p1: Vec3 = vec3(3, 8, 13);
2281+
let p2: Vec3 = vec3(2, 3, 4);
2282+
2283+
let result = p1.component_ceiling_div(p2);
2284+
2285+
assert_eq!(result, Vec3::new(2, 3, 4));
2286+
}
2287+
22372288
#[test]
22382289
pub fn test_sum() {
22392290
let vecs = [

0 commit comments

Comments
 (0)