@@ -27,7 +27,7 @@ use core::marker::PhantomData;
27
27
use core:: ops:: { Add , AddAssign , Div , DivAssign , Mul , MulAssign , Neg , Sub , SubAssign } ;
28
28
#[ cfg( feature = "mint" ) ]
29
29
use mint;
30
- use num_traits:: { Float , NumCast , Signed } ;
30
+ use num_traits:: { Float , NumCast , PrimInt , Signed } ;
31
31
#[ cfg( feature = "serde" ) ]
32
32
use serde;
33
33
@@ -138,6 +138,11 @@ impl<T: Default, U> Default for Vector2D<T, U> {
138
138
}
139
139
}
140
140
141
+ #[ inline]
142
+ fn ceiling_div < T : PrimInt > ( a : T , b : T ) -> T {
143
+ ( a + b - T :: one ( ) ) / b
144
+ }
145
+
141
146
impl < T , U > Vector2D < T , U > {
142
147
/// Constructor, setting all components to zero.
143
148
#[ inline]
@@ -253,6 +258,15 @@ impl<T, U> Vector2D<T, U> {
253
258
{
254
259
vec2 ( self . x * other. x , self . y * other. y )
255
260
}
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
+ }
256
270
}
257
271
258
272
impl < T : Copy , U > Vector2D < T , U > {
@@ -1089,6 +1103,19 @@ impl<T: Copy, U> Vector3D<T, U> {
1089
1103
vec3 ( self . x * other. x , self . y * other. y , self . z * other. z )
1090
1104
}
1091
1105
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
+
1092
1119
/// Cast this vector into a point.
1093
1120
///
1094
1121
/// Equivalent to adding this vector to the origin.
@@ -1996,6 +2023,18 @@ mod vector2d {
1996
2023
assert_eq ! ( result, Vec2 :: new( 15.0 , 25.0 ) ) ;
1997
2024
}
1998
2025
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
+
1999
2038
#[ test]
2000
2039
pub fn test_dot ( ) {
2001
2040
let p1: Vec2 = vec2 ( 2.0 , 7.0 ) ;
@@ -2234,6 +2273,18 @@ mod vector3d {
2234
2273
assert_eq ! ( p1 + & p2, vec3( 5.0 , 7.0 , 9.0 ) ) ;
2235
2274
}
2236
2275
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
+
2237
2288
#[ test]
2238
2289
pub fn test_sum ( ) {
2239
2290
let vecs = [
0 commit comments