Sierpinskis Chaos Game

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Joel

Betreff: Sierpinskis Chaos Game

BeitragFr, Jul 01, 2011 18:02
Antworten mit Zitat
Benutzer-Profile anzeigen
Da ich mich immer noch mit Fraktalen beschäftige habe ich mal Sierpinskis Chaos Game programmiert.
das grundlegende dabei ist, dass man ein punkt in einem Dreieck wählt, einen zufälligen Eckpunkt aussucht und den punkt auf die hälfte der strecke zwischen dem punkt und Eckpunkt setzt.

Ich habe diesem(dessen beispiele es hier im Forum schon hat) noch einige Parameter wie Skalierung, Rotation und Wahrscheinlichkeit hinzugefügt.
Diese Lektüre empfehle ich denen die mehr wissen wollen.

Das besondere ist, dass sich dadurch leicht natürlich wirkende formen zu erschaffen lassen, wie zum beispiel Bäume oder ähnliches.
user posted image
Ergebniss des "normalen" Chaos Games

user posted image
Ein Farn

BlitzMax: [AUSKLAPPEN]
SuperStrict
Graphics 1024, 764
Local Timer:TTimer = CreateTimer(30)

SetBlend(ALPHABLEND)

Global Eintrage:Eckpunkt[31]
Type Eckpunkt
Global NmrEintrage:Int = -1

Field X:Int, Y:Int
Field Skalierung:Double
Field Rotation:Int
Field Wahr:Float
Field Platz:Int

Method New()
NmrEintrage = NmrEintrage + 1
Eintrage[NmrEintrage] = Self
Platz = NmrEintrage
End Method
Method Loschen()
For Local n:Int = Platz + 1 To NmrEintrage
Eintrage[n - 1] = Eintrage[n]
Next
NmrEintrage = NmrEintrage - 1
End Method

Function Set:Eckpunkt(XPos:Int, YPos:Int, Skal:Float, Rot:Int, Wahr:Float)
Local tmp:Eckpunkt = New Eckpunkt
tmp.X = XPos
tmp.Y = YPos
tmp.Skalierung = Skal
tmp.Rotation = Rot
tmp.Wahr = Wahr
Return tmp
End Function

Function Update()
For Local n:Int = 0 To Eckpunkt.NmrEintrage
If MHit
If Eintrage[n].X - MX < 5 And MX - Eintrage[n].X < 5 And Eintrage[n].Y - MY < 5 And MY - Eintrage[n].Y < 5
tmpEckpunkt = Eintrage[n]
End If
End If
Next
End Function

Function Draw()
For Local n:Int = 0 To Eckpunkt.NmrEintrage
If Help = 1
If Eintrage[n] = tmpEckpunkt
SetColor(255, 0, 0)
DrawOval Eintrage[n].X - 5, Eintrage[n].Y - 5, 10, 10
SetColor(255, 255, 255)
Else
SetColor(150, 150, 150)
DrawOval Eintrage[n].X - 5, Eintrage[n].Y - 5, 10, 10
SetColor(255, 255, 255)
End If
EndIf
Next
End Function
End Type

Eckpunkt.Set(200, 600, 0.5, 0, 50)
Eckpunkt.Set(500, 100, 0.5, 0, 50)

Global tmpEckpunkt:Eckpunkt
tmpEckpunkt = Eckpunkt.Set(800, 600, 0.5, 0, 50)

Global Update:Int = 1, Help:Int = 1
Global Punkte:Int = 10000, Alpha:Float = 0.5

Global MHit:Int, MX:Int, MY:Int, MDown2:Int, MHit3:Int
While Not KeyHit(KEY_ESCAPE)
MHit = MouseHit(1)
MHit3 = MouseHit(3)
MDown2 = MouseDown(2)
MX = MouseX()
MY = MouseY()
EingabenUberprufen()

Eckpunkt.Update()

If Update = 1' wenn irgenwelche variablen geändert wurden updaten
Cls
Eckpunkt.Draw()
If Help = 1 Then DrawHelp()
SeedRnd 0
ChaosSpiel(Punkte, Eintrage[0].x, Eintrage[0].y)
Flip 0
Update = 0
EndIf

WaitTimer Timer
Wend
End


Function ChaosSpiel(me:Int, X:Double, Y:Double)
Local w:Double, lange:Double, tmp:Eckpunkt
Local GesammtWahr:Int
Local Add:Float[31]
Local Random:Float

SetAlpha(Alpha)

For Local n:Int = 0 To Eckpunkt.NmrEintrage
GesammtWahr = GesammtWahr + Eintrage[n].Wahr'Einträge verteilen
Add[n] = GesammtWahr
Next

For Local n:Int = 0 To me
Random = Rnd(GesammtWahr)
For Local n:Int = 0 To Eckpunkt.NmrEintrage
If Random - Add[n] <= 0 And Random - Add[n] > - Eintrage[n].Wahr'Einen Zufälligen Eintrag mit bestimmter wahrscheinlichkeit auswählen
tmp = Eintrage[n]
EndIf
Next

w = ATan2(X - tmp.X, Y - tmp.y) + tmp.Rotation + 180
lange = Sqr((X - tmp.X) ^ 2 + (Y - tmp.y) ^ 2) * tmp.Skalierung'Punkt bewegen und zeichnen
X = tmp.X - Sin(w) * lange
Y = tmp.y - Cos(w) * lange
Plot X, Y
Next
SetAlpha(1)
End Function

Function EingabenUberprufen()
If KeyDown(KEY_A)
tmpEckpunkt.Rotation = tmpEckpunkt.Rotation + 1
Update = 1
ElseIf KeyDown(KEY_Y)
tmpEckpunkt.Rotation = tmpEckpunkt.Rotation - 1
Update = 1
End If

If KeyDown(KEY_S)
tmpEckpunkt.Skalierung = tmpEckpunkt.Skalierung * 1.01
Update = 1
ElseIf KeyDown(KEY_X)
tmpEckpunkt.Skalierung = tmpEckpunkt.Skalierung * 0.99
Update = 1
End If

If KeyDown(KEY_D)
tmpEckpunkt.Wahr = tmpEckpunkt.Wahr + 1
Update = 1
ElseIf KeyDown(KEY_C)
tmpEckpunkt.Wahr = tmpEckpunkt.Wahr - 1
Update = 1
End If

If MDown2
tmpEckpunkt.x = MX
tmpEckpunkt.y = MY
Update = 1
EndIf

If MHit3
tmpEckpunkt = Eckpunkt.Set(MX, MY, 0.5, 0, 50)
Update = 1
EndIf

If KeyHit(KEY_BACKSPACE)
tmpEckpunkt.Loschen()
Update = 1
EndIf

If KeyHit(KEY_RIGHT)
Punkte = Punkte + 2000
Update = 1
ElseIf KeyHit(KEY_LEFT)
Punkte = Punkte - 2000
Update = 1
EndIf

If KeyDown(KEY_UP)
Alpha = Alpha + 0.01
Update = 1
ElseIf KeyDown(KEY_DOWN)
Alpha = Alpha - 0.01
Update = 1
EndIf

If KeyHit(KEY_F1) Then If Help = 1 Then Help = 0 Update = 1 Else Help = Help + 1 Update = 1
End Function

Function DrawHelp()
DrawText "tmp Rotation A/Y: " + tmpEckpunkt.Rotation, 0, 0
DrawText "tmp Skalierung S/X: " + tmpEckpunkt.Skalierung, 0, 15
DrawText "tmp Wahrscheinlichkeit D/C: " + tmpEckpunkt.Wahr, 0, 30
DrawText "Punkte Hoch/Runter: " + Punkte, 0, 45
DrawText "Punkte Tranzparenz Links/Rechts:" + Alpha, 0, 60
DrawText "Mausrad: Neuen Punkt erstellen, Backspace Loeschen", 0, 75
DrawText "Rechte Maustaste: Punkt auswählen linke bewegen", 0, 90
End Function

Leider hab ich erst später bemerkt, dass man die zufalls auswshl der punkte auch mit normalen lists hätte lösen können. aber so funktuinierts ja auch Smile

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group