Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3.

Similar presentations


Presentation on theme: "+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3."— Presentation transcript:

1 + CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3

2 + Scan conversion Algorithms Primitives and Attributes Why Scan Conversion? Algorithms for Scan Conversion: Lines Circles Ellipses Filling Polygons 2

3 + Scan Conversion Problem To represent a perfect image as a bitmapped image. 3

4 + Line Drawing Algorithms Lines are used a lot - want to get them right. Lines should appear straight, not jagged. Horizontal, vertical and diagonal easy, others difficult Lines should terminate accurately. Lines should have constant density. Line density should be independent of line length or angle. Lines should be drawn rapidly. Efficient algorithms. 4

5 + DDA: Digital Differential Analyzer (x i,y i ) (x i,Round(y i )) (x i,Round(y i +m)) (x i,y i +m) Desired Line Line: Left to Right: 1- Slope m>0: sample at unit x intervals ( Δ x= 1), calculate each succeeding y value as 5

6 + DDA 2- Slope m<0: sample at unit y intervals ( Δy= 1), calculate each succeeding x value as Line: from Right to Left 3- Slope m> 0: 4- Slope m< 0: 6

7 + DDA Faster than brute force. Based on Calculating either ∆x or ∆y. Mathematically well defined Floating point Round off error. Time consuming arithmetic AdvantagesDisadvantages 7

8 + Bresenhams Line Algorithm Accurate Efficient Integer Calculations Uses Symmetry for other lines Adapted to display circles, ellipses and curves It has been proven that the algorithm gives an optimal fit for lines 8

9 + Bresenhams Line Algorithm d2 d1 X k +1 y k +1 y ykyk 9

10 + Bresenhams Line Algorithm 10

11 + Bresenhams Line Algorithm The sign of pk is the same as the sign of d1 – d2, since Δ x> 0 for our example. Parameter c is independent and will be eliminated in the recursive calculations for pk. If the pixel at yk is closer to the line path than the pixel at yk+l (that is, d1 < d2), then decision parameter pk is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixel. 11

12 + Bresenhams Line Algorithm This recursive calculation of decision parameters is performed at each integer x position, starting at the left coordinate endpoint of the line. The first parameter, p o is evaluated from Eq at the starting pixel position (x o, y o ) and with m evaluated as Δ y/ Δ x: 12

13 + Bresenhams Line Drawing Algorithm 1. Input the two line endpoints, store the left endpoint (x0,y0). 2. Plot the first point (x0,y0). 3. Calculate constants ∆x, ∆y, and 2∆y - 2∆x and 2∆y, get starting values for decision parameter p k, p 0 =2∆y-∆x 4. At each x k along the line, starting at k = 0, do the following test: if p k < 0, the next point to plot is(x k+1, y k ) p k+1 = p k + 2∆y else, the next point to plot is(x k+1, y k+1 ) p k+1 =p k +2∆y-2∆x 5. Repeat step 4. ∆x times. 13

14 + Bresenhams Line Algorithm 14

15 + Midpoint Line Algorithm If (BlueLine < Midpoint) Plot_East_Pixel(); Else Plot_Northeast_Pixel(); 15

16 + Find an equation, given a line and a point, that will tell us if the point is above or below that line? Midpoint Line Algorithm If F(x,y) ==0 (x,y) on the line <0 for points below the line >0 for points above the line d=F(M) 16

17 + Midpoint Line Algorithm P=(x p, y p ) is pixel chosen by the algorithm in previous step To calculate d incrementally we require d new If d > 0 then choose NE P=(x p, y p ) Y p +2 M E NE x p +1xpxp x p +2 Previous Current Next ypyp Y p +1 M NE 17

18 + Midpoint Line Algorithm If d < 0 then choose E P=(x p, y p ) M E NE x p +1xpxp x p +2 Previous Current Next ypyp Yp+1Yp+1 Y p +2 MEME 18

19 + Midpoint Line Algorithm To find Initial value of d P=(x 0, y 0 ) M E NE x 0 +1x0x0 Start Initial d o Only fractional value Multiply by 2 to avoid fractions. Redefine d 0,  E,  NE 19

20 + Midpoint Line Algorithm Midpoint: Looks at which side of the line the mid point falls on. Bresenham: Looks at sign of scaled difference in errors. It has been proven that Midpoint is equivalent to Bresenhams for lines. 20

21 + void 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; WritePixels(x, y, color); while (x < x1) { if (d <= 0) {// Current d d += dE;// Next d at E x++; } else { d += dNE;// Next d at NE x++; y++ } Write8Pixels(x, y, color);}} 21

22 + Midpoint Circle Algorithm Implicit of equation of circle is: x 2 + y 2 - R 2 = 0, at origin Eight way symmetry  require to calculate one octant For each pixel (x,y), there are 8 symmetric pixels In each iteration only calculate one pixel, but plot 8 pixels 22

23 + Midpoint Circle Algorithm Define decision variable d as: P=(x p, y p ) M E x p +1xpxp x p +2 Previous Current Next MEME ypyp y p – 1 y p – 2 SE M SE 23

24 + Midpoint Circle Algorithm If d <= 0 then midpoint m is inside circle we choose E Increment x y remains unchanged P=(x p, y p ) M E x p +1xpxp x p +2 Previous Current Next MEME ypyp y p – 1 y p – 2 d < 0 24

25 + Midpoint Circle Algorithm If d > 0 then midpoint m is outside circle we choose E Increment x Decrement y Previous P=(x p, y p ) M SESE x p +1xpxp x p +2 Current Next M SE ypyp y p – 1 y p – 2 d > 0 25

26 + Midpoint Circle Algorithm Initial condition Starting pixel (0, R) Next Midpoint lies at (1, R – ½) d 0 = F(1, R – ½) = 1 + (R 2 – R + ¼) – R 2 = 5 / 4 – R To remove the fractional value 5 / 4 : Consider a new decision variable h as, h = d – ¼ Substituting d for h + ¼, d 0 = 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 26

27 + 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); } 27

28 + Midpoint Circle Algorithm 28 Void CirclePoints(int x, int y, float value) { SetPixel(x,y); SetPixel(x,-y); SetPixel(-x,y); SetPixel(-x,-y); SetPixel(y,x); SetPixel(y,-x); SetPixel(-y,x); SetPixel(-y,-x); }

29 + Midpoint Circle Algorithm Second-order differences can be used to enhance performance. E is chosen SE is chosen M SE M SE E 29

30 + 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);} } 30

31 + Midpoint Ellipse Algorithm Implicit equation is: F(x,y) = b 2 x 2 + a 2 y 2 – a 2 b 2 = 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 (x 1,y 1 )(-x 1,y 1 ) (x 1,-y 1 )(-x 1,-y 1 ) (-x 2,y 2 ) (-x 2,-y 2 ) (x 2,y 2 ) (x 2,-y 2 ) 31

32 + Midpoint Ellipse Algorithm Region 1 Region 2 S SE E Gradient Vector Tangent Slope = -1 32

33 + Midpoint Ellipse Algorithm In region 1 P=(x p, y p ) M E xp+1xp+1 xpxp xp+2xp+2 Previous Current Next MEME ypyp y p – 1 y p – 2 SESE M SE 33

34 + Midpoint Ellipse Algorithm In region 2 P=(x p, y p ) M S xp+1xp+1 xpxp xp+2xp+2 Previous Current Next MSMS ypyp y p – 1 y p – 2 SESE M SE 34

35 + Midpoint Ellipse Algorithm DPx=2*ry*ry; Dpy =2*rx*rx; x=0; y=ry; Px=0; Py =2*rx*rx*ry; f =ry*ry +rx*rx(0.25-ry ); ry2=ry *ry; Set4Pixel(x,y); while (px0)// Bottom case {y=y -1; Py =Py -Dpy ; f=f+ry2+Px-Py;} else// Top case f=f+ry2+Px; Set4Pixel(x,y); 35


Download ppt "+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3."

Similar presentations


Ads by Google