The Sierpinski Gasket In Winlogo


To triangle
pu setpos [-100 -100]
pd setpos [0 100]
setpos [100 100]
setpos [-100 -100]
end


Set up the Triangle.
Sets up the initial triangle using
the coordinates specified in the
brackets. Note winlogo uses a
four quadrant grid
 

to setcord
make "X1 0
make "X2 100
make "X3 -100
make "Y1 100
make "Y2 -100
make "Y3 -100
make "X4 0
make "X4 0
end

The Coordinates.
Sets the variables X1 to X4 and Y1.
to Y4 Note the variables X1 to X3
and Y1 to Y3 relate to the
coordinates of the corners of
the triangle.

to corner1
make "X4 sum :X1 :X4
make "Y4 sum :Y1 :Y4
make "X4 :X4/2
make "Y4 :Y4/2
end

The mathematics bit.
The 3 procedures work out the
midpoint between the corner
selected at random by the
sierpinski procedure and the
last point plotted on the screen.

The coordinates of the new point
then become X4 and Y4. These
are used on the next cycle of the
program.

to corner2
make "X4 sum :X2 :X4
make "Y4 sum :Y2 :Y4
make "X4 :X4/2
make "Y4 :Y4/2
end

to corner3
make "X4 sum :X3 :X4
make "Y4 sum :Y3 :Y4
make "X4 :X4/2
make "Y4 :Y4/2
end

to setsteps :iteration
make "a 1
make "c :iteration
end

How many times?
This needs an input to the
program of setsteps (number).
This number becomes the number
of cycles that the program will
go through.i.e if we type setsteps
100
the program will cycle 100 times.

to sierpinski
make "b random 3
make "b :b+1
test :b=1
iftrue [corner1]
test :b=2
iftrue [corner2]
test :b=3
iftrue [corner3]
setdot se :X4 :Y4 1
make "a :a+1
test :a=:c
iftrue [stop]
sierpinski
end

The main part of the program.
A random number is created from 1
to 3. This selects which corner
will be used to plot the midpoint.
Depending upon which number is
selected the program will call
one of the procedures corner1,
corner2 or corner3. Once the new
coordinates for X4 and Y4 have
been calculated the new point will
be plotted on the screen. The
next part counts how many cycles
(iterations) the program has
completed. If it has not reached
to the number we selected at
setsteps the program will run
itself again until it has
completed that many iterations.

Instructions to use the Program

Type the program in as above. After the program has been typed in you will need to run the following instructions
  • hideturtle
  • triangle
  • setcord
  • setseps "xxx"
  • sierpinski
nb xxx is the number you type in

This will operate the program for you.