ge211
ge211_geometry.cpp
1 #include "ge211_geometry.h"
2 
3 #include <cmath>
4 
5 namespace ge211 {
6 
7 namespace geometry {
8 
10  : rotation_{0}, scale_x_{1.0}, scale_y_{1.0},
11  flip_h_{false}, flip_v_{false}
12 { }
13 
14 Transform Transform::rotation(double degrees) noexcept
15 {
16  return Transform().set_rotation(degrees);
17 }
18 
20 {
21  return Transform().set_flip_h(true);
22 }
23 
25 {
26  return Transform().set_flip_v(true);
27 }
28 
29 Transform Transform::scale(double factor) noexcept
30 {
31  return Transform().set_scale(factor);
32 }
33 
34 Transform Transform::scale_x(double factor) noexcept
35 {
36  return Transform().set_scale_x(factor);
37 }
38 
39 Transform Transform::scale_y(double factor) noexcept
40 {
41  return Transform().set_scale_y(factor);
42 }
43 
44 Transform& Transform::set_rotation(double rotation) noexcept
45 {
46  while (rotation < 0) rotation += 360;
47  rotation_ = std::fmod(rotation, 360);
48  return *this;
49 }
50 
51 Transform& Transform::set_flip_h(bool flip_h) noexcept
52 {
53  flip_h_ = flip_h;
54  return *this;
55 }
56 
57 Transform& Transform::set_flip_v(bool flip_v) noexcept
58 {
59  flip_v_ = flip_v;
60  return *this;
61 }
62 
63 Transform& Transform::set_scale(double scale) noexcept
64 {
65  scale_x_ = scale;
66  scale_y_ = scale;
67  return *this;
68 }
69 
70 Transform& Transform::set_scale_x(double scale_x) noexcept
71 {
72  scale_x_ = scale_x;
73  return *this;
74 }
75 
76 Transform& Transform::set_scale_y(double scale_y) noexcept
77 {
78  scale_y_ = scale_y;
79  return *this;
80 }
81 
82 double Transform::get_rotation() const noexcept
83 {
84  return rotation_;
85 }
86 
87 bool Transform::get_flip_h() const noexcept
88 {
89  return flip_h_;
90 }
91 
92 bool Transform::get_flip_v() const noexcept
93 {
94  return flip_v_;
95 }
96 
97 double Transform::get_scale_x() const noexcept
98 {
99  return scale_x_;
100 }
101 
102 double Transform::get_scale_y() const noexcept
103 {
104  return scale_y_;
105 }
106 
107 bool Transform::is_identity() const noexcept
108 {
109  return *this == Transform();
110 }
111 
112 Transform Transform::operator*(const Transform& other) const noexcept
113 {
114  Transform result;
115  result.set_rotation(rotation_ + other.rotation_);
116  result.set_flip_h(flip_h_ ^ other.flip_h_);
117  result.set_flip_v(flip_v_ ^ other.flip_v_);
118  result.set_scale_x(scale_x_ * other.scale_x_);
119  result.set_scale_y(scale_y_ * other.scale_y_);
120  return result;
121 }
122 
124 {
125  Transform result;
126  result.set_rotation(-rotation_);
127  result.set_flip_h(flip_h_);
128  result.set_flip_v(flip_v_);
129  result.set_scale_x(1 / scale_x_);
130  result.set_scale_y(1 / scale_y_);
131  return result;
132 }
133 
134 bool operator==(const Transform& t1, const Transform& t2) noexcept
135 {
136  return t1.get_rotation() == t2.get_rotation() &&
137  t1.get_flip_h() == t2.get_flip_h() &&
138  t1.get_flip_v() == t2.get_flip_v() &&
139  t1.get_scale_x() == t2.get_scale_x() &&
140  t1.get_scale_y() == t2.get_scale_y();
141 }
142 
143 bool operator!=(const Transform& t1, const Transform& t2) noexcept
144 {
145  return !(t1 == t2);
146 }
147 
148 }
149 
150 }
Transform & set_flip_h(bool) noexcept
Modifies this transform to determine whether to flip horizontally.
static Transform flip_v() noexcept
Constructs a transform that flips the sprite vertically.
Transform & set_rotation(double) noexcept
Modifies this transform to have the given rotation, in degrees degrees.
bool operator!=(const Transform &t1, const Transform &t2) noexcept
Disequality for transforms.
The game engine namespace.
Definition: ge211.h:17
double get_scale_x() const noexcept
Returns how much the sprite will be scaled horizontally.
static Transform rotation(double) noexcept
Constructs a rotating transform, given the rotation in degrees clockwise.
bool operator==(const Transform &t1, const Transform &t2) noexcept
Equality for transforms.
bool get_flip_v() const noexcept
Returns whether the sprite will be flipped vertically.
Transform & set_flip_v(bool) noexcept
Modifies this transform to determine whether to flip vertically.
double get_scale_y() const noexcept
Returns how much the sprite will be scaled vertically.
static Transform scale_x(double) noexcept
Constructs a transform that scales the sprite in the x dimension.
Transform inverse() const noexcept
Returns the inverse of this transform.
Transform & set_scale_x(double) noexcept
Modifies this transform to scale the sprite horizontally.
Transform & set_scale(double) noexcept
Modifies this transform to scale the sprite by the given amount in both dimensions.
Transform & set_scale_y(double) noexcept
Modifies this transform to scale the sprite vertically.
A rendering transform, which can scale, flip, and rotate.
static Transform scale(double) noexcept
Constructs a transform that scales the sprite in both dimensions.
static Transform scale_y(double) noexcept
Constructs a transform that scales the sprite in the y dimension.
Transform operator*(const Transform &) const noexcept
Composes two transforms to combine both of their effects.
double get_rotation() const noexcept
Returns the rotation that will be applied to the sprite.
static Transform flip_h() noexcept
Constructs a transform that flips the sprite horizontally.
Transform() noexcept
Constructs the identity transform, which has no effect.
bool is_identity() const noexcept
Is this transformation the identity transformation that does nothing? Because floating point is appro...
bool get_flip_h() const noexcept
Returns whether the sprite will be flipped horizontally.