-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point3D.cpp
259 lines (190 loc) · 6.06 KB
/
Point3D.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Point3D.cpp: implementation of the CPoint3D class.
//
//////////////////////////////////////////////////////////////////////
# include "Point3D.h"
# include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPoint3D::CPoint3D()
{
this->w = 1.0f;
}
CPoint3D::~CPoint3D()
{
}
/*********************************************************************
NAME : CPoint3D
DESCRIPTION: Parameterized Constructor
PARAMETER : x : the value of the x coord
y : the value of the y coord
z : the value of the z coord
w : the value of the w coord
RETURN : NONE
EXCEPTION : NONE.
*********************************************************************/
CPoint3D::CPoint3D(float x, float y, float z, float w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
/*********************************************************************
NAME : SetPoint
DESCRIPTION: Set the value of the Coordinates
PARAMETER : x : the value of the x coord
y : the value of the y coord
z : the value of the z coord
RETURN : NONE
EXCEPTION : NONE.
*********************************************************************/
void CPoint3D::SetPoint (float x, float y, float z, float w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
/*********************************************************************
NAME : AtInfinity
DESCRIPTION: Checks the w coord to see if the point lies at infinity
(for practical purposes, "is the point usable?")
PARAMETER : NONE
RETURN : bool
EXCEPTION : NONE.
*********************************************************************/
bool CPoint3D::AtInfinity ()
{
if(w == 0.0f)
{
return true;
}
return false;
}
/*********************************************************************
NAME : IsCloser
DESCRIPTION: Returns true if it is closer to the reference point than
the given point. False otherwise.
PARAMETER : ptOfReference : The point to which both distances are
measured.
toCompare : The point with which the distance has
to be compared.
RETURN : bool
EXCEPTION : NONE.
*********************************************************************/
bool CPoint3D::IsCloser (CPoint3D ptOfReference, CPoint3D toCompare)
{
float thisDistance, thatDistance;
if (this->AtInfinity() )
{
return false;
}
if (toCompare.AtInfinity() )
{
return true;
}
thisDistance = (ptOfReference.x - this->x) * (ptOfReference.x - this->x) +
(ptOfReference.y - this->y) * (ptOfReference.y - this->y) +
(ptOfReference.z - this->z) * (ptOfReference.z - this->z);
thatDistance = (ptOfReference.x - toCompare.x) * (ptOfReference.x - toCompare.x) +
(ptOfReference.y - toCompare.y) * (ptOfReference.y - toCompare.y) +
(ptOfReference.z - toCompare.z) * (ptOfReference.z - toCompare.z);
if (thisDistance < thatDistance || thatDistance <= 0.002)
{
return true;
}
return false;
}
/*********************************************************************
NAME : GetX
DESCRIPTION: Gets the x coordinate of a the 3d Point.
PARAMETER : NONE
RETURN : float
EXCEPTION : NONE.
*********************************************************************/
float CPoint3D::GetX () const
{
return this->x;
}
/*********************************************************************
NAME : GetX
DESCRIPTION: Gets the x coordinate of a the 3d Point.
PARAMETER : NONE
RETURN : float
EXCEPTION : NONE.
*********************************************************************/
float CPoint3D::GetY () const
{
return this->y;
}
/*********************************************************************
NAME : GetZ
DESCRIPTION: Gets the z coordinate of a the 3d Point.
PARAMETER : NONE
RETURN : float
EXCEPTION : NONE.
*********************************************************************/
float CPoint3D::GetZ () const
{
return this->z;
}
/*********************************************************************
NAME : GetW
DESCRIPTION: Gets the w coordinate of a the 3d Point.
PARAMETER : NONE
RETURN : float
EXCEPTION : NONE.
*********************************************************************/
float CPoint3D::GetW () const
{
return this->w;
}
/*********************************************************************
NAME : operator-
DESCRIPTION: subtracts given point in space from this point and returns
the result
PARAMETER : Point to be subtracted from this point.
RETURN : CPoint3D: Resultant point
EXCEPTION : NONE.
*********************************************************************/
CPoint3D CPoint3D::operator-(const CPoint3D& that)
{
CPoint3D ptResultantPoint;
ptResultantPoint.x = this->x - that.x;
ptResultantPoint.y = this->y - that.y;
ptResultantPoint.z = this->z - that.z;
return ptResultantPoint;
}
/*********************************************************************
NAME : operator+ (CVector)
DESCRIPTION: Creates a given vector to the point to get to a new position
in space
PARAMETER : vecAdd : the vector to add.
RETURN : The resultant point in space.
EXCEPTION : NONE.
*********************************************************************/
CPoint3D CPoint3D::operator+ (const CVector& vecAdd)
{
CPoint3D result;
result.x = x + vecAdd.XComponent();
result.y = y + vecAdd.YComponent();
result.z = z + vecAdd.ZComponent();
return result;
}
/*********************************************************************
NAME : GetDistance
DESCRIPTION: Finds the distance between two points
PARAMETER : p1 : The first point
p2 : The second point
RETURN : float : the distance between the two points.
EXCEPTION : NONE
*********************************************************************/
float CPoint3D::GetDistance (CPoint3D p1, CPoint3D p2)
{
float distanceSquared = (p2.x - p1.x) * (p2.x - p1.x) +
(p2.y - p1.y) * (p2.y - p1.y) +
(p2.z - p1.z) * (p2.z - p1.z);
float distance = (float)sqrt (distanceSquared);
return distance;
}