Lab 3: Basic Graphics

This assignment assumes that you have read up through Chapter 4. Please bring your book to lab, as you will need it as a reference. This lab is more work than the last one, get started early.

The objectives for this assignment are for you to:

  • Understand the fundamental concepts of raster graphics and animation.
  • Translate between Cartesian and polar coordinates.
  • Write event-driven and time-driven programs.
  • Demonstrate use of various loop and selection structures in C.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab3
    cd cse20211/lab3
    For this assignment, you will be using the simple graphics library created for this class. Follow that link, download the example program, and skim through the function reference. (Return to it later as needed.)

    Part 1: Symbolic Typewriter

    (This program is a little silly, but will ensure that you know how to open windows, draw in various colors, and manage input before moving on to the next step.)

    Write a program called symbol.c that opens up a window, and waits for the user to press a mouse button or a key. On each key or button press, you should display a small symbol (about 10-20 pixels on each side) centered at the current mouse location, as follows:

  • Mouse button 1: Display a blue square outline.
  • Letter t: Display a green triangle outline.
  • Letter c: Display a white circle outline.
  • Numbers 3 through 9: Display a purple polygon with that many sides. (e.g. 8 means an octagon)
  • Letter q: Quit the program.
  • Hint:
  • Both the circle and polygons can (should) be implemented with a loop and one call to gfx_line. Think in polar coordinates.
  • Part 2: Bouncing Ball

    Pick one of the shapes from Part 1. Create an animation of the shape moving around the screen at a constant velocity. If the shape hits any of the four sides of the window, it should bounce back the other way. If the user clicks on the screen with mouse button one, move the shape to the location of the click, and give it a new random velocity. Call this program bounce.c.

    This doesn't require a lot of code, but will require that you proceed carefully to keep everything straight. Keep track of the shape's position (float x,y) and velocity (float vx, vy) in two dimensions independently. Each time through the main loop, apply the velocity to the ball's position over deltat seconds, draw the shape, flush the output (gfx_flush) then sleep (usleep) for deltat seconds.


  • Suggest that deltat=0.01 for a nice smooth animation.
  • Note that usleep requires #include <unistd.h> and takes an integer argument of microseconds.
  • Use rand() (requires #include <stdlib.h>)to generate random integer numbers. (See chapter 5.10.)
  • Use gfx_event_waiting at the end of each loop to see if the user has pressed a key, then call gfx_wait if necessary.
  • Part 3: Rotating Animation

    Write an interesting program called rotate.c that incorporates the following things:
  • Multiple shapes and colors.
  • Circular motion of some kind.
  • User control of the display in some way.
  • Some ideas to get you thinking.
  • Draw a rotating Ferris wheel: the wheel goes around, but the buckets stay horizontal. The user can speed it up or slow it down with the keyboard.
  • Draw a little spaceship on the screen, and let the user drive it around by rotating, accelerating, braking, and so forth.
  • Draw a sun with two planets going around it at different speeds and radii. The planets each have a moon going around them as well. The user can change the speed or the radii using the keyboard.
  • Draw a "cartoon headache" in which spinning stars go around in a woozy circle, and the user can change the shape or speed.
  • Turning In

    Turn in symbols.c, bounce.c, rotate.c, and report.txt. Your lab report should explain the final part of the assignment, explaning how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

    Turning In

    This assignment is due on Monday, Sept 23rd at noon. Late assignments are not accepted.

    Please review the general instructions for turning in.