Arty
float2.h
1 #ifndef FLOAT2_H
2 #define FLOAT2_H
3 
4 #include <cmath>
5 #include "common.h"
6 
7 struct float3;
8 struct float4;
9 
10 struct float2 {
11  union {
12  struct { float x, y; };
13  float values[2];
14  };
15 
16  float2() {}
17  explicit float2(float x) : x(x), y(x) {}
18  explicit float2(const float3& xy);
19  explicit float2(const float4& xy);
20  float2(float x, float y) : x(x), y(y) {}
21 
22  bool operator == (const float2& other) const {
23  return x == other.x && y == other.y;
24  }
25 
26  bool operator != (const float2& other) const {
27  return x != other.x || y != other.y;
28  }
29 
30  float operator [] (size_t i) const { return values[i]; }
31  float& operator [] (size_t i) { return values[i]; }
32 
33  float2& operator += (const float2& a) {
34  x += a.x; y += a.y;
35  return *this;
36  }
37 
38  float2& operator -= (const float2& a) {
39  x -= a.x; y -= a.y;
40  return *this;
41  }
42 
43  float2& operator *= (float a) {
44  x *= a; y *= a;
45  return *this;
46  }
47 
48  float2& operator *= (const float2& a) {
49  x *= a.x; y *= a.y;
50  return *this;
51  }
52 };
53 
54 inline float2 operator * (float a, const float2& b) {
55  return float2(a * b.x, a * b.y);
56 }
57 
58 inline float2 operator * (const float2& a, float b) {
59  return float2(a.x * b, a.y * b);
60 }
61 
62 inline float2 operator / (const float2& a, float b) {
63  return a * (1.0f / b);
64 }
65 
66 inline float2 operator - (const float2& a, const float2& b) {
67  return float2(a.x - b.x, a.y - b.y);
68 }
69 
70 inline float2 operator + (const float2& a, const float2& b) {
71  return float2(a.x + b.x, a.y + b.y);
72 }
73 
74 inline float2 operator * (const float2& a, const float2& b) {
75  return float2(a.x * b.x, a.y * b.y);
76 }
77 
78 inline float2 min(const float2& a, const float2& b) {
79  return float2(a.x < b.x ? a.x : b.x,
80  a.y < b.y ? a.y : b.y);
81 }
82 
83 inline float2 max(const float2& a, const float2& b) {
84  return float2(a.x > b.x ? a.x : b.x,
85  a.y > b.y ? a.y : b.y);
86 }
87 
88 inline float dot(const float2& a, const float2& b) {
89  return a.x * b.x + a.y * b.y;
90 }
91 
92 inline float lensqr(const float2& a) {
93  return dot(a, a);
94 }
95 
96 inline float length(const float2& a) {
97  return std::sqrt(dot(a, a));
98 }
99 
100 inline float2 normalize(const float2& a) {
101  return a * (1.0f / length(a));
102 }
103 
104 #endif // FLOAT2_H
Definition: float2.h:10
Definition: float3.h:10
Definition: float4.h:9