Latexi95 wrote:Koodailin ajankuluksi tuon kuvaamani algorithmin:
Kannattaisi olla tuo kuva, niin että käytävät ovat pikselin levyisiä, niin flood fill olisi paljon nopeampi.
Kiitos todella paljon! Nyt vaan tutustumaan periaatteeseen miten teit sen.
Seuraavaksi mun pitää tehdä lyhimmän reitin piirto alkupisteestä maaliin. mutta enköhän saa tehtyä itse
Labyrintin kokoon en voi oikeen vaikuttaa, koska tämä liittyy yhteen projektiin jossa on käytössä kyseinen labyrintti.
Muuten onko coolbasicilla mitenkään mahdollista aukasta .pmg muotoisia kuvia?
EDIT:laitanpa sen valmiin koodinki tänne jos joku joskus tarvii:
Code: Select all
//WAVEFRONT ALGORITHM
//----------------------------------------------------------------------
//SETUP
SCREEN 900,855 //screen size that we want to use.
Type wavefront//we make collections called wavefront
Field location_x//data we want use, when we call wavefront
Field location_y//data here is wavefront's location
End Type//type is ready
//we load image uge3_labyrinth from this location
//now location is in the same folder where this program is
//we call our labyrinth with name img
img = LoadImage("uge3_labyrinth.png")
Const mapsize_x = 900 //here we tell how large is map
Const mapsize_y = 885
Const goalpoint_x = 5 //goalpoint
Const goalpoint_y = 405
Const startpoint_x = 890 //startpoint
Const startpoint_y = 450
robot_x = startpoint_x //robot's location (first startpoint)
robot_y = startpoint_y
//WE MAKE ARRAY WHERE WE CHANGE PIXELS FROM LABYRINTH TO NUMBERS
Dim array(mapsize_x,mapsize_y) //we make array (or is better english word sheet?)
Lock Image(img) //we lock image for faster calculation
For x = 0 To mapsize_x //for loop to the whole map
For y = 0 To mapsize_y
//getpixel2 reads value from 32-bit pixel
pixel = GetPixel2(x, y, Image(img)) //value from labyrinth located x,y
If pixel = -1 Then //if loop. white pixel = -1
pixel = 0 //if white -> pixel = 0
Else
pixel = 1//if other than white -> pixel = 1
EndIf//ends if loop
array (x,y) = pixel //we tell to array that at location x,y pixel value is 1 or 0
Next y //return to for loop, until y = mapsize_y
Next x
Unlock Image(img)//unlock image, so it can be used in other places also
//---------------------------------------------------------------------------
//MAIN LOOP
Repeat //repeat starts main loop, repeat it until...
//FILL NUMBERS TO ARRAY
FloodFillArray(goalpoint_x, goalpoint_y)//we call function which fills numbers, starting from goal
//START MOVING ROBOT
If array(startpoint_x,startpoint_y) > 0 Then //first we check that robot's startpoint is not empty
Color 255,0,0 //red color for text
//text location 10,10 where we tell array's number at startpoint -2 (because we start wavefront from 2)
Text 10,10,"path from start to goal: " + (array(startpoint_x,startpoint_y)-2)
Lock Image(img)//again lock image, so it's faster to draw in it
While array(robot_x, robot_y) > 2 //loop until robot is in the goalpoint
//we check if array's number is smaller at right and we are not going to wall
//if not true, we check left. If that's not true, we check down and if that is not true we check up
If array(robot_x + 1, robot_y)<array(robot_x, robot_y) And array(robot_x + 1, robot_y) > 1 Then
robot_x + 1 //if true -> move robot right
ElseIf array(robot_x - 1, robot_y)<array(robot_x, robot_y) And array(robot_x - 1, robot_y) > 1 Then
robot_x - 1 //move robot left
ElseIf array(robot_x, robot_y + 1)<array(robot_x, robot_y) And array(robot_x, robot_y + 1) > 1 Then
robot_y + 1 //move robot down
ElseIf array(robot_x, robot_y - 1)<array(robot_x, robot_y) And array(robot_x, robot_y - 1) > 1 Then
robot_y - 1 //move robot up
EndIf //end for if loop
PutPixel2 robot_x, robot_y, 255, Image(img)//draw one pixel al robot's location
Wend //end for while loop
Unlock Image(img)//unlock image
Else//if happens so badly that robot's loc is still free space
Text 10,10,"WE DID NOT FIND ANY PATH!"
EndIf//ends if loop
DrawImage img, 0, 0 //we draw image of labyrinth to location 0,0
//if we want to look over labyrinth and see all distances to goalpoint, we call this text, otherwise it can be set as comment
//mouseX() is location of mouse
Text 250,10,"array: " + (array(MouseX(), MouseY())-2) +", location x: " + MouseX()+ "y: " + MouseY()
DrawScreen //draw everything to screen and clear all what happened before in mainloop
Forever //we tell to repeat main loop forever
//---------------------------------------------------------------------------------
//FUNCTION
Function FloodFillArray(startX, startY)//Function to fill numbers to array.
array(startX,startY) = 2 //fill's starting point must be different than free space
wave.wavefront = New (wavefront)//first wave
wave\location_x = startX//first wave's starting points
wave\location_y = startY
For wave.wavefront = Each wavefront//loop where we check every wave
x = wave\location_x //location x is same as wave's location
y = wave\location_y
num = array(x, y) + 1//number in array located x,y +1
//we check every location from x,y: left, right, up, down
If x > 0 Then//check if x is more than zero, so we can't go over map
If array(x - 1, y) = 0 Then//if location in left is free
array(x - 1, y) = num//location in left is num (=before location +1)
newWave.wavefront = New (wavefront)//we make new wave for next round(old wave is deleted in the end)
newWave\location_x = x - 1//we move our new location to left
newWave\location_y = y//location in y axel stays
EndIf//we end if..then loop
EndIf
If x < mapsize_x - 1 Then//check if x is less than mapsize
If array(x + 1, y) = 0 Then//if free pixel in right
array(x + 1, y) = num
newWave.wavefront = New (wavefront)//we make new wave also to left
newWave\location_x = x + 1
newWave\location_y = y
EndIf
EndIf
If y > 0 Then
If array(x, y - 1) = 0 Then//if free pixel in up
array(x, y - 1) = num
newWave.wavefront = New (wavefront)
newWave\location_x = x
newWave\location_y = y - 1
EndIf
EndIf
If y < mapsize_y - 1 Then
If array(x, y + 1) = 0 Then//if free pixel in down
array(x, y + 1) = num
newWave.wavefront = New (wavefront)
newWave\location_x = x
newWave\location_y = y + 1
EndIf
EndIf
Delete wave//we delete this wave
Next wave//next wave, so we return for..each loop again and start with newWave's
EndFunction//when all possible waves are checked, we end function
[/edit]