BU CAS CS 113
Introduction to Computer Science II with Intensive C
Fall 1997


Assignment 3: Using and Making Libraries -- Graphics Examples


Last Modified: Thu Oct 2 13:49:43 1997

Deadline

October 2, 1996

What to Submit

You should submit three files. Be sure the files you submit have exactly the following names: hw3-turtle.h, hw3-turtle.c, hw3-poly.c .

These programs are to be electronically submitted by using the submit program on csa. The code you submit should conform with the program assignment guidelines.

Assignment

  1. Implement a Turtle Graphics Package.

    "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. Thus a square would be formed by issuing commands to point north, move forward 2 inches, turn right (90 degrees), move forward 2 inches, turn right, move forward 2 inches, turn right, move forward 2 inches.

    Certain images are much easier to describe this way. For example, how does a turtle trace out a regular polygon with N sides? (A regular polygon is a plane figure with all sides the same length and all angles the same, squares and stop signs--which are regular octagons--are examples.)

    Using the graphics.h graphics package for your primitives, implement a turtle graphics package which includes the following functions:

        InitTurtle();
        PlaceTurtle(xpos, ypos);
        MoveTurtle(length);
        DrawTurtle(length);
        TurnLeft(angle);
        TurnRight(angle);
        ArcLeft(radius, angle);
        ArcRight(radius, angle);
        width = GetWindowWidth();
        height = GetWindowHeight();
        x = GetCurrentX();
        y = GetCurrentY();
        a = GetCurrentDirection();
    
    Important Notes:

    Most of these functions will be fairly straightforward to code. In fact, four of them are already done (why?). Nevertheless, a few hints are in order.

    1. In order to implement DrawTurtle() (which draws a straight line as the turtle "walks forward") and MoveTurtle() (which moves the turtle to the same place as the previous function, but without drawing a line) you will need to know how much the turtle moves in the x (horizontal) and y (vertical) directions. This can be determined using a little trigonometry. See the picture below.

      The formulas indicated by this picture are good for all angles, not just angles between 0 and 90 degrees. In order to use the cosine and sine functions, you will need to include math.h. (where?) The functions in math.h use radians, not degrees, so to get the sine and cosine of an angle of N degrees, you must use sin(N*PI/180.0), cos(N*PI/180.0). Also note, that this assumes that 0 degrees points directly right, 90 degrees straight up, 180 degrees left, 270 degrees down, as is standard in mathematics, but not on the compass, where north is 0 degrees.

    2. ArcLeft() and ArcRight() make a turn of angle degrees while tracing out a portion of a circle with radius radius. You need to think about how to translate what you want into DrawArc() from graphics.h. For this it is handy to know that the direction the turtle is going as it enters the arc will be 90 degrees different from the direction of the line segment running from the center of the circle to the point where the arc begins. The same is true as the turtle leaves the arc. This fact can be used to compute the start and sweep values for drawing the arc. It also implies that the turtle's angle changes by the same amount as the arc swept out. Again see the picture below:

    Be careful to put the correct things in hw3-turtle.h and hw3-turtle.c, especially comments.

    Files: hw3-turtle.h, hw3-turtle.c

  2. Use Turtle Graphics to Draw Pictures.
    1. The first thing you should do when your turtle graphics package appears ready is test it with the program hw3-draw. To do this use the Makefile for this assignment and type make hw3-draw. This will link your turtle.o with hw3-draw.o and should produce the program hw3-draw for you to run. (This is one way we will test your turtle graphics package, by linking it with other programs which make use of turtle graphics.) Run the program. It will allow you to test your turtle graphics package.

      Files: None.

    2. 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 approximately centered in the graphics window. Orient the polygon so one of the corners is at the top. (A square will then look like a diamond.) Repeat this until a value less than or equal to 0 is entered for either the side length or number of sides. This will result in one picture with several polygons all centered in the window. Do not try to reinitialize the turtle graphics environment between polygons, this will not work. If the user enters a non-positive number of sides, the program should terminate without asking for the length of the sides.

      To locate the polygon roughly in the center of the screen, use the fact that the distance from the center of the screen to a corner of the polygon is approximately numSides * lengthOfSide / 6. (I had 3 here before. That was an error, I was working with the diamter instead of the radius, sorry.) (One extra point if you can justify this approximation in your comments. Two extra points for working out the value exactly and explaining why your method works.)

      You should only use graphics functions from your turtle graphics package and not from graphics.h directly. You may, of course, write new functions for this program, but DO NOT ADD ANY ADDITIONAL FUNCTIONS TO THE TURTLE PACKAGE.

      Files:hw3-poly.c

    Makefile

    To check that your assignment compiles correctly, use the Makefile for this assignment:
    1. Copy this Makefile into the directory where your programs are.
    2. Name it Makefile.
      • Note:If you named it Makefile.a3, you will have to use the mv (short for move) command to rename it by typing
        mv Makefile.a3 Makefile
        or the cp command to copy it by typing
        cp Makefile.a3 Makefile
    3. type: make -k. (The -k means "keep going", which instructs make to build as much as it can, rather than quit after the first error.)

    Academic Honesty and Collaboration

    It is reasonable to discuss with others possible general approaches to problems. It is unreasonable to work together on a detailed solution, to copy a solution, or to give away a solution. If your common discussion can be detected by looking at the solutions, then there is too much collaboration. Such instances of academic dishonesty may result in a course grade of F or expulsion from Boston University.

    Do not allow your work to be used by others:

    Warning: If someone cheats by using your work, you will also be penalized.