Public Shared Sub ClosestPointOnSegmentFromPoint(ByVal x1 As Double, _
ByVal y1 As Double, _
ByVal x2 As Double, _
ByVal y2 As Double, _
ByVal px As Double, _
ByVal py As Double, _
ByRef nx As Double, _
ByRef ny As Double)
Dim vx As Double = x2 - x1
Dim vy As Double = y2 - y1
Dim wx As Double = px - x1
Dim wy As Double = py - y1
Dim c1 As Double = vx * wx + vy * wy
If c1 <= 0# Then
nx = x1
ny = y1
Return
End If
Dim c2 As Double = vx * vx + vy * vy
If c2 <= c1 Then
nx = x2
ny = y2
Return
End If
Dim Ratio As Double = c1 / c2
nx = x1 + ratio * vx
ny = y1 + ratio * vy
End Sub
Public Shared Sub ClosestPointOnSegmentFromPoint(ByVal x1 As Double, _
ByVal y1 As Double, _
ByVal z1 As Double, _
ByVal x2 As Double, _
ByVal y2 As Double, _
ByVal z2 As Double, _
ByVal px As Double, _
ByVal py As Double, _
ByVal pz As Double, _
ByRef nx As Double, _
ByRef ny As Double, _
ByRef nz As Double)
Dim vx As Double = x2 - x1
Dim vy As Double = y2 - y1
Dim vz As Double = z2 - z1
Dim wx As Double = px - x1
Dim wy As Double = py - y1
Dim wz As Double = pz - z1
Dim c1 As Double = vx * wx + vy * wy + vz * wz
If c1 <= 0# Then
nx = x1
ny = y1
nz = z1
Return
End If
Dim c2 As Double = vx * vx + vy * vy + vz * vz
If c2 <= c1 Then
nx = x2
ny = y2
nz = z2
Return
End If
Dim ratio As Double = c1 / c2
nx = x1 + ratio * vx
ny = y1 + ratio * vy
nz = z1 + ratio * vz
End Sub