190190#include < cstdint>
191191#include < optional>
192192#include < cassert>
193+ #include < array>
193194
194195#ifndef OLC_V2D_TYPE
195196#define OLC_V2D_TYPE
@@ -2104,7 +2105,7 @@ namespace olc::utils::geom2d
21042105 olc::v_2d<T2 > vClosest;
21052106 for (const auto & vContact : vAllIntersections)
21062107 {
2107- double dDistance = (vContact - c. pos ).mag2 ();
2108+ double dDistance = (vContact - q. origin ).mag2 ();
21082109 if (dDistance < dClosest)
21092110 {
21102111 dClosest = dDistance;
@@ -2120,8 +2121,37 @@ namespace olc::utils::geom2d
21202121 template <typename T1 , typename T2 , typename T3 >
21212122 inline std::optional<olc::v_2d<T2 >> project (const circle<T1 >& c, const rect<T2 >& r, const ray<T3 >& q)
21222123 {
2123- // TODO:
2124- return std::nullopt ;
2124+ const auto s1 = project (c, r.top (), q);
2125+ const auto s2 = project (c, r.bottom (), q);
2126+ const auto s3 = project (c, r.left (), q);
2127+ const auto s4 = project (c, r.right (), q);
2128+
2129+ std::vector<olc::v_2d<T2 >> vAllIntersections;
2130+ if (s1.has_value ()) vAllIntersections.push_back (s1.value ());
2131+ if (s2.has_value ()) vAllIntersections.push_back (s2.value ());
2132+ if (s3.has_value ()) vAllIntersections.push_back (s3.value ());
2133+ if (s4.has_value ()) vAllIntersections.push_back (s4.value ());
2134+
2135+ if (vAllIntersections.size () == 0 )
2136+ {
2137+ // No intersections at all, so
2138+ return std::nullopt ;
2139+ }
2140+
2141+ // Find closest
2142+ double dClosest = std::numeric_limits<double >::max ();
2143+ olc::v_2d<T2 > vClosest;
2144+ for (const auto & vContact : vAllIntersections)
2145+ {
2146+ double dDistance = (vContact - q.origin ).mag2 ();
2147+ if (dDistance < dClosest)
2148+ {
2149+ dClosest = dDistance;
2150+ vClosest = vContact;
2151+ }
2152+ }
2153+
2154+ return vClosest;
21252155 }
21262156
21272157 // project(c,t)
0 commit comments