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