WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. Vec2.h
Mission NodeSDK 6.1C++math

Vec2.h

SDK Source File: Vec2.h

Source / SDK 6.1 / C++ / math

Vec2.h

Vec2.h
CPP
1/* Copyright Statement:
2 *
3 * (C) 2021-2024 Cubios Inc. All rights reserved.
4 */
5
6#ifndef WASMLIBS_CPP_VEC2_H_
7#define WASMLIBS_CPP_VEC2_H_
8
9#include "Math.h"
10
11namespace Cubios
12{
13namespace Math
14{
15class Vec2
16{
17public:
18 /// constructors
19 Vec2();
20 Vec2(const float _x, const float _y);
21 Vec2(const Vec2& vec);
22 Vec2(const float* p);
23 void Set(const float _x, const float _y);
24 void Set(const Vec2& vec);
25 void Set(const float* p);
26 float Len() const;
27 void Norm();
28
29 void operator+=(const Vec2& v0);
30 void operator-=(const Vec2& v0);
31 void operator*=(const float s);
32 void operator/=(const float s);
33
34 bool IsEqual(const Vec2& v, const float tol) const;
35 /// fuzzy compare, returns -1, 0, +1
36 int Compare(const Vec2& v, float tol) const;
37
38 float X, Y;
39};
40
41inline Vec2::Vec2() : X(0.0f), Y(0.0f)
42{
43}
44
45inline Vec2::Vec2(const float _x, const float _y) : X(_x), Y(_y)
46{
47}
48
49inline Vec2::Vec2(const Vec2& vec) : X(vec.X), Y(vec.Y)
50{
51}
52
53inline Vec2::Vec2(const float* p) : X(p[0]), Y(p[1])
54{
55}
56
57inline void Vec2::Set(const float _x, const float _y)
58{
59 X = _x;
60 Y = _y;
61}
62
63inline void Vec2::Set(const Vec2& v)
64{
65 X = v.X;
66 Y = v.Y;
67}
68
69inline void Vec2::Set(const float* p)
70{
71 X = p[0];
72 Y = p[1];
73}
74
75inline float Vec2::Len() const
76{
77 return (float) sqrt(X * X + Y * Y);
78}
79
80inline void Vec2::Norm()
81{
82 float l = Len();
83 if (l > TINY)
84 {
85 X /= l;
86 Y /= l;
87 }
88}
89
90inline void Vec2::operator +=(const Vec2& v0)
91{
92 X += v0.X;
93 Y += v0.Y;
94}
95
96inline void Vec2::operator -=(const Vec2& v0)
97{
98 X -= v0.X;
99 Y -= v0.Y;
100}
101
102inline void Vec2::operator *=(const float s)
103{
104 X *= s;
105 Y *= s;
106}
107
108inline void Vec2::operator /=(const float s)
109{
110 X /= s;
111 Y /= s;
112}
113
114inline bool Vec2::IsEqual(const Vec2& v, const float tol) const
115{
116 if (fabs(v.X - X) > tol) return false;
117 else if (fabs(v.Y - Y) > tol) return false;
118 return true;
119}
120
121inline int Vec2::Compare(const Vec2& v, float tol) const
122{
123 if (fabs(v.X - X) > tol) return (v.X > X) ? +1 : -1;
124 else if (fabs(v.Y - Y) > tol) return (v.Y > Y) ? +1 : -1;
125 else return 0;
126}
127
128static inline
129Vec2 operator +(const Vec2& v0, const Vec2& v1)
130{
131 return Vec2(v0.X + v1.X, v0.Y + v1.Y);
132}
133
134static inline
135Vec2 operator -(const Vec2& v0, const Vec2& v1)
136{
137 return Vec2(v0.X - v1.X, v0.Y - v1.Y);
138}
139
140static inline Vec2 operator *(const Vec2& v0, const float s)
141{
142 return Vec2(v0.X * s, v0.Y * s);
143}
144
145static inline Vec2 operator -(const Vec2& v)
146{
147 return Vec2(-v.X, -v.Y);
148}
149
150//Dot product.
151static inline float operator %(const Vec2& v0, const Vec2& v1)
152{
153 return v0.X * v1.X + v0.Y * v1.Y;
154}
155
156}
157}
158#endif /* WASMLIBS_CPP_VEC2_H_ */
159
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

Color.h
Source / SDK 6.1 / C++ / math
Math.h
Source / SDK 6.1 / C++ / math
Transform.h
Source / SDK 6.1 / C++ / math
Color.h
Source / SDK 6.2 / C++ / math
Previous Node
Transform.h
Source / SDK 6.1 / C++ / math
Next Node
native access.h
Source / SDK 6.1 / C++ / Core