GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_vector2.cpp
[詳解]
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#include "math_vector2.h"
9
10
11namespace designlab
12{
13
15{
16 x += other.x;
17 y += other.y;
18 return *this;
19}
20
22{
23 x -= other.x;
24 y -= other.y;
25 return *this;
26}
27
29{
30 x *= s;
31 y *= s;
32 return *this;
33}
34
36{
37 x /= s;
38 y /= s;
39 return *this;
40}
41
43{
44 float length = GetLength();
45
46 if (math_util::IsEqual(length, 0.f))
47 {
48 return { 0, 0 };
49 }
50
51 return *this * (1.f / length);
52}
53
54std::string Vector2::ToString() const
55{
56 return std::string("( x : ") +
58 std::string(", y : ") +
60 std::string(")");
61}
62
63std::string Vector2::ToCsvString() const
64{
65 return math_util::FloatingPointNumToString(x) + std::string(",") +
67}
68
69} // namespace designlab
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
2次元の位置ベクトルを表す構造体.
std::string ToString() const
このベクトルを文字列にして返す. (x, y) の形式,小数点以下3桁まで.
Vector2 & operator*=(float s)
Vector2 GetNormalized() const
このベクトルを正規化したベクトルを返す.
Vector2 & operator+=(const Vector2 &other)
std::string ToCsvString() const
このベクトルをCSV形式の文字列にして返す. x, y, z の形式,小数点以下3桁まで.
float GetLength() const
このベクトルの長さを返す.
Vector2 & operator/=(float s)
Vector2 & operator-=(const Vector2 &other)