MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / ClosestPointPointLine

Method ClosestPointPointLine

Source/Engine/Core/Math/CollisionsHelper.cpp:12–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10#include "BoundingFrustum.h"
11
12void CollisionsHelper::ClosestPointPointLine(const Float2& point, const Float2& p0, const Float2& p1, Float2& result)
13{
14 const Float2 p = point - p0;
15 Float2 n = p1 - p0;
16
17 const float length = n.Length();
18 if (length < 1e-10f)
19 {
20 // Both points are the same, just give any
21 result = p0;
22 return;
23 }
24 n /= length;
25
26 const float dot = Float2::Dot(n, p);
27 if (dot <= 0.0f)
28 {
29 // Before first point
30 result = p0;
31 }
32 else if (dot >= length)
33 {
34 // After first point
35 result = p1;
36 }
37 else
38 {
39 // Inside
40 result = p0 + n * dot;
41 }
42}
43
44Float2 CollisionsHelper::ClosestPointPointLine(const Float2& point, const Float2& p0, const Float2& p1)
45{

Callers 4

OnSurfaceMouseUpMethod · 0.45
DrawConnectionsMethod · 0.45
IntersectsConnectionMethod · 0.45

Calls 2

DotFunction · 0.85
LengthMethod · 0.45

Tested by

no test coverage detected