Sierpinski Dreieck

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Addi

Betreff: Sierpinski Dreieck

BeitragFr, Dez 20, 2013 20:22
Antworten mit Zitat
Benutzer-Profile anzeigen
Hier mal ein kleines Programm, welches das Sierpinskie Dreieck beliebiger Iteration auf dem Blidschirm ausgibt.
Man sollte es mit den Iterationen allerdings nicht übertreiben, weil bei jeder Iteration neue Dreiecke entstehen
und so bei hoher Iterationszahl massig Speicher belegt wird (temporär).

Das Programm folgt dem IFS beschrieben auf folgender Seite:
http://ecademy.agnesscott.edu/...iertri.htm

Viel Spaß damit Very Happy

BlitzBasic: [AUSKLAPPEN]
Type Triangle
Field point1[1]
Field point2[1]
Field point3[1]
End Type

Type TriangleList
Field tri.Triangle
Field it
End Type

Const SIDE = 400

;----------;
;Start Dreieck

Local tri.Triangle = New Triangle
tri\point2[0] = SIDE
tri\point3[0] = SIDE/2
tri\point3[1] = -(Sin(60)*Float(SIDE))

Local triList.TriangleList = New TriangleList
triList\tri = tri

;----------;
;Die Frage nach der Iteration

AppTitle "Sierpinski Dreieck"

Local it = Input("Wieviele Iterationen?: "), tempTri.TriangleList, i, j

If it > 0 Then
For i = 0 To it - 1
For triList = Each TriangleList
If triList\it = i Then

;Jeweils 3 Kopien vom aktuellen 3Eck erstellen und diese Transformieren
For j = 0 To 2
tempTri = New TriangleList
tempTri\it = i + 1
tempTri\tri = CopyTriangle(triList\tri)
Insert tempTri After Last TriangleList

If j = 0 Then Transform1(tempTri\tri)
If j = 1 Then Transform2(tempTri\tri)
If j = 2 Then Transform3(tempTri\tri)
Next

;Vorgänger löschen
Delete triList\tri
Delete triList
Else
Exit
End If
Next
Next
End If

Delete Each TriangleList

;----------;
;Bild erstellen und darstellen
Graphics 800, 600, 16, 2

Local pic = CreateImage(800, 600)
SetBuffer ImageBuffer(pic)

For tri = Each Triangle
Line tri\point1[0] + (400 - SIDE/2), tri\point1[1] + 400, tri\point2[0] + (400 - SIDE/2), tri\point2[1] + 400
Line tri\point2[0] + (400 - SIDE/2), tri\point2[1] + 400, tri\point3[0] + (400 - SIDE/2), tri\point3[1] + 400
Line tri\point3[0] + (400 - SIDE/2), tri\point3[1] + 400, tri\point1[0] + (400 - SIDE/2), tri\point1[1] + 400
Next

Delete Each Triangle

SetBuffer FrontBuffer()
DrawImage pic, 0, 0

WaitKey
End

;----------;
;IFS Funktionen des Sierpinskie Dreiecks

;1. Funktion des IFS
Function Transform1(tri.Triangle)
Local i

For i = 0 To 1
tri\point1[i] = tri\point1[i]*.5
tri\point2[i] = tri\point2[i]*.5
tri\point3[i] = tri\point3[i]*.5
Next
End Function

;2. Funktion des IFS
Function Transform2(tri.Triangle)
Local i

For i = 0 To 1
tri\point1[i] = tri\point1[i]*.5
tri\point2[i] = tri\point2[i]*.5
tri\point3[i] = tri\point3[i]*.5
Next

tri\point1[0] = tri\point1[0] + SIDE/2
tri\point2[0] = tri\point2[0] + SIDE/2
tri\point3[0] = tri\point3[0] + SIDE/2
End Function

;3. Funktion des IFS
Function Transform3(tri.Triangle)
Local i

For i = 0 To 1
tri\point1[i] = tri\point1[i]*.5 + (-1 + 2*(i = 0)) * ((i = 0)*Float(SIDE/4) + (i = 1)*Float(Sqr(3)/4)*SIDE)
tri\point2[i] = tri\point2[i]*.5 + (-1 + 2*(i = 0)) * ((i = 0)*Float(SIDE/4) + (i = 1)*Float(Sqr(3)/4)*SIDE)
tri\point3[i] = tri\point3[i]*.5 + (-1 + 2*(i = 0)) * ((i = 0)*Float(SIDE/4) + (i = 1)*Float(Sqr(3)/4)*SIDE)
Next
End Function

;Lediglich eine Hilfsfunktion
Function CopyTriangle.Triangle(tri.Triangle)
Local tr.Triangle = New Triangle, i

For i = 0 To 1
tr\point1[i] = tri\point1[i]
tr\point2[i] = tri\point2[i]
tr\point3[i] = tri\point3[i]
Next

Return tr
End Function


user posted image


~EDITIERT~

Überlange unterbrechungsfreie Kommentarzeilen entfernt, weil diese das Forenlayout zerschiessen.
mfG, Holzchopf
  • Zuletzt bearbeitet von Addi am Fr, Dez 20, 2013 23:41, insgesamt 3-mal bearbeitet

Abrexxes

BeitragFr, Dez 20, 2013 21:19
Antworten mit Zitat
Benutzer-Profile anzeigen
Smile Erinnert mich an eine Frau die man immer in einem Nintendo RPG retten muss. Wie war doch gleich der Name Cool

SpionAtom

Betreff: Chaos game

BeitragSa, Dez 21, 2013 0:00
Antworten mit Zitat
Benutzer-Profile anzeigen
Ein faszinierender Algorithmus dazu: Chaos game (oder auf englisch hier)

Dieser Algorithmus funtioniert mit einem Zufallsgenerator. GEILOMAT!

BlitzBasic: [AUSKLAPPEN]
;http://en.wikipedia.org/wiki/Chaos_game
; 1. Take 3 points in a plane To form a triangle, you need Not draw it.
; 2. Randomly Select any point inside the triangle And consider that your current position.
; 3. Randomly Select any one of the 3 vertex points.
; 4. Move half the distance from your current position To the selected vertex.
; 5. Plot the current position.
; 6. Repeat from Step 3.
;
;umgesetzt von SpionAtom - Dezember 2o13

Graphics 640, 480, 0, 2
AppTitle("Sierpinski triangle - Chaos game")
SeedRnd MilliSecs()
SetBuffer BackBuffer()

Const MAX = 3
Global point[MAX] ;P1(point[0] | point[1]), P2(point[2] | point[3]), P3(point[4] | point[5])
Local iterations = 300

Repeat
;inputs
iterations = iterations + MouseZSpeed() * 10

Cls
SierpinskiChaos(iterations)
Color 255, 255, 255
Text 0, 0, "Iterations: " + iterations + " (use mousewheel to change)"
Color 255, 255, 0
For i = 0 To MAX - 1
Oval point[2 * i] - 3, point[2 * i + 1] - 3, 6, 6
Next

Flip()


Until KeyDown(1)
End



Function SierpinskiChaos(iterations)
;1. Take 3 points in a plane To form a triangle, you need Not draw it.
setPoints(MilliSecs() / 50.0)

;2. Randomly Select any point inside the triangle And consider that your current position.
sx# = GraphicsWidth() / 2
sy# = GraphicsHeight() / 2

LockBuffer()
For i = 1 To iterations

;3. Randomly Select any one of the 3 vertex points.
vertex = Rand(0, MAX - 1)
vertex_X = point[2 * vertex]
vertex_Y = point[2 * vertex + 1]

;4. Move half the distance from your current position To the selected vertex.
sx = (sx + vertex_X) / 2
sy = (sy + vertex_Y) / 2

;5. Plot the current position.
WritePixel sx, sy, 255 * $10000 + 155 * $100 + 55

;6. Repeat from Step 3.
Next
UnlockBuffer()

End Function


Function setPoints(angle#)
mx = GraphicsWidth() / 2
my = GraphicsHeight() / 2
If GraphicsWidth() > GraphicsHeight() Then radius = GraphicsHeight() / 2 - 30 Else radius = GraphicsWidth() / 2 - 30

For i = 0 To MAX - 1
point[2 * i] = mx - Sin(angle + i * 360 / MAX) * radius
point[2 * i + 1] = my - Cos(angle + i * 360 / MAX) * radius
Next

End Function



user posted image
os: Windows 10 Home cpu: Intel Core i7 6700K 4.00Ghz gpu: NVIDIA GeForce GTX 1080

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group