Hi,
here is a programming exercise for you: create an nth-level
Sierpinski-pyramid that rotates on the screen. (see
http://images.google.com/images?q=sierpinski%20pyramid&hl=en )
Since this is a somewhat advanced task, I've broken it down to several
steps. You should do it one step at a time, and of course you can use
anything you want, but if you use a part of code found on the net, be
sure to _fully_ understand it. Ask me if anything is not clear.
(Wang: You should first do your research, do this only after your
presentation...)
-------------------------------------------------------------------------------
1. Write lines of the nth-level Cantor set to the standard output
-------------------------------------------------------------------------------
The Cantor set ( http://en.wikipedia.org/wiki/Cantor_set ) is a part
of the [0, 1] line segment, that you get by: a) initially the set
consists of all points in [0, 1] b) then you take the middle part,
leaving two segments, [0, 1/3] and [2/3, 1] c) for every segment, take
the middle part, leaving four segments, [0, 1/9], [2/9, 1/3], [2/3,
7/9], [8/9, 1] d) do this infinitely
By the way, it can be shown that the dimension (at least, one kind of
dimension) of this set is log3(2), I can show you why if you want. We
cannot do the algorithm above, since it is infinite, but we can do an
approximation by iterating it n times. So the 0th-level Cantor set is
[0,1], the 1st-level Cantor set is ([0, 1/3], [2/3, 1]) and so on.
Write a function, that takes an integer argument (n) and prints the
endpoints of the lines (in any order) onto the screen. E.g., for n =
2, it should print:
Cantor set for n = 2
0 0.11111111
0.22222222 0.33333333
0.66666666 0.77777777
0.88888888 1
-------------------------------------------------------------------------------
2. Write lines of the nth-level Sierpinski triangle to the standard
output
-------------------------------------------------------------------------------
This is much the same as the previous step, but the Sierpinski
triangle ( http://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle )
takes an equilateral triangle, and in every step, take the middle part
(defined by connecting the middle point of the edges), leaving 3 times
as more triangles. So, for n = 0, it is the triangle defined by the
points (0, 0), (1/2, sqrt(3)/2), (1, 0) For n = 1, it is divided to
three triangles: [(0, 0), (1/4, sqrt(3)/4), (1/2, 0)], [(1/4,
sqrt(3)/4), (1/2, sqrt(3)/2), (3/4, sqrt(3)/4)], [(1/2, 0), (3/4,
sqrt(3)/4), (1, 0)] .,. and so on. By the way, the dimension of this
one is log2(3). The Cantor set and the Sierpinski triangle both have a
dimension that is not an integer, that's why they are called
"fractals".
Write a function, that takes an integer argument (n) and prints the
endpoints of the lines of these triangles onto the screen. E.g., for n
= 1, it should print:
Sierpinski triangle for n = 1
Triangle 1
0 0
0.25 0.4330127
0.5 0
Triangle 2
0.25 0.4330127
0.5 0.8660254
0.75 0.4330127
Triangle 3
0.5 0
0.75 0.4330127
1 0
[OPTIONAL START
Advanced version (this will result in a bit more
efficient code, but do this only if you want):
you should treat points and lines as different entities, so the output
should look like (the order of points, and thus the indices in the
triangle section may be different):
Sierpinski triangle for n = 1
Points
0 0
0.25 0.4330127
0.5 0
0.5 0.8660254
0.75 0.4330127
1 0
Triangles
0 1 2
1 3 4
2 4 5
OPTIONAL END]
-------------------------------------------------------------------------------
3. Create a graphical (3D) interface
-------------------------------------------------------------------------------
Instead of writing to the standard output, draw the lines in an OpenGL
window.
=======
3.1 Polygons
Now you have the lines, you should fill the triangles with some color.
=======
3.2 Rotation
Rotate the triangle around the y-axis slowly (for example, it should
turn 5 degrees in every 0.15 secs).
=======
3.3 Keyboard input
Pressing "q" should quit the program. Pressing "+" or "-" should
increase/decrease the level of detail (n).
-------------------------------------------------------------------------------
4. Sierpinski Pyramid
-------------------------------------------------------------------------------
It's time for the final step: alter your function to return the faces
of the nth-level Sierpinski pyramid. This is created from a
tetrahedron (for equations about tetrahedrons, see
http://en.wikipedia.org/wiki/Tetrahedron ), and has four triangles for
every pyramid part, so for e.g. n = 0 the vertices will be:
[(0, 0, 0), (0.5, 0.8164966, 0.28867513), (1, 0, 0), (0.5, 0, 0.8660254)]
Admittedly, this step may require a bit of thinking :)
-------------------------------------------------------------------------------
5. DONE!
-------------------------------------------------------------------------------
Have fun with your program - add some interesting colors, new features
and so on.
peter