summaryrefslogtreecommitdiff
path: root/engine/src/Point.cpp
blob: 8dd50bd61d45028be0408e3357f97e0804d3a6af (plain)
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
#include <kurator/engine/Point.h>

#include <cmath>


namespace kurator
{
namespace engine
{


double
Point::magnitude() const
{
	return std::sqrt(std::pow(x, 2) + std::pow(y, 2));
}


double
Point::distance(const Point& other) const
{
	return std::sqrt(std::pow(other.x - x, 2) + std::pow(other.y - y, 2));
}


double
Point::angle() const
{
	return std::atan2(y, x);  // (+x, _) is 0
}


Point
Point::rotate(const double angle) const
{
	return {
		x * std::cos(angle) - y * std::sin(angle),
		x * std::sin(angle) + y * std::cos(angle),
	};
}


Point
Point::scale(const double _scale) const
{
	return {x * _scale, y * _scale};
}


Point
Point::normalized() const
{
	return scale(1.0 / magnitude());
}


Point
Point::operator-(const Point& other) const
{
	return subtract(other);
}


Point
Point::operator+(const Point& other) const
{
	return {x + other.x, y + other.y};
}


Point
Point::subtract(const Point& other) const
{
	return {x - other.x, y - other.y};
}


Point
Point::subtract(const double _x, const double _y) const
{
	return {x - _x, y - _y};
}


bool
Point::operator==(const Point& other) const
{
	return x == other.x && y == other.y;
}


bool
Point::operator!=(const Point& other) const
{
	return x != other.x || y != other.y;
}


Point
abs(const Point& point)
{
	return {std::abs(point.x), std::abs(point.y)};
}


}  // namespace engine
}  // namespace kurator