GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_vector2.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_MATH_VECTOR2_H_
9#define DESIGNLAB_MATH_VECTOR2_H_
10
11#include <cmath>
12#include <string>
13
14#include "math_util.h"
15
16
17namespace designlab
18{
30struct Vector2 final
31{
32 constexpr Vector2() : x(0), y(0) {}
33 constexpr Vector2(float x_pos, float y_pos) : x(x_pos), y(y_pos) {}
34 constexpr Vector2(const Vector2& other) = default;
35 constexpr Vector2(Vector2&& other) noexcept = default;
36 constexpr Vector2& operator =(const Vector2& other) = default;
37
38 constexpr Vector2 operator +() const { return *this; }
39 constexpr Vector2 operator -() const { return{ -x, -y }; }
40
41 constexpr Vector2 operator +(const Vector2& other) const
42 {
43 return{ x + other.x, y + other.y };
44 }
45
46 constexpr Vector2 operator -(const Vector2& other) const
47 {
48 return{ x - other.x, y - other.y };
49 }
50
51 constexpr Vector2 operator *(float s) const { return{ x * s, y * s }; }
52 constexpr Vector2 operator /(float s) const { return{ x / s, y / s }; }
53
54 Vector2& operator +=(const Vector2& other);
55 Vector2& operator -=(const Vector2& other);
56 Vector2& operator *=(float s);
57 Vector2& operator /=(float s);
58
59 constexpr bool operator==(const Vector2& other) const
60 {
61 return math_util::IsEqual(x, other.x) && math_util::IsEqual(y, other.y);
62 }
63
64 constexpr bool operator!=(const Vector2& other) const
65 {
66 return !(*this == other);
67 }
68
69
73 [[nodiscard]]
74 constexpr float GetSquaredLength() const noexcept { return Dot(*this); }
75
79 [[nodiscard]]
80 float GetLength() const { return std::sqrt(GetSquaredLength()); }
81
85 [[nodiscard]]
86 constexpr float Dot(const Vector2& other) const noexcept
87 {
88 return x * other.x + y * other.y;
89 }
90
95 [[nodiscard]]
96 constexpr float Cross(const Vector2& other) const noexcept
97 {
98 return x * other.y - y * other.x;
99 }
100
104 [[nodiscard]]
105 float GetDistanceFrom(const Vector2& other) const noexcept
106 {
107 return (other - *this).GetLength();
108 }
109
112 [[nodiscard]]
113 Vector2 GetNormalized() const;
114
118 [[nodiscard]]
119 constexpr bool IsZero() const noexcept
120 {
121 return math_util::IsEqual(x, 0.0f) && math_util::IsEqual(y, 0.0f);
122 }
123
129 void Normalize() noexcept { *this = GetNormalized(); }
130
131
135 [[nodiscard]]
136 constexpr static Vector2 GetZeroVec() noexcept { return Vector2(0.f, 0.f); }
137
141 [[nodiscard]] std::string ToString() const;
142
146 [[nodiscard]] std::string ToCsvString() const;
147
148
149 float x;
150 float y;
151};
152
153
158constexpr Vector2 operator *(float s, const Vector2& v)
159{
160 return { s * v.x, s * v.y };
161}
162
163template <class Char>
164std::basic_ostream<Char>& operator <<(std::basic_ostream<Char>& os, const Vector2& v)
165{
166 return os << math_util::FloatingPointNumToString(v.x) <<
167 Char(',') << math_util::FloatingPointNumToString(v.y);
168}
169
170template <class Char>
171inline std::basic_istream<Char>& operator >>(
172 std::basic_istream<Char>& is, Vector2& v)
173{
174 Char unused = 0;
175 return is >> unused >> v.x >> unused >> v.y >> unused;
176}
177
178} // namespace designlab
179
180
181#endif // DESIGNLAB_MATH_VECTOR2_H_
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
Definition math_util.h:161
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
Definition math_util.h:45
constexpr Quaternion operator*(float s, const Quaternion &q)
std::basic_istream< Char > & operator>>(std::basic_istream< Char > &is, EulerXYZ &r)
入力ストリーム
Definition math_euler.h:128
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &os, const EulerXYZ &r)
出力ストリーム.Csv形式で出力する.カンマ区切り.単位は [rad].
Definition math_euler.h:117
2次元の位置ベクトルを表す構造体.
float GetDistanceFrom(const Vector2 &other) const noexcept
このベクトルと other の距離を返す.
std::string ToString() const
このベクトルを文字列にして返す. (x, y) の形式,小数点以下3桁まで.
constexpr Vector2 & operator=(const Vector2 &other)=default
constexpr bool IsZero() const noexcept
このベクトルが0ならば true.
constexpr Vector2 operator-() const
Vector2 & operator*=(float s)
constexpr Vector2 operator*(float s) const
constexpr Vector2 operator+() const
constexpr Vector2(Vector2 &&other) noexcept=default
constexpr float Dot(const Vector2 &other) const noexcept
自分・引数 の内積の結果を返す.
constexpr Vector2()
Vector2 GetNormalized() const
このベクトルを正規化したベクトルを返す.
constexpr bool operator==(const Vector2 &other) const
Vector2 & operator+=(const Vector2 &other)
constexpr float GetSquaredLength() const noexcept
このベクトルの長さの2乗を返す.
constexpr Vector2 operator/(float s) const
static constexpr Vector2 GetZeroVec() noexcept
零ベクトルを返す. 静的な関数なので Vector2::GetZeroVec() と呼ぶことができる.
constexpr float Cross(const Vector2 &other) const noexcept
自分×引数 の外積の結果を返す.
constexpr Vector2(float x_pos, float y_pos)
std::string ToCsvString() const
このベクトルをCSV形式の文字列にして返す. x, y, z の形式,小数点以下3桁まで.
float GetLength() const
このベクトルの長さを返す.
Vector2 & operator/=(float s)
void Normalize() noexcept
このベクトルを正規化する. 絶対値が0のベクトルの場合,そのまま0ベクトルになる.
constexpr Vector2(const Vector2 &other)=default
constexpr bool operator!=(const Vector2 &other) const
Vector2 & operator-=(const Vector2 &other)