ge211  2021.5.1
A student game engine
ge211_geometry.cxx
1 #include "ge211_geometry.hxx"
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(360 - 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 Transform::operator==(Transform const& other) const NOEXCEPT
135 {
136  return get_rotation() == other.get_rotation() &&
137  get_flip_h() == other.get_flip_h() &&
138  get_flip_v() == other.get_flip_v() &&
139  get_scale_x() == other.get_scale_x() &&
140  get_scale_y() == other.get_scale_y();
141 }
142 
143 bool Transform::operator!=(Transform const& other) const NOEXCEPT
144 {
145  return !(*this == other);
146 }
147 
148 }
149 
150 }
ge211::geometry::Transform::flip_v
static Transform flip_v()
Constructs a transform that flips the sprite vertically.
Definition: ge211_geometry.cxx:24
ge211::geometry::Transform::set_flip_h
Transform & set_flip_h(bool)
Modifies this transform to determine whether to flip horizontally.
Definition: ge211_geometry.cxx:51
ge211::geometry::Transform::flip_h
static Transform flip_h()
Constructs a transform that flips the sprite horizontally.
Definition: ge211_geometry.cxx:19
ge211::geometry::Transform::scale_y
static Transform scale_y(double)
Constructs a transform that scales the sprite in the y dimension.
Definition: ge211_geometry.cxx:39
ge211::geometry::Transform::set_flip_v
Transform & set_flip_v(bool)
Modifies this transform to determine whether to flip vertically.
Definition: ge211_geometry.cxx:57
ge211::geometry::Transform::rotation
static Transform rotation(double)
Constructs a rotating transform, given the rotation in degrees clockwise.
Definition: ge211_geometry.cxx:14
ge211::geometry::Transform::operator==
bool operator==(const Transform &other) const
Equality for Transforms.
Definition: ge211_geometry.cxx:134
ge211::geometry::Transform::set_scale_y
Transform & set_scale_y(double)
Modifies this transform to scale the sprite vertically.
Definition: ge211_geometry.cxx:76
ge211::geometry::Transform::get_scale_x
double get_scale_x() const
Returns how much the sprite will be scaled horizontally.
Definition: ge211_geometry.cxx:97
ge211::geometry::Transform::set_rotation
Transform & set_rotation(double)
Modifies this transform to have the given rotation, in degrees.
Definition: ge211_geometry.cxx:44
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::geometry::Transform::operator!=
bool operator!=(const Transform &other) const
Disequality for Transforms.
Definition: ge211_geometry.cxx:143
ge211::geometry::Transform::scale_x
static Transform scale_x(double)
Constructs a transform that scales the sprite in the x dimension.
Definition: ge211_geometry.cxx:34
ge211::geometry::Transform::scale
static Transform scale(double)
Constructs a transform that scales the sprite in both dimensions.
Definition: ge211_geometry.cxx:29
ge211::geometry::Transform::is_identity
bool is_identity() const
Is this transformation the identity transformation that does nothing? Because floating point is appro...
Definition: ge211_geometry.cxx:107
ge211::geometry::Transform::set_scale_x
Transform & set_scale_x(double)
Modifies this transform to scale the sprite horizontally.
Definition: ge211_geometry.cxx:70
ge211::geometry::Transform::get_scale_y
double get_scale_y() const
Returns how much the sprite will be scaled vertically.
Definition: ge211_geometry.cxx:102
ge211::geometry::Transform::set_scale
Transform & set_scale(double)
Modifies this transform to scale the sprite by the given amount in both dimensions.
Definition: ge211_geometry.cxx:63
ge211::geometry::Transform::Transform
Transform()
Constructs the identity transform, which has no effect.
Definition: ge211_geometry.cxx:9
ge211::geometry::Transform
A rendering transformation, which can scale, flip, and rotate.
Definition: ge211_geometry.hxx:793
ge211::geometry::Transform::get_flip_v
bool get_flip_v() const
Returns whether the sprite will be flipped vertically.
Definition: ge211_geometry.cxx:92
ge211::geometry::Transform::get_flip_h
bool get_flip_h() const
Returns whether the sprite will be flipped horizontally.
Definition: ge211_geometry.cxx:87
ge211::geometry::Transform::inverse
Transform inverse() const
Returns the inverse of this transform.
Definition: ge211_geometry.cxx:123
ge211::geometry::Transform::operator*
Transform operator*(const Transform &other) const
Composes two transforms to combine both of their effects.
Definition: ge211_geometry.cxx:112
ge211::geometry::Transform::get_rotation
double get_rotation() const
Returns the rotation that will be applied to the sprite.
Definition: ge211_geometry.cxx:82