CSC 240: Computer Graphics

Homework 2: Polygons and Fill

Due: Friday, Sept. 25, 11:59pm on Moodle

The goal of this homework is to implement a regular polygon function and complete the sweep fill method for convex polygons. This homework also contains a line implementation (solution to Homework 1), but you are free to put in your own line implementation.

  1. Download hw2.py (continue using whichever version of raster.py works for your Python version). In this file, notice how the Line class differs from what we have done before. In the constructor, the line algorithm is used to build up a list of pixels (using the Point class) that will be colored in the draw() method.

    Complete the constructor for the RegularPolygon class, using the Line class. At the end, you should have built up a list of all the points on the polygon. Then the draw() method (which is idential to the draw() method for Line) can be called to draw the outline of the polygon.

    Hint: how would you define the vertices of a regular polygon if it was centered at the origin? How could you change those points to achieve one centered at the given point c?

  2. The next step is to complete the fill() method for the RegularPolygon class, using the sweep algorithm discussed on Wednesday. The list of points has already been sorted by the y-coordinate value. For each of these y values, find the two x-values marking the boundary points of the polygon. Then draw a line between the two boundary points, which will gradually fill the polygon with color.

    Hint: are there always two boundary points? What if there is just one point matching a given y-value? What if there are more than 2?

  3. Use you completed RegularPolygon method to make three polygons, each with a different number of sides, a different position, and a different color. Save your code and PPM file and submit them on Moodle as before.
PAIR PROGRAMMING OPTION:

You are welcome to do pair programming for this assignment. Make sure to switch off at least between parts (1) and (2), or alternate more frequently if you like. In addition, you must also complete the first bonus item below (different outline and fill colors). If you choose this option, submit one assignment and make sure to put both your names.

BONUS OPTIONS:

  • Make the fill() method take a different color than the color of the outline of the polygon.
  • Make a creative image using your RegularPolygon class. How would you handle cases where the polygon is not completely contained by the image boundaries?
  • Implement the sweep algorithm for non-convex polygons!

Note: you can receive full credit for this assignment without doing the bonus (unless you are pair programming).