Skip to content

Commit

Permalink
Fix Rectangle::from_point_and_size
Browse files Browse the repository at this point in the history
It was setting the wrong size/pixels.
  • Loading branch information
tomassedovic committed Dec 22, 2017
1 parent acc7f36 commit 5f6c04a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ pub fn update(state_ptr: *mut State, dt_ms: u32) {
}

&engine::Draw::Rectangle(top_left, dimensions, color) => {
if dimensions.x > 0 && dimensions.y > 0 {
if dimensions.x >= 1 && dimensions.y >= 1 {
let rect = rect::Rectangle::from_point_and_size(top_left, dimensions);
for pos in rect.points() {
assert!(pos.x >= 0 && pos.x < 255);
Expand Down
33 changes: 31 additions & 2 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Rectangle {
assert!(size > (0, 0));
Rectangle {
top_left,
bottom_right: top_left + size,
bottom_right: top_left + size - (1, 1),
}
}

Expand All @@ -31,7 +31,7 @@ impl Rectangle {
}

pub fn dimensions(self) -> Point {
self.bottom_right - self.top_left
self.bottom_right - self.top_left + (1, 1)
}

/// Returns `true` if the point is within the areas specified by
Expand Down Expand Up @@ -105,3 +105,32 @@ impl Iterator for RectangleIterator {
}
}
}

#[cfg(test)]
mod tests{
use super::Rectangle;
use point::Point;

#[test]
fn smallest_rect() {
let rect = Rectangle::from_point_and_size((0, 0).into(), (1, 1).into());
assert_eq!(rect.dimensions(), Point::new(1, 1));
assert_eq!(rect.points().collect::<Vec<_>>().len(), 1);

let rect = Rectangle::from_point_and_size((5, 7).into(), (1, 1).into());
assert_eq!(rect.dimensions(), Point::new(1, 1));
assert_eq!(rect.points().collect::<Vec<_>>().len(), 1);
}

#[test]
fn rect_size_2() {
let rect = Rectangle::from_point_and_size((0, 0).into(), (2, 2).into());
assert_eq!(rect.dimensions(), Point::new(2, 2));
assert_eq!(rect.points().collect::<Vec<_>>().len(), 4);

let rect = Rectangle::from_point_and_size((5, 7).into(), (2, 2).into());
assert_eq!(rect.dimensions(), Point::new(2, 2));
assert_eq!(rect.points().collect::<Vec<_>>().len(), 4);
}

}

0 comments on commit 5f6c04a

Please sign in to comment.