forked from codysauermann/GeometricDataStructures2D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHalfSegment2D.cpp
More file actions
155 lines (136 loc) · 4.53 KB
/
HalfSegment2D.cpp
File metadata and controls
155 lines (136 loc) · 4.53 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
#include "Number.h"
#include "HalfSegment2D.h"
#include <climits>
HalfSegment2D::HalfSegment2D() // Default Constructor so that AttributedHalfSegment2D works.
{
}
HalfSegment2D::HalfSegment2D(const HalfSegment2D& hs)
{
this->s = hs.s;
this->isDominatingPointLeft = hs.isDominatingPointLeft;
}
HalfSegment2D::HalfSegment2D(Segment2D s, bool isDominatingPointLeft)
{
this->s = s;
this->isDominatingPointLeft = isDominatingPointLeft;
}
HalfSegment2D::HalfSegment2D(HalfSegment2D&& hs)
{
this->s = std::move(hs.s);
this->isDominatingPointLeft = std::move(hs.isDominatingPointLeft);
}
HalfSegment2D& HalfSegment2D::operator=(const HalfSegment2D& hs)
{
this->s = hs.s;
this->isDominatingPointLeft = hs.isDominatingPointLeft;
return *this;
}
HalfSegment2D& HalfSegment2D::operator=(HalfSegment2D&& hs)
{
this->s = std::move(hs.s);
this->isDominatingPointLeft = std::move(hs.isDominatingPointLeft);
return *this;
}
bool HalfSegment2D::operator==(const HalfSegment2D& hs)
{
return ((*this).s == hs.s && (*this).isDominatingPointLeft == hs.isDominatingPointLeft);
}
bool HalfSegment2D::operator>=(const HalfSegment2D& hs)
{
return !((*this) < hs);
}
bool HalfSegment2D::operator>(const HalfSegment2D& hs)
{
return !((*this) <= hs);
}
bool HalfSegment2D::operator<(const HalfSegment2D& hs)
{
// getDP() is not working for const hs so getting it this way
SimplePoint2D hsdp;
if(hs.isDominatingPointLeft)
hsdp = hs.s.leftEndPoint;
else
hsdp = hs.s.rightEndPoint;
// Case 1:
if (this->getDP() < hsdp)
return true;
else if (this->getDP() > hsdp)
return false;
// if equal, move onto case 2a
// Case 2a:
if (!this->isDominatingPointLeft && hs.isDominatingPointLeft) // this is right half segment and hs is left half segment
return true;
else if (!hs.isDominatingPointLeft && this->isDominatingPointLeft) // this is left half segment and hs is right half segment
return false;
// Case 2b:
Number m1, m2;
bool v1 = false, v2 = false;
if (this->s.rightEndPoint.x == this->s.leftEndPoint.x) // check for infinity slope
v1 = true;
else
m1 = (this->s.rightEndPoint.y - this->s.leftEndPoint.y) / (this->s.rightEndPoint.x - this->s.leftEndPoint.x); // calculate slope of this normally
if (hs.s.rightEndPoint.x == hs.s.leftEndPoint.x) // check for infinity slope
v2 = true;
else
m2 = (hs.s.rightEndPoint.y - hs.s.leftEndPoint.y) / (hs.s.rightEndPoint.x - hs.s.leftEndPoint.x); // calculate slope of hs normally
if (this->isDominatingPointLeft) // both are left half segments
{
Number zero = "0.0";
// so we are pretty much restricted to quadrants I and IV of the euclidean plane. Otherwise they could not both be left half segments.
// this means we just need to compare slopes to check for the counterclockwise rotation
if(v1 && v2)
;// case 3
else if(v2 && !v1)
{
if(m1 > zero)
return true;
return false;
}
else if(v1 && !v2)
{
if(m2 > zero)
return false;
return true;
}
else if (m2 > m1)
return true;
else if (m1 > m2)
return false;
// if slope is equal, it is case 3
}
else // both are right half segments - essentially opposite of if both are left
{
// so we are pretty much restricted to quadrants II and III of the euclidean plane. Otherwise they could not both be right half segments.
// this means we just need to compare slopes to check for the counterclockwise rotation
if(v1 && v2)
;// case 3
else if(v2 && !v1)
return true;
else if(v1 && !v2)
return false;
else if (m2 > m1)
return false;
else if (m1 > m2)
return true;
// if slope is equal, it is case 3
}
// Calculate Lengths
Number l1 = (this->s.rightEndPoint.y - this->s.leftEndPoint.y) * (this->s.rightEndPoint.y - this->s.leftEndPoint.y) + (this->s.rightEndPoint.x - this->s.leftEndPoint.x) * (this->s.rightEndPoint.x - this->s.leftEndPoint.x);
Number l2 = (hs.s.rightEndPoint.y - hs.s.leftEndPoint.y) * (hs.s.rightEndPoint.y - hs.s.leftEndPoint.y) + (hs.s.rightEndPoint.x - hs.s.leftEndPoint.x) * (hs.s.rightEndPoint.x - hs.s.leftEndPoint.x);
// Case 3:
return (l1 < l2);
}
bool HalfSegment2D::operator<=(const HalfSegment2D& hs)
{
return (*this < hs || *this == hs);
}
bool HalfSegment2D::operator!=(const HalfSegment2D& hs)
{
return !((*this) == hs);
}
SimplePoint2D HalfSegment2D::getDP()
{
if(this->isDominatingPointLeft)
return this->s.leftEndPoint;
return this->s.rightEndPoint;
}