WSU Tri-Cities CptS 121 (Spring, 2017)
Due Date:3/23/17

Homework 5: The Sierpinski Gasket

One of the major discoveries of 20th century mathematics is "fractals". These are mathematical shapes that (usually) exhibit "self-similarity": Similar patterns occur in them regardless of the scale you look at them. The most famous of these is the "Mandelbrot Set":

Mandel_zoom_00_mandelbrot_set.jpg

but there are many others. In this homework, you'll write a program that will draw one of them, the Sierpinski "gasket". (You may of course look up what one looks like on the web, but you'll be more amazed if you get your program to draw one first.)

This homework assumes you have familiarity with the grafic package from the previous lab. It's best to have a working version of that lab before starting this.

The sierpinski Program

Your assignment is to implement the sierpinski program to draw a Sierpinski gasket. Here is the template:

#include <stdio.h> // for I/O routines
#include <math.h>  // for cos(), sin(), and M_PI

#include "grafic.h"

int maxLevel; // >= 0 (a level 0 Sierpinski gasket is a triangle)
// note: This needs to be a global, since its set in `init()` and used
// in `redraw()`

void init(const char **appTitle_pp)
// initialize the "grafic" library
{
    /*
     * ASSIGNMENT
     *
     * set `*appTitle_pp` to a title string of your choice (you could
     *  include your name, for instance)
     * repeat the following indefinitely:
     *     prompt the user to enter a maximum level (hint: `printf()`)
     *     read the user's answer into `maxLevel` (hint: `scanf()`)
     *     if `maxLevel` is 0 or more
     *         exit the loop (which will return from the function)
     *     print an error message about entering a non-negative value
     *
     * (This function can be adapted from the one you developed in lab.)
     */
}

void triangle(double p0[2], double p1[2], double p2[2])
// draw the triangle (`p0`, `p1`, `p2`)
{
    /*
     * ASSIGNMENT
     *
     * draw a line from `p0` to `p1` (hint: `line()`)
     * draw a line from `p1` to `p2` (hint: `line()`)
     * draw a line from `p2` to `p0` (hint: `line()`)
     */
}

void midpoint(double p0[2], double p1[2], double pMid[2])
// set `pMid` to the midpoint of `p0` and `p1`
{
    /*
     * ASSIGNMENT
     *
     * set the x coordinate of `pMid` to the mean of the x coordinates
     *  of `p0` and `p1`
     * set the y coordinate of `pMid` to the mean of the y coordinates
     *  of `p0` and `p1`
     */
}

void sierpinski(int level, double p0[2], double p1[2], double p2[2])
// draw a Sierpinski gasket of level `level` within the triangle
// (`p0`, `p1`, `p2`)
{
    /*
     * ASSIGNMENT
     *
     * declare points `p01`, `p12`, and `p20` as 2D double arrays
     * if level is zero
     *     draw the triangle `p0`, `p1`, and `p2` (hint: `triangle()`)
     * otherwise
     *     set `p01` to the midpoint of `p0` and `p1` (hint: `midpoint()`)
     *     set `p12` to the midpoint of `p1` and `p2` (hint: `midpoint()`)
     *     set `p20` to the midpoint of `p2` and `p0` (hint: `midpoint()`)
     *     draw a Sierpinski gasket of `level - 1` using `p0`, `p01`,
     *      and `p20` (hint: `sierpinski()`)
     *     draw a Sierpinski gasket of `level - 1` using `p01`, `p1`,
     *      and `p12` (hint: `sierpinski()`)
     *     draw a Sierpinski gasket of `level - 1` using `p20`, `p12`,
     *      and `p2` (hint: `sierpinski()`)
     */
}

void redraw(void)
// redraw the figure for the "grafic" library
{
    /*
     * ASSIGNMENT
     *
     * declare points `p0`, `p1`, and `p2` as 2D double arrays
     * declare `radius` and set it to 1.125
     * declare `yOffset` and set it to `0.25 * radius`
     * set `p0`'s components to `radius * cos( -M_PI/6)`
     *  and `radius * sin( -M_PI/6) - yOffset`
     * set `p1`'s components to `0` and `radius - yOffset`
     * set `p2`'s components to `radius * cos(-5*M_PI/6)`
     *  and `radius * sin(-5*M_PI/6) - yOffset`
     * draw a `maxLevel` sierpinski gasket based on `p0`, `p1`, and `p2`
     *  (hint: `sierpinski()`)
     */
}

Implement the psudocode. Note how easy recursion (in sierpinski()) makes this. It could be done without recursion, but the code would be longer, more bug-prone, and far less elegant.

Email your sierpinski.c (and only sierpinski.c) to the instructor as an attachment. Be sure to include "cpts121" and "hw5" in the subject line of the email.