Box: Make range calculations mathematically correct

This commit is contained in:
Peter Keresztes Schmidt 2021-05-15 00:57:35 +02:00
parent dc79ec52c2
commit 29488900a1
6 changed files with 16 additions and 33 deletions

View File

@ -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_; }

View File

@ -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<int>(width - 1));
int x2 = ZM::clamp(p2.x_, 0, static_cast<int>(width - 1));
int y1 = ZM::clamp(p1.y_, 0, static_cast<int>(height - 1));
int y2 = ZM::clamp(p2.y_, 0, static_cast<int>(height - 1));
double dx = x2 - x1;
double dy = y2 - y1;

View File

@ -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_); }

View File

@ -874,8 +874,8 @@ std::vector<Zone> 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<int32>(monitor->Width())
|| polygon.Extent().Lo().y_ < 0 || polygon.Extent().Hi().y_ > static_cast<int32>(monitor->Height())) {
Error("Zone %d/%s for monitor %s extends outside of image dimensions, (%d,%d), (%d,%d), fixing",
Id,
Name,

View File

@ -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<Vector2>{{1, 1}, {5, 1}, {5, 5}, {1, 5}});
}

View File

@ -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));
}
}