Name: Blumstein, David EXECUTION TESTS ========= ===== ============================================================ hw3-poly < poly.in ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: Graphics in dblumstn1.ps ============================================================ hw3-poly2 < poly.in ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: How long should each side be? [ <=0 to exit ] ------------------------------ Let's draw a centered polygon! ------------------------------ How many sides should it have? [ <1 to exit ]: Graphics in dblumstn2.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 360.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 360.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 360.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 360.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 360.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 dblumstn3.ps SOURCE FILES ====== ===== /* * File: hw3-turtle.h * Author: David Blumstein 194-58-3662 * Class: CS 113, Fall '97 * Assignment: HW3 - turtle graphics interface * Due Date: 10/2/97 * Modified: 9/30/97 *------------------------------------- * * * USAGE: #include "hw3-turtle.h" * ===== * * OVERVIEW: interface for turtle graphics library - 12 functions * ======== * * ERROR HANDLING: turtle may move and draw outside of screen * ============== * * COMMENTS: IMPORTANT- Call InitTurtle only once. * ======== * Those functions that I did not write (already in graphics.h) * could not be used by clients without prototypes here. */ #ifndef turtle_h #define turtle_h #include "graphics.h" /* primitives are derived from here */ /* Function: InitTurtle * Usage: InitTurtle(); * Purpose: Sets up environment for turtle graphics package and puts turtle * in center of window, facing up * (Call once only.) */ void InitTurtle(); /* Function: PlaceTurtle * Usage: PlaceTurtle(double xpos, double ypos); * Action: Puts turtle at absolute (xpos, ypos) */ void PlaceTurtle(double xpos, double ypos); /* Function: MoveTurtle * Usage: MoveTurtle(double length); *Action: Moves turtle relatively distance 'length' without drawing */ void MoveTurtle(double length); /* Function: DrawTurtle * Usage: DrawTurtle(double length) * Action: Moves turtle, while drawing, a distance of 'length' */ void DrawTurtle(double length); /* Functions: TurnLeft/ TurnRight * Usage: TurnLeft/TurnRight (double angle); * Action: turtle turns 'angle' degrees in the direction specified */ void TurnLeft(double angle); void TurnRight(double angle); /*Functions: ArcLeft / ArcRight * Usage: ArcLeft/ ArcRight (double radius, double angle); * Action: turtle moves and draws left/right in an circular arc of radius * 'radius' at angle 'angle' */ void ArcLeft(double radius, double angle); void ArcRight(double radius, double angle); /* Functions: GetWindowWidth / GetWindowHeight * Usage: x= GetWindowWidth(); / GetWindowHeight(); * Action: returns specified window dimension */ /* double GetWindowWidth(); double GetWindowHeight(); */ /*Functions: GetCurrentX / GetCurrentY * Usage: a= GetCurrentX(); / a=GetCurrentY(); *Action: returns specified coordinate of turtle */ /* double GetCurrentX(); double GetCurrentY(); */ /* Function: GetCurrentDirection * Usage: a=GetCurrentDirection(); * Action: returns turtle direction (an angle) */ double GetCurrentDirection(); #endif /*-------- End File: hw3-turtle.h-----------*/ /* * File: hw3-turtle.c * Author: David Blumstein * Class: CS 113, Fall 97 * Assignment: HW 3, turtle graphics implementation * Due Date: 10/2/97 * Last modified: 9/30/97 * ----------------------------------------------------- * * USAGE: #include "turtle.h" * ====== * * OVERVIEW: This file contains the implemenation for a turtle graphics * ========= package called "turtle.h" "Turtle graphics" refers to graphics * libraries which can be imagined as producing graphics by giving instruction * to a turtle (or a remotely controlled car) which tell it what direction to * go and how far. As the turtle moves, it produces the graphical image. * * * Algorithm Notes: All values sent to functions should be doubles. Uses * ================ the graphics package "graphics.h" for basic actions. Often * automatically converts from degrees to radians to perform * calcs. Calcs for movement/drawing are based on * trigonometric identities. * * Known Bugs: none. * =========== * * Error Handling: The turtle is allowed to draw outside the window * =============== * * Enhancements: I wrote a function which replaces the turtle when he moves * ============= off the window. However, I have commented this function out * of the program because of various difficulties, including * limitations in "graphics.h" * * Other Comments: The static function FixAngle is an function which converts * =============== angle measures so they remain between 0 and 360. * The lack thereof would not impede functionality, though * it could create confusion in situations of: (-1200 degrees, * 1400000 degrees, etc.) */ #include #include #include #include "graphics.h" #define PI 3.14159 /* PI */ static double Direction; /* global (within turtle.c) variable for direction */ static void FixAngle(); /* keeps angle measure between 0 and 360 */ /*static void CheckLocat(); */ /* Function: CheckLocat * Usage: CheckLocat(); * Action: If turtle has moved out of the graphics window, this moves it * back to the edge from which it left * * **Commented out because it causes difficulties when changing window size * ** and when drawing shapes larger than original graphics window * * static void CheckLocat() * { * if (GetCurrentX()>GetWindowWidth()) * left edge * * MovePen(GetWindowWidth(), GetCurrentY()); * * if (GetCurrentX()<0) * right edge * * MovePen(0, GetCurrentY()); * * if (GetCurrentY()>GetWindowHeight()) * top edge * * MovePen(GetCurrentX(), GetWindowHeight()); * * if (GetCurrentY()<0 ) * bottom edge * * MovePen(GetCurrentX(), 0); * } * --------End Function: CheckLocat----------*/ /* Function: FixAngle * Usage: FixAngle(); * Action: Converts irregular angles to regular #s (e.g. 560=200) * Important for negative or huge turns * HOWEVER: Does NOT affect functionality of program to have * angles with irregular measures, it is just ugly. * */ static void FixAngle() { while (1) /* this loop fixes angles above 360, e.g. 370=10 */ { if (Direction<360) break; Direction-=360; } while (1) /* this loop fixes angles below 0, e.g. -45=270 */ { if (Direction>0) break; Direction+=360; } } /*--------End Static Function: FixAngle -------------*/ /* Funcion: InitTurtle * Usage: IntiTurtle(); * Action: initializes graphics window, puts turtle in center of window facing * up. */ void InitTurtle() { InitGraphics(); /* inits window */ MovePen(GetWindowWidth()/2,GetWindowHeight()/2); /* centers turtle */ Direction=90.0; /* points him up */ } /*---- End function: InitTurtle----- */ /* Function: PlaceTurtle * Usage: PlaceTurtle(double xpos, double ypos); * Action: Puts turtle at absolute (xpos, ypos) */ void PlaceTurtle(double xpos, double ypos) { MovePen(xpos, ypos); /* puts turtle */ /*CheckLocat(); * check boundaries */ } /*---- End function: PlaceTurtle----- */ /* Function: GetCurrentDirection * Usage: a=GetCurrentDirection(); * Action: returns turtle direction (an angle in degrees) */ double GetCurrentDirection() { return (Direction); } /*-------End Function: GetCurrentDirection ----*/ /* Functions: TurnLeft/ TurnRight * Usage: TurnLeft/TurnRight (double angle); * Action: turtle turns 'angle' degrees in the direction specified, then angle * is 'fixed' */ void TurnLeft(double angle) { Direction+=angle; FixAngle(); /* adjust angle */ } void TurnRight(double angle) { Direction-=angle; FixAngle(); /* adjust angle */ } /* ------End functions: TurnLeft / TurnRight -----*/ /* Function: MoveTurtle * Usage: MoveTurtle(double length); *Action: Moves turtle relatively distance length, without drawing */ void MoveTurtle(double length) { /* variables d, x, and y are used to simplify code */ double d=Direction; double x=GetCurrentX(); double y=GetCurrentY(); /* move turtle * (uses trig. to find x and y distance given direction and magnitude) */ MovePen( ( x+ ( (double)length*cos(d*PI/180) ) ), ( y+ ( (double)length*sin(d*PI/180) ) ) ); /*CheckLocat(); * check boundaries */ } /*------End Function: MoveTurtle ------*/ /* Function: DrawTurtle * Usage: DrawTurtle(double length) * Action: Moves turtle, while drawing, a distance of 'length' */ void DrawTurtle(double length) { /* draws with moving * (uses trig. to find x and y distance given direction and magnitude) */ DrawLine(length*(cos(Direction*PI/180)) , length*(sin(Direction*PI/180)) ); /*CheckLocat(); * checks boundaries */ } /* ------End Function: DrawTurtle ----------*/ /*Functions: ArcLeft / ArcRight * Usage: ArcLeft/ ArcRight (double radius, double angle); * Action: turtle moves and draws left/right in an circular arc of radius * 'radius' at angle 'angle' */ void ArcLeft(double radius, double angle) { DrawArc(radius, Direction-90, angle); /* draws arc */ TurnLeft(angle); /* points turtle out of the turn, in direction of turn */ /*CheckLocat(); * checks boundaries */ } void ArcRight(double radius, double angle) { DrawArc(radius, Direction+90, -1*angle); /* draws arc */ TurnRight(angle); /* sets turtle direction when leaving turn to follow arc */ /*CheckLocat(); * checks boundaries */ } /* --------End Functions: ArcLeft / ArcRight ------*/ /*----------End File: hw3-turtle.c ----------*/ /* * File: hw3-poly.c * Author: David Blumstein 194-58-3662 * Class: CS 113, Fall 97 * Assignment: HW 3, regular polygons * Due Date: 10/2/97 * Last modified: 9/30/97 * ----------------------------------------------------- * * USAGE: hw3-poly.c no command line * ====== * * OVERVIEW: Regular Polygons. Write a program which repeatedly asks the user * ======== for the number of sides and the length of a side (in that order) * in a regular polygon and then draws the polygon centered in the * graphics window; Orients the polygon so one of the corners is at * the top. (A square will then look like a diamond.) Repeating this * until a value less than or equal to 0 is entered for either the * side length of number of sides * * Algorithm Notes: I calculated an exact distance to the corner. Details * =============== below (inside function) * To make a figure, I put the turtle distanceToCorner away * from center. Then I loop through the number of sides, * drawing and making the proper turn each time. * * Known Bugs: none. * =========== * * Error Handling: turtle may draw outside the window * =============== * * Enhancements: ** Calc for the exact distance to corner follows... ** * ============= * * Other Comments: a one sided polygon is a point and can't be visibly drawn. * =============== a two sided polygon is a line of length 'length'. * Some shapes may be partially outside the visible window, * though these shapes are complete. */ #include #include #include #include "simpio.h" #include "hw3-turtle.h" #define PI 3.14159 /* Pi */ /* prototypes */ void GetInput(int * number, double * length); void DrawPoly(int number, double length); /* main */ int main() { int number; /* number of sides to polygon */ double length; /* length of each side of polygon */ InitTurtle(); /* loops forever (exiting program is accomplished during GetInput) */ while (1) { GetInput(&number, &length); DrawPoly(number, length); } /* end loop */ exit(1); /* if While loop is exited, it is an error */ } /* -------------End Main-----------*/ /* Function: GetInput * Usage: test=GetInput(int * number, double * length); * Action: gets user input: 1.#sides & 2.length of each side * returns test for quit value */ void GetInput(int * number, double * length) { printf("\n------------------------------"); printf("\nLet's draw a centered polygon!"); printf("\n------------------------------"); printf("\n\nHow many sides should it have? [ <1 to exit ]: "); * number= GetInteger(); if (* number<1) /* if user enters quit value, we exit now */ exit(0); /* (quit values are 0 or negative) */ printf("\nHow long should each side be? [ <=0 to exit ] "); * length= GetReal(); if (*length<=0) /* checks against quit values, they are 0 and smaller */ exit(0); } /* ----------End Function: GetInput-----------------------------*/ /* * Function: DrawPoly * Usage: DrawPoly(int number, double length); * Action: draws a centered polygon with 'number' sides each of length 'length' */ void DrawPoly(int number, double length) { int i; /* loop */ double rads=2*PI; /* number of radians; for most polygons rads=2PI */ double distanceToCorner; /* distance from center of figure to a corner */ /* distanceToCorner can be worked out by realizing that: * 1. imagine a triangle with one angle at the center of the figure * and the other two angles at two adjacent corners of the figure * 2. the measure of such an angle would be (# rads in figure)/(#sides) * 3. bisect the angle, bisecting the side. * 4. there are now 2 smaller triangles, each has one angle (#rads/#sides)/2, * and one 90 degree angle. * 5. the length of the base of these triangles is (length of side)/2 * 6. for a four+ sided figure, sin of the center angle (cos(rads/2/sides)) * is equal to the base divided by the hypotonuse(distanceToCorner) * 7. for a three sided figure, cos of the center angle is equal to the * base divided by the hypotonuse */ distanceToCorner= (length/2) / (sin (rads/(2*number)) ) ; /* special case: number sides=3, rads=PI, distanceToCorner is different * (see #7 above) */ if (number==3) { rads=PI; distanceToCorner= (length/2)/ cos(rads/2/number); } /* end special case */ /* in case sin or cos delivers a negative number */ if (distanceToCorner<0) distanceToCorner*=-1; /* starts turtle at one corner */ MoveTurtle(distanceToCorner); /* points him in the correct direction */ TurnLeft(90.0+ (360/ (2*(double)number) ) ); /* for each side, draws side, then turns */ for (i=0; i