COSC 8 -- Problem Solving with CS

CS 8, Fall 2009

Problem Set 1: Snowflake Fractal


General Instructions

Before you do anything, please read the entire assignment carefully. It contains many suggestions which can save you a lot of struggle later on. To borrow an epigram from the carpenter's trade, think twice, code once. You will have a much easier and more fun time with all the assignments in this class, if you keep that in mind!

You should print out hard copy of your code and any example outputs, and either turn them in at lecture or in the TA's mailbox at Sudikoff by class time on Wed. Oct. 7, 2009. Late assignments will be strongly penalized, so please start early.

For this assignment, you are to work alone. You may not share code with anyone else, nor may you look at anyone else's code.

What to Turn In

For CS 8 programming assignments, such as this one, you must turn in a hard copy listing of any procedures you wrote yourself, or modified from the example code provided with the assignment. You must also turn in example output for each section of the problem set, as applicable.

You should not turn in printouts of any code we provided you which you did not make any changes to, and please be sure to label everything carefully, so that the graders will know what they are looking at.

You must also e-mail an electronic version of your code to cs8hw@cs.dartmouth.edu with the subject "CS 8 Problem Set 1", by no later than 2:00 am on the date it is due. Please organize your code in one or more plain text files and attach them as enclosures to your message. Do not include your code directly in the body of the e-mail message! If you are sending more than one file, please make sure the files have descriptive names, and that you include comments that will let the graders know what they are looking at.

Code submitted electronically must be sent as plain text, in one or more files with the filename extension ".hs" or ".lhs" (e.g., mycode.hs). Do not send your programs as Word documents, RTF, or other "formatted" types of files.

Output

For each part of any assignment, we require output which demonstrates that your code correctly solves the problem it is supposed to solve. If you turn in code with no output, you may receive only partial credit, or no credit, for that part of the assignment.

Some problems will give specific test cases you need to provide output for. However, even if specific test cases are not provided, and even if it is not listed under "What to Turn In" for a particular problem, you must provide some sample output that shows how your code behaves on typical inputs.

If you are unable to get your code working, please turn in whatever you do have, so that we can determine if you should receive partial credit. Please do not include output which suggests your code works if it doesn't -- however, if your code works for some of the test cases, and not for others, you should turn in whatever output you are able to generate, even if it is incorrect. You will get credit for the "Testing" part of the score, even if the tests indicate that the program is incorrect.

Output is not required to be submitted electronically, although you are welcome to do so if you wish. If you do choose to submit output electronically, put your output samples in a separate file from your code submissions. Output may be submitted in any reasonable file format, though plain text is easiest to read.


Background: Fractals

In this problem set, we will be experimenting with fractals. Among the most famous fractals are the Peano Curve, the Hilbert Curve, the Cantor set, the Julia set, the Mandelbrot set, and the von Koch Snowflake, the latter of which was first described by Helge von Koch in 1904. These figures are not only aesthetically pleasing, but also have some important applications. For example, the Hilbert curve is used in dithering and compressing bit-mapped images. In fact, a computational geometer recently used it to specify the order that points should be processed when computing a geometric data structure called the Voronoi Diagram!

For more information about fractals and their mathematical properties, we have a short handout on the topic. For even more depth on the subject, see:

Fractals: Form, Chance, and Dimension, by Benoit B. Mandelbrot
published by Freeman Press, 1977.


Snowflake Fractal

For this assignment you are to implement a Haskell program that draws the Snowflake fractal described as Exercise 3.2 on p. 47 of SOE. However, the Figure 3.3 on p. 46 is incorrect - at lower levels it draws triangles, not stars. A correct five-level snowflake is:

Five-level Fractal Snowflake

To draw the figure you should write a function drawSnowflake with type signature


drawSnowflake :: Window -> Float -> [Color] -> Point -> IO ()

The parameters are the drawing window, the radius of the snowflake, a list of colors for each successive level, and the point defining the center of the snowflake. Rather than stopping when the stars are too small, I suggest stopping when there are no colors left. The snowflake drawn above resulted from the call:


drawSnowflake w 243 [Red, Blue, Yellow, Green, White] (375, 375) 

where w is a 750 by 750 drawing window.


Some Suggestions

The following suggestions may or may not be useful, depending on your approach.

Computing the corners of the star

The parameters to drawSnowflake include the center of the snowflake and the radius. Remember that points on a circle can be computed using the sin and cos functions, which are built into Haskell. Talk to me or the course staff if you don't know how to compute a point on a circle given a center, radius, and angle.
Note:
The sin and cos functions expect their angle to be specified in radians, not degrees. Zero radians is the direction horizontal to the right. Positive angles increase counter-clockwise, but because the graphics y-axis goes in the opposite direction as the mathematical y-axis positive angles will increase clockwise on the graphics window.

Drawing nothing

You may find it useful to have a value that draws nothing. The way to do this is shown on the fourth line of p. 53: return () The reason why the return is needed will be discussed when we talk about things called monads.

Write helper functions

Writing small helper functions makes your code easier to read and debug. Similarly, assigning names to expressions using "let" or "where" constructs makes your code easier to read and can prevent duplicating expressions.

Arithmetic sequences

The first "Details" box on p. 62 describes a notation for creating a list that is an arithmetic sequence. (You do not need to understand anything else in that chapter to understand this box.)

When to round

The Sierpinski's Triangle example keeps all coordinates in integers, using the div function when it halves the size. For the snowflake fractal it gives a more symmetric picture to keep track of radii as floating point numbers. You can use the round function to get the nearest integer when it is time to compute the vertices of polygons to be drawn.

What to Turn In

Submit a listing of the Haskell code for your program and a printout of the snowflake fractal that it draws. You should comment your code. A header comment should describe the program and give your name. Add comments to describe what functions do and what the parameters are, if it is not obvious from the function and parameter names. Email a copy of your code to cs8hw@cs.dartmouth.edu.

The rubric for grading can be found here.



Department of Computer Science, Dartmouth College, Hanover, New Hampshire, USA

Last modified: 06-October-2009