Horizon
triangle.h
1 #ifndef H_TRIANGLE
2 #define H_TRIANGLE
3 
4 #include "vector2.h"
5 #include "edge.h"
6 
7 #include <assert.h>
8 #include <math.h>
9 namespace delaunay {
10 template <class T>
11 class Triangle
12 {
13  public:
14  using EdgeType = Edge<T>;
15  using VertexType = Vector2<T>;
16 
17  Triangle(const VertexType &_p1, const VertexType &_p2, const VertexType &_p3)
18  : p1(_p1), p2(_p2), p3(_p3),
19  e1(_p1, _p2), e2(_p2, _p3), e3(_p3, _p1)
20  {}
21 
22  bool containsVertex(const VertexType &v)
23  {
24  return p1 == v || p2 == v || p3 == v;
25  }
26 
27  bool circumCircleContains(const VertexType &v)
28  {
29  float ab = (p1.x * p1.x) + (p1.y * p1.y);
30  float cd = (p2.x * p2.x) + (p2.y * p2.y);
31  float ef = (p3.x * p3.x) + (p3.y * p3.y);
32 
33  float circum_x = (ab * (p3.y - p2.y) + cd * (p1.y - p3.y) + ef * (p2.y - p1.y)) / (p1.x * (p3.y - p2.y) + p2.x * (p1.y - p3.y) + p3.x * (p2.y - p1.y)) / 2.f;
34  float circum_y = (ab * (p3.x - p2.x) + cd * (p1.x - p3.x) + ef * (p2.x - p1.x)) / (p1.y * (p3.x - p2.x) + p2.y * (p1.x - p3.x) + p3.y * (p2.x - p1.x)) / 2.f;
35  float circum_radius = sqrtf(((p1.x - circum_x) * (p1.x - circum_x)) + ((p1.y - circum_y) * (p1.y - circum_y)));
36 
37  float dist = sqrtf(((v.x - circum_x) * (v.x - circum_x)) + ((v.y - circum_y) * (v.y - circum_y)));
38  return dist <= circum_radius;
39  }
40 
41  VertexType p1;
42  VertexType p2;
43  VertexType p3;
44  EdgeType e1;
45  EdgeType e2;
46  EdgeType e3;
47 };
48 
49 template <class T>
50 inline std::ostream &operator << (std::ostream &str, const Triangle<T> & t)
51 {
52  return str << "Triangle:" << std::endl << "\t" << t.p1 << std::endl << "\t" << t.p2 << std::endl << "\t" << t.p3 << std::endl << "\t" << t.e1 << std::endl << "\t" << t.e2 << std::endl << "\t" << t.e3 << std::endl;
53 
54 }
55 
56 template <class T>
57 inline bool operator == (const Triangle<T> &t1, const Triangle<T> &t2)
58 {
59  return (t1.p1 == t2.p1 || t1.p1 == t2.p2 || t1.p1 == t2.p3) &&
60  (t1.p2 == t2.p1 || t1.p2 == t2.p2 || t1.p2 == t2.p3) &&
61  (t1.p3 == t2.p1 || t1.p3 == t2.p2 || t1.p3 == t2.p3);
62 }
63 }
64 
65 #endif