Name: Zhang, Qia EXECUTION TESTS ========= ===== ============================================================ hw3-poly < poly.in This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Graphics in qiazhang1.ps ============================================================ hw3-poly2 < poly.in This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Please enter the side length of the polygon: This program generates polygons which are appromimately centered in the graphic window. You will be asked for the properties of the polygon. Enter a non-positive value to quit. Please enter the number of sides for your polygon: Graphics in qiazhang2.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 0.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 0.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 0.00 degrees. Location is now (5.25, 5.25). Enter Command (D, M, TL, TR, P, AL, AR, O, Q): Direction is now 0.00 degrees. Location is now (5.25, 5.25). Graphics in qiazhang3.ps SOURCE FILES ====== ===== /* * File: hw3-turtle.h * ====== ============= * Author: Carolyn Zhang * Class: CS113 * Assignment: Making Libraies, Graphics * Due: October 2, 1997 * Last Modified: Oct 2, 1997 * ---------------------------------------------------------------------- * * OVERVIEW: * ========= * This interface provides several functions for graphing. The turtle * package uses the "graphics.h" graphics package as primitives. * */ #ifndef _hw3_turtle_h #define _hw3_turtle_h #include "graphics.h" /* * FUNCTION: InitTurtle * USAGE: InitTurtle(); * -------------------- * This function creates the graphics window on the screen, and positions * the turtle in the center of the graphics window facing north. * The call to "InitTurtle" must precede any operation and is usually the * first statement in the "main" function. */ void InitTurtle(); /* * FUNCTION: PlaceTurtle * USAGE: PlaceTurtle(xpos, ypos); * ------------------------------- * This function place the turtle to the position (xpos, ypos) in absolute * coordinates without drawing anything. */ void PlaceTurtle(double xpos, double ypos); /* * FUNCTION: MoveTurtle * USAGE: Moveturtle(length); * -------------------------- * This function moves the turtle forward a distance of "length" from its * current direction without drawing anything. Negative value for "length" * will result in moving the turtle backward in its current direction. */ void MoveTurtle(double length); /* * FUNCTION: DrawTurtle * USAGE: DrawTurtle(length); * -------------------------- * This function draws a line from the current point to a distance of * "length" forward from current direction of the turtle. Negative value * of "length" will draw a line of magnitude "length" from current point, * in the opposite direction of turtle. The final position becomes the new * current point. */ void DrawTurtle(double length); /* * FUNCTION: TurnLeft * USAGE: TurnLeft(angle); * ----------------------- * This function turns the turtle left "angle" degrees from its current * direction. If "angle" has a negative value, then turn a magnitude of * "angle" degrees in the opposite direction. The final direction will * become the new current direction. */ void TurnLeft(double angle); /* * FUNCTION: TurnRight * USAGE: TurnRight(angle); * ------------------------ * This function turns the turtle right "angle" degrees from its current * direction. If "angle" has a negative value, then turn a magnitude of * "angle" degrees in the opposite direction. The final direction will * become the new current direction. */ void TurnRight(double angle); /* * FUNCTION: ArcLeft * USAGE: ArcLeft(radius, angle); * ------------------------------ * This function draws a circular arc with radius "radius". The arc * always begins at the current point and start at the 3 o'clock position. * The arc finishs at "angle" degrees counterclockwise from the start. * If "angle" is negative, the arc is drawn clockwise. */ void ArcLeft(double radius, double angle); /* * FUNCTION: ArcRight * USAGE: ArcRight(radius, angle); * ------------------------------ * This function draws a circular arc with radius "radius". The arc * always begins at the current point and start at the 9 o'clock position. * The arc finishs at "angle" degrees clockwise from the start. If "angle" * is negative, the arc is drawn counterclockwise. */ void ArcRight(double radius, double angle); /* * FUNCTION: GetCurrentDirection * USAGE: GetCurrentDirection(); * ----------------------------- * This function returns the current direction the turtle is facing in * degrees between 0 and 360. Here, 90 degrees is facing north, 0 degree * is facing east. */ double GetCurrentDirection(); /* * FUNCITON: GetWindowWidth * USAGE: width = GetWindowWidth(); * -------------------------------- * FUNCTION: GetWindowHeight * USAGE: height = GetWindowHeight(); * ---------------------------------- * These functions returns the width and height of the graphics window * respectively. */ /* * FUNCTION: GetCurrentX * USAGE: x = GetCurrentX(); * ------------------------- * FUNCTION: GetCurrentY * USAGE: y = GetCurrentY(); * ------------------------- * These functions returns the absolute coordinates of the current point, * respectively. */ #endif /* ----------------------- End of File: hw3-turtle.h ------------------ */ /* * File: hw3-turtle.c * ================== * Author: Carolyn Zhang * Class: CS113 * Assignment: Making Libraies-Graphics * Due: October 2, 1997 * Last Modified: Oct 2, 1997 * --------------------------------------------------------------------- * * OVERVIEW: * ========= * This file implements the hw3-turtle.h interface. * * Algorithm Notes: * ================ * 1. Use trigonometry identities: * x-component = length * cosine(angle) * y-component = length * sine(angle) * 2. Conversion of angle from degrees to radians: * radian = degree*PI/180 * * Known Bugs: none * =========== * * Enhancements: * ============= * The GetCurrentDirection function keeps the degrees of the direction * in the range of [0,360). This means the direction returned by the * function will be between 0 and 360 degrees, inclusive of 0 degree. * */ #include #include #include "genlib.h" #include "simpio.h" #include "graphics.h" #include "hw3-turtle.h" /* Constant: PI used to convert degrees to radians. */ #define PI 3.1415926535 /* Private global variable */ static double direction; /* ................ Prototype Definitions .................. */ /* * FUNCTION: InitTurtle * -------------------- * This function creates a graphics window by calling "InitGraphics". * The turtle is placed in the center of the graphics window and facing * north by the following steps: * 1. Get the window's width and height. * 2. Move the turtle half the width in the x direction, and half the * height in the y direction. * 3. Assign the original direction of turtle to 90 degrees. */ void InitTurtle() { double x, y; InitGraphics(); x = GetWindowWidth(); y = GetWindowHeight(); MovePen(x/2, y/2); direction = 90; } /* * FUNCITON: GetCurrentDirection * ----------------------------- * This function returns the current direction of the turtle within the * range of [0, 360) by the following steps: * 1. If direction is negative, keeping adding 360 degrees. * 2. If direction is more than or equal to 360 degrees, keep substracting * 360 degrees from it. * 3. Return the direction. */ double GetCurrentDirection() { while(direction < 0) direction += 360; while(direction >= 360) direction -= 360; return direction; } /* * FUNCTION: TurnRight * ------------------- * This function turns turtle to "angle" degrees to the right by decreasing * the degrees of direction by "angle" degrees. */ void TurnRight(double angle) { direction -= angle; } /* * FUNCTION: TurnLeft * ------------------- * This function turns turtle to "angle" degrees to the left by increasing * the degrees of direction by "angle" degrees. */ void TurnLeft(double angle) { direction += angle; } /* * FUNCTION: PlaceTurtle * --------------------- * This function place the turtle to (xpos, ypos)position in the absolute * coordinates by calling the "MovePen" function. */ void PlaceTurtle(double xpos, double ypos) { MovePen(xpos, ypos); } /* * FUNCTION: MoveTurtle * -------------------- * This function moves the turtle "length" distance forward in its current * direction by the following steps: * 1. Assign "angle" with the radian value of the current direction. * 2. Find the current coordinate of the turtle. * 3. Use the trig identity to convert the the length and the angle into * their x and y components. This will be the relative coordinate of * the turtle from the current coordinate. * 4. The desired absolute coordinate is the sum of the current coordinate * and the relative coordinate. Use "MovePen" to arrive at that point. */ void MoveTurtle(double length) { double x, y, xpos, ypos; double angle; angle = direction*PI/180; x = GetCurrentX(); y = GetCurrentY(); xpos = length * cos(angle); /* convert length and angle into x and */ ypos = length * sin(angle); /* y components. */ MovePen(x+xpos, y+ypos); } /* * FUNCTION: DrawTurtle * -------------------- * This function draws a line of length "length" in the direction of the * turtle: * 1. Assign "angle" with the radian value of the current direction. * 2. Use the trig identity to convert the the length and the angle into * their x and y components. This will be the relative coordinate of * the turtle from the current coordinate. * 3. Pass the x and y components to "DrawLine". */ void DrawTurtle(double length) { double x, y; double angle; angle = direction*PI/180; x = length * cos(angle); y = length * sin(angle); DrawLine(x, y); } /* * FUNCTION: ArcLeft * ----------------- * This function draws an arc "angle" degrees counterclockwise from the * current point. The direction the turtle is going as it enters the arc * is 90 degrees from the line segment of the starting point and the center * of the circle. This is done by the following steps: * 1. Calculate the starting angle of the arc by adjusting the current * direction of the turtle as the 0 degrees of the "DrawArc"'s "start" * parameter. * 2. The direction of the turtle as it leaves the arc is increamented by * the amount of "angle". */ void ArcLeft(double radius, double angle) { double start = ( GetCurrentDirection()-90 ); DrawArc(radius, start, angle); direction += angle; } /* * FUNCTION: ArcRight * ------------------ * This function draws an arc "angle" degrees clockwise from the * current point. The direction the turtle is going as it enters the arc * is 90 degrees from the line segment of the starting point and the center * of the circle. This is done by the following steps: * 1. Calculate the starting angle of the arc by adjusting the current * direction of the turtle as the 180 degrees of the "DrawArc"'s "start" * parameter. * 2. The direction of the turtle as it leaves the arc is decreament of * "angle". */ void ArcRight(double radius, double angle) { double start = 180 + ( GetCurrentDirection()-90 ); DrawArc(radius, start, -angle); direction -= angle; } /* ----------------- End of File: hw3-turtle.c --------------------- */ /* * File: hw3-poly.c * ===== ========== * Author: Carolyn Zhang * Class: CS113 * Assignment: Using Libraies * Due: October 2, 1997 * Last Modified: Oct 2, 1997 * ------------------------------------------------------------------------ * * USAGE: hw3-poly * ====== * * OVERVIEW: * ========= * This program draws regular polygons based on the user's input. It accept * positive integers as the number of sides and positive real number as the * length of the polygon. The program generates the polygons which are * approximately centered in the graphics window. The polygons are symmetric * to the y-axis with one corner up. * * Known Bugs: NONE * =========== * * */ #include #include #include "genlib.h" #include "simpio.h" #include "hw3-turtle.h" /* Function Prototypes */ /* * FUNCTION: GetInfo * USAGE: if ( GetInfo(&numSides, &length) ); * ------------------------------------------ * This function give user instruction for using the program, and prompt for * the user input of the number of sides for the polygon and the length of * the sides. The function returns TRUE if the inputs are valid, returns * FALSE if the inputs are invalid. */ bool GetInfo(int *numSidesPtr, double *lengthPtr); /* * FUNCTION: PolyStart * USAGE: PolyStart(numSides, length); * ----------------------------------- * This function gets into the starting position for drawing the polygons. */ void PolyStart(int numSides, double length); /* * FUNCTION: Center * USAGE: Center(); * ---------------- * This function place the drawing tool in the center of the graphics * window and facing north. */ void Center(void); /* * FUNCTION: PolyDraw * USAGE: PolyDraw(numSides, length); * ---------------------------------- * This function draws the polygon from the current point. */ void PolyDraw(int numSides, double length); int main() { int numSides; double length; InitTurtle(); /* initialize the turtle graphics package */ while (GetInfo(&numSides, &length)) /* Check if user input are valid */ { PolyStart(numSides, length); PolyDraw(numSides, length); } /* end while statement */ return (0); } /* end main */ /* * FUNCTION: GetInfo * ------------------------------------------ * This function does the following: * 1. Display instruction for user. * 2. Prompt user for the number of sides of the polygon. * a. If user enters negative value, return FALSE. * b. If user enters a value less than three, prompt user to try again. * c. If user enters a non-integer , let user try again. * 3. Prompt user for the length of the polygon sides. * a. If user enters non-positive value, return FALSE. * 4. return TRUE. */ bool GetInfo(int *numSidesPtr, double *lengthPtr) { printf("\n This program generates polygons which are appromimately"); printf("\ncentered in the graphic window. You will be asked for the "); printf("\nproperties of the polygon. Enter a non-positive value to quit."); while(TRUE) /* Loop until either non-positive value, or valid input */ { printf("\nPlease enter the number of sides for your polygon: "); *numSidesPtr = GetInteger(); if (*numSidesPtr<=0) /* Check for non-positive value*/ return (FALSE); if (*numSidesPtr>2) /* Check for valid input */ break; else { printf("\nA regular polygon has more than %d sides.",*numSidesPtr); printf("\nTry again."); } } /* end while statement */ printf("\nPlease enter the side length of the polygon: "); *lengthPtr = GetReal(); if (*lengthPtr<=0) /* Check for non-positive value */ return (FALSE); else return (TRUE); } /* End GetInfo */ /* * FUNCTION: PolyStart * ------------------- * This function place the turtle top corner of the polygon. * 1. Center the turtle, facing up. * 2. approximate the distance from the center of polygon to one corner. * 3. move the turtle up that distance. */ void PolyStart(int numSides, double length) { double y; Center(); /* Place center of polygon at approximately the center * of the graphics window, with the turtle facing up. */ y = numSides * length / 6; /* approximate the distance. */ MoveTurtle(y); } /* end of PolyStart */ /* * FUNCTION: Center * ---------------- * This function centers the turtle in the graphics window, and place it * facing up. * 1. place turtle in the center of window. * 2. Adjust the current direction of turtle to 90 degrees. */ void Center(void) { double x, y; double angle; x = GetWindowWidth(); y = GetWindowHeight(); PlaceTurtle(x/2, y/2); /* Center turtle in the window. */ angle = GetCurrentDirection(); TurnLeft( 90 - angle ); /* Adjust direction of turtle to north */ } /* end of Center */ /* * FUNCTION: PolyDraw * ------------------ * This function draws the polygon from the current position, assuming the * turtle is facing north. * 1. turn turtle to the direction of the first side. * 2. Loop through each sides. * a. draw the lenth * b. turn to the direction of the next side. */ void PolyDraw(int numSides, double length) { int i; /* current direction is facing north */ TurnRight(90 + 180/(double)numSides); /*first turn */ for (i=1; i<=numSides; i++) { DrawTurtle(length); /* Draw side */ TurnRight( 360/(double)numSides ); /* turn to direction of next side*/ } } /* end of PolyDraw */ /* ------------------ End of File: hw3-poly.c ------------------------- */ -- END OF LOG FILE FOR qiazhang --