Skip to content

Commit 3ad6bc5

Browse files
authored
Merge pull request OneLoneCoder#78 from OneLoneCoder/project(c,t,r)
Added Projection of Circle on to Triangle
2 parents 800078a + b68d19d commit 3ad6bc5

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
@@ -2192,8 +2192,35 @@ namespace olc::utils::geom2d
21922192
template<typename T1, typename T2, typename T3>
21932193
inline std::optional<olc::v_2d<T2>> project(const circle<T1>& c, const triangle<T2>& t, const ray<T3>& q)
21942194
{
2195-
// TODO:
2196-
return std::nullopt;
2195+
const auto s1 = project(c, t.side(0), q);
2196+
const auto s2 = project(c, t.side(1), q);
2197+
const auto s3 = project(c, t.side(2), q);
2198+
2199+
std::vector<olc::v_2d<T2>> vAllIntersections;
2200+
if (s1.has_value()) vAllIntersections.push_back(s1.value());
2201+
if (s2.has_value()) vAllIntersections.push_back(s2.value());
2202+
if (s3.has_value()) vAllIntersections.push_back(s3.value());
2203+
2204+
if (vAllIntersections.size() == 0)
2205+
{
2206+
// No intersections at all, so
2207+
return std::nullopt;
2208+
}
2209+
2210+
// Find closest
2211+
double dClosest = std::numeric_limits<double>::max();
2212+
olc::v_2d<T2> vClosest;
2213+
for (const auto& vContact : vAllIntersections)
2214+
{
2215+
double dDistance = (vContact - q.origin).mag2();
2216+
if (dDistance < dClosest)
2217+
{
2218+
dClosest = dDistance;
2219+
vClosest = vContact;
2220+
}
2221+
}
2222+
2223+
return vClosest;
21972224
}
21982225

21992226

0 commit comments

Comments
 (0)