Homework Assignment 6
CS 316, Spring 2003
Due on Tuesday, April 1, 2003

For this assignment you will do some graphics programming using a simple graphics library included with DrScheme, the "viewport graphics library". The documentation for this library is contained in the manual "PLT Miscellaneous Libraries: Reference Manual", which is part of DrScheme's built in documentation. The fastest way to get to this online documentation is to start DrScheme, go to the Help menu and click on the first menu item, "Help Desk", and then use the Help Desk's search box to search for the string "viewport graphics". You can also read this documentation over the Internet at the following URL:

http://download.plt-scheme.org/doc/203/html/misclib/misclib-Z-H-2.html

This assignment has three problems. The first two problems have you draw "recursive," fractal images and for the third problem you will draw an iterative, "bitmapped" image.

1.) Write a Scheme program that draws a "Sierpinski triangle". This is a fairly simple fractal image. It is defined by taking a triangle, dividing the triangle into four sub-triangles (by bisecting each of the three sides of the original triangle) and then recursing on three of the four sub-triangles (the three which have a vertex in common with the original triangle). Click repeatedly on either of the triangles below to watch how a Sierpinski triangle is built up (these are drawn by a Java applet).

Your Scheme program should be able to draw a Sierpinski triangle starting from any triangle in the graphics window. Your program does not need to "animate" the drawing of the Sierpinski triangle (like the Java applet does with the above two images). Your program should draw a Sierpinski triangle with 7 or 8 levels of recursion.

2.) Write a Scheme program that draws a "C-curve". A C-curve is a slightly more complicated fractal than a Sierpinki triangle. A C-curve is defined by starting with a line segment, and then replacing the line segment with two line segments in such a way that the two new line segments, along with the original line segment, make up an isosceles right triangle with the original line segment as the hypotenuse and the two new line segments as the legs. After the original line segment is replaced by the two new line segments, we recurse on each of the two new line segments. Click repeatedly on the line segment below to see how a C-curve is built up. After each click, the green segments are the segments that were replaced to get the next stage of the development of the C-curve.

Your Scheme program should be able to draw a C-curve starting from any line segment in the graphics window. Your program does not need to "animate" the drawing of the C-curve (like the Java applet does with the above image). Your program should draw a C-curve with 15 or 16 levels of recursion.

3.) In this problem you will draw an image that is defined pixel by pixel in an iterative manner. The image will be drawn in a 400 by 400 pixel graphics window. Your program will set every pixel in the graphics window to either black or white. Here is an algorithm for deciding the color of any given pixel. First, choose a square region in the mathematical xy-plane. This square region is determined by choosing three numbers, the x and y coordinates of the upper left hand corner of the square region and the dimension of the sides of the square. Denote these three numbers by x-corner, y-corner, and square-length. We will think of each point in the (square) graphics window as representing a point from the chosen square region in the mathematical xy-plane. Given a point in the graphics window with pixel coordinates i and j, we associate that point with a point in the plane region with coordinates x and y. (We will distinguish coordinates in the mathematical plane from coordinates in the graphics window. We call coordinates in the mathematical plane "world coordinates" and we call coordinates in the graphics window "pixel coordinates".) The mapping from pixel coordinates to world coordinates is defined to maintain the relative position of the coordinates within their respective square. So, for example, if x-corner is -100, y-corner is 100, and square-length is 200, then the pixel coordinates i=100 and j=200 (which represent a point in the graphics window one quarter of the way across and half way down from the upper left hand corner) would translate to the world coordinates x=-50 and y=0. To determine how the pixel with pixel coordinates i and j should be colored, first find the world coordinates x and y associated to the pixel coordinates i and j, then compute the value of the expression x^2+y^2 (call this value r), then find the integer part of r. If the integer part of r is even, color the pixel black, otherwise color the pixel white.

The images drawn by this algorithm depend on the choices of the three parameters x-corner, y-corner, and square-length. So that you can get an idea of what kinds of images you will be producing, here is a Java applet that implements this algorithm. Try changing the values of the parameters and then clicking on the "Redraw" button. For example, try the values, -83, 83, 166, or the values -1000, 1000, and 2002 (the variety of images is really amazing). The idea for this algorithm comes from the September, 1986, issue of Scientific American.

Note: There is a simple change that you can make to your program that has a dramatic and interesting effect on the images it produces. Instead of using the expression x^2+y^2, use the expression sqrt(x^2+y^2).

4.) To help you get started with graphics programming in Scheme using the viewport graphics library, here is a Scheme program, koch-curve.scm, that draws a fractal called a Koch Curve. A Koch Curve is defined by starting with a line segment, dividing the line segment into three equal length pieces, and then replacing the middle piece with two line segments in such a way that the two new line segments, along with the replaced piece, make up an equilateral triangle. After the middle piece is replaced by the two new line segments, we recurse on each of the four remaining line segments. Click repeatedly on the line segment below to see how a Koch Curve is built up.


Return to the main homework page.
Return to the CS 316 home page.


compliments and criticisms