Skip to content

Commit c0d6e0e

Browse files
mdsteeleCobrand
authored andcommitted
Deprecate Rect::contains, and add Rect::contains_point
1 parent 1f1d8fa commit c0d6e0e

File tree

1 file changed

+115
-4
lines changed

1 file changed

+115
-4
lines changed

src/sdl2/rect.rs

+115-4
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,83 @@ impl Rect {
315315
self.raw.h = clamp_size(height) as i32;
316316
}
317317

318-
/// Checks whether this rect contains a given point.
318+
/// Checks whether this rect contains a given point, or touches it on the
319+
/// right and/or bottom edges. This method is deprecated in favor of
320+
/// [`Rect::contains_point`](#method.contains_point).
321+
///
322+
/// For [historical
323+
/// reasons](https://github.com/AngryLawyer/rust-sdl2/issues/569), this
324+
/// method differs in behavior from
325+
/// [`SDL_PointInRect`](https://wiki.libsdl.org/SDL_PointInRect) by
326+
/// including points along the bottom and right edges of the rectangle, so
327+
/// that a 1-by-1 rectangle actually covers an area of four points, not
328+
/// one.
329+
///
330+
/// # Examples
331+
///
332+
/// ```
333+
/// use sdl2::rect::{Rect, Point};
334+
/// let rect = Rect::new(1, 2, 3, 4);
335+
/// assert!(rect.contains(Point::new(1, 2)));
336+
/// assert!(!rect.contains(Point::new(0, 1)));
337+
/// assert!(rect.contains(Point::new(3, 5)));
338+
/// assert!(rect.contains(Point::new(4, 6))); // N.B.
339+
/// assert!(!rect.contains(Point::new(5, 7)));
340+
/// ```
341+
#[deprecated(since = "0.30.0", note = "use `contains_point` instead")]
319342
pub fn contains<P>(&self, point: P) -> bool where P: Into<(i32, i32)> {
320343
let (x, y) = point.into();
321344
let inside_x = x >= self.left() && x <= self.right();
322345
inside_x && (y >= self.top() && y <= self.bottom())
323346
}
324347

348+
/// Checks whether this rectangle contains a given point.
349+
///
350+
/// Points along the right and bottom edges are not considered to be inside
351+
/// the rectangle; this way, a 1-by-1 rectangle contains only a single
352+
/// point. Another way to look at it is that this method returns true if
353+
/// and only if the given point would be painted by a call to
354+
/// [`Renderer::fill_rect`](
355+
/// ../render/struct.Renderer.html#method.fill_rect).
356+
///
357+
/// # Examples
358+
///
359+
/// ```
360+
/// use sdl2::rect::{Rect, Point};
361+
/// let rect = Rect::new(1, 2, 3, 4);
362+
/// assert!(rect.contains_point(Point::new(1, 2)));
363+
/// assert!(!rect.contains_point(Point::new(0, 1)));
364+
/// assert!(rect.contains_point(Point::new(3, 5)));
365+
/// assert!(!rect.contains_point(Point::new(4, 6)));
366+
/// ```
367+
pub fn contains_point<P>(&self, point: P) -> bool
368+
where P: Into<(i32, i32)> {
369+
let (x, y) = point.into();
370+
let inside_x = x >= self.left() && x < self.right();
371+
inside_x && (y >= self.top() && y < self.bottom())
372+
}
373+
374+
/// Checks whether this rectangle completely contains another rectangle.
375+
///
376+
/// This method returns true if and only if every point contained by
377+
/// `other` is also contained by `self`; in other words, if the
378+
/// intersection of `self` and `other` is equal to `other`.
379+
///
380+
/// # Examples
381+
///
382+
/// ```
383+
/// use sdl2::rect::Rect;
384+
/// let rect = Rect::new(1, 2, 3, 4);
385+
/// assert!(rect.contains_rect(rect));
386+
/// assert!(rect.contains_rect(Rect::new(3, 3, 1, 1)));
387+
/// assert!(!rect.contains_rect(Rect::new(2, 1, 1, 1)));
388+
/// assert!(!rect.contains_rect(Rect::new(3, 3, 2, 1)));
389+
/// ```
390+
pub fn contains_rect(&self, other: Rect) -> bool {
391+
other.left() >= self.left() && other.right() <= self.right() &&
392+
other.top() >= self.top() && other.bottom() <= self.bottom()
393+
}
394+
325395
/// Returns the underlying C Rect.
326396
pub fn raw(&self) -> *const ll::SDL_Rect {
327397
&self.raw
@@ -380,15 +450,44 @@ impl Rect {
380450
}
381451
}
382452

383-
/// Determine whether two rectangles intersect.
453+
/// Determines whether two rectangles intersect.
454+
///
455+
/// Rectangles that share an edge but don't actually overlap are not
456+
/// considered to intersect.
457+
///
458+
/// # Examples
459+
///
460+
/// ```
461+
/// use sdl2::rect::Rect;
462+
/// let rect = Rect::new(0, 0, 5, 5);
463+
/// assert!(rect.has_intersection(rect));
464+
/// assert!(rect.has_intersection(Rect::new(2, 2, 5, 5)));
465+
/// assert!(!rect.has_intersection(Rect::new(5, 0, 5, 5)));
466+
/// ```
384467
pub fn has_intersection(&self, other: Rect) -> bool {
385468
unsafe {
386469
ll::SDL_HasIntersection(self.raw(), other.raw()) != 0
387470
}
388471
}
389472

390-
/// Calculate the intersection of two rectangles.
473+
/// Calculates the intersection of two rectangles.
474+
///
475+
/// Returns `None` if the two rectangles don't intersect. Rectangles that
476+
/// share an edge but don't actually overlap are not considered to
477+
/// intersect.
478+
///
391479
/// The bitwise AND operator `&` can also be used.
480+
///
481+
/// # Examples
482+
///
483+
/// ```
484+
/// use sdl2::rect::Rect;
485+
/// let rect = Rect::new(0, 0, 5, 5);
486+
/// assert_eq!(rect.intersection(rect), Some(rect));
487+
/// assert_eq!(rect.intersection(Rect::new(2, 2, 5, 5)),
488+
/// Some(Rect::new(2, 2, 3, 3)));
489+
/// assert_eq!(rect.intersection(Rect::new(5, 0, 5, 5)), None);
490+
/// ```
392491
pub fn intersection(&self, other: Rect) -> Option<Rect> {
393492
let mut out = unsafe { mem::uninitialized() };
394493

@@ -403,8 +502,20 @@ impl Rect {
403502
}
404503
}
405504

406-
/// Calculate the union of two rectangles.
505+
/// Calculates the union of two rectangles (i.e. the smallest rectangle
506+
/// that contains both).
507+
///
407508
/// The bitwise OR operator `|` can also be used.
509+
///
510+
/// # Examples
511+
///
512+
/// ```
513+
/// use sdl2::rect::Rect;
514+
/// let rect = Rect::new(0, 0, 5, 5);
515+
/// assert_eq!(rect.union(rect), rect);
516+
/// assert_eq!(rect.union(Rect::new(2, 2, 5, 5)), Rect::new(0, 0, 7, 7));
517+
/// assert_eq!(rect.union(Rect::new(5, 0, 5, 5)), Rect::new(0, 0, 10, 5));
518+
/// ```
408519
pub fn union(&self, other: Rect) -> Rect {
409520
let mut out = unsafe {
410521
mem::uninitialized()

0 commit comments

Comments
 (0)