Download Scan Conversion - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!
Lecture 16:
Scan Conversion
OpenGL Pipeline
Model Transform
View Transform
Perspective Transform Clipping^
Perspective Division
Viewport Transform
Scan Conversion
v v v
Scan Converting Lines
• Line Drawing
- Draw a line on a raster screen between two points
• What’s wrong with this statement of problem?
- Doesn’t say anything about which points are allowed as end points
- Doesn’t give a clear meaning of “draw”
- Doesn’t say what constitutes a “line” in raster form
- Doesn’t say how to measure success of proposed algorithm
Scan Converting Lines
• Line Drawing
• Problem Statement:
– Given two points P and Q in XY plane, both with
integer coordinates, determine which pixels on
raster screen should be on in order to make
picture of a unit width line segment starting at P
and ending at Q
Vertical Distance
- Why can we use vertical distance as a measure of which
point is closer?
- Because vertical distance is proportional to actual distance
- We can show this with similar triangles
- The true distances to line (in blue) are directly proportional to vertical distances to line (in black)
- Therefore, point with smaller vertical distance to line is closest
Strategy 1: Incremental Algorithm
Strategy 2 – Brsenham’s Algorithm
- Problem:
- Given endpoints (Ax, Ay) and (Bx, By) of a line, determine the best sequence of intervening pixels to represent a line
- First, make two simplifying assumptions:
- (Ax < Bx) that is, moving from left to write
- (0 < m < 1) that is, slope up and to the right at between ( and 45 degrees)
- We can remove both assumptions later…
- Define
- Width W = Bx – Ax
- Height H = By - Ay
Strategy 2 – Brsenham’s Algorithm
- Based on our assumptions:
- W and H are positive
- H < W
- As x steps in the +
increments, y can:
- Stay the same
- Increment by 1
- The key is to figure out
which of these cases we want
P ( xP , yP ) x xP 1 Previous pixel
E pixel
NE pixel
Strategy 2 – Brsenham’s Algorithm
- Line passes between E and NE
- Point that is closer to intersection point 𝑄 must be chosen
- Observe on which side of line midpoint 𝑀 lies:
- E is closer to line if midpoint 𝑀 lies above line, i.e., line crosses bottom half
- NE is closer to line if midpoint 𝑀 lies below line, i.e., line crosses top half
- Error (vertical distance between chosen pixel and actual line) is always ≤.
E pixel
NE pixel
𝑀
𝑄
Algorithm chooses NE as next pixel for line shown
Now we need to find a way to calculate on which side of the line the midpoint is
Strategy 2 – Brsenham’s Algorithm
represent the line
- The equation F(x, y) is doubled to
avoid floats later…
- Note that when F(x, y) == 0, then
(x, y) is on the line
- If (x, y) is not on the line, we get a
positive or negative value for F
- We can think of this as a measure of error (signed distance) to the line
Strategy 2 – Brsenham’s Algorithm
• Bonus: we can compute F(x, y) incrementally:
• Initial midpoint M = (Ax+1, Ay+0.5)
– Plugging into F(Mx, My) = -2W(y-Ay) + 2H(x-Ax)
– We get an initial F = 2H – W
• The next midpoint will be:
– If y stays the same: F(Mx, My) += 2H
– If y = y+1: F(Mx, My) += 2(W – H)
Brsenham’s Algorithm – Psueocode
void Bresenham (IntPoint a, IntPoint b) { //assume a.x < b.x, and 0 < H/W < 1 int y = a.y; int W = b.x – a.x; int H = b.y – a.y; int F = 2*H – W; //current error term
for (int x = a.x; x <= b.x; x++) { WritePixel (x, y); if (F < 0) { F += 2H; } else { y++; F += 2(H – W); } } }
Scan Converting Circles
• Version 1:
– Increment x
– Find y value
– WritePixel (x, y)
• Terrible!
(17, 0)
(0, 17)
Scan Converting Circles
- Version 1:
- Increment x
- Find y value
- WritePixel (x, y)
- Terrible!
- Version 2:
- Use polar coordinates
- For angle from (0 to 360)
- Find x, y
- WritePixel (x, y)
- Less bad…
(17, 0)
(0, 17)
(17, 0)
(0, 17)