Microsoft Small Basic
Program Listing:
Embed this in your website
<object id='sbapp' data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='640' height='480'> <param name='source' value='http://smallbasic.com/program/ClientBin/SBWeb.xap'/> <param name='onError' value='onSilverlightError' /> <param name='background' value='white' /> <param name='minRuntimeVersion' value='3.0.40624.0' /> <param name='autoUpgrade' value='true' /> <param name='initParams' value='programId=QRS854' /> </object>
' Sierpinski Triangle created using the Chaos Game algorithm (http://en.wikipedia.org/wiki/Sierpinski_triangle)
' By Neil Kendall, August 2011
' SmallBASIC Challenge of the Week 9 - Advanced Challenge (http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/15a4a5bf-98d8-42f9-93ce-7246ef584a2c)
' Triangle vertices created at random, initial start point is also random [possible extension - see below].
Size
=
400
' Max sixe of Graphics Window - change this to suit your screen resolution
Iterations
=
30000
' Change this value for number of pixels plotted. The higher the value the better the picture, but slower to draw.
TriangleNumber
=
1
' Keep track of number of triangles plotted
' Set up graphics window
GraphicsWindow
.
Height
=
Size
GraphicsWindow
.
Width
=
Size
GraphicsWindow
.
BackgroundColor
=
"Blue"
GraphicsWindow
.
BrushColor
=
"White"
Loop
:
' Start again after each triangle is plotted
GraphicsWindow
.
Clear
(
)
TriangleColour
=
graphicswindow
.
GetRandomColor
(
)
' Get a random colour to draw the triangle
GraphicsWindow
.
DrawText
(
5
,
5
,
"Random Sierpinski Triangle. Number "
+
TriangleNumber
)
' Display triangle number
' Generate 3 random vertices for the triangle
' Possible extension for you - make sure the random vertices 'look' like a triangle - E.g. Some sets of 3 random vertices are almost in a straight line so the triangle does not look good.
Vertex1X
=
Math
.
GetRandomNumber
(
Size
)
Vertex1Y
=
Math
.
GetRandomNumber
(
Size
)
Vertex2X
=
Math
.
GetRandomNumber
(
Size
)
Vertex2Y
=
Math
.
GetRandomNumber
(
Size
)
Vertex3X
=
Math
.
GetRandomNumber
(
Size
)
Vertex3Y
=
Math
.
GetRandomNumber
(
Size
)
' Set Initial Plot Position (random). If this position is within the area of the random triangle then random pixels outside of the triangle will not be plotted. [Very good extension exercise for you!]
CurrentPosX
=
Math
.
GetRandomNumber
(
Size
)
CurrentPosY
=
Math
.
GetRandomNumber
(
Size
)
For
count
=
0
To
Iterations
RandomVertex
=
math
.
GetRandomNumber
(
3
)
' Choose a random vertex
If
RandomVertex
=
1
Then
NewPositionX
=
(
CurrentPosX
+
Vertex1X
)
/
2
' Calculate the X-coord of the midpoint between the current position and Vertex1
NewPositionY
=
(
CurrentPosY
+
Vertex1Y
)
/
2
' Calculate the Y-coord of the midpoint between the current position and Vertex1
GraphicsWindow
.
SetPixel
(
NewPositionX
,
NewPositionY
,
TriangleColour
)
CurrentPosX
=
NewPositionX
' Make the current position the new calculated position
CurrentPosY
=
NewPositionY
EndIf
If
RandomVertex
=
2
Then
NewPositionX
=
(
CurrentPosX
+
Vertex2X
)
/
2
' Calculate the X-coord of the midpoint between the current position and Vertex2
NewPositionY
=
(
CurrentPosY
+
Vertex2Y
)
/
2
' Calculate the Y-coord of the midpoint between the current position and Vertex2
GraphicsWindow
.
SetPixel
(
NewPositionX
,
NewPositionY
,
TriangleColour
)
CurrentPosX
=
NewPositionX
' Make the current position the new calculated position
CurrentPosY
=
NewPositionY
EndIf
If
RandomVertex
=
3
Then
NewPositionX
=
(
CurrentPosX
+
Vertex3X
)
/
2
' Calculate the X-coord of the midpoint between the current position and Vertex3
NewPositionY
=
(
CurrentPosY
+
Vertex3Y
)
/
2
' Calculate the Y-coord of the midpoint between the current position and Vertex3
GraphicsWindow
.
SetPixel
(
NewPositionX
,
NewPositionY
,
TriangleColour
)
CurrentPosX
=
NewPositionX
' Make the current position the new calculated position
CurrentPosY
=
NewPositionY
EndIf
EndFor
TriangleNumber
=
TriangleNumber
+
1
Goto
Loop
Copyright (c) Microsoft Corporation. All rights reserved.