Seite 1 von 1

Ameisensimulation:Geordnetes Chaos

Verfasst: 29.06.2006 12:11
von Olaf
:?
Eine witzige Simulation:
Man Stellt sich ein leeres Blatt vor. Auf dieses Blatt setzt man eine imagimäre
Ameise.
Move:
Die Ameise macht einen schritt vorwärts. Ist der Punkt unter ihr weiss, so färbt sie den Punkt schwarz und dreht sich nach links. Ist der Punkt aber schwarz, färbt sie ihn weiss und dreht sich nach rechts.
Dann, um es in PB zu sagen: "Goto Move:" :-)

Info: Die Rechts-links-Drehung wäre auch mit Winkeln zu realisieren gewesen, aber die im Code verwendete Änderung ist ein bisschen Laufzeit-effizienter.

Nach dem Start muss "p" gedrückt werden.

Code: Alles auswählen

Enumeration
#Window
#Pause
#Image
#Print
#Slow
EndEnumeration

OpenWindow(#Window,0,0,600,400,"Ameisensimulation",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
AddKeyboardShortcut(#Window,#PB_Shortcut_P,#Pause)
AddKeyboardShortcut(#Window,#PB_Shortcut_D,#Print)
AddKeyboardShortcut(#Window,#PB_Shortcut_S,#Slow)
CreateImage(#Image,600,400)
	StartDrawing(ImageOutput(#Image))
		Box(0,0,600,400,RGB(255,255,255))
	StopDrawing()
	StartDrawing(WindowOutput(#Window))
		Box(0,0,600,400,RGB(255,255,255))
	StopDrawing()
	
	;x=1
	;antx=360
	;anty=370
	
	;sehr chaotisch:
	x=1
	antx=80
	anty=100
	
	;x=1
	;antx=20
	;anty=20
	
	;x=1
	;antx=370
	;anty=370
	
	;x=-1
	;antx=30
	;anty=30
	
	;y=1
	;antx=20
	;anty=20
	
	;y=1
	;antx=380
	;anty=200
	
	;y=-1
	;antx=32
	;anty=390
	
	;x=1
	;antx=2
	;anty=394
Repeat
	event=WindowEvent()
	Select event
		Case #PB_Event_MoveWindow
			pause=1
		Case #PB_Event_Repaint
			StartDrawing(WindowOutput(#Window))
				DrawImage(ImageID(#Image),0,0)
			StopDrawing()
		Case #PB_Event_Menu
			Select EventMenu()
				Case #Print
					SaveImage(#Image,"Ameise.bmp")
				Case #Pause
					If pause=0
						pause=1
						Delay(100)
					Else
						pause=0
						Delay(100)
					EndIf
				Case #Slow
					If slow=0
						slow=1
						Delay(100)
					Else
						slow=0
						Delay(100)
					EndIf
			EndSelect
	EndSelect
	If pause=0
		Gosub MoveAnt
	EndIf
	If slow=1
		Delay(1)
	EndIf
Until event=#PB_Event_CloseWindow
End

MoveAnt:
StartDrawing(WindowOutput(#Window))
Color=Point(AntX,AntY)
If Color=RGB(255,255,255)
	Color=RGB(0,0,0)
Else
	Color=RGB(255,255,255)
EndIf
	Plot(AntX,AntY,Color)
StopDrawing()
StartDrawing(ImageOutput(#Image))
	Plot(AntX,AntY,Color)
StopDrawing()
If Color=RGB(255,255,255)
	;links
	If x=-1
		x=0
		y=1
	ElseIf x=1
		x=0
		y=-1
	ElseIf y=-1
		x=-1
		y=0
	ElseIf y=1
		x=1
		y=0
	EndIf
Else
	;rechts
	If x=-1
		x=0
		y=-1
	ElseIf x=1
		x=0
		y=1
	ElseIf y=-1
		x=1
		y=0
	ElseIf y=1
		x=-1
		y=0
	EndIf
EndIf
If AntX>598
x=-1
y=0
AntX-2
	If AntY<2
		AntY+1
	ElseIf AntY>398
		AntY-1
	EndIf
ElseIf AntX<2
x=1
y=0
AntX+2
	If AntY<2
		AntY+1
	ElseIf AntY>398
		AntY-1
	EndIf
ElseIf AntY>398
x=0
y=-1
AntY-2
	If AntX<2
		AntX+1
	ElseIf AntX>598
		AntX-1
	EndIf
ElseIf AntY<2	
x=0
y=1
AntY+2
	If AntY<2
		AntY+1
	ElseIf AntY>398
		AntY-1
	EndIf
Else
AntX+x
Anty+y
EndIf
Return

Verfasst: 29.06.2006 12:28
von hardfalcon
Könnte man das nicht nehmen, um eine Datei zu verschlüsseln? Sodass der Schlüssel quasi die Ausgangskoordinaten der Ameise wären?

Verfasst: 30.06.2006 01:52
von PureLust
Hallo Olaf,

wirklich eine nette kleine Wusel-Ameise hast Du da.
;)

Sie war mir nur etwas zu langsam um darauf zu warten bis sie den danzen Bildschirm umgegraben hat. :mrgreen:
Also hab ich Deinen Code mal in ein paar Zeilen verändert, so dass Deine kleine Ameise nun noch 'nen Gang zulegen konnte.

Die eigentliche Veränderung besteht im Grunde darin, dass die Punkte nun nicht mehr doppelt sowohl in das Fenster als AUCH in das Image gezeichnet werden, sondern nur noch in das Image.
Dieses wird dann durch einen Timer alle 30ms in das Fenster gezeichnet.
So halbiert sich die Anzahl der Plot-Befehle und durch den Refresh alle 30ms entsteht dennoch der Eindruck eines flüssigen Bildes.
Auch habe ich die Aufrufe der Start- und StopDrawing() Befehle ein wenig minimiert.
Alles in allem tankt Deine kleine Ameise nun also in etwa 3.5fach angereichertes Cerosin. :allright:

Wie gesagt, keine wirklich großen Veränderungen.
Und auch die Grundstruktur Deines Codes habe ich an sonsten gelassen, damit Du bei Interesse die Veränderungen leicht nachvollziehen kannst.

Code: Alles auswählen

Enumeration 
#Window 
#Pause 
#Image 
#Print 
#Slow 
EndEnumeration 

OpenWindow(#Window,0,0,600,400,"Ameisensimulation",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget) 
AddKeyboardShortcut(#Window,#PB_Shortcut_P,#Pause) 
AddKeyboardShortcut(#Window,#PB_Shortcut_D,#Print) 
AddKeyboardShortcut(#Window,#PB_Shortcut_S,#Slow) 
CreateImage(#Image,600,400) 
   StartDrawing(WindowOutput(#Window)) 
      Box(0,0,600,400,RGB(255,255,255)) 
   StopDrawing() 
   StartDrawing(ImageOutput(#Image)) 
      Box(0,0,600,400,RGB(255,255,255))
      
      ;############### Nette Ergebnisse bringen auch die zwei folgenden zusätzlichen Schleifen:  ;o)  ##############
;       For n = 50 To 550 Step 50
;       	LineXY(n,0,n,400,0)
;       Next n
      ;############### Diese beiden Schleifen können auch unabhängig voneinander eingebunden werden. ###############
;       For n = 50 To 350 Step 50
;       	LineXY(0,n,600,n,0)
;       Next n
      
;    StopDrawing() 
    
   ;x=1 
   ;antx=360 
   ;anty=370 
    
   ;sehr chaotisch: 
   x=1 
   antx=80 
   anty=100 
    
   ;x=1 
   ;antx=20 
   ;anty=20 
    
   ;x=1 
   ;antx=370 
   ;anty=370 
    
   ;x=-1 
   ;antx=30 
   ;anty=30 
    
   ;y=1 
   ;antx=20 
   ;anty=20 
    
   ;y=1 
   ;antx=380 
   ;anty=200 
    
   ;y=-1 
   ;antx=32 
   ;anty=390 
    
   ;x=1 
   ;antx=2 
   ;anty=394 
RedrawDelay = 30
NextRedraw = ElapsedMilliseconds()+RedrawDelay
Repeat 
   event=WindowEvent()
   If event=0 And ElapsedMilliseconds() >= NextRedraw
		event = #PB_Event_Repaint
		NextRedraw = ElapsedMilliseconds()+RedrawDelay
	EndIf
   Select event 
      Case #PB_Event_MoveWindow 
         pause=1 
      Case #PB_Event_Repaint 
         StopDrawing() 
         StartDrawing(WindowOutput(#Window)) 
            DrawImage(ImageID(#Image),0,0) 
         StopDrawing() 
			StartDrawing(ImageOutput(#Image)) 
      Case #PB_Event_Menu 
         Select EventMenu() 
            Case #Print 
               SaveImage(#Image,"Ameise.bmp") 
            Case #Pause 
               If pause=0 
                  pause=1 
                  Delay(100) 
               Else 
                  pause=0 
                  Delay(100) 
               EndIf 
            Case #Slow 
               If slow=0 
                  slow=1 
                  Delay(100) 
               Else 
                  slow=0 
                  Delay(100) 
               EndIf 
         EndSelect 
   EndSelect 
   If pause=0 
      Gosub MoveAnt 
   EndIf 
   If slow=1 
      Delay(1) 
   EndIf 
Until event=#PB_Event_CloseWindow 
End 

MoveAnt: 
Color=Point(AntX,AntY) 
If Color=RGB(255,255,255) 
   Color=RGB(0,0,0) 
Else 
   Color=RGB(255,255,255) 
EndIf 
Plot(AntX,AntY,Color) 
If Color=RGB(255,255,255) 
   ;links 
   If x=-1 
      x=0 
      y=1 
   ElseIf x=1 
      x=0 
      y=-1 
   ElseIf y=-1 
      x=-1 
      y=0 
   ElseIf y=1 
      x=1 
      y=0 
   EndIf 
Else 
   ;rechts 
   If x=-1 
      x=0 
      y=-1 
   ElseIf x=1 
      x=0 
      y=1 
   ElseIf y=-1 
      x=1 
      y=0 
   ElseIf y=1 
      x=-1 
      y=0 
   EndIf 
EndIf 
If AntX>598 
x=-1 
y=0 
AntX-2 
   If AntY<2 
      AntY+1 
   ElseIf AntY>398 
      AntY-1 
   EndIf 
ElseIf AntX<2 
x=1 
y=0 
AntX+2 
   If AntY<2 
      AntY+1 
   ElseIf AntY>398 
      AntY-1 
   EndIf 
ElseIf AntY>398 
x=0 
y=-1 
AntY-2 
   If AntX<2 
      AntX+1 
   ElseIf AntX>598 
      AntX-1 
   EndIf 
ElseIf AntY<2    
x=0 
y=1 
AntY+2 
   If AntY<2 
      AntY+1 
   ElseIf AntY>398 
      AntY-1 
   EndIf 
Else 
AntX+x 
Anty+y 
EndIf 
Return 
Als kleines Gimmik habe ich im oberen Teil noch zwei Schleifen hinzugefügt, die noch ein paar zusätzliche nette Effekte ergeben und die Du ja mal ausprobieren kannst. ;)

Viel Spaß also damit und nochmals Dank für diese nette Idee mit der kleinen, wuselige Turbo-Ameise. :allright:
PureLust.

Verfasst: 20.07.2006 15:41
von Olaf
@PL THX für die Prozeduren :allright:
sieht gut aus

Gute Idee mit den 30 ms.
An die Grafik-Performance hatt ich am Anfang garnicht gedacht, weil's
bei mir schon sehr schnell ging.