Sign in to follow this  
Followers 0
multiloser

Sierpenski Triangle Simulation

1 post in this topic

#1 ·  Posted (edited)

I made a program which graphically iterates points on a graph, creating a Sierpenski Triangle.

The way it works is you must first press the "first point" button, then you press anywhere you want on the graph.

When you press "Red", "Blue", or "Green", the dot automatically travels half of the distance towards the selected dot.

"Random" randomly selects Red/Blue/Green to travel to, and Random 10 and Random 1000 do it 10 times and 1000 times respectively.

You need to have a jpg in C:/dot.jpg, so I *attached* mine *to this post*. I know that I can make the script self extract an image, but I'll do that later.

This was just a proof of concept...

Press "1000 Randoms" one or two times and see what happens! It's pretty sweet!

*EDIT*

Any further suggestions would be appreciated though!

CODE
#include <GUIConstants.au3>

#include <Misc.au3>

#Include <GuiEdit.au3>

Opt("MouseCoordMode",0)

$width = 700 ;default 345

$height = 750 ;default 330

GUICreate("Chaos Game", $width, $height)

$GraphWidth = 650 ;this is simply the pixel width of the control

$GraphHeight = 650 ;this is simply the pixel height of the control

$graph = GUICtrlCreateGraphic(25, 25, $GraphWidth, $GraphHeight)

GUICtrlSetBkColor(-1,0xFFFFFF) ;graph background colour

GUICtrlSetColor(-1,0x000000) ;graph border colour

GUICtrlSetGraphic(-1,$GUI_GR_HINT,0) ;turn off hints

Global $coord1x = 325

Global $coord1y = 75

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0xec1a23)

GUICtrlSetGraphic($graph,$GUI_GR_Pixel,$coord1x,$coord1y) ;;;;;;;; RED DOT

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord1x-1,$coord1y-1,3,3)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord1x-2,$coord1y-2,5,5)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord1x-3,$coord1y-3,7,7)

Global $coord2x = 25

Global $coord2y = 570

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x0000fe)

GUICtrlSetGraphic($graph,$GUI_GR_Pixel,$coord2x,$coord2y) ;;;;;;;; BLUE DOT

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord2x-1,$coord2y-1,3,3)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord2x-2,$coord2y-2,5,5)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord2x-3,$coord2y-3,7,7)

Global $coord3x = 625

Global $coord3y = 570

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x00fe00)

GUICtrlSetGraphic($graph,$GUI_GR_Pixel,$coord3x,$coord3y) ;;;;;;;; GREEN DOT

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord3x-1,$coord3y-1,3,3)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord3x-2,$coord3y-2,5,5)

GUICtrlSetGraphic($graph,$GUI_GR_Rect,$coord3x-3,$coord3y-3,7,7)

Global $Place = GuiCtrlCreateButton("Place 1st", 32, 680, 135, 30)

Global $Red = GuiCtrlCreateButton("Red", 199, 680, 135, 30)

Global $Blue = GuiCtrlCreateButton("Blue", 366, 680, 135, 30)

Global $Green = GuiCtrlCreateButton("Green", 533, 680, 135, 30)

Global $Dot

Global $currentx

Global $currenty

Global $newy

Global $newx

Global $x

Global $y

Global $Random = GuiCtrlCreateButton("Random", 32, 715, 135, 30)

Global $Random10 = GuiCtrlCreateButton("10 Randoms", 199, 715, 135, 30)

Global $Random1000 = GuiCtrlCreateButton("1000 Randoms (long)", 366, 715, 135, 30)

Global $iii = 9

$io = 0

GUISetState()

Do

$msg = GuiGetMsg()

If $msg = $place Then

sleep(10)

Do

if _IsPressed("01") = 1 Then

GUICtrlDelete ( $Dot )

$mousex = MouseGetPos(0)

$mousey = MouseGetPos(1)

$x = $mousex - 28

$y = $mousey - 55

$Dot = GUICtrlCreatePic ( "C:/dot.jpg", $x+22, $y+22,7,7)

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x000000)

GUICtrlSetGraphic($graph,$GUI_GR_pixel,$x,$y)

GUICtrlSetGraphic($graph,$GUI_GR_rect,$x-1,$y-1,3,3)

Global $currentx = $x

Global $currenty = $y

$io = 1

EndIf

Until $io = 1

EndIf

If $msg = $red Then

red()

EndIf

If $msg = $blue Then

blue()

EndIf

If $msg = $green Then

green()

EndIf

If $msg = $random Then

Global $col = Random (1,3,1)

If $col = 1 Then

red()

EndIf

If $col = 2 Then

blue()

EndIf

If $col = 3 Then

green()

EndIf

EndIf

If $msg = $random10 Then

$iii = 0

Do

Global $col = Random (1,3,1)

If $col = 1 Then

red()

EndIf

If $col = 2 Then

blue()

EndIf

If $col = 3 Then

green()

EndIf

$iii = $iii + 1

Until $iii = 10

EndIf

If $msg = $random1000 Then

$iii = 0

Do

Global $col = Random (1,3,1)

If $col = 1 Then

red()

EndIf

If $col = 2 Then

blue()

EndIf

If $col = 3 Then

green()

EndIf

$iii = $iii + 1

Until $iii = 1000

MsgBox (0, "Done", "1000 Random complete!")

EndIf

Sleep(1)

Until $msg = $GUI_EVENT_CLOSE

func green()

$newx = ($currentx + $coord3x)/2

$newy = ($currenty + $coord3y)/2

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x000000)

GUICtrlSetGraphic($graph,$GUI_GR_pixel,$newx,$newy)

GUICtrlSetGraphic($graph,$GUI_GR_rect,$newx-1,$newy-1,3,3)

$currentx = $newx

$currenty = $newy

GUICtrlSetPos ( $dot, $newx+22, $newy+22 ,7,7 )

EndFunc

func red()

$newx = ($currentx + $coord1x)/2

$newy = ($currenty + $coord1y)/2

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x000000)

GUICtrlSetGraphic($graph,$GUI_GR_pixel,$newx,$newy)

GUICtrlSetGraphic($graph,$GUI_GR_rect,$newx-1,$newy-1,3,3)

$currentx = $newx

$currenty = $newy

GUICtrlSetPos ( $dot, $newx+22, $newy+22 ,7,7 )

EndFunc

func blue()

$newx = ($currentx + $coord2x)/2

$newy = ($currenty + $coord2y)/2

GUICtrlSetGraphic($graph,$GUI_GR_COLOR, 0x000000)

GUICtrlSetGraphic($graph,$GUI_GR_pixel,$newx,$newy)

GUICtrlSetGraphic($graph,$GUI_GR_rect,$newx-1,$newy-1,3,3)

$currentx = $newx

$currenty = $newy

GUICtrlSetPos ( $dot, $newx+22, $newy+22 ,7,7 )

EndFunc

func close()

Exit

EndFunc

Edited by multiloser

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0