This is part of my code. I pass the x & y coordinates of a screen pixel to the sub and need to calculate parameters for the surrounding 8 pixels. This code works but as I need to call the sub a few thousand times, I think it could be written easier. A loop would be good to make the code easier to read but would it be quicker?
Many thanks.
Many thanks.
B4X:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Type PathType(X As Int, Y As Int, Value1 As Int, Value2 As Int, Value3 As Int, ParentX As Int, ParentY As Int)
Dim CurrentCoordinate As PathType 'Store coords + f,g,h, parentx, parenty values
Dim OpenList, ClosedList As List '2 lists for pathfinding, sort with OpenList.SortType("Value3", true)
Dim PixelInterval As Int=4 'Distance between two point on path
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
BeginPathfinder
End Sub
Sub BeginPathfinder
'Here we start pathfinding
'Define starting point and add start to open list
CurrentCoordinate.X=x1
CurrentCoordinate.Y=y1
CurrentCoordinate.Value1=0
CurrentCoordinate.Value2=0
CurrentCoordinate.Value3=0
CurrentCoordinate.ParentX=x1
CurrentCoordinate.ParentY=y2
OpenList.Add(CurrentCoordinate)
'Add the neighbours
AddReachable(x1,y1)
Dim CurrentCoordinate As PathType 'Reset
CurrentCoordinate=OpenList.Get(0)
ClosedList.Add(CurrentCoordinate) 'Add lowest F score to closed list
OpenList.RemoveAt(0) 'Remove it from open list
cvs.DrawCircle(CurrentCoordinate.X,CurrentCoordinate.Y,2,Colors.blue,False,2) 'draw point
iv.Invalidate
End Sub
Sub AddReachable(x As Int, y As Int)
'F=Value1, G=Value2, H=Value3
'G is path to point from start. Horizontal and vertical border points are 10, diagonals are 14
'H is the Manhattan distance to the finish point (x2,y2)
'F=G+H
'Add the 8 surrounding points to the open list
Dim CurrentCoordinate As PathType 'Need to redim to clear the old values!
'Up
CurrentCoordinate.X=x
CurrentCoordinate.Y=y-PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=10
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Up and right
CurrentCoordinate.X=x+PixelInterval
CurrentCoordinate.Y=y-PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=14
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Right
CurrentCoordinate.X=x+PixelInterval
CurrentCoordinate.Y=y
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=10
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Right and down
CurrentCoordinate.X=x+PixelInterval
CurrentCoordinate.Y=y+PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=14
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Down
CurrentCoordinate.X=x
CurrentCoordinate.Y=y+PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=10
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Down and left
CurrentCoordinate.X=x-PixelInterval
CurrentCoordinate.Y=y+PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=14
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Left
CurrentCoordinate.X=x-PixelInterval
CurrentCoordinate.Y=y
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=10
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
Dim CurrentCoordinate As PathType
'Up and left
CurrentCoordinate.X=x-PixelInterval
CurrentCoordinate.Y=y-PixelInterval
CurrentCoordinate.Value3=(Abs(x2-x)+Abs(y2-y))*10
CurrentCoordinate.Value2=14
CurrentCoordinate.Value1=CurrentCoordinate.Value2+CurrentCoordinate.Value3
CurrentCoordinate.ParentX=x
CurrentCoordinate.ParentY=y
OpenList.Add(CurrentCoordinate)
OpenList.SortType("Value1",True) 'Sort list according to F, smallest first.
End Sub