-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
177 lines (143 loc) · 2.6 KB
/
Copy pathpoint.cpp
File metadata and controls
177 lines (143 loc) · 2.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "point.h"
#include <cmath>
Point::Point(float px, float py, float pz) : x(px), y(py), z(pz)
{}
Vector::Vector (float vx, float vy, float vz) : x(vx), y(vy), z(vz)
{}
Vector::Vector(const Point &p)
{
x=p.x;
y=p.y;
z=p.z;
}
float Vector::length() const
{
return sqrt(squaredLength());
}
float Vector::squaredLength() const
{
return x*x+y*y+z*z;
}
void Vector::normalize()
{
float inv = 1/length();
x*=inv;
y*=inv;
z*=inv;
}
// P+Q
Point operator+(const Point& p, const Point& q)
{
return Point(p.x+q.x, p.y+q.y, p.z+q.z);
}
Point& operator+=(Point& p, const Point& q)
{
p.x+=q.x;
p.y+=q.y;
p.z+=q.z;
return p;
}
// P-Q
Vector operator-(const Point& p, const Point& q)
{
return Vector(p.x-q.x, p.y-q.y, p.z-q.z);
}
// P+v
Point operator+(const Point& p, const Vector &v)
{
return Point(p.x+v.x, p.y+v.y, p.z+v.z);
}
Point& operator+=(Point& p, const Vector &v)
{
p.x+=v.x;
p.y+=v.y;
p.z+=v.z;
return p;
}
// P-v
Point operator-(const Point& p, const Vector& v)
{
return Point(p.x-v.x, p.y-v.y, p.z-v.z);
}
Point& operator-=(Point& p, const Vector &v)
{
p.x-=v.x;
p.y-=v.y;
p.z-=v.z;
return p;
}
Vector operator+(const Vector& u, const Vector& v)
{
return Vector(u.x+v.x, u.y+v.y, u.z+v.z);
}
Vector& operator+=(Vector& u, const Vector& v)
{
u=u+v;
return u;
}
// v-u
Vector operator-(const Vector& u, const Vector& v)
{
return Vector(u.x-v.x, u.y-v.y, u.z-v.z);
}
Vector& operator-=(Vector& u, const Vector &v)
{
u=u-v;
return u;
}
// aP
Point operator* (const Point& p, float f)
{
return Point(p.x*f, p.y*f, p.z*f);
}
Point operator* (float f, const Point& p)
{
return p*f;
}
// P/a
Point operator/ (const Point& p, float f)
{
float inv = 1./f;
return Point(p.x*inv, p.y*inv, p.z*inv);
}
Point& operator/=(Point& p, float f)
{
float inv = 1./f;
p.x*=inv;
p.y*=inv;
p.z*=inv;
return p;
}
// av
Vector operator* (const Vector& v, float f)
{
return Vector(v.x*f, v.y*f, v.z*f);
}
Vector operator* (float f, const Vector& v)
{
return v*f;
}
// v/a
Vector operator/ (const Vector& v, float f)
{
float inv = 1./f;
return Vector(v.x*inv, v.y*inv, v.z*inv);
}
Vector& operator/=(Vector& v, float f)
{
float inv = 1./f;
v.x*=inv;
v.y*=inv;
v.z*=inv;
return v;
}
ostream& operator<< (ostream &os, const Point &p)
{
os << p.x << ", " << p.y << ", " << p.z << endl;
return os;
}
ostream& operator<< (ostream &os, const Vector &v)
{
os << v.x << ", " << v.y << ", " << v.z << endl;
return os;
}