Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
vec2.h
1 #pragma once
2 
3 #include <cmath>
4 #include "../utils/unicode.h"
5 #include "../utils/formatter.h"
6 #include "utils.h"
7 
8 namespace smlt {
9 
10 struct Vec3;
11 struct Vec4;
12 struct Degrees;
13 
14 struct Vec2 {
15 
16 public:
17  static const Vec2 NEGATIVE_X;
18  static const Vec2 POSITIVE_X;
19  static const Vec2 NEGATIVE_Y;
20  static const Vec2 POSITIVE_Y;
21 
22  static const Vec2 DOWN;
23  static const Vec2 LEFT;
24  static const Vec2 ONE;
25  static const Vec2 RIGHT;
26  static const Vec2 UP;
27  static const Vec2 ZERO;
28 
29  float x;
30  float y;
31 
32  Vec2():
33  x(0.0f), y(0.0f) {
34  }
35 
36  Vec2(float x, float y):
37  x(x), y(y) {
38 
39  }
40 
41  Vec2 rotated_by(const Degrees& degrees) const;
42 
43  float length() const {
44  return fast_sqrt(x * x + y * y);
45  }
46 
47  float length_squared() const {
48 #ifdef __DREAMCAST__
49  return MATH_Sum_of_Squares(x, y, 0.0f, 0.0f);
50 #else
51  return dot(*this);
52 #endif
53  }
54 
55  void normalize() {
56  float l = fast_inverse_sqrt(length_squared());
57  x *= l;
58  y *= l;
59  }
60 
61  Vec2 normalized() const {
62  Vec2 ret = *this;
63  ret.normalize();
64  return ret;
65  }
66 
67  void limit(float l) {
68  if(length() > l) {
69  normalize();
70  *this *= l;
71  }
72  }
73 
74  Vec2 operator*(float rhs) const {
75  Vec2 result(x * rhs, y * rhs);
76  return result;
77  }
78 
79  Vec2& operator*=(float rhs) {
80  *this = *this * rhs;
81  return *this;
82  }
83 
84  Vec2& operator+=(const Vec2& rhs) {
85  *this = *this + rhs;
86  return *this;
87  }
88 
89  Vec2& operator-=(const Vec2& rhs) {
90  *this = *this - rhs;
91  return *this;
92  }
93 
94  Vec2 operator+(const Vec2& rhs) const {
95  return Vec2(x + rhs.x, y + rhs.y);
96  }
97 
98  Vec2& operator/=(float rhs) {
99  float l = fast_divide(1.0f, rhs);
100  *this *= l;
101  return *this;
102  }
103 
104  Vec2 operator/(float rhs) const {
105  float l = fast_divide(1.0f, rhs);
106  Vec2 result(x * l, y * l);
107  return result;
108  }
109 
110  Vec2 operator-() const {
111  return Vec2(-x, -y);
112  }
113 
114  Vec2 operator-(const Vec2& rhs) const {
115  return Vec2(x - rhs.x, y - rhs.y);
116  }
117 
118  float dot(const Vec2& rhs) const {
119 #ifdef __DREAMCAST__
120  return MATH_fipr(x, y, 0.0f, 0.0f, rhs.x, rhs.y, 0.0f, 0.0f);
121 #else
122  return x * rhs.x + y * rhs.y;
123 #endif
124  }
125 
126  Vec3 xyz(float z = 0.0f) const;
127 
128  Vec4 xyzw(float z=0.0f, float w=1.0f) const;
129 
130  friend std::ostream& operator<<(std::ostream& stream, const Vec2& vec);
131 
132  bool equals(const Vec2& rhs) const {
133  return x == rhs.x && y == rhs.y;
134  }
135 
136  friend bool operator==(const Vec2& lhs, const Vec2& rhs);
137 };
138 
139 bool operator==(const Vec2& lhs, const Vec2& rhs);
140 bool operator!=(const Vec2& lhs, const Vec2& rhs);
141 Vec2 operator*(float lhs, const Vec2& rhs);
142 
143 std::ostream& operator<<(std::ostream& stream, const Vec2& vec);
144 
145 }
smlt::Vec3
Definition: vec3.h:23
smlt
Definition: animation.cpp:25
smlt::Degrees
Definition: degrees.h:7
smlt::Vec4
Definition: vec4.h:12
smlt::Vec2
Definition: vec2.h:14