Public Class PathFindingData
Sub New(ByVal PathImage As Bitmap)
'load all accessible points here
Dim temp(PathImage.Width - 1, PathImage.Height - 1) As PathWay
For x As Integer = 0 To PathImage.Width - 1
For y As Integer = 0 To PathImage.Height - 1
temp(x, y) = New PathWay(New Point(x, y), GetPointAccesible(PathImage, x, y))
Next
Next
'generate neighbours
Me.PathWays = New List(Of PathWay)
Dim neighbours(8) As Point
Dim p As PathWay
For x As Integer = 0 To PathImage.Width - 1
For y As Integer = 0 To PathImage.Height - 1
p = temp(x, y)
If p.accessible Then
neighbours(0) = New Point(x - 1, y - 1) '1
neighbours(1) = New Point(x, y - 1) '2
neighbours(2) = New Point(x + 1, y - 1) '3
neighbours(3) = New Point(x + 1, y) '4
neighbours(4) = New Point(x + 1, y + 1) '5
neighbours(5) = New Point(x, y + 1) '6
neighbours(6) = New Point(x - 1, y + 1) '7
neighbours(7) = New Point(x - 1, y) '8
For Each neighbour As Point In neighbours
If neighbour.X >= 0 And neighbour.Y >= 0 And neighbour.X < PathImage.Width And neighbour.Y < PathImage.Height Then
If temp(neighbour.X, neighbour.Y).accessible Then
p.neighbours.Add(temp(neighbour.X, neighbour.Y))
End If
End If
Next
Me.PathWays.Add(p)
End If
Next
Next
End Sub
Private Function GetPointAccesible(ByVal IMG As Bitmap, ByVal x As Integer, ByVal y As Integer) As Boolean
Dim c As Color = IMG.GetPixel(x, y)
If c.R = 255 And c.G = 255 And c.B = 255 Then Return True
Return False
End Function
Public PathWays As List(Of PathWay)
Public Class PathWay
Sub New(ByVal location As Point, Optional ByVal accesible As Boolean = True)
Me.location = location
Me.accessible = accesible
Me.neighbours = New List(Of PathWay)
End Sub
Public location As Point
Public neighbours As List(Of PathWay)
Public accessible As Boolean
End Class
Public Function GetPathWay(ByVal location As Point) As PathWay
For Each p As PathWay In PathWays
If p.location = location Then Return p
Next
Return Nothing
End Function
End Class