-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCPoint.h
More file actions
164 lines (137 loc) · 4.24 KB
/
Copy pathDCPoint.h
File metadata and controls
164 lines (137 loc) · 4.24 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
156
157
158
159
160
161
162
163
164
#ifndef DCPOINT_H
#define DCPOINT_H
struct DCPoint
{
public:
// x
double x;
// y
double y;
// Default constructor
DCPoint()
{
this->x = -1;
this->y = -1;
}
// Constructor
DCPoint(double x, double y)
{
this->x = x;
this->y = y;
}
// Scale a point
DCPoint Resize(double val)
{
DCPoint newP;
newP.x = this->x * val;
newP.y = this->y * val;
return newP;
}
// if a point is (-1, -1)
bool Invalid()
{
if(((int)x) == -1 && ((int)y) == -1)
{ return true; }
return false;
}
// Normalize
DCPoint Norm()
{
double vlength = std::sqrt( x * x + y * y );
return DCPoint(this->x / vlength, this->y / vlength);
}
// Euclidean distance
double Distance(DCPoint other)
{
double xDist = x - other.x;
double yDist = y - other.y;
return sqrt(xDist * xDist + yDist * yDist);
}
// Euclidean distance
double Distance(double otherX, double otherY)
{
double xDist = x - otherX;
double yDist = y - otherY;
return sqrt(xDist * xDist + yDist * yDist);
}
// squared euclidean distance
double DistanceSquared(DCPoint other)
{
double xDist = x - other.x;
double yDist = y - other.y;
return (xDist * xDist + yDist * yDist);
}
// squared euclidean distance
double DistanceSquared(double otherX, double otherY)
{
double xDist = x - otherX;
double yDist = y - otherY;
return (xDist * xDist + yDist * yDist);
}
// operator overloading
DCPoint operator+ (const DCPoint& other) { return DCPoint(x + other.x, y + other.y); }
// operator overloading
DCPoint operator- (const DCPoint& other) { return DCPoint(x - other.x, y - other.y); }
bool operator== (const DCPoint& other)
{ return (abs(this->x - other.x) < M_EPS && abs(this->y - other.y) < M_EPS); }
// operator overloading
bool operator!= (const DCPoint& other)
{ return (abs(this->x - other.x) >= M_EPS || abs(this->y - other.y) >= M_EPS); }
// operator overloading
DCPoint operator+= (const DCPoint& other)
{
x += other.x;
y += other.y;
return *this;
}
// operator overloading
DCPoint operator-= (const DCPoint& other)
{
x -= other.x;
y -= other.y;
return *this;
}
// operator overloading
DCPoint operator/ (const double& val) { return DCPoint(x / val, y / val); }
// operator overloading
DCPoint operator* (const double& val) { return DCPoint(x * val, y * val); }
// operator overloading
DCPoint operator*= (const double& val)
{
x *= val;
y *= val;
return *this;
}
// operator overloading
DCPoint operator/= (const double& val)
{
x /= val;
y /= val;
return *this;
}
// length of a vector
double Length() { return sqrt(x * x + y * y); }
// squared length of a vector
double LengthSquared() { return x * x + y * y; }
// dot product
double Dot(DCPoint otherPt) { return x * otherPt.x + y * otherPt.y; }
// cross product
DCPoint Cross(DCPoint otherPt)
{
//U x V = Ux*Vy-Uy*Vx
return DCPoint(x * otherPt.y, y * otherPt.x);
}
// linear dependency test
bool IsLinearDependent(DCPoint otherPoint)
{
double det = (this->x * otherPoint.y) - (this->y * otherPoint.x);
if(det > -M_EPS && det < M_EPS) { return true; }
return false;
}
// angle direction
DCPoint DirectionTo(DCPoint otherPt)
{
return DCPoint(otherPt.x - this->x, otherPt.y - this->y);
}
};
#endif // DCPOINT_H