' TRUE if line objects L1 and L2 self-intersect
Sub intersects(L1 As line, L2 As line) As Boolean
Return (pointside(L1, L2.p0) <> (pointside(L1, L2.p1))) And (pointside(L2, L1.p0) <> (pointside(L2, L1.p1)))
End Sub
' Determine on which side of line L point P lies
Sub pointside(L As line, P As point) As Int
Dim a As Float
a = ((L.p1.x - L.p0.x) * (P.y - L.p0.y)) - ((P.x - L.p0.x) * (L.p1.y - L.p0.y))
If (a > 0) Then Return 1
If (a < 0) Then Return -1
Return 0
End Sub
Type point (x As Float, y As Float)
Type line (p0 As point, p1 As point)
. . . . .
. . . . .
' Convert a list of point objects to a list of line objects
Sub makeLines(points As List) As List
Dim result As List
result.Initialize
Dim p, oldP As point
Dim first As Boolean = True
For Each p As point In points
If Not(first) Then
result.Add(createLine(oldP, p))
End If
oldP = p
first = False
Next
Return result
End Sub
Public Sub createLine (p0 As point, p1 As point) As line
Dim L As line
L.Initialize
L.p0 = p0
L.p1 = p1
Return L
End Sub
' Return TRUE if the closed path represented by the list of point objects in [shape] do not self-intersect.
Sub validShape(shape As List) As Boolean
If (shape.Size > 4) Then
Dim result As Boolean = True
Dim lines As List = makeLines(shape)
Dim A, B As line
Dim i, j As Int
i = 0
Do While result And (i < lines.Size - 2)
A = lines.Get(i)
For j = i + 2 To lines.Size - 2
B = lines.Get(j)
result = Not(intersects(A, B))
Next
i = i + 1
Loop
Return result
Else
Return True
End If
End Sub
For segment A I have 2 points, each point have a Lat and lng (each is Double) I don't use LatLng variable.@yfleury - As you are working with Lat/Lon values I suggest that you use Double variables for the point coordinates rather than Float, especially if you are working at metre precision rather than kilometres.
' Replace this line ...
' Type point (x As Float, y As Float)
' with this line ...
Type point(x as Double, y as Double)