diff options
Diffstat (limited to 'triangle.cpp')
-rw-r--r-- | triangle.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/triangle.cpp b/triangle.cpp new file mode 100644 index 0000000..825f983 --- /dev/null +++ b/triangle.cpp | |||
@@ -0,0 +1,123 @@ | |||
1 | #include "triangle.h" | ||
2 | |||
3 | Triangle::Triangle(double _x0, double _y0, double _x1, double _y1, double _x2, double _y2) | ||
4 | { | ||
5 | x0 = _x0; | ||
6 | y0 = _y0; | ||
7 | if ((_x1 - _x0)*(_y1 + _y0) + (_x2-_x1)*(_y2+_y1) + (_x0-_x2)*(_y0+_y2) < 0) | ||
8 | { | ||
9 | x1 = _x1; | ||
10 | x2 = _x2; | ||
11 | y1 = _y1; | ||
12 | y2 = _y2; | ||
13 | } else { | ||
14 | x1 = _x2; | ||
15 | x2 = _x1; | ||
16 | y1 = _y2; | ||
17 | y2 = _y1; | ||
18 | } | ||
19 | |||
20 | // Calculate line test constants | ||
21 | a1 = y1 - y0; | ||
22 | b1 = x0 - x1; | ||
23 | c1 = -y0*b1 - x0*a1; | ||
24 | |||
25 | a2 = y2 - y1; | ||
26 | b2 = x1 - x2; | ||
27 | c2 = -y1*b2 - x1*a2; | ||
28 | |||
29 | a3 = y0 - y2; | ||
30 | b3 = x2 - x0; | ||
31 | c3 = -y2*b3 - x2*a3; | ||
32 | |||
33 | // Extent | ||
34 | minx = std::min(x0, std::min(x1, x2)); | ||
35 | miny = std::min(y0, std::min(y1, y2)); | ||
36 | maxx = std::max(x0, std::max(x1, x2)); | ||
37 | maxy = std::max(y0, std::max(y1, y2)); | ||
38 | } | ||
39 | |||
40 | bool Triangle::in_bounds(double x, double y) const | ||
41 | { | ||
42 | return ((x*a1+y*b1+c1 <= 0) && (x*a2+y*b2+c2 <= 0) && (x*a3+y*b3+c3 <= 0)); | ||
43 | } | ||
44 | |||
45 | void Triangle::random_point(double& x, double& y) const | ||
46 | { | ||
47 | for (;;) | ||
48 | { | ||
49 | double _x = (rand() % (int)(maxx-minx))+minx; | ||
50 | double _y = (rand() % (int)(maxy-miny))+miny; | ||
51 | if (in_bounds(_x, _y)) | ||
52 | { | ||
53 | x = _x; | ||
54 | y = _y; | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | void Triangle::random_vertex(double& x, double& y) const | ||
61 | { | ||
62 | switch (rand()%3) | ||
63 | { | ||
64 | case 0: | ||
65 | { | ||
66 | x = x0; | ||
67 | y = y0; | ||
68 | break; | ||
69 | } | ||
70 | |||
71 | case 1: | ||
72 | { | ||
73 | x = x1; | ||
74 | y = y1; | ||
75 | break; | ||
76 | } | ||
77 | |||
78 | case 2: | ||
79 | { | ||
80 | x = x2; | ||
81 | y = y2; | ||
82 | break; | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | double Triangle::getX0() const | ||
88 | { | ||
89 | return x0; | ||
90 | } | ||
91 | |||
92 | double Triangle::getX1() const | ||
93 | { | ||
94 | return x1; | ||
95 | } | ||
96 | |||
97 | double Triangle::getX2() const | ||
98 | { | ||
99 | return x2; | ||
100 | } | ||
101 | |||
102 | double Triangle::getY0() const | ||
103 | { | ||
104 | return y0; | ||
105 | } | ||
106 | |||
107 | double Triangle::getY1() const | ||
108 | { | ||
109 | return y1; | ||
110 | } | ||
111 | |||
112 | double Triangle::getY2() const | ||
113 | { | ||
114 | return y2; | ||
115 | } | ||
116 | |||
117 | Matrix3x3 affineTransform(Triangle from, Triangle to) | ||
118 | { | ||
119 | Matrix3x3 A {{from.getX0(), from.getY0(), 1}, {from.getX1(), from.getY1(), 1}, {from.getX2(), from.getY2(), 1}}; | ||
120 | Matrix3x3 B {{to.getX0(), to.getY0(), 1}, {to.getX1(), to.getY1(), 1}, {to.getX2(), to.getY2(), 1}}; | ||
121 | |||
122 | return B * A.inv(); | ||
123 | } | ||