Permalink
Browse files

Fix Rectangle::from_point_and_size

It was setting the wrong size/pixels.
  • Loading branch information...
tomassedovic committed Dec 22, 2017
1 parent acc7f36 commit 5f6c04af284af313a4b70126aac3a0c2719d02c0
Showing with 32 additions and 3 deletions.
  1. +1 −1 src/main.rs
  2. +31 −2 src/rect.rs
@@ -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);
@@ -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),
}
}

@@ -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
@@ -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.