> | # We start with a triangle.
# For us, a triangle is a list, a list of points. # a point is itself a list, a list of coordinates # (namely, x and y coordinates) |
> | # so:
# a point at (0,0): |
> | [0,0]; |
> | # a point at (1,0):
|
> | [1,0]; |
> | # and a point at (0.5,1): |
> | [0.5,1]; |
> | # and now a triangle with these points as vertices:
|
> | [[0,0],[1,0],[0.5,1]]; |
> | # now let's plot the thing: |
> | plots[polygonplot]([%], axes=none,
color=black, scaling=constrained); |
> |
> |
> | # now we want to transform the triangle.
# The first step is to scale it down by 0.5, that gives |
> | [[0,0],[0.5,0],[0.25,0.5]]; |
> | # next, we want to translate it by 0.5 in the
# direction of the x-axis: |
> | [[0.5,0],[1,0],[.75,0.5]]; |
> | # and by 0.25 along the x-axis and by 0.5 along the y axis
# the original scaled triangle, I mean: |
> | [[0.25,0.5],[0.75,0.5],[0.5,1]]; |
> | # Now all three triangles together
# guess what, as a list, of course: |
> | L := [[[0,0],[0.5,0],[0.25,0.5]],[[0.5,0],[1,0],[.75,0.5]],[[0.25,0.5],[0.75,0.5],[0.5,1]]]; |
> | # are you starting to get confused by the many lists?
# No worries, this is as bad as it gets! |
> | # let's plot it: |
> | plots[polygonplot](L, axes=none,
color=black, scaling=constrained); |
> | # now, this is Sierpinski(1).
# Sierpinski(2) would be to do the scaling and shifting again, |
> | # once for each triangle in the list.
# that gives us 3*3 = 9 triangles. |
> | # Now we need to let Maple do that.
# The right thing to do is to write a procedure. |
> | # that procedure takes a triangle, scales it
# and shifts it. # Let s be the scaling factor (here 0.5) # let x and y be the shift along the x and y axes, respectively. # Here we go: |
> | transform := proc(T,s,x,y)
return [ [T[1][1]*s+x,T[1][2]*s+y], [T[2][1]*s+x,T[2][2]*s+y], [T[3][1]*s+x,T[3][2]*s+y]]; end: |
> | # Ok let's try it, we give it the original triangle, |
> | # and the scale and x/y shift: |
> | T := [[0,0],[1,0],[0.5,1]]; |
> | transform(T,0.5,0,0); |
> | transform(T,0.5,0.5,0); |
> | transform(T,0.5,0.25,0.5); |
> | # OK, that's good.
# now let's try to write a procedure Sierpinski: |
> | Sierpinski := proc(n)
local T,L; T := [[0,0],[1,0],[0.5,1]]; transform(T,0.5,0,0); transform(T,0.5,0.5,0); transform(T,0.5,0.25,0.5); end; |
> | Sierpinski(1); |
> | # oops, that was just one triangle!
# yes, we need to put the triangles in a list, that's what L is good for: |
> | Sierpinski := proc(n)
local T,L; T := [[0,0],[1,0],[0.5,1]]; L := [transform(T,0.5,0,0), transform(T,0.5,0.5,0), transform(T,0.5,0.25,0.5)]; end; |
> | L := Sierpinski(1); |
> | plots[polygonplot](L, axes=none,
color=black, scaling=constrained); |
> | # OK, that works. But how do we get to the next level?
# how do we make each triangle become three triangles? # let's try by putting in a for loop: |
> | Sierpinski := proc(n)
local T,L,i; T := [[0,0],[1,0],[0.5,1]]; for i to n do L := [transform(T,0.5,0,0), transform(T,0.5,0.5,0), transform(T,0.5,0.25,0.5)]; end; end; |
> | Sierpinski(1); |
> | # hmm, nothing happened. Why? |
> | # Well, in the for loop, we need to run throuh all the triangles in L
# and do the transform. # hence we need another for loop. # Remember that the length of a list was not length? # Right? It was nops. # So we need something like for k to nops(L); # Also, we need a second list where we put our transformed stuff in. # Call that list M. # Once we are finished with creating M, we let the new L be equal to M. # This gives: |
> |
> | Sierpinski := proc(n)
local L,M,i,k; L := [[[0,0],[1,0],[0.5,1]]]; for i to n do M := []; for k to nops(L) do M := [op(M), transform(L[k], 0.5,0,0), transform(L[k], 0.5,0.5,0), transform(L[k], 0.5,0.25,0.5)]; end; L := M; end; plots[polygonplot]([L], axes=none, color=black, scaling=constrained) end; |
> | Sierpinski(1); |
> | Sierpinski(2); |
> | Sierpinski(3); |
> | Sierpinski(4); |
> | Sierpinski(5); |
> | Sierpinski(6); |
> | Sierpinski(7); |
> | Sierpinski(8); |
> |
> |
> | # HOMEWORK |
> |
> | # hw 1
# a) How many black triangles are drawn by calling Sierpinsky(n)? # b) What is the ratio of black area in the triangle after a call # to Sierpinski(n)? Does that ratio converge as n goes to infinity? # c) Apparently the Sierpinsky triangle is not quite eqilateral # (it is slightly too high). # Can you fix the code to make it equilateral? |
> | # |