The assignment: is is a continuation of Lab 1, which emulated a raster display and implemented some unfilled SRGP primitives. In this assignment you will first implement the ability to set a clip rectangle, and then clip lines, polylines, and polygons to the clip rectangle. Then you will implement fill algorithms for rectangles and polygons.
Part 1: Implementing clip routines.
First allow the user to set the clip rectangle. The clip rectangle will be part of the global attribute group/state of the "screen" rectangle (like the color attribute) -- there will only be one attribute group (you don't have to worry about saving and restoring attribute groups). You can do this with a call to SRGP_setClipRectangle() (which you implement -- which requires defining a rectangle structure), or you can define a new function SRGP_setClipRectangleCoord() (which has four arguments: xLowerLeft, yLowerLeft, xUpperRight, yUpperRight).
Second, modify your basic line-drawing algorithm to incorporate clipping. You can use either the Cohen-Sutherland or the Liang-Barsky clipping algorithm (or even the Cyrus-Beck algorithm). If your your basic line-drawing algorithm happened to be SRGP_lineCoord, you could modify it as follows:
SRGP_lineCoord ( int x0, int y0, int x1, int y1 )
call Cohen-Sutherland or Liang-Barsky
if ( accept /* Cohen-Sutherland */ or visible /* Liang-Barsky */ )
call SRGP_lineCoordOriginal( Round(x0), Round(y0), Round(x1), Round(y1) )
where SRGP_lineCoordOriginal is the original SRGP_lineCoord of Lab 1, and
the final values of x0, y0, x1, and y1 would have been converted to
reals (doubles) and possibly modified by the clipping algorithm.
There may be a better way to do this.
Part 2: Implementing fill routines.
Part A: Implement all the SRGP (upright) "rectangle" routines:
SRGP_rectangle, SRGP_rectanglePt, SRGP_rectangleCoord, SRGP_fillRectangle,
SRGP_fillRectanglePt, and SRGP_fillRectangleCoord.
This should be easy and straightforward, but for filled rectangles
remember that the convention is not to include the pixels on the top
or rignt sides.
Hint: It may be useful to have a
FillSpan( x_start, x_end, y )
routine to fill spans between
x_start
and
x_end
on the y scanline (this may be
even more useful for filling polygons).
Part B: Implement filled polygons, i.e. SRGP_fillPolygon and SRGP_fillPolygonCoord. For this part, the ET should be an array of linked lists as in Section 3.6, but the lists need not be sorted. After thinking about it a bit, I think it may be easiest to make the AET an array (which will make sorting easier). In the ET and AET table nodes, the x value and the the reciprocal slope, 1/m, can be real numbers (doubles). I don't think roundoff errors will be a problem, however it will be necessary to see if x really represents an integer or not. The following expression
abs ( (double) Round ( x ) - x ) < 0.0001will be true when x really represents an integer ( you can use some small number other than 0.0001 if you like).
Note that if a span starts and ends at the same integer pixel coordinate, then that pixel should be drawn. This is explained near the bottom of page 94 with vertex B of Figure 3.22 as an example.
Testing for Part 2B: Include the following tests (you are encouraged to do others): show that your fill algorithm works for the polygons of Figures 3.22, 3.23, 3.24, and 3.25. Also test with a pentagram as in Figure 2.10, an "octagram", and the "special polygon" below (coordinates are given below). Here are the coordinates, as I read them or made them up:
Extra Challenges/credit:
(1) (2 points)
The above algorithm does not accurately clip a polygon -- the clipped
polygon (of a polygon that overlaps the clip rectangle)
should have edges along the boundary of the clip rectangle.
Add code to clip polygons correctly -- by using the Sutherland-Hodgman
polygon clipping algorithm for instance.
(2) (up to 2 points)
Maintain both the ET lists and the AET as sorted linked lists and merge an ET
list into the AET in a single pass.
If you do this, I think it should be possible to modify the algorithm on
page 98 by sorting the AET after Step 3.5 (instead of at the end of Step 3.2)
-- this way the AET will always be sorted when you do Step 3.3.
The AET entries with y = y_max should also
be removed in a single pass. It may be possible to both add the ET list and
remove the AET entries in the same pass, but this is _not_ required.
(3) (2 points)
In ET and AET nodes, maintain both the x value and
the reciprocal slope, 1/m, should be maintained as rational numbers -- that is
with numerators and denominators.
This makes the algorithm a purely integer algorithm (no need for rounding,
etc.).
Hint: do this, (as in Figure 3.26), "scale up" x by the value
y_max - y_min =
the denominator of 1/m.
Also the rounded values for determining the
span endpoints should probably
be calculated separately (without changing the x value in the node).
What to turn in:
Turn in the following items from the
Computer Science Lab Report Format:
1. (1 point) The Basic Information (your name, class, section,
TA's name, assignment number, and date) can be on a separate cover sheet or as
(highlighted) comments at the top of your main program file.
2. (1 point) The Problem Statement is a brief description of
The assignment: above. Note: the problem statement can (and should)
be in a comment at the top of your main program file.
4. & 5. (3 points) describe your data structures for filling
polygons, and any changes to the algorithm in the text.
9. (up to 6 points if done) Document the Extra Credit part
of your program if you did it (by coded/hand-written comments and highlighting
-- as usual).
11. (10 points) Include all program listings and output.
These listings should show good style,
be appropriately commented, have the important parts
highlighted, and include handwritten explanations if it helps the
reader's understanding of the code.
13. & 14. (20 points) The most important part.
Do live demos that shows
that clipping and filling work, plus turn in printouts of clipped lines,
polylines, and filled polygons.
The Test Descriptions are handwritten explanations on these
printouts of what each program run demonstrates.
15. Include a description of any Known Bugs if needed.
You may be able to gain back lost points by a careful analysis of
what went wrong and possible fixes.
16. This is optional (and no points usually),
but it is useful to think about Possible Improvements.
17. Comments on the lab assignment are also optional
(and again, no points usually), but appreciated!
Important Note: You may discuss algorithms with other students, but the program and lab report should be your own work.