Академический Документы
Профессиональный Документы
Культура Документы
Rasterization
Final step in pipeline: rasterization (scan conv.)
From screen coordinates (float) to pixels (int)
Writing pixels into frame buffer
Separate z-buffer, display, shading, blending
Concentrate on primitives:
Lines
Polygons
Uses Incremental Function Evaluation
f(xi+1) = f(xi) + fxi
Fast but cumulative Error
Need to find f(x0)
DDA Algorithm
DDA (“Digital Differential Analyzer”)
Assume 0 m 1 else flip about y= x line.
(xi,Round(yi+m)) Desired Line
yi 1 mxi 1 B
m( xi x) B
(xi,yi)
yi mx
yi 1 yi m x 1 (xi,Round(yi))
(xi,yi +m)
void Line(int x0, int y0, int x1, int y1, int value) {
double y = y0;
double m = (y1 – y0) / (x0 – x1); // 0 <= m <= 1
for (int x = x0; x <= x1; x++) {
WritePixel(x, Round(y), value);
y += m;
}
} Require to eliminate floating point operations & variables
Midpoint Line Algorithm
Assume
0 m 1 [ else flip about y= x line. ]
(x0, y0) and (x1, y1) are integer values
At each iteration we determine if the line
intersects the next pixel above or below its
midpoint y value. d=F(M) > 0 NE
if above then NE ynew = yp+1 NE
yp+1
otherwise E ynew = yp M
E yp
dy P=(xp, yp) xp xp+1
y xB
dx
F ( x, y ) dy x dx y B dx
now, d F ( M ) d=F(M) < 0 E NE yp+1
M
F ( x p 1, y p 12 )
P=(xp, yp)
E yp
dy ( x p 1) dx ( y p ) B dx
1
2
xp xp+1
y d y d
Midpoint Line Algorithm
P=(xp, yp) is pixel chosen by the algorithm in previous step
To calculate d incrementally we require dnew
If d > 0 then choose NE
d dy ( x p 1) dx ( y p 12 ) B dx ( x p 2, y 32 )
( x p 1, y 12 )
d new F ( x p 2, y p 32 ) Yp+2
dy ( x p 2) dx ( y p 32 ) B dx NE
MNE
d new d dy dx M Yp+1
NE yp
E
P=(xp, yp)
d new d NE xp xp+1 xp+2
NE dy dx
Previous
Current
Next
Midpoint Line Algorithm
If d < 0 then choose E
d dy ( x p 1) dx ( y p 12 ) B dx
( x p 2, y p 12 )
d new F ( x p 2, y p 12 ) ( x p 1, y p 12 )
Yp+2
dy ( x p 2) dx ( y p 12 ) B dx
NE Yp+1
d new d dy
M ME
E
yp
d new d E P=(xp, yp) xp
E
xp+1 xp+2
E dy
Previous
Current
Next
Midpoint Line Algorithm
To find Initial value of d
d 0 F ( x0 1, y0 12 ) ( x0 1, y0 12 )
dy ( x p 1) dx ( y p 12 ) B dx
NE
dy x p dx y p B dx dy 12 dx
M
F ( x0 , y0 ) dy dx
1
2 E
P=(x0, y0) x0 x0+1
d 0 dy 12 dx Only fractional value
Initial do
Start
[as ( x0 , y0 ) is on the line]
Multiply by 2 to avoid fractions. Redefine d0, E, NE
d 0 2 dy dx
NE 2 (dy dx)
E 2 dy
Midpoint Line Algorithm ( x p 2, y p 12 )
( x p 1, y p ) 1
2
void MidpointLine(int x0, int y0, int x1, int y1, int color)
{
NE
int dx = x1 – x0, dy = y1 – y0; (xi,yi)
int d = 2*dy – dx; M
WritePixel(x, y, color);
Previous
Current
Next
while (x < x1) {
if (d <= 0) { // Current d
d += dE; // Next d at E
( x p 2, y p 32 )
x++; ( x p 1, y p 2 )
1
} else {
d += dNE; // Next d at NE
NE
x++; (xi,yi)
y++ M
} E
WritePixel(x, y, color); P=(xp, yp) x x +1 xp+2
p p
Previous
Current
Next
}
Midpoint Circle Algorithm
Implicit of equation of circle is:
x2 + y2 - R2 = 0
Eight way symmetry require to calculate one
octant
Define decision variable d as:
d F ( M ) F ( x p 1, y p 12 )
x p 1 y p 12 R 2
2 2
( x p 1, y p 12 ) ( x p 2, y p 12 )
d 0
P=(xp, yp) E
M is inside Circle yp
M ME
Choose E yp – 1
SE MSE
d 0
d 0 yp – 2
M is outside Circle xp xp+1 xp+2
Choose either
Previous
Current
Choose SE
Next
we choose E
Midpoint Circle Algorithm
If d <= 0 then midpoint m is inside circle
we choose E
Increment x
y remains unchanged
( x p 1, y p 12 ) ( x p 2, y p 12 )
d x p 1 y p 12 R 2
2 2
P=(xp, yp) E
yp
ME
d< 0 M
d new F ( x p 2, y p 12 ) yp – 1
x p 2 y p 1 2
2
2 R2 xp xp+1 xp+2
yp – 2
Previous
Current
d new d 2 x p 3
Next
E
d new d E
Midpoint Circle Algorithm
If d > 0 then midpoint m is outside circle
we choose E
Increment x
Decrement y ( x p 1, y p 12 ) ( x p 2, y p 32 )
P=(xp, yp)
d x p 1 y p
yp
2 1 2
2 R 2
d> 0
M
yp – 1
SE MSE
d new F ( x p 2, y p )
3
2 yp – 2
x p 2 y p
xp xp+1 xp+2
2 3 2
R 2
Previous
Current
2
Next
d new d 2 x p 2 x p 5
SE
d new d SE
Midpoint Circle Algorithm
Initial condition
Starting pixel (0, R)
d = F(1, R – ½) = 1 + (R2 – R + ¼) – R2 = 5/ – R
0 4
Enew 2( x p 1) 3 Enew E 2
( x p 1, y p 1)
SEnew 2( x p 1) 2( y p 1) 5 SEnew SE 4
Midpoint Circle Algorithm
void MidpointCircle(int radius, int value) {
int x = 0;
int y = radius ;
int d = 1 – radius ;
int dE = 3;
int dSE = -2*radius +5;
CirclePoints(x, y, value);
while (y > x) {
if (d < 0) { /* Select E */
d += dE;
dE += 2;
dSE += 2;
} else { /* Select SE */
d += dSE;
dE += 2;
dSE += 4;
y – –;
}
x++;
CirclePoints(x, y, value);
}
}
Midpoint Ellipse Algorithm
Implicit equation is: F(x,y) = b2x2 + a2y2 – a2b2 = 0
We have only 4-way symmetry (-x ,y ) 1 1 (x1,y1)
In Region 1 dx > dy
(-x2,-y2) (x2,-y2)
Increase x at each step
(-x1,-y1) (x1,-y1)
y may decrease
In Region 2 dx < dy
E Tangent
Decrease y at each step Slope = -1 Gradient
x may increase SE Vector
At region boundary: Region 1
Region 2 S SE
dy
2 x b2 2 y a2 0
dx dy
In Region 1 1
dy b2 x dx
2
dx a y b2 x a 2 y
Midpoint Ellipse Algorithm
d F ( x p 1, y p 12 ) In region 1
b 2 ( x p 1) a 2 ( y p 12 ) a 2b 2
if d 0 then move to E
( x p 1, y p 12 ) ( x p 2, y p 12 )
d new F ( x p 2, y p 12 )
P=(xp, yp) E
yp
b 2 ( x p 2) a 2 ( y p 12 ) a 2b 2 M ME
yp – 1
SE
d new d b 2 (2 x p 3) MSE
yp – 2
E xp xp+1 xp+2
if d 0 then move to SE ( x p 2, y p 32 )
Previous
Current
Next
d new F ( x p 2, y p 32 )
b 2 ( x p 2) a 2 ( y p 32 ) a 2b 2
d new d b 2 (2 x p 3) a 2 (2 y p 2)
SE
Midpoint Ellipse Algorithm
d F ( x p 12 , y p 1) In region 2
b 2 ( x p 12 ) a 2 ( y p 1) a 2b 2
Previous
Current
if d 0 then move to S
Next
xp xp+1 xp+2
d new F ( x p 12 , y p 2)
P=(xp, yp)
yp
b ( x p 12 ) a ( y p 2) a b
2 2 2 2
yp – 1
S SE
d new d a 2 (2 y p 3) M
yp – 2
S MS MSE
( x p , y p 1)
1
2
if d 0 then move to SE ( x p 12 , y p 2) ( x p 2 , y p 2)
3
d new F ( x p 32 , y p 2)
b 2 ( x p 32 ) a 2 ( y p 2) a 2b 2
d new d b 2 (2 x p 2) a 2 (2 y p 3)
SE
Self Study
Pseudo code for midpoint ellipse algorithm
Derive 2nd order difference equations for
midpoint ellipse algorithm.
Side Effects of Scan Conversion
Consider equation y = mx + b
For m = 0.5, b = 1 and x = 3 : y = 2.5
44
1.
Intensity
of light source. 1
Remedy
Check each pixel to see whether it has already been
written to prior to writing a new point.