Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_2d/triangle2.cpp
10277 views
1
/**************************************************************************/
2
/* triangle2.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "triangle2.h"
32
33
Vector2 Triangle2::get_random_point_inside() const {
34
real_t a = Math::random(0.0, 1.0);
35
real_t b = Math::random(0.0, 1.0);
36
if (a > b) {
37
SWAP(a, b);
38
}
39
40
return vertex[0] * a + vertex[1] * (b - a) + vertex[2] * (1.0f - b);
41
}
42
43
Vector2 Triangle2::get_closest_point_to(const Vector2 &p_point) const {
44
Vector2 edge0 = vertex[1] - vertex[0];
45
Vector2 edge1 = vertex[2] - vertex[0];
46
Vector2 v0 = vertex[0] - p_point;
47
48
real_t a = edge0.dot(edge0);
49
real_t b = edge0.dot(edge1);
50
real_t c = edge1.dot(edge1);
51
real_t d = edge0.dot(v0);
52
real_t e = edge1.dot(v0);
53
54
real_t det = a * c - b * b;
55
real_t s = b * e - c * d;
56
real_t t = b * d - a * e;
57
58
if (s + t < det) {
59
if (s < 0.f) {
60
if (t < 0.f) {
61
if (d < 0.f) {
62
s = CLAMP(-d / a, 0.f, 1.f);
63
t = 0.f;
64
} else {
65
s = 0.f;
66
t = CLAMP(-e / c, 0.f, 1.f);
67
}
68
} else {
69
s = 0.f;
70
t = CLAMP(-e / c, 0.f, 1.f);
71
}
72
} else if (t < 0.f) {
73
s = CLAMP(-d / a, 0.f, 1.f);
74
t = 0.f;
75
} else {
76
real_t inv_det = 1.f / det;
77
s *= inv_det;
78
t *= inv_det;
79
}
80
} else {
81
if (s < 0.f) {
82
real_t tmp0 = b + d;
83
real_t tmp1 = c + e;
84
if (tmp1 > tmp0) {
85
real_t numer = tmp1 - tmp0;
86
real_t denom = a - 2 * b + c;
87
s = CLAMP(numer / denom, 0.f, 1.f);
88
t = 1 - s;
89
} else {
90
t = CLAMP(-e / c, 0.f, 1.f);
91
s = 0.f;
92
}
93
} else if (t < 0.f) {
94
if (a + d > b + e) {
95
real_t numer = c + e - b - d;
96
real_t denom = a - 2 * b + c;
97
s = CLAMP(numer / denom, 0.f, 1.f);
98
t = 1 - s;
99
} else {
100
s = CLAMP(-d / a, 0.f, 1.f);
101
t = 0.f;
102
}
103
} else {
104
real_t numer = c + e - b - d;
105
real_t denom = a - 2 * b + c;
106
s = CLAMP(numer / denom, 0.f, 1.f);
107
t = 1.f - s;
108
}
109
}
110
111
return vertex[0] + s * edge0 + t * edge1;
112
}
113
114