forked from SaraAghajanzadeh/poly-split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.cpp
180 lines (147 loc) · 4.11 KB
/
line.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "line.h"
#include <vector.h>
#include <cassert>
Line::Line()
{
}
Line::Line(const Vector &start, const Vector &end)
: start(start), end(end)
{
A = start.y - end.y;
B = end.x - start.x;
C = start.x * end.y - end.x * start.y;
}
Line::Line(double A, double B, double C)
: A(A), B(B), C(C)
{
if(fabs(A) <= POLY_SPLIT_EPS && fabs(B) >= POLY_SPLIT_EPS)
{
start.x = -1000;
start.y = -(C / B);
end.x = 1000;
end.y = start.y;
}
else if(fabs(B) <= POLY_SPLIT_EPS && fabs(A) >= POLY_SPLIT_EPS)
{
start.x = -(C / A);
start.y = -1000;
end.x = start.x;
end.y = 1000;
}
else
{
start.x = -1000;
start.y = -((A * start.x + C) / B);
end.x = 1000;
end.y = -((A * end.x + C) / B);
}
}
double Line::getDistance(const Vector &point) const
{
double n = A * point.x + B * point.y + C;
double m = sqrt(A * A + B * B);
assert(m != 0);
return n / m;
}
Vector Line::getLineNearestPoint(const Vector &point) const
{
Vector dir(B, -A);
double u = (point - start).dot(dir) / dir.squareLength();
return start + dir * u;
}
Vector Line::getSegmentNearestPoint(const Vector &point) const
{
Vector dir(B, -A);
double u = (point - start).dot(dir) / dir.squareLength();
if(u < 0)
return start;
else if(u > 1)
return end;
else
return start + dir * u;
}
int Line::pointSide(const Vector &point) const
{
double s = A * (point.x - start.x) + B * (point.y - start.y);
return (s > 0 ? 1 : (s < 0 ? -1 : 0));
}
#define inside(v, min, max) (((min) <= (v) + (POLY_SPLIT_EPS)) && ((v) <= (max) + (POLY_SPLIT_EPS)))
#define det(a, b, c, d) (((a) * (d)) - ((b) * (c)))
#define minimum(a, b) (((a) > (b)) ? (b) : (a))
#define maximum(a, b) (((a) < (b)) ? (b) : (a))
int Line::crossLineSegment(const Line &line, Vector &result) const
{
double d = det(A, B, line.A, line.B);
if(d == 0)
return 0;
result.x = -det(C, B, line.C, line.B) / d;
result.y = -det(A, C, line.A, line.C) / d;
return inside(result.x, minimum(line.start.x, line.end.x), maximum(line.start.x, line.end.x)) &&
inside(result.y, minimum(line.start.y, line.end.y), maximum(line.start.y, line.end.y));
}
int Line::crossSegmentSegment(const Line &line, Vector &result) const
{
double d = det(A, B, line.A, line.B);
if(d == 0)
return 0;
result.x = -det(C, B, line.C, line.B) / d;
result.y = -det(A, C, line.A, line.C) / d;
return inside(result.x, minimum(start.x, end.x), maximum(start.x, end.x)) &&
inside(result.y, minimum(start.y, end.y), maximum(start.y, end.y)) &&
inside(result.x, minimum(line.start.x, line.end.x), maximum(line.start.x, line.end.x)) &&
inside(result.y, minimum(line.start.y, line.end.y), maximum(line.start.y, line.end.y));
}
int Line::crossLineLine(const Line &line, Vector &result) const
{
double d = det(A, B, line.A, line.B);
if(d == 0)
return 0;
result.x = -det(C, B, line.C, line.B) / d;
result.y = -det(A, C, line.A, line.C) / d;
return 1;
}
Line Line::getBisector(const Line &l1, const Line &l2)
{
double q1 = sqrt(l1.A * l1.A + l1.B * l1.B);
double q2 = sqrt(l2.A * l2.A + l2.B * l2.B);
double A = l1.A / q1 - l2.A / q2;
double B = l1.B / q1 - l2.B / q2;
double C = l1.C / q1 - l2.C / q2;
return Line(A, B, C);
}
double Line::getTanAngle(const Line &l1, const Line &l2)
{
return (l1.A * l2.B - l2.A * l1.B) / (l1.A * l2.A + l1.B * l2.B);
}
Vector Line::getStart() const
{
return start;
}
Vector Line::getEnd() const
{
return end;
}
double Line::length() const
{
double x = end.x - start.x;
double y = end.y - start.y;
return sqrt(x * x + y * y);
}
double Line::squareLength() const
{
double x = end.x - start.x;
double y = end.y - start.y;
return x * x + y * y;
}
Line Line::reverse() const
{
return Line(end, start);
}
Vector Line::getPointAlong(double t) const
{
return start + (end - start).norm() * t;
}
Line Line::directedLine(const Vector &p, const Vector &d)
{
return Line(p, p + d);
}