forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline2d.cpp
181 lines (151 loc) · 3.69 KB
/
line2d.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
181
/*
Copyright (C) 2012-2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <math.h>
#include <float.h>
#include <algorithm>
#include <iostream>
#include <limits>
#include "bbutil.h"
#include "line2d.h"
Line2D::Line2D(double x1, double y1, double x2, double y2) {
if (x1 == x2) {
m_ = DBL_MAX;
b_ = DBL_MIN;
xMin_ = xMax_ = x1;
} else {
m_ = (y2 - y1) / (x2 - x1);
b_ = y1 - (m_ * x1);
xMin_ = std::min(x1, x2);
xMax_ = std::max(x1, x2);
}
yMin_ = std::min(y1, y2);
yMax_ = std::max(y1, y2);
x1_ = x1;
y1_ = y1;
x2_ = x2;
y2_ = y2;
theta_ = std::numeric_limits<double>::quiet_NaN();
inverse_ = 0;
// Hesse normal form
nx_ = -(y2 - y1);
ny_ = x2 - x1;
double norm = 1./sqrt(nx_*nx_ + ny_*ny_);
nx_ *= norm;
ny_ *= norm;
pp_ = nx_*x1 + ny_*y1;
}
double Line2D::nx() {
return nx_;
}
double Line2D::ny() {
return ny_;
}
double Line2D::pp() {
return pp_;
}
double Line2D::m() {
return m_;
}
double Line2D::b() {
return b_;
}
double Line2D::x1() {
return x1_;
}
double Line2D::y1() {
return y1_;
}
double Line2D::x2() {
return x2_;
}
double Line2D::y2() {
return y2_;
}
double Line2D::xMin() {
return xMin_;
}
double Line2D::xMax() {
return xMax_;
}
double Line2D::yMin() {
return yMin_;
}
double Line2D::yMax() {
return yMax_;
}
double Line2D::theta() {
if (std::isnan(theta_)) {
theta_ = atan2(y2_ - y1_, x2_ - x1_);
}
return theta_;
}
void Line2D::shift(double dx, double dy) {
x1_ += dx;
x2_ += dx;
xMin_ += dx;
xMax_ += dx;
y1_ += dy;
y2_ += dy;
yMin_ += dy;
yMax_ += dy;
if (inverse_ != 0) {
inverse_->shift(dy, dx);
}
}
double Line2D::distance(double xx, double yy) {
return abs(nx_*xx + ny_*yy - pp_);
}
double Line2D::signedDistance(double xx, double yy) {
return nx_*xx + ny_*yy - pp_;
}
bool Line2D::contains(double xx, double yy, double eps) {
if (xx >= xMin_*(1.-eps) - eps && xx <= xMax_*(1.+eps) + eps &&
yy >= yMin_*(1.-eps) - eps && yy <= yMax_*(1.+eps) + eps &&
distance(xx,yy) < eps) {
return true;
}
return false;
}
bool Line2D::contains(double xx, double yy) {
return contains(xx, yy, DEFAULT_EPS);
}
Line2D* Line2D::getInverse() {
if (inverse_ == 0) {
inverse_ = new Line2D(this->y1(), this->x1(), this->y2(), this->x2());
}
return inverse_;
}
bool Line2D::intersects(Line2D *line) {
double linem = line->m();
if (m_ == linem) {
return false;
} else if (m_ == DBL_MAX || linem == DBL_MAX) {
if (m_ == DBL_MAX && linem == 0) {
return (xMin_ >= line->xMin() && xMax_ <= line->xMax()
&& yMin_ <= line->yMin() && yMax_ >= line->yMax());
} else {
return getInverse()->intersects(line->getInverse());
}
}
double x = (line->b() - b_) / (m_ - linem);
return (x >= xMin_ && x <= xMax_ && x >= line->xMin() && x <= line->xMax());
}
Line2D::~Line2D() {
if (inverse_ != 0) {
delete inverse_;
}
}