MCPcopy Create free account
hub / github.com/Panzerschrek/Chasm-Reverse / DistanceToLineSegment

Function DistanceToLineSegment

PanzerChasm/math_utils.cpp:20–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20float DistanceToLineSegment(
21 const m_Vec2& from,
22 const m_Vec2& v0,
23 const m_Vec2& v1 )
24{
25 const m_Vec2 v0_v1_vec= v1 - v0;
26 const float v0_v1_vec_square_length= v0_v1_vec.SquareLength();
27
28 const m_Vec2 vec_from_v0_to_pos= from - v0;
29 const float vecs_dot= vec_from_v0_to_pos * v0_v1_vec;
30
31 if( vecs_dot <= 0.0f )
32 {
33 return vec_from_v0_to_pos.Length();
34 }
35 else if( vecs_dot >= v0_v1_vec_square_length )
36 {
37 return ( from - v1 ).Length();
38 }
39 else
40 {
41 if( v0_v1_vec_square_length == 0.0f )
42 return ( from - v0 ).Length();
43
44 const m_Vec2 projection_to_line= v0_v1_vec * ( vecs_dot / v0_v1_vec_square_length );
45 const m_Vec2 vec_from_line= vec_from_v0_to_pos - projection_to_line;
46
47 return vec_from_line.Length();
48 }
49
50}
51
52} // namespace PanzerChasm

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected