-
Notifications
You must be signed in to change notification settings - Fork 1
/
LineSegment.cpp
193 lines (156 loc) · 4.66 KB
/
LineSegment.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
182
183
184
185
186
187
188
189
190
191
192
193
/*
Micha³ Piotrak
numer albumu: 269336
Przeciêcie graniastos³upów
*/
#include <iostream>
#include <sstream>
#include <cmath>
#include "LineSegment.h"
using namespace std;
LineSegment::LineSegment()
{
}
LineSegment::LineSegment(double x1, double y1, double x2, double y2): v1(Vertex(x1, y1)), v2(Vertex(x2, y2))
{
}
LineSegment::LineSegment(const Vertex& _v1, const Vertex& _v2) : v1(_v1), v2(_v2)
{
}
LineSegment::LineSegment(const LineSegment& segment)
{
this->v1 = segment.v1;
this->v2 = segment.v2;
}
LineSegment &LineSegment::operator=(const LineSegment& segment)
{
this->v1 = segment.v1;
this->v2 = segment.v2;
return *this;
}
void LineSegment::setV1(const Vertex& v1)
{
this->v1 = v1;
}
void LineSegment::setV2(const Vertex& v2)
{
this->v2 = v2;
}
Vertex LineSegment::getV1() const
{
return v1;
}
Vertex LineSegment::getV2() const
{
return v2;
}
bool LineSegment::isOnSegment(const Vertex& p, const Vertex& q, const Vertex& r)
{
if (q.getX() <= fmax(p.getX(), r.getX()) && q.getX() >= min(p.getX(), r.getY()) &&
q.getY() <= fmax(p.getY(), r.getY()) && q.getY() >= min(p.getY(), r.getY()))
return true;
return false;
}
bool LineSegment::isOnSegment(const Vertex& q)
{
if (q.getX() <= fmax(v1.getX(), v2.getX()) && q.getX() >= min(v1.getX(), v2.getY()) &&
q.getY() <= fmax(v1.getY(), v2.getY()) && q.getY() >= min(v1.getY(), v2.getY()))
return true;
return false;
}
bool LineSegment::isOnLine(const Vertex& point) const
{
float len = v1.getDistance(v2);
if (approximatelyEqual(v1.getDistance(point) + point.getDistance(v2), len))
return true;
else
return false;
}
int LineSegment::getOrientation(const Vertex& p, const Vertex& q, const Vertex& r)
{
int val = (q.getY() - p.getY()) * (r.getX() - q.getX()) - (q.getX() - p.getX()) * (r.getY() - q.getY());
if (val == 0) return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
bool LineSegment::doIntersect(const LineSegment& line) const
{
// Find the four orientations needed for general and special cases
int o1 = getOrientation(v1, v2, line.v1);
int o2 = getOrientation(v1, v2, line.v2);
int o3 = getOrientation(line.v1, line.v2, v1);
int o4 = getOrientation(line.v1, line.v2, v2);
// General case
if (o1 != o2 && o3 != o4)
return true;
// Special Cases
// v1, v2 and line.v1 are colinear and line.v1 lies on segment v1-v2
if (o1 == 0 && isOnSegment(v1, line.v1, v2)) return true;
// v1, v2 and line.v2 are colinear and v2 lies on segment v1-v2
if (o2 == 0 && isOnSegment(v1, line.v2, v2)) return true;
// line.v1, line.v2 and v1 are colinear and v1 lies on segment line.v1-line.v2
if (o3 == 0 && isOnSegment(line.v1, v1, line.v2)) return true;
// line.v1, line.v2 and v2 are colinear and v2 lies on segment line.v1-line.v2
if (o4 == 0 && isOnSegment(line.v1, v2, line.v2)) return true;
return false; // Doesn't fall in any of the above cases
}
pair<bool, Vertex> LineSegment::getIntersectionPoint(const LineSegment& line) const
{
double x1 = v1.getX();
double y1 = v1.getY();
double x2 = v2.getX();
double y2 = v2.getY();
double x3 = line.v1.getX();
double y3 = line.v1.getY();
double x4 = line.v2.getX();
double y4 = line.v2.getY();
double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (d == 0)
{
return pair<bool, Vertex>(false, Vertex());
}
if (v1 == line.v1 || v1 == line.v2)
{
return pair<bool, Vertex>(true, v1);
}
if (v2 == line.v1 || v2 == line.v2)
{
return pair<bool, Vertex>(true, v2);
}
double intersectionX = doubleRound(((x3 - x4) * (x1 * y2 - y1 * x2) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d);
double intersectionY = doubleRound(((y3 - y4) * (x1 * y2 - y1 * x2) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d);
if (intersectionX + EPSILON < min(x1, x2) || intersectionX > max(x1, x2) + EPSILON)
{
return pair<bool, Vertex>(false, Vertex());
}
else if (intersectionX + EPSILON < min(x3, x4) || intersectionX > max(x3, x4) + EPSILON)
{
return pair<bool, Vertex>(false, Vertex());
}
else if (intersectionY + EPSILON < min(y1, y2) || intersectionY > max(y1, y2) + EPSILON)
{
return pair<bool, Vertex>(false, Vertex());
}
else if (intersectionY + EPSILON < min(y3, y4) || intersectionY > max(y3, y4) + EPSILON)
{
return pair<bool, Vertex>(false, Vertex());
}
else
{
return pair<bool, Vertex>(true, Vertex(intersectionX, intersectionY, true));
}
}
string LineSegment::toString() const
{
stringstream ss;
ss << "LineSegment: [" << v1.toString() << ", " << v2.toString() << "]";
return ss.str();
}
bool LineSegment::approximatelyEqual(double a, double b)
{
//return fabs(a - b) <= fmax(1.0, max(fabs(a), fabs(b))) * EPSILON;
return fabs(a - b) <= EPSILON;
}
double LineSegment::doubleRound(double d)
{
return round(d * 1000) / 1000;
}