From 96b08ee75c80528c9858b052d3ee0751e138d76e Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Thu, 21 Feb 2019 14:34:37 +0100 Subject: [PATCH] Add new function Triangle::CircumcicleContains --- meson.build | 1 + poly2tri/common/shapes.cc | 24 ++++++++++++++++++++++++ poly2tri/common/shapes.h | 4 ++++ unittest/TriangleTest.cpp | 15 +++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 unittest/TriangleTest.cpp diff --git a/meson.build b/meson.build index 7c0dfcb..44c118d 100644 --- a/meson.build +++ b/meson.build @@ -14,6 +14,7 @@ boost_test_dep = dependency('boost', modules : [ 'unit_test_framework' ], requir if boost_test_dep.found() test('Unit Test', executable('unittest', [ 'unittest/main.cpp', + 'unittest/TriangleTest.cpp', ], dependencies : [boost_test_dep, thread_dep], link_with : lib)) endif diff --git a/poly2tri/common/shapes.cc b/poly2tri/common/shapes.cc index fe99a8d..e7130a5 100644 --- a/poly2tri/common/shapes.cc +++ b/poly2tri/common/shapes.cc @@ -365,4 +365,28 @@ void Triangle::DebugPrint() std::cout << *points_[0] << " " << *points_[1] << " " << *points_[2] << std::endl; } +bool Triangle::CircumcicleContains(const Point& point) const +{ + assert(IsCounterClockwise()); + const double dx = points_[0]->x - point.x; + const double dy = points_[0]->y - point.y; + const double ex = points_[1]->x - point.x; + const double ey = points_[1]->y - point.y; + const double fx = points_[2]->x - point.x; + const double fy = points_[2]->y - point.y; + + const double ap = dx * dx + dy * dy; + const double bp = ex * ex + ey * ey; + const double cp = fx * fx + fy * fy; + + return (dx * (fy * bp - cp * ey) - dy * (fx * bp - cp * ex) + ap * (fx * ey - fy * ex)) < 0; +} + +bool Triangle::IsCounterClockwise() const +{ + return (points_[1]->x - points_[0]->x) * (points_[2]->y - points_[0]->y) - + (points_[2]->x - points_[0]->x) * (points_[1]->y - points_[0]->y) > + 0; +} + } diff --git a/poly2tri/common/shapes.h b/poly2tri/common/shapes.h index 7f0b1e7..272aefd 100644 --- a/poly2tri/common/shapes.h +++ b/poly2tri/common/shapes.h @@ -207,8 +207,12 @@ Triangle& NeighborAcross(const Point& opoint); void DebugPrint(); +bool CircumcicleContains(const Point&) const; + private: +bool IsCounterClockwise() const; + /// Triangle points Point* points_[3]; /// Neighbor list diff --git a/unittest/TriangleTest.cpp b/unittest/TriangleTest.cpp new file mode 100644 index 0000000..bdeeae7 --- /dev/null +++ b/unittest/TriangleTest.cpp @@ -0,0 +1,15 @@ +#include +#include + +BOOST_AUTO_TEST_CASE(TriangleTest) +{ + p2t::Point a(0, 0); + p2t::Point b(1, 0); + p2t::Point c(0.5, .5); + p2t::Triangle triangle(a, b, c); + BOOST_CHECK(triangle.Contains(&a)); + BOOST_CHECK(triangle.Contains(&b)); + BOOST_CHECK(triangle.Contains(&c)); + BOOST_CHECK(triangle.CircumcicleContains(p2t::Point(0.5, 0.1))); + BOOST_CHECK(!triangle.CircumcicleContains(p2t::Point(1, 0.4))); +}