1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use point2d::Point2d;
use std::ops::Add;
use std::ops::Sub;
use std::ops::Div;
use std::ops::Mul;
use std::cmp;

struct Box2dBuilder {
    x0: i32,
    x1: i32,
    y0: i32,
    y1: i32,
}

impl Box2dBuilder {
    fn new() -> Self{
        Box2dBuilder{x0: 0, x1: 0, y0: 0, y1: 0}
    }

    fn set_x0(mut self, x_:i32) -> Self{
        self.x0 = x_;
        self
    }

    fn set_x1(mut self, x_:i32) -> Self{
        self.x1 = x_;
        self
    }

    fn set_y0(mut self, y_:i32) -> Self{
        self.y0 = y_;
        self
    }

    fn set_y1(mut self, y_:i32) -> Self{
        self.y1 = y_;
        self
    }

    fn finalize(self) -> Box2d {
        assert!(self.x0 <= self.x1, 
            "x0 must be smaller than x1 but one has x0={:} and x1={:}", self.x0, self.x1);
        assert!(self.y0 <= self.y1, 
            "x0 must be smaller than x1 but one has y0={:} and y1={:}", self.y0, self.y1);
        Box2d{ x0: self.x0, x1: self.x1, y0: self.y0, y1: self.y1, }
    }
}

#[derive(Debug,Clone,Copy)]
pub struct Box2d
{
    pub x0: i32,
    pub x1: i32,
    pub y0: i32,
    pub y1: i32,
}

impl Box2d {
    pub fn new(x0: i32, x1: i32, y0: i32, y1: i32) -> Box2d {
        Box2dBuilder::new().set_x0(x0).set_x1(x1).set_y0(y0).set_y1(y1).finalize()
    }

    pub fn new_from_usize(x0: usize, x1: usize, y0: usize, y1: usize) -> Box2d {
        Box2dBuilder::new().set_x0(x0 as i32).set_x1(x1 as i32).set_y0(y0 as i32).set_y1(y1 as i32).finalize()
    }

    /// Surface of points in the 2d box (each point has surface 1)
    pub fn size(&self) -> usize {
        ((self.x1-self.x0)*(self.y1-self.y0)) as usize
    }

    /// Weighted surface of the Box2d (corners weight 1/4 and edges 1/2)
    pub fn weighted_size(&self) -> f64 {
        self.enlarge(-1).size() as f64 + 1.0 + 0.5*(self.x1-self.x0-2) as f64 + 0.5*(self.y1-self.y0-2) as f64
    }

    pub fn enlarge(&self, dx: i32) -> Box2d {
        Box2dBuilder::new().set_x0(self.x0-dx).set_x1(self.x1+dx).set_y0(self.y0-dx).set_y1(self.y1+dx).finalize()
    }

    pub fn translate(&self, p: Point2d) -> Box2d {
        *self+p
    }

    pub fn multiply(&self, alpha: i32) -> Box2d {
        Box2dBuilder::new().set_x0(self.x0*alpha).set_x1(self.x1*alpha).set_y0(self.y0*alpha).set_y1(self.y1*alpha).finalize()
    }

    pub fn divide(&self, alpha: i32) -> Box2d {
        *self / alpha
    }

    /// Returns an array containing the 4 corners of a box2d
    /// ordered in the trigonometric direction
    /// 
    /// # Arguments
    ///
    /// * `&self` - A Box2d
    ///
    /// # Example
    ///
    /// ```
    /// extern crate palarust;
    /// use palarust::box2d::Box2d;
    /// use palarust::point2d::Point2d;
    /// fn main () {
    ///     let b = Box2d::new(1, 4, 2, 8);
    ///     
    ///     assert_eq!(b.get_corners(),  
    ///         [Point2d::new(1,2), Point2d::new(4,2), Point2d::new(4,8), Point2d::new(1,8)]);
    /// }
    /// ```
    pub fn get_corners(&self) -> [Point2d; 4] {
        [Point2d::new(self.x0, self.y0), Point2d::new(self.x1, self.y0), Point2d::new(self.x1, self.y1), Point2d::new(self.x0, self.y1)]
    }

    /// Tests if a Box2d contains a Point2d
    /// 
    /// # Arguments
    ///
    /// * `&self` - A Box2d
    /// * `p`  - A Point2d
    ///
    /// # Example
    ///
    /// ```
    /// extern crate palarust;
    /// use palarust::box2d::Box2d;
    /// use palarust::point2d::Point2d;
    /// fn main () {
    ///     let b = Box2d::new(1, 4, 2, 8);
    ///     let p1 = Point2d::new(2, 3);
    ///     let p2 = Point2d::new(-1, -1);
    ///     
    ///     assert!(b.contains(p1),  "p1 must be contained in box");
    ///     assert!(!b.contains(p2), "p2 must not be contained in box");
    /// }
    /// ```
    pub fn contains(&self, p: Point2d) -> bool {
        p.is_contained(*self)

    }

    /// Computes the intersection of two box2d and returns an option.
    /// 
    /// # Arguments
    ///
    /// * `&self` - A Box2d
    /// * `rhs`  - A Box2d
    ///
    /// # Example
    ///
    /// ```
    /// extern crate palarust;
    /// use palarust::box2d::Box2d;
    /// fn main () {
    ///     let b = Box2d::new(1, 4, 2, 8);
    ///     let b1 = Box2d::new(2, 3, 3, 5);
    ///     let b2 = Box2d::new(-4, 2, -1, 4);
    ///     let b3 = Box2d::new(-4, -2, -4, -1);
    ///     
    ///     let int1 = b.intersection(b1);
    ///     match int1 {
    ///         Some(int1) => assert_eq!(int1, Box2d::new(2, 3, 3, 5)),
    ///         None => panic!("There should be an intersection between the two boxes"),
    ///     }
    ///     assert_eq!(int1, b1.intersection(b));
    ///
    ///     let int2 = b.intersection(b2);
    ///     match int2 {
    ///         Some(int2) => assert_eq!(int2, Box2d::new(1, 2, 2, 4)),
    ///         None => panic!("There should be an intersection between the two boxes"),
    ///     }
    ///     assert_eq!(int2, b2.intersection(b));
    ///
    ///     let int3 = b.intersection(b3);
    ///     match int3 {
    ///         Some(int3) => panic!("There should be an intersection between the two boxes"),
    ///         None => assert!(true, "There is no intersection"),
    ///     }
    ///     assert_eq!(int3, b3.intersection(b));
    ///
    /// }
    /// ```
    pub fn intersection(&self, rhs: Box2d) -> Option<Box2d> {
        let x0 = cmp::max(self.x0, rhs.x0);
        let x1 = cmp::min(self.x1, rhs.x1);
        let y0 = cmp::max(self.y0, rhs.y0);
        let y1 = cmp::min(self.y1, rhs.y1);

        // if no corner is contained in the self box
        if x0 <= x1 && y0 <= y1 {
            // else return the intersecting box
            Some(Box2d::new(x0,x1,y0,y1))
        } else {
            // return None
            None
        }

    }
}


// =============== Basic operation with Boxes ========================= //

/// The equality of two Box2d 
/// 
/// # Arguments
///
/// * `&self` - A Box2d
/// * `&other`  - A Box2d
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     
///     assert_eq!(b, Box2d::new(1, 4, 2, 8));
///     assert!(b != Box2d::new(1, 4, 2, 9), "b is not equal to Box2d::new(1, 4, 2, 9).");
/// }
/// ```
impl PartialEq for Box2d {
    fn eq(&self, other: &Box2d) -> bool {
        (self.x0 == other.x0 && self.x1 == other.x1 && self.y0 == other.y0 && self.y1 == other.y1)
    }
}

/// The addition of a Box2d with a Point2d (in this order)
/// 
/// # Arguments
///
/// * `self` - A Box2d
/// * `rhs`  - A Point2d
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::point2d::Point2d;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     let p = Point2d::new(-1, 4);
///     
///     assert_eq!(b+p, Box2d::new(0, 3, 6, 12) );
/// }
/// ```
impl Add<Point2d> for Box2d {
    type Output = Box2d;

    fn add(self, rhs: Point2d) -> Box2d {
        Box2d::new(self.x0+rhs.x, self.x1+rhs.x, self.y0+rhs.y, self.y1+rhs.y)
    }
}

/// The addition of a Point2d with a Box2d (in this order)
/// 
/// # Arguments
///
/// * `self` - A Point2d
/// * `rhs`  - A Box2d
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::point2d::Point2d;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     let p = Point2d::new(-1, 4);
///     
///     assert_eq!(p+b, b+p);
/// }
/// ```
impl Add<Box2d> for Point2d {
    type Output = Box2d;

    fn add(self, rhs: Box2d) -> Box2d {
        Box2d::new(self.x+rhs.x0, self.x+rhs.x1, self.y+rhs.y0, self.y+rhs.y1)
    }
}

/// The multiplication of a Box2d with a scalar (in this order)
/// 
/// # Arguments
///
/// * `self` - A Box2d
/// * `rhs`  - A i32
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     let alpha = 2;
///     
///     assert_eq!(b*alpha, Box2d::new(2, 8, 4, 16));
/// }
/// ```
impl Mul<i32> for Box2d {
    type Output = Box2d;

    fn mul(self, rhs: i32) -> Box2d {
        Box2d::new(self.x0*rhs, self.x1*rhs, self.y0*rhs, self.y1*rhs)
    }
}

/// The subtraction of a Box2d with a Point2d (in this order)
/// 
/// # Arguments
///
/// * `self` - A Box2d
/// * `rhs`  - A Point2d
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::point2d::Point2d;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     let p = Point2d::new(-1, 4);
///     
///     assert_eq!(b-p, Box2d::new(2, 5, -2, 4) );
/// }
/// ```
impl Sub<Point2d> for Box2d {
    type Output = Box2d;

    fn sub(self, rhs: Point2d) -> Box2d {
        Box2d::new(self.x0-rhs.x, self.x1-rhs.x, self.y0-rhs.y, self.y1-rhs.y)
    }
}

/// The division of a Box2d with an i32 (in this order)
/// 
/// # Arguments
///
/// * `self` - A Box2d
/// * `rhs`  - An i32
///
/// # Example
///
/// ```
/// extern crate palarust;
/// use palarust::point2d::Point2d;
/// use palarust::box2d::Box2d;
/// fn main () {
///     let b = Box2d::new(1, 4, 2, 8);
///     let alpha = 2i32;
///     
///     assert_eq!(b/alpha, Box2d::new(0, 2, 1, 4) );
/// }
/// ```
impl Div<i32> for Box2d {
    type Output = Box2d;

    fn div(self, rhs: i32) -> Box2d {
        Box2d::new(self.x0/rhs, self.x1/rhs, self.y0/rhs, self.y1/rhs)
    }
}