Вы находитесь на странице: 1из 23

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.
(xi,Round(yi+m)) Desired Line
yi 1  mxi 1  B
 m( xi  x)  B
(xi,yi)
 yi  mx
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 xB
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

int dE = 2*dy, dNE = 2*(dy – dx); E


P=(xp, yp) x xp+1 xp+2
int x = x0, y = y0; p

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)

 Next Midpoint lies at (1, R – ½)

 d = F(1, R – ½) = 1 + (R2 – R + ¼) – R2 = 5/ – R
0 4

 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 Algorithm
void 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.
E
Enew  2( x p  1)  3 Enew  E  2
M ( x p  1, y p )
SEnew  2( x p  1)  2 y p  5 SEnew  SE  2
SE MS
E

E is chosen Initial value :


E  2 x p  3
(xp , y p ) E  3
SE  2 x p  2 y p  5 (0, R )
SE  2 R  5
SE is chosen

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)

 There exists two regions (-x ,y )


2 2 (x2,y2)

 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

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 + b
For m = 0.5, b = 1 and x = 3 : y = 2.5

So the point (3,2.5) is plotted at alias location (3,3)


Remedy
Apply 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

44
1.
 Intensity
of light source. 1

Thus, on a raster display with perfect squareness, a


diagonal line of pixels will appear dimmer that a
horizontal or vertical line.
Remedy
1. If speed of scan conversion main then ignore
2. By increasing the number of pixels in diagonal lines
3. By increasing the number of pixels on diagonal lines.
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

Remedy
Check each pixel to see whether it has already been
written to prior to writing a new point.

Вам также может понравиться