From 082db8371bbfe3cbdc1545105b1a7dc4213113cc Mon Sep 17 00:00:00 2001 From: Ibrahim Imad Date: Mon, 5 Feb 2024 12:05:55 +0200 Subject: [PATCH 1/2] added constrain() functions that constrain a point inside a shape added distance() to struct v_2d that calcualte the distance between two points --- olcUTIL_Geometry2D.h | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index cedf02a..e47059f 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -338,6 +338,12 @@ namespace olc return (*this) - 2.0 * (this->dot(n) * n); } + inline constexpr auto distance(const v_2d& p) const + { + // Find the distance between two points + return std::sqrt(std::pow(this->x - p.x, 2) + std::pow(this->y - p.y, 2)); + } + // Allow 'casting' from other v_2d types template inline constexpr operator v_2d() const @@ -1168,8 +1174,52 @@ namespace olc::utils::geom2d } + // ========================================================================================================================= + // constrain(shape, point) =================================================================================================== + + template + inline olc::v_2d constrain(const circle& c, const olc::v_2d& p) + { + if (contains(c, p)) + { + // The point is already inside the circle, no need to constrain + return p; + } + else { + // Constrain the point to the closest position inside the circle + return closest(c, p); + } + } + + template + inline olc::v_2d constrain(const rect& r, const olc::v_2d& p) + { + if (contains(r, p)) + { + // The point is already inside the rect, no need to constrain + return p; + } + else { + // Constrain the point to the closest position inside the trct + return closest(r, p); + } + + } + template + inline olc::v_2d constrain(const triangle& t, const olc::v_2d& p) + { + if (contains(t, p)) + { + // The point is already inside the triangle, no need to constrain + return p; + } + else { + // Constrain the point to the closest position inside the triangle + return closest(t, p); + } + } From c59c1452a5d2e4b2bea6532a2fccf308f7124290 Mon Sep 17 00:00:00 2001 From: Ibrahim Imad Date: Tue, 6 Feb 2024 13:02:09 +0200 Subject: [PATCH 2/2] added consrain() function for lines,points --- olcUTIL_Geometry2D.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index e47059f..fe578fa 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -1177,6 +1177,20 @@ namespace olc::utils::geom2d // ========================================================================================================================= // constrain(shape, point) =================================================================================================== + template + inline olc::v_2d constrain(const line& l, const olc::v_2d& p) + { + if (contains(l, p)) + { + // The point is already on the line, no need to constrain + return p; + } + else { + // Constrain the point to the closest position on the line + return closest(l, p); + } + } + template inline olc::v_2d constrain(const circle& c, const olc::v_2d& p) {