diff --git a/src/zm_box.h b/src/zm_box.h index 9b9e33bc2..22210f2f8 100644 --- a/src/zm_box.h +++ b/src/zm_box.h @@ -32,7 +32,7 @@ class Box { public: Box() = default; - Box(Vector2 lo, Vector2 hi) : lo_(lo), hi_(hi), size_(Vector2::Range(hi, lo)) {} + Box(Vector2 lo, Vector2 hi) : lo_(lo), hi_(hi), size_(hi - lo) {} const Vector2 &Lo() const { return lo_; } const Vector2 &Hi() const { return hi_; } diff --git a/src/zm_image.cpp b/src/zm_image.cpp index f61974004..5fc53253f 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -2389,10 +2389,11 @@ void Image::Outline( Rgb colour, const Polygon &polygon ) { const Vector2 &p1 = polygon.GetVertices()[i]; const Vector2 &p2 = polygon.GetVertices()[j]; - int x1 = p1.x_; - int x2 = p2.x_; - int y1 = p1.y_; - int y2 = p2.y_; + // The last pixel we can draw is width/height - 1. Clamp to that value. + int x1 = ZM::clamp(p1.x_, 0, static_cast(width - 1)); + int x2 = ZM::clamp(p2.x_, 0, static_cast(width - 1)); + int y1 = ZM::clamp(p1.y_, 0, static_cast(height - 1)); + int y2 = ZM::clamp(p2.y_, 0, static_cast(height - 1)); double dx = x2 - x1; double dy = y2 - y1; diff --git a/src/zm_vector2.h b/src/zm_vector2.h index 0daa51d77..c7630ad36 100644 --- a/src/zm_vector2.h +++ b/src/zm_vector2.h @@ -37,11 +37,6 @@ class Vector2 { return inf; } - static Vector2 Range(const Vector2 &coord1, const Vector2 &coord2) { - Vector2 result((coord1.x_ - coord2.x_) + 1, (coord1.y_ - coord2.y_) + 1); - return result; - } - bool operator==(const Vector2 &rhs) const { return (x_ == rhs.x_ && y_ == rhs.y_); } bool operator!=(const Vector2 &rhs) const { return (x_ != rhs.x_ || y_ != rhs.y_); } diff --git a/src/zm_zone.cpp b/src/zm_zone.cpp index 413a407c5..7a73b93ed 100644 --- a/src/zm_zone.cpp +++ b/src/zm_zone.cpp @@ -874,8 +874,8 @@ std::vector Zone::Load(Monitor *monitor) { continue; } - if (polygon.Extent().Lo().x_ < 0 || polygon.Extent().Hi().x_ >= (int) monitor->Width() - || polygon.Extent().Lo().y_ < 0 || polygon.Extent().Hi().y_ >= (int) monitor->Height()) { + if (polygon.Extent().Lo().x_ < 0 || polygon.Extent().Hi().x_ > static_cast(monitor->Width()) + || polygon.Extent().Lo().y_ < 0 || polygon.Extent().Hi().y_ > static_cast(monitor->Height())) { Error("Zone %d/%s for monitor %s extends outside of image dimensions, (%d,%d), (%d,%d), fixing", Id, Name, diff --git a/tests/zm_box.cpp b/tests/zm_box.cpp index 8141bfa54..e85056c85 100644 --- a/tests/zm_box.cpp +++ b/tests/zm_box.cpp @@ -34,15 +34,10 @@ TEST_CASE("Box: construct from lo and hi") { SECTION("basic properties") { REQUIRE(b.Lo() == Vector2(1, 1)); REQUIRE(b.Hi() == Vector2(5, 5)); - // Should be: - // REQUIRE(b.Size() == Vector2(4 ,4)); - REQUIRE(b.Size() == Vector2(5, 5)); - // Should be: - // REQUIRE(b.Area() == 16); - REQUIRE(b.Area() == 25); - // Should be: - // REQUIRE(b.Centre() == Vector2(3, 3)); - REQUIRE(b.Centre() == Vector2(4, 4)); + + REQUIRE(b.Size() == Vector2(4 ,4)); + REQUIRE(b.Area() == 16); + REQUIRE(b.Centre() == Vector2(3, 3)); REQUIRE(b.Vertices() == std::vector{{1, 1}, {5, 1}, {5, 5}, {1, 5}}); } diff --git a/tests/zm_poly.cpp b/tests/zm_poly.cpp index 817b5a32c..8ce775d6c 100644 --- a/tests/zm_poly.cpp +++ b/tests/zm_poly.cpp @@ -31,10 +31,7 @@ TEST_CASE("Polygon: construct from vertices") { Polygon p(vertices); REQUIRE(p.Area() == 18); - //REQUIRE(p.Centre() == Vector2(2, 2)); - // Mathematically should be: - //REQUIRE(p.Extent().Size() == Vector2(6, 6)); - REQUIRE(p.Extent().Size() == Vector2(7, 7)); + REQUIRE(p.Extent().Size() == Vector2(6, 6)); } TEST_CASE("Polygon: clipping") { @@ -56,24 +53,19 @@ TEST_CASE("Polygon: clipping") { Polygon p(v); REQUIRE(p.GetVertices().size() == 11); - REQUIRE(p.Extent().Size() == Vector2(9, 8)); - // should be: - // REQUIRE(p.Extent().Size() == Vector2(8, 7)); - // related to Vector2::Range + REQUIRE(p.Extent().Size() == Vector2(8, 7)); SECTION("boundary box larger than polygon") { Polygon c = p.GetClipped(Box({1, 0}, {11, 9})); REQUIRE(c.GetVertices().size() == 11); - REQUIRE(c.Extent().Size() == Vector2(9, 8)); + REQUIRE(c.Extent().Size() == Vector2(8, 7)); } SECTION("boundary box smaller than polygon") { Polygon c = p.GetClipped(Box({2, 4}, {10, 7})); REQUIRE(c.GetVertices().size() == 8); - REQUIRE(c.Extent().Size() == Vector2(9, 4)); - // should be: - // REQUIRE(c.Extent().Size() == Vector2(8, 3)); + REQUIRE(c.Extent().Size() == Vector2(8, 3)); } }