From 8505871a87b59b27acc0912a6dd815231c3b78b1 Mon Sep 17 00:00:00 2001 From: Bernhard Liebl Date: Wed, 27 Dec 2017 09:28:02 +0100 Subject: More exact picking for canvas editor --- core/math/geometry.cpp | 11 +++++++++++ core/math/geometry.h | 3 +++ 2 files changed, 14 insertions(+) (limited to 'core') diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 39bd34f03c..e1f2f91ebd 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -30,6 +30,17 @@ #include "geometry.h" #include "print_string.h" +bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon) { + + Vector indices = Geometry::triangulate_polygon(p_polygon); + for (int j = 0; j + 3 <= indices.size(); j += 3) { + int i1 = indices[j], i2 = indices[j + 1], i3 = indices[j + 2]; + if (Geometry::is_point_in_triangle(p_point, p_polygon[i1], p_polygon[i2], p_polygon[i3])) + return true; + } + return false; +} + void Geometry::MeshData::optimize_vertices() { Map vtx_remap; diff --git a/core/math/geometry.h b/core/math/geometry.h index cd069bd7a3..6a29a53f8f 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -512,6 +512,9 @@ public: return true; } + + static bool is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon); + static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) { Vector2 p = p_point - p_segment[0]; -- cgit v1.2.3