Skip to content

Commit

Permalink
Fix: Intersect error
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-xuan committed Dec 10, 2024
1 parent 2159750 commit 4ea334e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
7 changes: 5 additions & 2 deletions includes/line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "point.hpp"
#include "shared.hpp"

#include <cmath>
#include <iostream>
#include <optional>
#include <string>
Expand Down Expand Up @@ -97,8 +98,10 @@ class Line {
double other_min_y = std::min(other.start.GetY(), other.end.GetY());
double other_max_y = std::max(other.start.GetY(), other.end.GetY());

if (Between<double>(x, this_min_x, this_max_x) && Between<double>(y, this_min_y, this_max_y) &&
Between<double>(x, other_min_x, other_max_x) && Between<double>(y, other_min_y, other_max_y)) {
if (Between<double>(std::floor(x * 1e6), std::floor(this_min_x * 1e6), std::floor(this_max_x * 1e6)) &&
Between<double>(std::floor(y * 1e6), std::floor(this_min_y * 1e6), std::floor(this_max_y * 1e6)) &&
Between<double>(std::floor(x * 1e6), std::floor(other_min_x * 1e6), std::floor(other_max_x * 1e6)) &&
Between<double>(std::floor(y * 1e6), std::floor(other_min_y * 1e6), std::floor(other_max_y * 1e6))) {
return std::optional<Point>({x, y});
}

Expand Down
10 changes: 4 additions & 6 deletions includes/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <iostream>
#include <memory>
#include <set>
#include <stdexcept>
#include <vector>

class Polygon {
Expand Down Expand Up @@ -42,14 +41,13 @@ class Polygon {
double a = 0;
for (size_t i = 0; i < vertexs->size(); i++) {
double x1 = vertexs->at(i).GetX();
double y1 = vertexs->at((i + 1) % vertexs->size()).GetX();
double x2 = vertexs->at(i).GetY();
double y1 = vertexs->at(i).GetY();
double x2 = vertexs->at((i + 1) % vertexs->size()).GetX();
double y2 = vertexs->at((i + 1) % vertexs->size()).GetY();

a += (x1 * y2 + x2 * y1);
a += (x1 * y2 - x2 * y1);
}
a *= 0.5;
clockwise = (a < 0);
clockwise = (a > 0);
}

public:
Expand Down
2 changes: 2 additions & 0 deletions polygondust/vectorization/vectorization_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def vectorization(polygons: list[Polygon], no_show=False):

polygon_result = context.GetResult()

print(len(polygon.GetVertex()))

print("Area", polygon_result.GetArea())

if not no_show:
Expand Down
8 changes: 2 additions & 6 deletions src/point.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "point.hpp"
#include <cmath>

double Point::GetX() const{
return std::round(this->x * 1e6) / 1e6;
}
double Point::GetX() const { return this->x; }

double Point::GetY() const {
return std::round(this->y * 1e6) / 1e6;
}
double Point::GetY() const { return this->y; }
8 changes: 4 additions & 4 deletions tests/cpp/ut_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ TEST(POLYGON_TEST, test_polygon_with_concave_polygon_should_return_correct_local
}

TEST(POLYGON_TEST, test_polygon_with_counterwise_vertex_set_should_determine_counterwise) {
std::shared_ptr<Polygon> polygon = std::make_shared<Polygon>(std::vector<Point>{
Point(0, 0), Point(2, 4), Point(4, 2), Point(6, 4), Point(8, 0), Point(6, 2), Point(4, 1), Point(2, 2)});
std::shared_ptr<Polygon> polygon =
std::make_shared<Polygon>(std::vector<Point>{Point(0, 0), Point(4, 2), Point(2, 5)});

ASSERT_EQ(polygon->IsClockwise(), false);
ASSERT_EQ(polygon->IsClockwise(), true);
}

TEST(POLYGON_TEST, test_polygon_with_clockwise_vertex_set_should_determine_clockwise) {
std::shared_ptr<Polygon> polygon =
std::make_shared<Polygon>(std::vector<Point>{Point(0, 0), Point(-5, 0), Point(-2.5, 3)});

ASSERT_EQ(polygon->IsClockwise(), true);
ASSERT_EQ(polygon->IsClockwise(), false);
}

0 comments on commit 4ea334e

Please sign in to comment.