Lecture Slides

CS 130
Scan Conversion
Raster Graphics
2
1
Image Formation
• Computer graphics forms images,
generally two dimensional, using
processes analogous to physical
imaging systems like:
- Cameras
- Human visual system
• Images are generated and shown with
specialized software and hardware
3
Introduction
• Synthetic images can be created like those
taken by a camera
For this class, we will draw directly on the image plane
4
2
Scan conversion
• Image Plane is a N x M grid of pixels
• This grid is similar to a coordinate system only
it is discrete.
5
Scan conversion
Scan conversion is taking the graphic from the
continuous (thin) line to turning on pixels
6
3
Scan conversion
Scan conversion algorithms compute integer
coordinates for pixels near the line or circle
Algorithms are invoked many, many times and
so must be efficient
Result should be visually pleasing, for example,
they should have constant density
Obviously, they should be able to draw the gamet
of possible lines/circles and remain defined
7
Math Review
• 2D math for lines
(X2, Y2)
How do we determine the
equation of the line?
(X1, Y1)
8
4
Math Review
• 2D math for lines
Slope-Intercept formula for a line
(X2, Y2)
Slope = (Y2 – Y1)/(X2 – X1)
( Y - Y1)/(X - X1)
(X, Y)
Solving For Y
(X1, Y1)
Y = [(Y2 – Y1)/(X2 – X1)]X
+ [-(Y2 – Y1)/(X2 - X1)]X1 + Y1 or
Y=mX+b
9
Math Review
• Explicit (functional) representation
y = f(x)
y is the dependent, x independent variable
Find value of y from value of x
Example, for a line:
y = mx + b
for a circle:
x2 + y2 = r2
10
5
Math Review
• Implicit representation
f(x,y) = 0
Called a membership function, test value of (x,y)
to see if it belongs to the curve
x,y treated the same, axis invariant
Example, for a line:
ax + by + c = 0
for a circle:
x2 + y2 – r2 = 0
ax + by + c < 0 ?
11
Math Review
• Parametric Representation
x = x(u), y = y(u)
where new parameter u (or often t) determines the value
of x and y (and possibly z) for each point
x,y treated the same, axis invariant
12
6
Math Review
Parametric formula for a line
(X2, Y2)
X = X1 + t(X2 – X1)
Y = Y1 + t(Y2 – Y1)
for parameter t from 0 to 1
(X, Y)
(X1, Y1)
Therefore, when
t = 0 we get (X1,Y1)
t = 1 we get (X2,Y2)
Varying t gives the points along the line segment
13
Math Review
• 2D math for circles
Circle centered at origin
X2 + Y2 = R2
where R is the radius
We can plot this as:
R
Y = +/- R2 - X2
14
7
Math Review
• 2D math for circles
Y = R2 - X2 includes
a fair amount of computation
May want to parametric form:
x = R cos 
y = R sin 
R
With theta going from 0 to 360,
Then use with a look-up sin/cos
15
Scan conversion
Scan conversion is taking the graphic from the
continuous (thin) line to turning on pixels
16
8
Scan conversion for lines
0<M<1
M>1
17
DDA algorithm for lines
Parametric Lines: the DDA algorithm for 0 < M < 1
(digital differential analyzer)
Yi+1 = m xi+1 + B
= m(xi + x) + B
= yi + m(x)
x = (xi+1 - xi)
<- must round to find int
We increment by 1 pixel in X
We turn on [xi, Round(yi)] - or same for Y if m > 1
18
9
Scan conversion for lines
19
Fast scan conversion
for lines
DDA includes Round( ); and this is fairly slow
For Fast Lines, we want to do only integer math +,We do this using the Midpoint Algorithm
To do this, lets look at lines with y-intercept B
and with slope between 0 and 1:
y = (dy/dx)x + B ==>
f(x,y) = (dy)x - (dx)y + B(dx) = 0
Removes the division => slope treated as 2 integers
20
10
Midpoint Algorithm
For lines in the first octant, the next pixel is to the
right or to the right and up one
By looking at the
difference between the
line and the midpoint, e,
we only need to look
at the sign
i.e.) no Round( ) needed
midpoint e
21
Midpoint: Decision
Variable
Rewrite such that f(x,y) = ax + by + c
(from f(x,y) = (dy)x - (dx)y + B(dx) =0)
Then, f(x + 1, y + 1/2) is the next column's midpoint
= a(x + 1) + b(y + 1/2) + c = d
(this d is called the decision variable)
if (d = 0) then the line passes on the midpoint
if (d > 0) then the line passes above
if (d < 0) then the line passes below
22
11
Midpoint: Decision
Variable
The question is how do we determine the value of
d for a given line?
For example to start, f(xo + 1, yo + 1/2) = d
d = a(xo + 1) + b(yo + 1/2) + c
= a(xo) + b(yo) + c + a + b/2
= f(xo,yo) + a + b/2
but f(xo,yo) = 0
= a + b/2
What about d after this first case?
23
Scan Converting Circles
• What problems arise when we think about scan
conversion for circles (ie. when we increment
one pixel in x, say)
R
24
12
Scan Converting Circles
• What problems arise when we think about scan
conversion for circles (ie. when we increment
one pixel in x, say)
25
Scan Converting Circles
• Better approach takes into account the 8 way
symmetry of each octant in a circle
(Lets call this function circle_points )
26
13
Fast Circles
• Only consider the first octant of the circle of
radius r centered at origin
• Start from (r,0) and go
until x < y (at 45 degrees)
• The decision is at each
step to go up, or go over
to the left and up
27
Fast Circles - via the
midpoint
f(x,y) = x2 + y2 - r2 = 0
(x -1,y +1) midpoint e
(x,y +1)
Then for next decision (midpoint decides row)
f(x-1/2 , y +1) = d
= (x - 1/2)2 + (y + 1)2 -r2
Starting at y = 0, x = r this becomes
= (r - 1/2)2 + (0 + 1)2 - r2
= 5/4 - r
(x,y)
28
14
Fast Circles - via the
midpoint
All thats left is to find the new d for the next step
If (go up and over)
d* = f(x - 1 - 1/2, (y + 1) + 1) = (x -3/2)2 + (y + 2)2 - r2
...
= d - 2(x -1) + 2 (y + 1) + 1
= d + 2(y - x) + 5
Else (just go up)
d* = f(x -1/2, (y + 1) + 1)
...
= d + 2(y) + 3
29
15