COMPILATION =========== gccx-stan -g -Wall -I /home/course/cs113/F96/include -c hw7-koch.c gccx-stan -g -Wall -I /home/course/cs113/F96/include -o hw7-koch hw7-koch.o /home/course/cs113/F96/include/turtle.o EXECUTION TESTS ========= ===== ============================================================ hw7-koch /home/course/cs113/GradesF96/HW7/inputs/keystrokes.1 This program will draw Koch's snowflake Enter the length of one side: Enter degree of snowflake (max = 5): ============================================================ hw7-koch /home/course/cs113/GradesF96/HW7/inputs/keystrokes.2 This program will draw Koch's snowflake Enter the length of one side: Enter degree of snowflake (max = 5): Data files: ========== ==> /home/course/cs113/GradesF96/HW7/inputs/keystrokes.1 <== 4 0 ==> /home/course/cs113/GradesF96/HW7/inputs/keystrokes.2 <== 2 2 ============================================================ SOURCE FILES ====== ===== /* * File: hw7-koch.c * ===== ========== * Class: CAS CS 113 * Assignment: HW7 * Due Date: 19 November 1996 * Last modified: 10 November 1996 * ----------------------------------------------------- * * USAGE: hw7-koch (no commandline arguments) * ====== * * OVERVIEW: * ========= * This program displays a famous fractal known as a Koch Snowflake. The * user is asked to input the length of the sides, and the degree of * generation to output for the fractal. * * Algorithm Notes: * ================ * A recursive function DrawKochSegment() is used under the wrapper function * DrawKochSnowflake() to print each line segment of the fractal. * * Known Bugs: none * =========== * * Enhancements: none * ============= * * Error Handling: * =============== * User enters a non-numerical number * Error Message: Text displayed as to why input is unacceptable. * Error Action: User is given another chance to re-enter. * * Other Comments: * =============== * Generally, MAX_DEGREE should be kept under 7, as this fractal will * take a VERY long time to compute. * */ /* program shell for hw7, fall 1996 */ /* edit this file, adding CODE and COMMENTS as necessary. */ #include #include "turtle.h" #include "simpio.h" #include "genlib.h" /* -------------------- * Constant: MAX_DEGREE * -------------------- * Set this to the maximum number of * iterations to allow for the fractal */ const int MAX_DEGREE = 5; /* -------------------------------------- * Function: GetParameters * Usage: GetParameters(&length, °ree) * -------------------------------------- * A call to GetParameters() reads in the appropriate input data * from the user and places it in "length" and "degree" */ void GetParameters(double *lengthPtr, int *degreePtr); /* ---------------------------------------- * Function: DrawKochSnowflake * Usage: DrawKochSnowflake(length, degree) * ---------------------------------------- * DrawKochSnowflake() draws a fractal Koch snowflake at the * current turtle position with segment length of "length" and * iteration depth "degree". */ void DrawKochSnowflake(double length, int degree); /* -------------------------------------- * Function: DrawKochSegment * Usage: DrawKochSegment(length, degree) * -------------------------------------- * This function, used by DrawKochSnowflake() recursively * draws a segment of a Koch snowflake with length "length" and * iteration depth "degree". */ void DrawKochSegment(double length, int degree); int main(void) { double length = 1; int degree = 1; InitTurtle(); GetParameters(&length, °ree); /* position and orient turtle */ PlaceTurtle(0.2, length/3.0); TurnRight(GetCurrentDirection()); /* sets angle to 0 = directly right */ TurnLeft(60); /* sets angle to 60 degrees */ DrawKochSnowflake(length,degree); return(0); } void GetParameters(double *lengthPtr, int *degreePtr) { printf("This program will draw Koch's snowflake\n\n"); printf("Enter the length of one side: "); *lengthPtr = GetReal(); printf("Enter degree of snowflake (max = %i): ",MAX_DEGREE); *degreePtr = GetInteger(); if (*degreePtr > MAX_DEGREE) { *degreePtr = MAX_DEGREE; printf("Your degree is too large. Using %d instead.",*degreePtr); } if (*degreePtr < 0) { *degreePtr = 0; printf("Your degree is too small. Using %d instead.",*degreePtr); } } void DrawKochSnowflake(double length, int degree) { /* Draw a triangle with sides made up of KochSegments */ DrawKochSegment(length, degree); TurnRight(120); DrawKochSegment(length, degree); TurnRight(120); DrawKochSegment(length, degree); } void DrawKochSegment(double length, int degree) { /* Base Case -- Draw a straight line */ if (degree <= 0) { DrawTurtle(length); return; } /* Recursive Case -- Draw a Koch segment with a triangle in the * middle, which is made up of Koch segments */ DrawKochSegment(length / 3.0, degree - 1); TurnLeft(60); DrawKochSegment(length / 3.0, degree - 1); TurnRight(120); DrawKochSegment(length / 3.0, degree - 1); TurnLeft(60); DrawKochSegment(length / 3.0, degree - 1); } /* -----------------------> END OF FILE: hw7-koch.c <------------------ */