Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scan Conversion Line and Circle

Similar presentations


Presentation on theme: "Scan Conversion Line and Circle"— Presentation transcript:

1 Scan Conversion Line and Circle
Computer Graphic Scan Conversion Line and Circle

2 Scan Conversion The process of representing a continuous graphical objects as a collection of discrete pixels by identifying their locations and setting them On is called scan conversion The process of determining which pixels will provide the best approximation to the desired line is properly known as rasterization. Combined with the process of rendering the picture in scan line order it is known as scan conversion.

3 Lines: A line is defined using a start point and an end-point
Points and Line Points: A point in two dimensional space is given as an ordered pair (x, y) In three dimensions a point is given as an ordered triple (x, y, z) Lines: A line is defined using a start point and an end-point In 2d: (xstart, ystart) to (xend, yend) In 3d: (xstart, ystart , zstart) to (xend, yend , zend)

4 x y (2, 7) (6, 7) The line from (2, 7) to (7, 3) (2, 3) (7, 3) (7, 1)

5 Simple Straight Line Equation
The slope-intercept Equation of a line is: where: The equation of the line gives us the corresponding y point for every x point

6 Example: Let’s draw a portion of the line given by the equation:
2 3 4 5 6 7

7 Digital Differential Analyser ( DDA) for a line
Let endpoints coordinates are (x1,y1 ) and (x2,y2) respectively , so we have A very simple procedure for scan converting a straight line is given as follows: Plot the first pixel at (x,y)= (x1,y1 ) increment the x- and y-values by dx and dy respectively to get the next pixel . We consider either dx or dy to be unity and compute the other . Plot pixel at (x+dx,y+dy) if dx=1 the Else dy=1 Continue the above steps until the final (x2,y2) is reached

8 DDA Algorithm for a line (x1,y1,x2,y2) Begin
dx=x2-x1, dy=y2-y1; if |dx|>|dy| then steps=|dx| else steps=|dy| Xinc=dx/steps Yinc=dy/steps x=x1; y=y1; setpixel(x1,y1); for k=1 to steps do begin x=x+Xinc; y=y+Yinc; setpixel (round(x),round(y)); end end /*of DDA/ Q1: For each step how many floating point operations are there? A:4 Q2: For each step how many integer operations are there? A: 2

9 Example1: Trace the DDA algorithm for drawing a line segment from (2,3) to (8,7)
solution : dx=8-2= dy=7-3=4 |dx|>|dy| then steps=|dx|=6 Xinc=dx/steps=1 Yinc=dy/steps =0.67 now starting with the point (2,3) ,generate the next points in sequence to reach the point (8,7). the computational steps are shown in table below Xold Yold Xnew Ynew Round(x) Round(y) 2 3 3.67 4 4.34 5 5.01 6 5.68 7 6.35 8 7.02

10 Example1: Trace the DDA algorithm for drawing a line segment from (2,3) to (8,10)
solution : dx=8-2= dy=10-3=7 |dy|>|dx| then steps=|dy|=7 Xinc=dx/steps=0.86 Yinc=dy/steps =1 now starting with the point (2,3) ,generate the next points in sequence to reach the point (8,10). the computational steps are shown in table below Xold Yold Xnew Ynew Round(x) Round(y) 2 3 2.86 4 3.72 5 4.58 6 5.44 7 6.3 8 7.16 9 8.02 10

11 Line Drawing Algorithm Drawbacks
DDA is the simplest line drawing algorithm but Not very efficient Round operation is expensive Optimized algorithms typically used. n Integer DDA n E.g.Bresenham’s algorithm

12 Bresenham’s algorithm
Uses only integer operations and does not use multiplication or division. Algorithm always increments by one unit in either X or Y depending on the slope of the line. The increment in the other variable, either zero or one, is determined by examining the distance (error) between the actual line location and the nearest grid locations.

13 Big Idea Move across the x axis in unit intervals and at each step choose between two different y coordinates For example, from position (2, 3) we have to choose between (3, 3) and (3, 4) We would like the point that is closer to the original line 5 (xk+1, yk+1) 4 (xk, yk) 3 (xk+1, yk) 2 2 3 4 5

14 Deriving The Bresenham Line Algorithm
At sample position xk+1 the vertical separations from the mathematical line are labelled dupper and dlower y yk yk+1 xk+1 dlower dupper The y coordinate on the mathematical line at xk+1 is:

15 So, dupper and dlower are given as follows:
We can use these to make a simple decision about which pixel is closer to the mathematical line

16 Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences between the end-points:
So, a decision parameter pk for the kth step along a line is given by: Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations.

17 At step k+1 the decision parameter is given as:
Subtracting pk from this we get: But, xk+1 is the same as xk+1 so: where yk+1 - yk is either 0 or 1 depending on the sign of pk The first decision parameter p0 is evaluated at (x0, y0) is given as:

18 BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0)
Input the two line end-points, storing the left end-point in (x0, y0) Plot the point (x0, y0) Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as: 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: Otherwise, the next point to plot is (xk+1, yk+1) and: Repeat step 4 (Δx ) times Q: In each step how many floating point operations are there? A: 0 Q: In each step how many integer operations are there? A: 3 or 4

19 Bresenham Algorithm Besgin: If(slope>1) Begin temp=dx;dx=dy;dy=temp
temp=x;x=y;y=temp temp=xf;xf=yf;yf=temp End P=2dy-dx Setpixel(x,y)

20 For k=1 to dx do Begin If(p<0) then If (x<xf) then x=x+1; Else x=x-1; p=p+2dy End Else Begin if(y<yf) then y=y+1 else y=y-1 else x=x-1; p=p+2(dy-dx); If (slope>1) then setpixel(y,x) Else setpixel(x,y) End;

21 Example 3: Trace the Bresenham’s algorithm for drawing the line segment from (2,3) to (8,7)
P0=2dy-dx=2*4-6=2; dx=6; dy=4 Pk<0pk+1=pk+2dy;xk+1=xk+1;yk+1=yk Pk>=0pk+1=pk+2dy-2dx; xk+1=xk+1; yk+1=yk+1 k xk yk pk Pk+1 Xk+1 Yk+1 2 3 -2 4 1 6 5 7 8 9

22 Drawing Circles 1-Drawing Circles Using Explicit Representation
Using Parametric Representation Midpoint Circle Drawing Algorithm 1-Drawing Circles Using Explicit Representation Very simple Considerable computation at each step. The spacing between plotted pixel positions is not uniform.

23

24 2- Drawing Circles Using Parametric Representation
Still considerable computation at each step.

25 Optimisation and speed-up
Symmetry of a circle can be used Calculations of point coordinates only for a first one-eighth of a circle (-x,y) (x,y) (-y,x) (y,x) (-y,-x) (y,-x) (-x,-y) (x,-y)

26 Mid-Point Circle Algorithm (Bresenham)
Assume that we have just plotted point (xk, yk) The next point is a choice between (xk+1, yk) and (xk+1, yk-1) We would like to choose the point that is nearest to the actual circle So how do we make this choice? (xk+1, yk) (xk+1, yk-1) (xk, yk)

27 Let’s re-jig the equation of the circle slightly to give us:
The equation evaluates as follows: By evaluating this function at the midpoint between the candidate pixels we can make our decision

28 Assuming we have just plotted the pixel at (xk,yk) so we need to choose between (xk+1,yk) and (xk+1,yk-1) Our decision variable can be defined as: If pk < 0 the midpoint is inside the circle and the pixel at yk is closer to the circle Otherwise the midpoint is outside and yk-1 is closer

29 (0, r)

30 MID-POINT CIRCLE ALGORITHM
Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a circle centred on the origin as: Calculate the initial value of the decision parameter as: Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next point along the circle centred on (0, 0) is (xk+1, yk) and: Otherwise the next point along the circle is (xk+1, yk-1) and:

31 Determine symmetry points in the other seven octants
Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values: Repeat steps 3 to 5 until x >= y

32 Midpoint Circle Algorithm. Example
p0=1-r=-9, (x0,y0)= (0,10), 2x0=0, 2y0=20 k pk (xk+1,yk+1) 2xk+1 2yk+1 1 2 3 4 5 6 -9 -6 -1 -3 8 (1,10) (2,10) (3,10) (4,9) (5,9) (6,8) (7,7) 10 12 14 20 18 16

33 Midpoint Circle Algorithm. Summary
As in Bresenham's line algorithm, the midpoint method calculates pixel positions along the circumference of a circle using integer additions and subtractions, assuming that the circle parameters are specified un integer screen Coordinates.


Download ppt "Scan Conversion Line and Circle"

Similar presentations


Ads by Google