Skip to content

Commit b061d70

Browse files
authored
Merge branch 'OneLoneCoder:main' into main
2 parents 027109b + 3ad6bc5 commit b061d70

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

olcUTIL_Geometry2D.h

+29-2
Original file line numberDiff line numberDiff line change
@@ -2495,8 +2495,35 @@ namespace olc::utils::geom2d
24952495
template<typename T1, typename T2, typename T3>
24962496
inline std::optional<olc::v_2d<T2>> project(const circle<T1>& c, const triangle<T2>& t, const ray<T3>& q)
24972497
{
2498-
// TODO:
2499-
return std::nullopt;
2498+
const auto s1 = project(c, t.side(0), q);
2499+
const auto s2 = project(c, t.side(1), q);
2500+
const auto s3 = project(c, t.side(2), q);
2501+
2502+
std::vector<olc::v_2d<T2>> vAllIntersections;
2503+
if (s1.has_value()) vAllIntersections.push_back(s1.value());
2504+
if (s2.has_value()) vAllIntersections.push_back(s2.value());
2505+
if (s3.has_value()) vAllIntersections.push_back(s3.value());
2506+
2507+
if (vAllIntersections.size() == 0)
2508+
{
2509+
// No intersections at all, so
2510+
return std::nullopt;
2511+
}
2512+
2513+
// Find closest
2514+
double dClosest = std::numeric_limits<double>::max();
2515+
olc::v_2d<T2> vClosest;
2516+
for (const auto& vContact : vAllIntersections)
2517+
{
2518+
double dDistance = (vContact - q.origin).mag2();
2519+
if (dDistance < dClosest)
2520+
{
2521+
dClosest = dDistance;
2522+
vClosest = vContact;
2523+
}
2524+
}
2525+
2526+
return vClosest;
25002527
}
25012528

25022529

0 commit comments

Comments
 (0)