DigitalCurling3  1.0.0
A curling simulation system for curling AIs
vector2.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2022 UEC Takeshi Ito Laboratory
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
25 
26 #ifndef DIGITALCURLING3_VECTOR2_HPP
27 #define DIGITALCURLING3_VECTOR2_HPP
28 
29 #include <cmath>
30 #include "json/common.hpp"
31 
32 namespace digitalcurling3 {
33 
34 
35 
37 struct Vector2 {
38  float x;
39  float y;
40 
42  constexpr Vector2() : x(0.f), y(0.f) {}
43 
48  constexpr Vector2(float x, float y) : x(x), y(y) {}
49 
54  constexpr Vector2 & operator += (Vector2 v)
55  {
56  x += v.x;
57  y += v.y;
58  return *this;
59  }
60 
65  constexpr Vector2 & operator -= (Vector2 v)
66  {
67  x -= v.x;
68  y -= v.y;
69  return *this;
70  }
71 
76  constexpr Vector2 & operator *= (float f)
77  {
78  x *= f;
79  y *= f;
80  return *this;
81  }
82 
87  constexpr Vector2 & operator /= (float f)
88  {
89  x /= f;
90  y /= f;
91  return *this;
92  }
93 
97  float Length() const
98  {
99  return std::hypot(x, y);
100  }
101 };
102 
108 {
109  return { -v.x, -v.y };
110 }
111 
118 {
119  return { v1.x + v2.x, v1.y + v2.y };
120 }
121 
128 {
129  return { v1.x - v2.x, v1.y - v2.y };
130 }
131 
137 constexpr Vector2 operator * (float f, Vector2 v)
138 {
139  return { f * v.x, f * v.y };
140 }
141 
147 constexpr Vector2 operator * (Vector2 v, float f)
148 {
149  return f * v;
150 }
151 
157 constexpr Vector2 operator / (Vector2 v, float f)
158 {
159  return { v.x / f, v.y / f };
160 }
161 
162 
163 // json
164 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
165  Vector2,
166  x,
167  y
168 )
169 
170 } // namespace digitalcurling3
171 
172 #endif // DIGITALCURLING3_VECTOR2_HPP
digitalcurling3::operator/
constexpr Vector2 operator/(Vector2 v, float f)
ベクトルとスカラー値の除算を行います
Definition: vector2.hpp:157
digitalcurling3::Vector2::Vector2
constexpr Vector2(float x, float y)
指定された座標で初期化します
Definition: vector2.hpp:48
digitalcurling3::Vector2
2次元ベクトル
Definition: vector2.hpp:37
common.hpp
必要なJSON変換関数を定義します
digitalcurling3::Vector2::y
float y
y座標
Definition: vector2.hpp:39
digitalcurling3::Vector2::operator*=
constexpr Vector2 & operator*=(float f)
ベクトルにスカラー値を乗算します
Definition: vector2.hpp:76
digitalcurling3::Vector2::operator-=
constexpr Vector2 & operator-=(Vector2 v)
ベクトルの減算を行います
Definition: vector2.hpp:65
digitalcurling3::Vector2::Vector2
constexpr Vector2()
(0, 0)で初期化します
Definition: vector2.hpp:42
digitalcurling3
Digital Curling ライブラリはこの名前空間の中に定義されます
Definition: polymorphic_json.hpp:37
digitalcurling3::operator-
constexpr Vector2 operator-(Vector2 v)
ベクトルを反転します
Definition: vector2.hpp:107
digitalcurling3::Vector2::x
float x
x座標
Definition: vector2.hpp:38
digitalcurling3::Vector2::operator/=
constexpr Vector2 & operator/=(float f)
ベクトルをスカラー値で除算します
Definition: vector2.hpp:87
digitalcurling3::operator*
constexpr Vector2 operator*(float f, Vector2 v)
ベクトルとスカラー値の乗算を行います
Definition: vector2.hpp:137
digitalcurling3::Vector2::operator+=
constexpr Vector2 & operator+=(Vector2 v)
ベクトルの加算を行います
Definition: vector2.hpp:54
digitalcurling3::operator+
constexpr Vector2 operator+(Vector2 v1, Vector2 v2)
ベクトル同士の加算を行います
Definition: vector2.hpp:117
digitalcurling3::Vector2::Length
float Length() const
ベクトルの長さを得ます
Definition: vector2.hpp:97