Add new function Triangle::CircumcicleContains

This commit is contained in:
Jan Niklas Hasse 2019-02-21 14:34:37 +01:00
parent e9454880fb
commit 96b08ee75c
4 changed files with 44 additions and 0 deletions

View File

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

View File

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

View File

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

15
unittest/TriangleTest.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <boost/test/unit_test.hpp>
#include <poly2tri/common/shapes.h>
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)));
}