03-scan conversion
TRANSCRIPT
Scan Conversion
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.
1
)(
1
11
xmyy
xmy
Bxxm
Bmxy
ii
i
i
ii
(xi,yi)
(xi,Round(yi))
(xi,Round(yi+m))
(xi,yi +m)
Desired Line
void Line(int x0, int y0, int x1, int y1, int value) {double y = y0;double m = (y1 – y0) / (x0 – x1); // 0 <= m <= 1for (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.
if above then NE ynew = yp+1 otherwise E ynew = yp
dydy
dxBydxxdy
yxF
MFdnow
dxBydxxdyyxF
Bxdx
dyy
pp
pp
)()1(
),1(
)(,
),(
21
21
P=(xp, yp)
M
E
NE
xp xp+1
d=F(M) < 0 E yp+1
yp
d=F(M) > 0 NE
P=(xp, yp)
M
E
NE
xp+1xp
yp+1
yp
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
dxdyNE
NEdd
dxdydd
dxBydxxdy
yxFd
dxBydxxdyd
new
NE
new
pp
ppnew
pp
)()2(
),2(
)()1(
23
23
21
P=(xp, yp)
M
E
NE
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 yxp
),2( 23 yxp
yp
Yp+1
Yp+2MNE
Midpoint Line Algorithm If d < 0 then choose E
dyE
Edd
dydd
dxBydxxdy
yxFd
dxBydxxdyd
new
E
new
pp
ppnew
pp
)()2(
),2(
)()1(
21
21
21
P=(xp, yp)
M
E
NE
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx
),2( 21 pp yx
yp
Yp+1
Yp+2
ME
Midpoint Line Algorithm To find Initial value of d
line] the on is [as
),(
),(
)()1(
),1(
00
21
0
21
00
21
21
21
000
yx
dxdyd
dxdyyxF
dxdydxBydxxdy
dxBydxxdy
yxFd
pp
pp
P=(x0, y0)
M
E
NE
x0+1x0
Sta
rt
Init
ial d
o
),1( 21
00 yx
Only fractional value
dyE
dxdyNE
dxdyd
2
)(2
20
Multiply by 2 to avoid fractions. Redefine d0, E, NE
Midpoint Line Algorithmvoid MidpointLine(int x0, int y0, int x1, int y1, int color){
int dx = x1 – x0, dy = y1 – y0;int d = 2*dy – dx;int dE = 2*dy, dNE = 2*(dy – dx);int x = x0, y = y0;WritePixel(x, y, color);while (x < x1) {
if (d <= 0) { // Current dd += dE; // Next d at Ex++;
} else {d += dNE; // Next d at NEx++;y++
} WritePixel(x, y, color);
}}
(xi,yi)
P=(xp, yp)
M
E
NE
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx
),2( 23 pp yx
(xi,yi)
P=(xp, yp)
M
E
NE
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx
),2( 21 pp yx
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:
SE Choose
Circle outside s
E Choose
Circle inside s
iM
d
iM
d
Ryx
yxFMFd
pp
pp
0
0
1
),1()(
22
212
21
P=(xp, yp)
M
E
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
SE MSE
E choose we
either Choose
0d
Midpoint Circle Algorithm If d <= 0 then midpoint m is inside circle
we choose E Increment x y remains unchanged
Edd
xdd
Ryx
yxFd
Ryxd
new
E
pnew
pp
ppnew
pp
32
2
),2(
1
22
212
21
22
212 P=(xp,
yp)M
E
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
d < 0
Midpoint Circle Algorithm If d > 0 then midpoint m is outside circle
we choose E Increment x Decrement y
SEdd
xxdd
Ryx
yxFd
Ryxd
new
SE
ppnew
pp
ppnew
pp
522
2
),2(
1
22
232
23
22
212
Pre
vio
us
P=(xp, yp) M
SE
xp+1xp xp+2
Curr
ent
Next
),1( 21 pp yx ),2( 2
3 pp yx
MSE
yp
yp – 1 yp – 2
d > 0
Midpoint Circle Algorithm
Initial condition Starting pixel (0, R) Next Midpoint lies at (1, R – ½) d0 = F(1, R – ½) = 1 + (R2 – R + ¼) – R2 = 5/4 – R To remove the fractional value 5/4 :
Consider a new decision variable h as, h = d – ¼ Substituting d for h + ¼,
d0=5/4 – R h = 1 – R d < 0 h < – ¼ h < 0 Since h starts out with an integer value and is incremented
by integer value (E or SE), e can change the comparison to just h < 0
Midpoint Circle Algorithmvoid MidpointCircle(int radius, int value) {
int x = 0;int y = radius ;int d = 1 – radius ;CirclePoints(x, y, value);while (y > x) {
if (d < 0) { /* Select E */d += 2 * x + 3;
} else { /* Select SE */d += 2 * ( x – y ) + 5;y – –;
}x++;CirclePoints(x, y, value);
}}
Midpoint Circle Algorithm Second-order differences can be used to enhance
performance.
522
32),(
pp
ppp yxSE
xEyx
2
2
52)1(2
3)1(2),1(
SESE
EE
yxSE
xEyx
new
new
ppnew
pnewpp
4
2
5)1(2)1(2
3)1(2)1,1(
SESE
EE
yxSE
xEyx
new
new
ppnew
pnewpp
E is chosen
SE is chosen 52
3),0(
RSE
ER
:value Initial
M
SE MS
E
E
Midpoint Circle Algorithmvoid 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 There exists two regions
In Region 1 dx > dy Increase x at each step y may decrease
In Region 2 dx < dy Decrease y at each step x may increase
At region boundary: Region 1
Region 2S SE
E
SE
Gradient Vector
TangentSlope = -1
(x1,y1)(-x1,y1)
(x1,-y1)(-x1,-y1)
(-x2,y2)
(-x2,-y2)
(x2,y2)
(x2,-y2)
ya
xb
dx
dy
dx
dyaybx
2
2
22 022
yaxb
dx
dy
22
1 1 Region In
Midpoint Ellipse Algorithm In region 1
SE
ppnew
pp
ppnew
E
pnew
pp
ppnew
pp
pp
yaxbdd
bayaxb
yxFd
SEto move then 0 d if
xbdd
bayaxb
yxFd
E to move then 0 d if
bayaxb
yxFd
)22()32(
)()2(
),2(
)32(
)()2(
),2(
)()1(
),1(
22
222322
23
2
222122
21
222122
21
P=(xp, yp)
M
E
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
SE MSE
),2( 23 pp yx
Midpoint Ellipse Algorithm In region 2
SE
ppnew
pp
ppnew
S
pnew
pp
ppnew
pp
pp
yaxbdd
bayaxb
yxFd
SEto move then 0 d if
yadd
bayaxb
yxFd
Sto move then 0 d if
bayaxb
yxFd
)32()22(
)2()(
)2,(
)32(
)2()(
)2,(
)1()(
)1,(
22
222232
23
2
222212
21
222212
21
P=(xp, yp)
MS
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
)1,( 21 pp yx
)2,( 21 pp yx
MS
yp
yp – 1
yp – 2
SE
MSE
)2,( 23 pp yx
Self Study
Pseudo code for midpoint ellipse algorithm
Derive 2nd order difference equations for midpoint ellipse algorithm.
Side Effects of Scan Conversion
The most common side effects when working with
raster devices are:
1. Aliasing
2. Unequal intensity
3. Overstrike
1. Aliasing• jagged appearance of curves or diagonal lines on a display screen, which is caused by low screen resolution.
Refers to the plotting of a point in a location other than its true location in order to fit the point into the raster.
Consider equation y = mx + bFor m = 0.5, b = 1 and x = 3 : y = 2.5
So the point (3,2.5) is plotted at alias location (3,3)
RemedyApply anti-aliasing algorithms. Most of these algorithms introduce extra pixels and pixels are intensified proportional to the area of that pixel covered by the object. [dithering.]
2. Unequal Intensity
Human perception of light is dependent on Density and Intensity of light source.
Thus, on a raster display with perfect squareness, a diagonal line of pixels will appear dimmer that a horizontal or vertical line.
Remedy1. If speed of scan conversion main then ignore2. By increasing the number of pixels in diagonal
lines3. By increasing the number of pixels on diagonal
lines.
1.44
1
3. Overstrike
The same pixel is written more than once.
This results in intensified pixels in case of photographic media, such as slide or transparency
RemedyCheck each pixel to see whether it has already been written to prior to writing a new point.