-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment2D.cpp
More file actions
155 lines (135 loc) · 3.51 KB
/
Segment2D.cpp
File metadata and controls
155 lines (135 loc) · 3.51 KB
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
// Douglas
#include "Segment2D.h"
#include <cmath>
#include <iostream>
Segment2D::Segment2D() // Created default constructor so that HalfSegment2D works.
{
}
Segment2D::Segment2D(const Segment2D& s)
{
this->leftEndPoint = s.leftEndPoint;
this->rightEndPoint = s.rightEndPoint;
}
Segment2D::Segment2D(SimplePoint2D l, SimplePoint2D r)
{
if (l < r)
{
this->leftEndPoint = l;
this->rightEndPoint = r;
}
else
{
this->leftEndPoint = r;
this->rightEndPoint = l;
}
}
Segment2D::Segment2D(Segment2D&& s)
{
this->leftEndPoint = std::move(s.leftEndPoint);
this->rightEndPoint = std::move(s.rightEndPoint);
}
Segment2D& Segment2D::operator=(const Segment2D& s)
{
this->leftEndPoint = s.leftEndPoint;
this->rightEndPoint = s.rightEndPoint;
return *this;
}
Segment2D& Segment2D::operator=(Segment2D&& s)
{
this->leftEndPoint = std::move(s.leftEndPoint);
this->rightEndPoint = std::move(s.rightEndPoint);
return *this;
}
bool Segment2D::operator==(const Segment2D& s) const
{
return (this->leftEndPoint == s.leftEndPoint && (*this).rightEndPoint == s.rightEndPoint);
}
bool Segment2D::operator>=(const Segment2D& s) const
{
return !((*this) < s);
}
bool Segment2D::operator>(const Segment2D& s) const
{
return !((*this) <= s);
}
bool Segment2D::operator<(const Segment2D& s) const
{
if (this->leftEndPoint < s.leftEndPoint)
return true;
else
return (this->leftEndPoint == s.leftEndPoint && this->rightEndPoint < s.rightEndPoint);
}
bool Segment2D::operator<=(const Segment2D& s) const
{
return ((*this) < s || (*this) == s);
}
bool Segment2D::operator!=(const Segment2D& s) const
{
return !((*this) == s);
}
std::pair<bool, SimplePoint2D> Segment2D::findIntersection(Segment2D s)
{
Number x1 = leftEndPoint.x;
Number y1 = leftEndPoint.y;
Number x2 = rightEndPoint.x;
Number y2 = rightEndPoint.y;
Number x3 = s.leftEndPoint.x;
Number y3 = s.leftEndPoint.y;
Number x4 = s.rightEndPoint.x;
Number y4 = s.rightEndPoint.y;
Number denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
SimplePoint2D empty;
Number zero = "0.0";
if (denominator == zero)
{
return std::make_pair(false, empty);
}
else
{
Number ia = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
Number ib = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
Number one = "1.0";
if (ia < zero || ia > one || ib < zero || ib > one)
return std::make_pair(false, empty); // The intersection point does not exist in at least one of the segments.
else
{
Number x = x1 + ia * (x2 - x1);
Number y = y1 + ia * (y2 - y1);
SimplePoint2D p(x, y);
return std::make_pair(true, p);
}
}
return std::make_pair(false, empty);
}
std::pair<bool, SimplePoint2D> Segment2D::findIntersection_excludeEndpoints(Segment2D s)
{
std::pair<bool, SimplePoint2D> p = findIntersection(s);
if(p.first)
{
if(p.second == leftEndPoint || p.second == rightEndPoint)
return std::make_pair(false, p.second);
else
return p;
}
return std::make_pair(false, p.second);
}
bool Segment2D::poiOnSeg(SimplePoint2D p)
{
Number x1 = leftEndPoint.x;
Number y1 = leftEndPoint.y;
Number x2 = rightEndPoint.x;
Number y2 = rightEndPoint.y;
Number dx = x2 - x1;
Number dy = y2 - y1;
Number a = dy;
Number zero = "0.0";
Number b = zero - dx;
Number c = dx * y1 - dy * x1;
Number x = p.x;
Number y = p.y;
if (a * x + b * y + c != zero)
return false;
if ((x1 <= x && x <= x2 || x2 <= x && x <= x1) && (y1 <= y && y <= y2 || y2 <= y && y <= y1))
return true;
return false;
}