Kiwano Engine v1.3.x
Vec2.hpp
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/math/Scalar.h>
23
24namespace kiwano
25{
26namespace math
27{
28template <typename _Ty>
29struct Vec2T
30{
31 using ValueType = _Ty;
32
33 ValueType x;
34 ValueType y;
35
36 Vec2T()
37 : x(ValueType(0))
38 , y(ValueType(0))
39 {
40 }
41
42 Vec2T(ValueType x, ValueType y)
43 : x(x)
44 , y(y)
45 {
46 }
47
48 Vec2T(const Vec2T& other)
49 : x(other.x)
50 , y(other.y)
51 {
52 }
53
54 inline ValueType Length() const
55 {
56 return static_cast<ValueType>(math::Sqrt(static_cast<float>(x * x + y * y)));
57 }
58
59 inline bool IsOrigin() const
60 {
61 return (x == 0) && (y == 0);
62 }
63
64 inline void Set(ValueType x, ValueType y)
65 {
66 this->x = x;
67 this->y = y;
68 }
69
70 inline const Vec2T operator+(const Vec2T& other) const
71 {
72 return Vec2T(x + other.x, y + other.y);
73 }
74
75 inline const Vec2T operator-(const Vec2T& other) const
76 {
77 return Vec2T(x - other.x, y - other.y);
78 }
79
80 inline const Vec2T operator*(ValueType val) const
81 {
82 return Vec2T(x * val, y * val);
83 }
84
85 inline const Vec2T operator/(ValueType val) const
86 {
87 return Vec2T(x / val, y / val);
88 }
89
90 inline const Vec2T operator-() const
91 {
92 return Vec2T(-x, -y);
93 }
94
95 inline Vec2T& operator+=(const Vec2T& other)
96 {
97 x += other.x;
98 y += other.y;
99 return (*this);
100 }
101
102 inline Vec2T& operator-=(const Vec2T& other)
103 {
104 x -= other.x;
105 y -= other.y;
106 return (*this);
107 }
108
109 inline Vec2T& operator*=(ValueType val)
110 {
111 x *= val;
112 y *= val;
113 return (*this);
114 }
115
116 inline Vec2T& operator/=(ValueType val)
117 {
118 x /= val;
119 y /= val;
120 return (*this);
121 }
122
123 inline bool operator==(const Vec2T& other) const
124 {
125 return (x == other.x) && (y == other.y);
126 }
127
128 inline bool operator!=(const Vec2T& other) const
129 {
130 return (x != other.x) || (y != other.y);
131 }
132};
133} // namespace math
134} // namespace kiwano
Definition: Vec2.hpp:30