300 likes | 470 Views
Scan Conversion. A. Samal. Scan Conversion. Last step in the graphics pipeline Efficiency is a central issue Common primitives Lines Polygons Circles Hardware implementations preferable. Lines. Compute the coordinate of the pixels that lie on a line on a 2D raster
E N D
Scan Conversion A. Samal
Scan Conversion • Last step in the graphics pipeline • Efficiency is a central issue • Common primitives • Lines • Polygons • Circles • Hardware implementations preferable
Lines • Compute the coordinate of the pixels that lie on a line on a 2D raster • Defined by the coordinates of the end points • Criteria • Thin • As close to ideal as possible • 1 pixel wide • If -1<= slope <= 1 exactly 1 pixel in one column • Else exactly 1 pixel in one row • Constant brightness, independent of the length & orientation • Should be drawn fast
Problem • for each x plot pixel at closest y • Problems for steep lines
Basic Incremental Algorithm Desired Line
Basic Incremental Algorithm • Brute force algorithm • Inefficient • Each iteration • Floating point multiply • Floating point addition • Rounding
Basic Incremental Algorithm • Eliminate the multiplication • Incremental computation • Digital Differential Analyzer (DDA)
Basic Incremental Algorithm • If the magnitude of the slope is more than 1 change the role of x and y in the algorithm • y increases faster than x • Simple but inefficient • Still there are floating point operations • Bresenham’s algorithm • Uses only integer arithmetic • Avoids round function • Extended to drawing circles
Midpoint Line Algorithm NE M E Previous Pixel Current Pixel Choices Next Pixel Choices
Compute the distance between Q and E Q and NE See which side of the line M lies M lies below the line Choose NE M lies above the line Choose E M lies on the line Choose either NE M E Previous Pixel Current Pixel Choices Next Pixel Choices Midpoint Line Algorithm
NE M E Previous Pixel Current Pixel Choices Next Pixel Choices Midpoint Line Algorithm
What happens at the next grid point? Depends on whether we choose N or NE If E is chosen NE M E Previous Pixel Current Pixel Choices Next Pixel Choices Midpoint Line Algorithm
NE M E Previous Pixel Current Pixel Choices Next Pixel Choices Midpoint Line Algorithm • If NE is chosen
Midpoint Line Algorithm • Summary • Choose between the two pixels (E/NE) based on the sign of the decision variable d=F(M) • Update the decision variable by adding the change along that direction • Repeat this process until the end point is reached
Midpoint Line Algorithm • Fractional arithmetic still problematic • Should be avoided if possible • Redefine F(x,y) by multiplying by 2 • Thus F(x,y) = 2(ax+by+c) • Replace all constants and the decision variable by 2 • Removes all multiplications; only additions left
Midpoint Line Algorithm MidpointLine(x0,y0,x1,y1,color) { int dx,dy,dE,dNE,d,x,y; dx=x1-x0; dy=y1-y0; d=2*dy-dx; dE=2*dy; dNE=2*(dy-dx); x=x0;y=y0; DrawPixel(x,y,color); for(x=x0;x<x1; x++) { if (d<=0) d+=dE ; else {d+=dNE; y++;} DrawPixel(x,y,color); }
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9