Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scan Conversion (From geometry to pixels)

Similar presentations


Presentation on theme: "Scan Conversion (From geometry to pixels)"— Presentation transcript:

1 Scan Conversion (From geometry to pixels)
COMPUTER GRAPHICS Scan Conversion (From geometry to pixels)

2 LINE-DRAWING ALGORITHMS
On raster systems, lines are plotted with pixels, and step sizes in the horizontal and vertical directions are constrained by pixel separations. That is, we must "sample" a line at discrete positions and determine the nearest pixel to the line at sampled position. Sampling is measuring the values of the function at equal intervals Idea: A line is sampled at unit intervals in one coordinate and the corresponding integer values nearest the line path are determined for the other coordinate.

3 Towards the Ideal Line We can only do a discrete approximation
Illuminate pixels as close to the true path as possible, consider bi-level display only Pixels are either lit or not lit In the raster line alg., we sample at unit intervals and determine the closest pixel position to the specified line path at each step

4 What is an ideal line Must appear straight and continuous
Only possible with axis-aligned and 45o lines Must interpolate both defining end points Must have uniform density and intensity Consistent within a line and over all lines What about anti-aliasing ? Aliasing is the jagged edges on curves and diagonal lines in a bitmap image. Anti-aliasing is the process of smoothing out those jaggies. Graphics software programs have options for anti-aliasing text and graphics. Enlarging a bitmap image accentuates the effect of aliasing. Must be efficient, drawn quickly Lots of them are required

5 Simple Line The Cartesian slope-intercept equation for a straight line is : y = mx + b with m as the slope of the line and b as the y intercept. Simple approach: increment x, solve for y

6 Line-Drawing Algorithms: DDA Bresenham’s Midpoint Algorithm

7 Algorithms for displaying lines are based on the Cartesian slope-intercept equation
y = m.x + b where m and b can be calculated from the line endpoints: m = (y1-y0) / (x1-x0) b = y0 - m. x0 For any x interval x along a line the corresponding y interval y = m.x y1 y0 x0 x1

8 Simple Line Based on slope-intercept algorithm from algebra:
y = mx + b Simple approach: increment x, solve for y Floating point arithmetic required

9 Does it Work? It works for lines with a slope of 1 or less,
but doesn’t work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work. Solution? - use symmetry.

10 Modify algorithm per octant
OR, increment along x-axis if dy<dx else increment along y-axis

11 DDA Algorithm The digital differential analyser (DDA) is a scan-conversion line algorithm based on using x or y. A line is sampled at unit intervals in one coordinate and the corresponding integer values nearest the line path are determined for the other coordinate.

12 Line with positive slope
If m<=1, Sample at unit x intervals (dx=1) Compute successive y values as yk+1=yk+m <=k<=xend-x0 Increment k by 1 for each step Round y to nearest integer value. If m>1, Sample at unit y intervals (dy=1) Compute successive x values as xk+1=xk+1/m <=k<=yend-y0 Round x to nearest integer value.

13 inline int round (const float a) { return int (a + 0.5); }
void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) > fabs (dy)) steps = fabs (dx); else steps = fabs (dy); xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement; }

14 DDA algorithm Need a lot of floating point arithmetic.
2 ‘round’s and 2 adds per pixel. Is there a simpler way ? Can we use only integer arithmetic ? Easier to implement in hardware.

15 Bresenham's line algorithm
Accurate and efficient Uses only incremental integer calculations The method is described for a line segment with a positive slope less than one The method generalizes to line segments of other slopes by considering the symmetry between the various octants and quadrants of the xy plane

16 Bresenham's line algorithm
Decide what is the next pixel position (11,11) or (11,12) Specified line path 13 12 11 10 10 11 12 13

17 Illustrating Bresenham’s Approach
For the pixel position xk+1=xk+1, which one we should choose: (xk+1,yk) or (xk+1, yk+1) yk+3 yk+2 y=mx+b yk+1 yk xk xk+1 xk+2 xk+3

18 Bresenham’s Approach y=m(xk + 1)+b dlower=y-yk =m(xk + 1)+b-yk
dupper dlower y=m(xk + 1)+b dlower=y-yk =m(xk + 1)+b-yk dupper=(yk+1)-y = yk+1 -m(xk + 1)-b dlower- dupper= 2m(xk + 1)-2yk+2b-1 Rearrange it to have integer calculations: m=Δy/Δx Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c

19 The Decision Parameter
Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c pk has the same sign with dlower- dupper since Δx>0. c is constant and has the value c= 2Δy + Δx(2b-1) c is independent of the pixel positions and is eliminated from decision parameter pk. If dlower< dupper then pk is negative. Plot the lower pixel (East) Otherwise Plot the upper pixel (North East)

20 Succesive decision parameter
At step k+1 pk+1= 2Δy.xk+1 - 2Δx. yk+1 + c Subtracting two subsequent decision parameters yields: pk+1-pk= 2Δy.(xk+1-xk) - 2Δx. (yk+1-yk) xk+1=xk+1 so pk+1= pk + 2Δy - 2Δx. (yk+1-yk) yk+1-yk is either 0 or 1 depending on the sign of pk First parameter p0 p0=2 Δy - Δx

21 Bresenham's Line-Drawing Algorithm for I m I < 1
1. Input the twoline endpoints and store the left endpoint in (x0 ,y0). 2. Load (x0 ,y0) into the frame buffer; that is, plot the first point. 3. Calculate constants Δx, Δy, 2Δy, and 2Δy - 2Δx, and obtain the starting value for the decision parameter as p0=2 Δy - Δx 4. At each xk along the line, starting at k = 0, perform the following test: If pk < 0, the next point to plot is (xk+1, yk) and pk+1=pk + 2Δy Otherwise, the next point to plot is (xk+1, yk+1) and pk+1=pk + 2Δy - 2Δx 5. Repeat step 4 Δx -1 times.

22 Trivial Situations: Do not need Bresenham

23 Example Draw the line with endpoints (20,10) and (30, 18).
Δx=30-20=10, Δy=18-10=8, p0 = 2Δy – Δx=16-10=6 2Δy=16, and 2Δy - 2Δx=-4 Plot the initial position at (20,10), then

24

25 Example Line end points: Deltas: initially

26 Graph 13

27 Continue the process... 13

28 Graph 13

29 Graph 13

30 Graph 13

31 /* Bresenham line-drawing procedure for |m| < 1.0. */
void lineBres (int x0, int y0, int xEnd, int yEnd) { int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0); int x, y, p = 2 * dy - dx; int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx); /* Determine which endpoint to use as start position. */ if (x0 > xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; setPixel (x, y); while (x < xEnd) { x++; if (p < 0) p += twoDy; y++; p += twoDyMinusDx;

32 Line-drawing algorithm should work in every octant, and special cases

33 Simulating the Bresenham algorithm in drawing 8 radii on a circle of radius 20
Horizontal, vertical and 45 radii handled as special cases

34 Circle / Curve Drawing in OpenGL
Routines for drawing circles or ellipses are not included in the OpenGL core library. Does include functions for displaying Bezier splines GLU (OpenGL Utility) library has some routines for drawing spheres, cylinders, B-splines. Rational B-splines can be used to display circles and ellipses. GLUT (OpenGL Utility Toolkit) has some routines to display 3D shapes such as cones, spheres All these discussed later…

35 Another method: Approximate it using a polyline.

36 Scan Converting Circles
Explicit: y = f(x) We could draw a quarter circle by incrementing x from 0 to R in unit steps and solving for +y for each step. Method needs lots of computation, and gives non-uniform pixel spacing

37 Scan Converting Circles
Parametric: Draw quarter circle by stepping through the angle from 0 to 90 avoids large gaps but still unsatisfactory How to set angular increment Computationally expensive trigonometric calculations

38 Scan Converting Circles
Implicit: f(x,y) = x2+y2-R2 If f(x,y) = 0 then it is on the circle. f(x,y) > 0 then it is outside the circle. f(x,y) < 0 then it is inside the circle. Try to adapt the Bresenham midpoint approach Again, exploit symmetries

39

40 Generalising the Bresenham midpoint approach
Set up decision parameters for finding the closest pixel to the cırcumference at each sampling step Avoid square root calculations by considering the squares of the pixel separation distances Use direct comparison without squaring. Adapt the midpoint test idea: test the halfway position between pixels to determine if this midpoint is inside or outside the curve This gives the midpoint algorithm for circles Can be adapted to other curves: conic sections

41 Eight-way Symmetry - only one octant’s calculation needed

42 The 2nd Octant is a good arc to draw
It is a well-defined function in this domain single-valued no vertical tangents: |slope|  1 Lends itself to the midpoint approach only need consider E or SE Implicit formulation F(x,y) = x 2 + y 2 – r 2 For (x,y) on the circle, F(x,y) = 0 F(x,y) > 0  (x,y) Outside F(x,y) < 0  (x,y) Inside

43 Choose E or SE Decision variable d is x 2 + y 2 – r 2
Then d = F(M)  0  SE Or d = F(M) <  E

44 F (M)  0  SE current pixel ideal curve

45 F (M) < 0  E ideal curve

46 Use the implicit form of the circle equation
Decision Variable p As in the Bresenham line algorithm we use a decision variable to direct the selection of pixels. Use the implicit form of the circle equation p = F (M ) = x 2 + y 2 – r 2

47 Midpoint coordinates are

48 Assuming we have just plotted point at (xk,yk) we determine whether move E or SE by evaluating the circle function at the midpoint between the two candidate pixel positions pk is the decision variable if pk <0 the midpoint is inside the circle Thus the pixel above the midpoint is closer to the ideal circle, and we select pixel on scan line yk. i.e. Go E

49 If pk >0 the midpoint is outside the circle.
Thus the pixel below the midpoint is closer to the ideal circle, and we select pixel on scan line yk-1. i.e. Go SE Calculate successive decision parameter values p by incremental calculations.

50 recursive definition for successive decision parameter values p
Where yk+1 = yk if p<0 (move E) yk+1 = yk-1 if p>0 (move SE) yk+1 and xk+1 can also be defined recursively

51 Initialisation x0 = 0, y0 = r Initial decision variable found by evaluating circle function at first midpoint test position For integer radius r p0 can be rounded to p0 =1-r since all increments are integer.

52

53 Midpoint Circle Algorithm (cont.)

54 Example r=10

55 Conic Sections A conic section, or conic, is any figure that can be formed by slicing a double cone with a plane Parabola Circle Ellipse Hyperbola

56 General equation of a Conic Section
Parabola: A = 0 OR B = 0 Circle: A = B Ellipse: AB, but both have the same sign Hyperbola: A and B have different signs

57 Ellipses P = (x,y) Ellipse generated about foci F1 and F2
d1 + d2 = constant

58 Scan Converting Ellipses
b a a is the length of the semi-major axis along the x axis. b is the length of the semi-minor axis along the y axis. The midpoint algorithm can also be applied to ellipses. For simplicity, we draw only the arc of the ellipse that lies in the first quadrant, the other three quadrants can be drawn by symmetry

59 Regions

60 Region 1

61 Region 2

62 Area Filling How can we generate a solid color/patterned polygon area?

63 An edge equations is the equations of the line defining the edges
Each line defines 2 half-spaces

64 Edge equations A triangle can be defined as the intersection of three positive half-spaces A2x + B2y + C2 < 0 A2x + B2y + C2 > 0 A3x + B3y + C3 < 0 A3x + B3y + C3 > 0 A1x + B1y + C1 > 0 A1x + B1y + C1 < 0

65 Edge Equations So…simply turn on those pixels for which all edge equations evaluate to > 0: + - Lots of implementation details to consider….

66

67 (Line Walking approach)
(When deciding if scan-line is inside the polygon)

68

69 Area Filling (Scan line Approach)
For each scan line (increasing y order) (1) Find intersections (the extrema of spans) (2) Sort intersections (increasing x order) (3) Fill in between pair of intersections

70 Data Structure

71

72

73

74

75

76

77 8

78

79 AF' AB FE' CD AB CB ED

80 Area Filling – for irregular areas
Paint the interior point by point out to the boundary. Pixel Adjacency 4-connected 8-connected

81 Area Filling – for irregular areas
Boundary-Fill Algorithm starting at a point inside the figure and painting the interior in a specified color or intensity. Used in interactive painting packages Like identifying connected components

82

83 the fill

84 Boundary Filling

85


Download ppt "Scan Conversion (From geometry to pixels)"

Similar presentations


Ads by Google