GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_line_segment2.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
9
10#include "cassert_define.h"
11#include "math_util.h"
12
13
14namespace designlab
15{
16
18{
19 if (IsParallel(other))
20 {
21 return Vector2{ 0, 0 }; // 平行ならば交点は存在しない.
22 }
23
24 const Vector2 v1 = end - start;
25 const Vector2 v2 = other.end - other.start;
26 const Vector2 v3 = other.start - start;
27 const float d = v1.Cross(v2);
28
29 const float t1 = v3.Cross(v2) / d;
30 const float t2 = v3.Cross(v1) / d;
31
32 // t1, t2が0~1の範囲内にあるかならば,交点は線分上に存在する.
33
34 if (t1 < 0.0f - MathConst<float>::kAllowableError ||
38 {
39 return Vector2{ 0, 0 }; // 交点は線分の外.
40 }
41
42 return start + v1 * t1;
43}
44
46{
47 if (IsParallel(other))
48 {
49 return false; // 平行ならば交点は存在しない.
50 }
51
52 const Vector2 v1 = end - start;
53 const Vector2 v2 = other.end - other.start;
54 const Vector2 v3 = other.start - start;
55 const float d = v1.Cross(v2);
56
57 const float t1 = v3.Cross(v2) / d;
58 const float t2 = v3.Cross(v1) / d;
59
60 // t1, t2が0~1の範囲内にあるかならば,交点は線分上に存在する.
61
62 if (t1 < 0.0f - MathConst<float>::kAllowableError ||
66 {
67 return false; // 交点は線分の外.
68 }
69
70 return true;
71}
72
73bool LineSegment2::CheckAndGetIntersection(const LineSegment2& other, Vector2* intersection) const
74{
75 // 交点を代入するポインタは nullptr であってはならない.
76 assert(intersection != nullptr);
77
78 // 端点一致の場合,その点を返す
79 if ((start == other.start && end != other.end) ||
80 (start == other.end && end != other.start))
81 {
82 (*intersection) = start;
83 return true;
84 }
85 else if ((end == other.start && start != other.end) ||
86 (end == other.end && start != other.start))
87 {
88 (*intersection) = end;
89 return true;
90 }
91
92 if (IsParallel(other))
93 {
94 return false; // 平行ならば交点は存在しない.
95 }
96
97 const Vector2 v1 = end - start;
98 const Vector2 v2 = other.end - other.start;
99 const Vector2 v3 = other.start - start;
100 const float d = v1.Cross(v2);
101
102 const float t1 = v3.Cross(v2) / d;
103 const float t2 = v3.Cross(v1) / d;
104
105 // t1, t2が0~1の範囲内にあるかならば,交点は線分上に存在する.
106
107 if (t1 < 0.0f - MathConst<float>::kAllowableError ||
111 {
112 return false; // 交点は線分の外.
113 }
114
115 *intersection = start + v1 * t1;
116 return true;
117}
118
119} // namespace designlab
2次元の線分を表す構造体.
Vector2 start
線分の始点.
Vector2 GetIntersection(const LineSegment2 &other) const
他の線分との交点を求める.
bool CheckAndGetIntersection(const LineSegment2 &other, Vector2 *intersection) const
他の線分と交点が存在しているかどうか調べ,交点を返す関数.
constexpr bool IsParallel(const LineSegment2 &other) const
引数の線分と自身が平行かどうか調べる関数. 全て constexpr 関数で処理できるため非常に高速.
Vector2 end
線分の終点.
bool HasIntersection(const LineSegment2 &other) const
他の線分と交点が存在しているかどうか調べる関数.
float 型と double 型の定数を提供するクラス.
Definition math_const.h:25
2次元の位置ベクトルを表す構造体.
constexpr float Cross(const Vector2 &other) const noexcept
自分×引数 の外積の結果を返す.