Week 11 Tutorial
Note that Monday is a public holiday — if your tutorial is on then, go to one of the Tuesday or Wednesday ones.
Question 1
Here is the code for drawing a line from lectures:
void drawLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
float m = dy / (float) dx;
float b = y1 - m*x1;
for (x = x1; x <= x2; x++) {
y = m*x+b;
drawPixel(x,Math.round(y));
}
}
It has the following problems:
- Lines with a slope greater than one have gaps.
- Lines with x2 < x1 are not drawn.
- Vertical lines cause division by zero.
Question 2
Work out a version of Bresenham’s algorithm that works in the third octant, that is, when y always increases and x may or may not decrease — the choice is between going up and going up and left.
Question 3

Consider the scan line algorithm described in class. For the polygon drawn here, what do the lists AEL and IEL look like just before filling the pixels on scan line 5? What pixels will be filled?
Question 4
Write a method fillTriangle(Point a, Point b, Point c)
that fills a triangle using the method fillSpan(int startx,int endx, int y)
that fills in the pixels between (startx,y) and (endx,y). (Assume
both methods belong to the same class.)
You can also assume that a.y < b.y < c.y and
b is to the right of ac.
Question 5
You can use the triangle filling method to fill a general polygon if you divide the polygon up into triangles. Devise an algorithm for cutting up a polygon into triangles.