-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
103 lines (84 loc) · 1.94 KB
/
point.cpp
File metadata and controls
103 lines (84 loc) · 1.94 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
#include "primitives.hpp"
Point::Point(double x, double y, double z, const Rect* parent, const Color& color) : x(x), y(y), z(z), parent(parent), color(color) {}
Point Point::toGlobalCoordinate() const
{
if (parent)
{
return Point{ parent->x + x, parent->y + y, z, parent, color };
}
else
{
return Point{ x, y, z, nullptr, color };
}
}
Point operator*(const Point& p, double s)
{
return Point{ p.x*s, p.y*s, p.z*s, p.parent, p.color };
}
Point operator/(const Point& p, double s)
{
return Point{ p.x/s, p.y/s, p.z/s, p.parent, p.color };
}
bool Point::operator==(const Point& other) const
{
return (x == other.x) && (y == other.y) && (z == other.z) && (parent == other.parent);
}
bool Point::operator!=(const Point& other) const
{
return !(*this == other);
}
Point Point::flipped() const
{
return Point{ y, x, z, parent, color };
}
Point operator+(const Point& p1, const Point& p2)
{
return Point{ p1.x + p2.x, p1.y + p2.y, p1.z + p2.z };
}
Point operator-(const Point& p1, const Point& p2)
{
return Point{ p1.x - p2.x, p1.y - p2.y, p1.z - p2.z };
}
Point4D operator*(const Point4D& p, double s)
{
return Point4D{ p.x*s, p.y*s, p.z*s, 1.0 };
}
Point4D operator/(const Point4D& p, double s)
{
return Point4D{ p.x / s, p.y / s, p.z / s, 1.0 };
}
Point4D operator-(const Point4D& x, const Point4D& y)
{
return Point4D{ x.x - y.x,
x.y - y.y,
x.z - y.z,
1.0 };
}
Point4D operator+(const Point4D& x, const Point4D& y)
{
return Point4D{ x.x + y.x,
x.y + y.y,
x.z + y.z,
1.0 };
}
double dot(const Point4D& v1, const Point4D& v2)
{
auto result = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
return result;
}
Point4D cross(const Point4D& v1, const Point4D& v2)
{
return Point4D{ v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x,
1.0 };
}
double length(const Point4D& v)
{
return std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
Point4D normalize(const Point4D& v)
{
auto l = length(v);
return v / l;
}