# 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 co-ordinates specified in the
brackets. Note winlogo uses a

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 Co-ordinates.
Sets the variables X1 to X4 and Y1.
to Y4 Note the variables X1 to X3
and Y1 to Y3 relate to the
co-ordinates 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 co-ordinates of the new point
then become X4 and Y4. These
are used on the next cycle of the
programme.

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 requires an input to the
programme of setsteps (number).
This number becomes the number
of cycles that the programme will
go through.i.e if we type setsteps
100
the programme 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 programme.
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 programme will call
one of the procedures corner1,
corner2 or corner3. Once the new
co-ordinates 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 programme has
completed. If it has not reached
to the number we selected at
setsteps the programme 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