Name: Han, Raymond EXECUTION TESTS ========= ===== ============================================================ hw3-poly < poly.in This program will draw a polygon on the screen. You will be asked to enter the number of sides of the polygon and the length of one side. This program will exit when you enter a non-positive number of sides or length of side. Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Graphics in rayhan1.ps ============================================================ hw3-poly2 < poly.in This program will draw a polygon on the screen. You will be asked to enter the number of sides of the polygon and the length of one side. This program will exit when you enter a non-positive number of sides or length of side. Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Enter the length of one of the sides of the polygon. (negative value to quit): Enter the number of sides the polygon should have. (negative value to quit): Graphics in rayhan2.ps ============================================================ hw3-draw < draw.in This program can be used to test an implementation of turtle.h. The following options are available and may be given in either upper or lower case. D - DrawTurtle M - MoveTurtle P - PlaceTurtle O - Orient the turtle (set its direction) TL - TurnLeft TR - TurnRight AL - ArcLeft AR - ArcRight Q - Quit the program Additional Parameters (lengths and directions) will be requested from the user as necessary. Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 90.00 degrees. Location is now (4.25, 6.50). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): How many degrees right? Direction is now 0.00 degrees. Location is now (4.25, 6.50). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 0.00 degrees. Location is now (4.75, 6.50). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Radius = Angle = Direction is now 90.00 degrees. Location is now (5.25, 7.00). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 90.00 degrees. Location is now (5.25, 7.50). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): How many degrees left? Direction is now 225.00 degrees. Location is now (5.25, 7.50). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 225.00 degrees. Location is now (4.90, 7.15). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Radius = Angle = Direction is now 0.00 degrees. Location is now (4.54, 8.00). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 0.00 degrees. Location is now (5.54, 8.00). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): horizontal distance from left side (in inches)? vertical distance from bottom (in inches)? Direction is now 0.00 degrees. Location is now (2.25, 5.25). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Radius = Angle = Direction is now 360.00 degrees. Location is now (2.25, 5.25). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Draw how far forward (in inches)? Direction is now 360.00 degrees. Location is now (5.25, 5.25). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Invalid command. Try again. Direction is now 360.00 degrees. Location is now (5.25, 5.25). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Direction is now 360.00 degrees. Location is now (5.25, 5.25). Graphics in rayhan3.ps SOURCE FILES ====== ===== /* * File: hw3-turtle.h * Assignment: HW-3 * Class: CS113 * Last modified: October 2, 1997 * Date Due: October 2, 1997 * ----------------------------------- * This library allows the user to draw pictures using a "turtle" * that is placed on the screen. This library out performs the * graphics.h library in that the user does not need to understand * trigonometry or angles in order to draw. With this library, the * turtle will remember it's last location and direction, thus eliminating * this as a headache for the user. * * A technical note: North (straight up) is 90 degrees. * * Please Note: Do not call graphics.h or InitGraphics() in the user's code. * */ #ifndef _turtle_h #define _turtle_h #include #include "graphics.h" /* * Function: InitTurtle * Usage: InitTurtle(); * ----------------------------- * This function initializes the library and places the "turtle" * in the middle of the screen facing straight up. * * Important note -- do not call graphics.h or InitGraphics(). * We have already done this for you. * */ void InitTurtle(void); /* * Function: PlaceTurtle * Usage: PlaceTurtle(xpos, ypos); * ----------------------------- * This function allows the user to place the turtle using xpos * and ypos in an absolute sense of the screen. */ void PlaceTurtle(double xpos, double ypos); /* * Function: MoveTurtle * Usage: MoveTurtle(length); * ----------------------------- * This function will allow the turtle to move the length without drawing * in the direction that it was orginally pointing in from the last move * that it made. */ void MoveTurtle(double length); /* * Function: DrawTurtle * Usage: DrawTurtle(length); * ----------------------------- * This function will allow the turtle to move the length and draw * while moving in the direction that it was orginally pointing in * from the the last move that it made. */ void DrawTurtle(double length); /* * Function: TurnLeft * Usage: TurnLeft(angle); * ----------------------------- * This function will rotate the turtle left the number of degrees * stated here from the direction that it was originally pointing * from the last move that it made. */ void TurnLeft(double angle); /* * Function: TurnRight * Usage: TurnRight(angle); * ----------------------------- * This function will rotate the turtle right the number of degrees * stated here from the direction that it was originally pointing * from the last move that it made. */ void TurnRight(double angle); /* * Function: ArcLeft * Usage: ArcLeft(radius, angle); * ----------------------------- * This function will allow the user to draw an arc of a * circle. The portion of the circle will be the length from * the current angle the turtle is facing sweeping left the * number of degrees specified by angles. The circle will be of * radius radius. */ void ArcLeft(double radius, double angle); /* * Function: ArcRight * Usage: ArcRight(radius, angle); * ----------------------------- * This function will allow the user to draw an arc of a * circle. The portion of the circle will be the length from * the current angle the turtle is facing sweeping right the * number of degrees specified by angles. The circle will be of * radius radius. */ void ArcRight(double radius, double angle); /* * Function: GetWindowWidth * Usage: width=GetWindowWidth(); * ----------------------------- * This function allows the user to determine the width of * the drawing window and returns this number. * * Please note: This function is already written for us in * graphics.h and thus will not be repeated here. * * The function prototype is: * double GetWindowWidth(void); * */ /* * Function: GetWindowHeight * Usage: height=GetWindowHeight(); * ----------------------------- * This function allows the user to determine the Height of * the drawing window and returns this number. * * Please note: This function is already written for us in * graphics.h and thus will not be repeated here. * * The function prototype is: * double GetWindowHeight(void); * */ /* * Function: GetCurrentX * Usage: x=GetCurrentX(); * ----------------------------- * This function allows the user to determine the current * value of the x position of the turtle and returns that number. * * Please note: This function is already written for us in * graphics.h and thus will not be repeated here. * * The function prototype is: * double GetCurrentX(void); * */ /* * Function: GetCurrentY * Usage: y=GetCurrentY(); * ----------------------------- * This function allows the user to determine the current * value of the y position of the turtle and returns that number. * * Please note: This function is already written for us in * graphics.h and thus will not be repeated here. * * The function prototype is: * double GetCurrentY(void); * */ /* * Function: GetCurrentDirection * Usage: a=GetCurrentDirection(); * ----------------------------- * This function allows the user to determine the current * direction of the turtle in degrees and returns that angle. */ double GetCurrentDirection(void); #endif /* * File: hw3-turtle.c * Assignment: HW-3 * Class: CS113 * Last Modified: October 2, 1997 * Date Due: October 2, 1997 * -------------------------------------- * This file implements the hw3-turtle.h * interface. */ #include #include #include "genlib.h" #include "simpio.h" #include "math.h" #include "graphics.h" #include "hw3-turtle.h" /*------------------------------------*/ /*Define CONSTANTS*/ #define PI 3.141596 /*------------------------------------*/ /* Start global static variable defitions*/ static double turtleDirection; /*used for the direction*/ /*------------------------------------*/ /*Start Helper Prototypes*/ static double XChange(double length); static double YChange(double length); /*------------------------------------*/ /*begin functions*/ /* * Function: InitTurtle * Usage: InitTurtle(); * ----------------------------- * This function works like the InitGraphics function. * In actuality, the InitGraphics Function is called from * this function. This initialized all further graphics commands. * */ void InitTurtle() { InitGraphics(); /*call to InitGraphics to allow of all graphic.h functions*/ MovePen((GetWindowWidth()/2), (GetWindowHeight()/2)); turtleDirection=90; /* the above 2 lines set the direction and position of the turtle * to the desired level of centered on the screen and facing up. */ } /*end InitTurtle()*/ /* * Function: PlaceTurtle * Usage: PlaceTurtle(xpos, ypos); * ----------------------------- * This function places the turtle at xpos, ypos by passing the variable * onto MovePen in the graphics.h library. */ void PlaceTurtle(double xpos, double ypos) { MovePen(xpos, ypos); } /*end PlaceTurtle*/ /* * Function: MoveTurtle * Usage: MoveTurtle(length); * ----------------------------- * The function uses the MovePen command of graphics.h by taking the current X and Y * and then, by using the XYChange Functions, adds the change in each direction * to the current direction, thus placing it in the new postion. */ void MoveTurtle(double length) { MovePen(GetCurrentX()+XChange(length), GetCurrentY()+YChange(length)); } /*end MoveTurtle*/ /* * Function: DrawTurtle * Usage: DrawTurtle(length); * ----------------------------- * This function utilizes the DrawLine function in graphics.h and utilizes the * XYChange Helper Functions to determine the length of the line in each direction. */ void DrawTurtle(double length) { DrawLine(XChange(length), YChange(length)); } /*end DrawTurtle*/ /* * Function: TurnLeft * Usage: TurnLeft(angle); * ----------------------------- * This function changes the angle of turtleDirection by angle (given * by the user). The reason that we add angle is because the * values of angles increase to the left and we want to turn left. */ void TurnLeft(double angle) { turtleDirection=turtleDirection+angle; } /*end TurnLeft*/ /* * Function: TurnRight * Usage: TurnRight(angle); * ----------------------------- * This function changes the angle of turtleDirection by angle (given * by the user). The reason that we subtract angle is because the values * of angles increase to the left and we want to turn to the right. */ void TurnRight(double angle) { turtleDirection=turtleDirection-angle; } /*end TurnRight*/ /* * Function: ArcLeft * Usage: ArcLeft(radius, angle); * ----------------------------- * This function utilizes the DrawArc function from graphics.h. * The radius and the angle concepts are the same, but we need to * remember that we want to start pointing in a certain direction, * but DrawArc works by the user telling it the angle of the circle * in which to start. Thus, we need to correct for this by subtracting * ninety degrees from turtle direction. */ void ArcLeft(double radius, double angle) { DrawArc(radius, turtleDirection-90, angle); TurnLeft(angle); } /*end ArcLeft*/ /* * Function: ArcRight * Usage: ArcRight(radius, angle); * ----------------------------- * This function utilizes the DrawArc function from graphics.h. * The radius and the angle concepts are the same, but we need to * remember that we want to start pointing in a certain direction, * but DrawArc works by the user telling it the angle of the circle * in which to start. Thus, we need to correct for this by adding * ninety degrees from turtle direction. We also have to use the * negative of the angle because it is arcing to the right. */ void ArcRight(double radius, double angle) { DrawArc(radius, turtleDirection+90, -angle); TurnRight(angle); } /*end ArcRight*/ /* * Function: GetCurrentDirection * Usage: a=GetCurrentDirection(); * ----------------------------- * This returns the current direction of the turtle to the user. */ double GetCurrentDirection(void) { return (turtleDirection); } /*end GetCurrentDirection*/ /*------------------------*/ /*Start Helper Functions Code*/ /* * Function: XChange * Usage: XChange(length) * ----------------------------- * This computes the change in the x direction of a line that is * going length length and has an angle of turtle Direction. We * use some simple trigonometry. However, the most important part is * to remember to change angles to radians. */ double XChange(double length) { double dx; /*change in x position*/ double radDirection; /*turtle Direction in Radians*/ radDirection=(turtleDirection)*PI/180.0; dx=length*cos(radDirection); return (dx); } /*end XChange Helper Function*/ /* * Function: YChange * Usage: YChange(length) * ----------------------------- * This computes the change in the y direction of a line that is * going length length and has an angle of turtle Direction. We * use some simple trigonometry. However, the most important part is * to remember to change angles to radians. * */ double YChange(double length) { double dy; /*change in y position*/ double radDirection; /*turtle Direction in Radians*/ radDirection=(turtleDirection)*PI/180.0; dy=length*sin(radDirection); return (dy); } /*end YChange Helper Function*/ /* End Program hw3-turtle.c*/ /* * File: hw3-poly.c * Author: Raymond Han * Class: CS113 * Assignment: Homework 3 * Due Date: October 2, 1997 * Last modified: October 2, 1997 * ----------------------------------------------------- * USAGE: hw3-poly (no command line arguments) * ====== * * OVERVIEW: This program draws as many regular polygons as you want * onto the screen. This program uses the hw3-turtle.h library as opposed * to the graphics.h library because of a simpler interface. * ========= * * Algorithm Notes: * ================ * * Known Bugs: * =========== * * Enhancements: We use the actual radius formula. * ============= * * Error Handling: If the user enters a non-positive value for either * the number of sides or the length of a side, the program exits. * =============== * * Other Comments: * =============== * */ #include #include #include "genlib.h" #include "simpio.h" #include "math.h" #include "hw3-turtle.h" /*------------------------*/ /* Constants */ #define ANGLES_IN_CIRCLE 360 #define PI 3.141596 #define RIGHT_ANGLE 90 /*------------------------*/ /* Function Prototypes */ void DisplayInstructions(void); void GetInfo(int *numOfSides, double *lengthOfSide); void DrawPolygon(int numOfSides, double lengthOfSide); /*------------------------*/ /* Main Program */ void main() { int numOfSides; /*the number of sides in the polygon*/ double lengthOfSide; /*the length of a side of the polygon*/ InitTurtle(); /*initializing the TurtleLibrary*/ DisplayInstructions(); while(TRUE) /* This is an endless loop. This will go on and on until the user * enters a non-positive numOfSide or lengthOfSide. */ { GetInfo(&numOfSides, &lengthOfSide); DrawPolygon(numOfSides, lengthOfSide); } /* end while(TRUE) */ exit(1); /* If we exit the program here, something is screwy */ } /* end main */ /* * Function: DisplayInstructions * Usage: DisplayInstructions(); * ----------------------------- * This function prints to standard output instructions for the user. */ void DisplayInstructions() { printf("This program will draw a polygon on the screen.\n\n"); printf("You will be asked to enter the number of sides\n"); printf("of the polygon and the length of one side.\n"); printf("This program will exit when you enter a non-positive\n"); printf("number of sides or length of side.\n"); } /* end DisplayInstructions() */ /* Function: GetInfo * Usage: GetInfo(&numOfSides, &lengthOfSide); * -------------------------- * This function gets the number of sides and the length * of a side of the polygon from the user. Also, this function * checks to see if the values entered are non-positive and if * they are, the function exits the program. */ void GetInfo(int *numOfSidesPtr, double *lengthOfSidePtr) { printf("Enter the number of sides the polygon should have.\n"); printf("(negative value to quit): "); *numOfSidesPtr=GetInteger(); if (*numOfSidesPtr<3) exit(0); /*this error checks to make sure that *that we can draw a polygon of this number *of sides */ printf("Enter the length of one of the sides of the polygon.\n"); printf("(negative value to quit): "); *lengthOfSidePtr=GetReal(); if (*lengthOfSidePtr<=0) exit(0); } /*end GetInfo()*/ /* Function: DrawPolygon * Usage: DrawPolygon(numOfSides, lengthOfSide); * -------------------------- * This function draw a polygon of numOfSides sides and with each * side being of length lengthOfSide. This function also brings us * back to the center of the polygon. */ void DrawPolygon(int numOfSides, double lengthOfSide) { double radius; double radDegrees; int i; /*counter variables*/ /* We could use this definition of radius (with the explanation below * radius=(((numOfSides*lengthOfSide)/3)/2); * but we will use the more exact form after the explanation. * * This formula works because the radius is half of the diameter, * hence the divide by two. Then we know that C=dPI, thus d=c/PI. * If the assume that the number of sides of the polygon were * approaching infinity, hence a circle, we could find the circumference * of the polygon by multiplying the number of sides times the * length of each side. Then, assuming an approximation of PI of 3, * d would equal the numOfSides*lengthOfSide/3. This divided by 2 * would be the radius. */ radDegrees=(0.5*(ANGLES_IN_CIRCLE/(double)numOfSides))*PI/180.0; radius=(0.5*lengthOfSide)/sin(radDegrees); /* The reason that this formula works is as follows: * * 1) we can break a polygon with n sides into n number of congruent * isoceles trianges each with the unique angle (let's call that a * for simplicity) being 360 degrees divided * by n, the number of sides. One of the equal sides will be the radius * of the polygon. The unique side will be the length of a side. Let's * call this side s. * * 2) we biset these triangles, cutting the length of the side and the * unique angle in half. * * 3) now we have a right triangle, with the hypotonuse being the radius * that we want. The angle that was the unique angle that we called a, * is now cut in half. So a is (360/n)*(1/2). Finally, the side opposite * a is also cut in half, making the length (1/2)s. * * 4) we use the rule that sin(angle)=opposite/hypotonuse to determine * that sin(0.5a)=0.5c/hypotonuse(which is the radius). * * 5) we rearrange, change the angles to radians and then substitute * variables to get the above line. */ /* The next two lines put the turtle at the tip of the first line * of the polygon facing the correct direction. */ MoveTurtle(radius); TurnRight(RIGHT_ANGLE); TurnRight((ANGLES_IN_CIRCLE/(double)numOfSides)/2); /* This for loop does the actual drawing of the polygon. */ for (i=0; i