Computer Graphic Scan Conversion Line and Circle.

Post on 11-Jan-2016

242 views 3 download

Tags:

transcript

Computer Graphic

Scan Conversion

Line and Circle

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.

Scan Conversion

Points and Line

Points:o A point in two dimensional space is given as an

ordered pair (x, y)o 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-pointo In 2d: (xstart, ystart) to (xend, yend)

o In 3d: (xstart, ystart , zstart) to (xend, yend , zend)

x

y

(2, 3)

(6, 7)

(7, 1)

(7, 3)

(2, 7)

The line from (2, 7) to (7, 3)

The slope-intercept Equation of a line is:

where:

The equation of the line gives us the corresponding y point for every x point

01

01

xx

yy

x

ym

00 xmyb

bxmy

Simple Straight Line Equation

Example: Let’s draw a portion of the line given by the equation:

5

4

5

3 xy

5

32

5

43

5

3)3( y

5

13

5

44

5

3)4( y

5

43

5

45

5

3)5( y

5

24

5

46

5

3)6( y

55

47

5

3)7( y

25

42

5

3)2( y

x

y

2 3 4 5 6 7

2

5

Digital Differential Analyser ( DDA) for a lineLet endpoints coordinates are (x1,y1 ) and (x2,y2) respectively , so we have

12

12

xx

yy

x

ym

ym

xxmy 1

or

A very simple procedure for scan converting a straight line is given as follows:

1. Plot the first pixel at (x,y)= (x1,y1 )

2. 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

3. Continue the above steps until the final (x2,y2) is reached

myy ii 1

mxx ii

11

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/

Example1: Trace the DDA algorithm for drawing a line segment from (2,3) to (8,7)

solution : dx=8-2=6 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 3.67 3 4

3 3.67 4 4.34 4 4

4 4.34 5 5.01 5 5

5 5.01 6 5.68 6 6

6 5.68 7 6.35 7 6

7 6.35 8 7.02 8 7

Example1: Trace the DDA algorithm for drawing a line segment from (2,3) to (8,10)

solution : dx=8-2=6 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 4

2.86 4 3.72 5 4 5

3.72 5 4.58 6 5 6

4.58 6 5.44 7 5 7

5.44 7 6.3 8 6 8

6.3 8 7.16 9 7 9

7.16 9 8.02 10 8 10

Line Drawing Algorithm Drawbacks

DDA is the simplest line drawing algorithm but Not very efficient Round operation is expensiveOptimized algorithms typically used.Integer DDAE.g.Bresenham’s algorithm

1.Uses only integer operations and does not use multiplication or division.

2. Algorithm always increments by one unit in either X or Y depending on the slope of the line.

3. 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.

Bresenham’s algorithm

Move across the x axis in unit intervals and at each step choose between two different y coordinates

Big Idea

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

2 3 4 5

2

4

3

5

(xk, yk)

(xk+1, yk)

(xk+1, yk+1)

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:

bxmy k )1(

So, dupper and dlower are given as follows:

and:

We can use these to make a simple decision about which pixel is closer to the mathematical line

klower yyd

kk ybxm )1(

yyd kupper )1(

bxmy kk )1(1

122)1(2 byxmdd kkupperlower

Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences between the end-points:

)122)1(2()(

byxx

yxddx kkupperlower

)12(222 bxyyxxy kk

cyxxy kk 22So, a decision parameter pk for the kth step along a line

is given by:

cyxxy

ddxp

kk

upperlowerk

22

)(

Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations.

At step k+1 the decision parameter is given as:

cyxxyp kkk 111 22

Subtracting pk from this we get:

)(2)(2 111 kkkkkk yyxxxypp

But, xk+1 is the same as xk+1 so:

)(22 11 kkkk yyxypp

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:

xyp 20

BRESENHAM’S LINE DRAWING ALGORITHM(for |m| < 1.0)

1. Input the two line end-points, storing the left end-point in (x0, y0)

2. Plot the point (x0, y0)

3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as:

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:

Otherwise, the next point to plot is (xk+1, yk+1) and:

5. Repeat step 4 (Δx ) times

xyp 20

ypp kk 21

xypp kk 221

Bresenham Algorithm Besgin:

;

;

12

12

yydy

xxdx

dx

dyslope

yyxxyyxx ff

2211 ;;;

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)

For k=1 to dx do

Begin

If(p<0) then

Begin

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

If (x<xf) then x=x+1;

else x=x-1; p=p+2(dy-dx);

End

If (slope>1) then setpixel(y,x)

Else setpixel(x,y)

End;

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<0pk+1=pk+2dy;xk+1=xk+1;yk+1=yk

Pk>=0pk+1=pk+2dy-2dx; xk+1=xk+1; yk+1=yk+1

k xk yk pk Pk+1 Xk+1 Yk+1

0 2 3 2 -2 3 4

1 3 4 -2 6 4 4

2 4 4 6 2 5 5

3 5 5 2 -2 6 6

4 6 6 -2 6 7 6

5 7 6 6 2 8 7

6 8 7 2 -2 9 8

Drawing Circles

1. Using Explicit Representation

2. Using Parametric Representation

3. Midpoint Circle Drawing Algorithm

1-Drawing Circles Using Explicit Representation

22

222

xc)(xrycy

ryc)(yxc)(x

Very simple

Considerable computation at each step.

The spacing between plotted pixel positions is not uniform.

20020 220 y

20120 221 y

20220 222 y

61920 2219 y

02020 2220 y

2- Drawing Circles Using Parametric Representation

rsinθycy

rcosθxcx

xc

ycr

θ

Still considerable computation at each step.

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)

(y,x)

(x,-y)

(y,-x)

(-x,y)

(-y,x)

(-y,-x)

(-x,-y)

(xk+1, yk)

(xk+1, yk-1)

(xk, yk)

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?

Mid-Point Circle Algorithm (Bresenham)

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

222),( ryxyxfcirc

,0

,0

,0

),( yxfcirc

boundary circle theinside is ),( if yx

boundary circle on the is ),( if yx

boundary circle theoutside is ),( if yx

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

222 )21()1(

)21,1(

ryx

yxfp

kk

kkcirck

222 )2

1()1()

2

1,1( ryxyxfplet kkkkk

otherwise 1

0

1

)2

1()2()

2

1,1(

1

1

221

2111

k

kkk

kk

kkkkk

y

pifyy

xxwhere

ryxyxfp

otherwise 5)(2

0 321

kkk

kkkk yxp

pifxpp

),0(),( since4

5)

2

1()10()

2

1,1(

00

222000

ryx

rrryxfp

(0, r)

MID-POINT CIRCLE ALGORITHM

1. 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:

2. Calculate the initial value of the decision parameter as:

3. 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:

),0(),( 00 ryx

rrp 145

0

321 kkk xpp

5)(21 kkkk yxpp

4. Determine symmetry points in the other seven octants

5. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values:

6. Repeat steps 3 to 5 until x >= ycxxx cyyy

k pk (xk+1,yk+1) 2xk+1 2yk+1

0

1

2

3

4

5

6

-9

-6

-1

6

-3

8

5

(1,10)

(2,10)

(3,10)

(4,9)

(5,9)

(6,8)

(7,7)

2

4

6

8

10

12

14

20

20

20

18

18

16

14

r=10,p0=1-r=-9,(x0,y0)= (0,10),2x0=0, 2y0=20

Midpoint Circle Algorithm. Example

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.

Midpoint Circle Algorithm. Summary