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