80
Output Primitives Points and Lines Line Drawing Algorithms DDA Algorithm Bresenham’s Line Algorithm Midpoint Circle Algorithm Midpoint Ellipse Algorithm Filled Area Primitives

Output primitives in Computer Graphics

Embed Size (px)

Citation preview

Page 1: Output primitives in Computer Graphics

Output PrimitivesPoints and LinesLine Drawing AlgorithmsDDA AlgorithmBresenham’s Line AlgorithmMidpoint Circle AlgorithmMidpoint Ellipse AlgorithmFilled Area Primitives

Page 2: Output primitives in Computer Graphics

Points and Lines

• Point is the fundamental element of picture representation.

• It is the position in the plan defined as either pair or triplets of number depending upon the dimension.

• Two points represent line or edge and 3 or more points a polygon.

• Curved lines are represented by the short straight lines.

Page 3: Output primitives in Computer Graphics

Line Drawing Algorithms• Slope-Intercept Equation

• Slope

• Intercept

• Interval Calculation

bxmy .

12

12

xx

yym

11 .xmyb

xmy . m

yx

Page 4: Output primitives in Computer Graphics

Line Drawing Algorithm

Page 5: Output primitives in Computer Graphics
Page 6: Output primitives in Computer Graphics
Page 7: Output primitives in Computer Graphics

DDA Algorithm

Digital Differential Analyzer– Sample the line at unit intervals in one coordinate– Determine the corresponding integer values

nearest the line path in another co-ordinate

Page 8: Output primitives in Computer Graphics

DDA Algorithm (left to right)

• Slope

• For |m|<1 (|Δy|< |Δx|)– Sample line at unit interval in x co-ordinate

• For |m|>1 (|Δy|> |Δx|)– Sample line at unit interval in y co-ordinate

x

y

xx

yym

kk

kk

1

1

myy kk 1

mxx kk

11

11 kk xxx

11 kk yyy

Page 9: Output primitives in Computer Graphics

DDA Algorithm (right to left)

• Slope

• For |m|<1 (|Δy|< |Δx|)– Sample line at unit interval in x co-ordinate

• For |m|>1 (|Δy|> |Δx|)– Sample line at unit interval in y co-ordinate

myy kk 1

mxx kk

11

11 kk xxx

11 kk yyy

x

y

xx

yym

kk

kk

1

1

Page 10: Output primitives in Computer Graphics

DDA Algorithm1. Input the two line endpoints and store the left endpoint in (x0,y0)2. Plot first point (x0,y0)3. Calculate constants Δx, Δy4. If |Δx| > |Δy| steps = |Δx| else steps = |Δy|5. Calculate XInc = |Δx| / steps and YInc = |Δy| / steps 6. At each xk along the line, starting at k=0, Plot the next pixel at (xk + XInc, yk +

YInc)7. Repeat step 6 steps times

Page 11: Output primitives in Computer Graphics

Pseudo CodeVoid lineDDA(int xa, int ya, int xb, int yb){

int dx = xb – xa, dy = yb – ya, steps, k;float xIncrement, yIncrement, x = xa, y = ya;

if( abs (dx) > abs (dy) ) steps = abs (dx);else steps = abs (dy);xIncrement = dx / (float) steps;yIncrement = dy / (float) steps;setPixel (ROUND (x), ROUND (y));for (k=0; k<steps; k++){

x += xIncrement;y += yIncrement;setPixel (ROUND(x), ROUND(y));

}}

Page 12: Output primitives in Computer Graphics
Page 13: Output primitives in Computer Graphics
Page 14: Output primitives in Computer Graphics

• Use DDA algorithm for rasterizing line (0,0) to (6,6).

• Use DDA algorithm for rasterizing line (0,0) to (4,6).

Page 15: Output primitives in Computer Graphics
Page 16: Output primitives in Computer Graphics
Page 17: Output primitives in Computer Graphics
Page 18: Output primitives in Computer Graphics

Bresenham’s Line Algorithm

• Uses only incremental integer calculations

• Which pixel to draw ?– (11,11) or (11,12) ?– (51,50) or (51,49) ?– Answered by Bresenham

Page 19: Output primitives in Computer Graphics

• For |m|<1– Start from left end point (x0,y0) step to each

successive column (x samples) and plot the pixel whose scan line y value is closest to the line path.

– After (xk,yk) the choice could be (xk+1,yk) or (xk+1,yk+1)

Page 20: Output primitives in Computer Graphics

Then

And

Difference between separations

bxmy k )1(

kyyd 1kk ybxm )1(

yyd k )1(2

bxmy kk )1(1

122)1(221 byxmdd kk

Page 21: Output primitives in Computer Graphics

Defining decision parameter [1]

Sign of pk is same as that of d1-d2 for Δx>0 (left to right sampling)

For Recursive calculation, initially

cyxxy kk .2.2

Constant=2Δy + Δx(2b-1) Which is independent of pixel position

cyxxyp kkk 111 .2.2

)(2)(2 111 kkkkkk yyxxxypp

c eliminated here

)(22 11 kkkk yyxypp

because xk+1 = xk + 1

yk+1-yk = 0 if pk < 0yk+1-yk = 1 if pk ≥ 0

xyp 20 Substitute b = y0 – m.x0

and m = Δy/Δx in [1]

)( 21 ddxpk

Page 22: Output primitives in Computer Graphics

Algorithm Steps (|m|<1)1. Input the two line endpoints and store the left endpoint in (x0,y0)2. Plot first point (x0,y0)3. Calculate constants Δx, Δy, 2Δy and 2 Δy- 2Δx, and obtain p0 = 2Δy – Δx4. At each xk along the line, starting at k=0, perform the following test:

If pk<0, the next point plot is (xk+1,yk) and Pk+1 = pk + 2Δy

Otherwise, the next point to plot is (xk + 1, yk+1) andPk+1 = pk + 2Δy - 2Δx

5. Repeat step 4 Δx times

Page 23: Output primitives in Computer Graphics

What’s the advantage?

• Answer: involves only the calculation of constants Δx, Δy, 2Δy and 2Δy- 2Δx once and integer addition and subtraction in each steps

Page 24: Output primitives in Computer Graphics

ExampleEndpoints (20,10) and (30,18)Slope m = 0.8Δx = 10, Δy = 8P0 = 2Δy - Δx = 6

2Δy = 16, 2Δy-2Δx = -4

Plot (x0,y0) = (20,10)

Page 25: Output primitives in Computer Graphics

• Use Bresenham’s algorithm for rasterizing the line (5,5) to (13,9)

Page 26: Output primitives in Computer Graphics
Page 27: Output primitives in Computer Graphics

• Digitize a line with endpoints (15,18) and (10,15) using BLA.

Page 28: Output primitives in Computer Graphics
Page 29: Output primitives in Computer Graphics

• Digitize a line with endpoints (15,15) and (10,18) using BLA.

Page 30: Output primitives in Computer Graphics
Page 31: Output primitives in Computer Graphics

Algorithm Steps (|m|>1)1. Input the two line endpoints and store the left endpoint in (x0,y0)2. Plot first point (x0,y0)3. Calculate constants Δx, Δy, 2Δx and 2 Δx- 2Δy, and obtain p0 = 2Δx – Δy4. At each xk along the line, starting at k=0, perform the following test:

If pk<0, the next point plot is (xk, yk+1) and Pk+1 = pk + 2Δx

Otherwise, the next point to plot is (xk + 1, yk+1) andPk+1 = pk + 2Δx - 2Δy

5. Repeat step 4 Δx times

Page 32: Output primitives in Computer Graphics

• Use BLA algorithm for rasterizing line (0,0) to (4,6).

Page 33: Output primitives in Computer Graphics

Circle-Generating Algorithms (Basic Foundations)

• Circle Equation:

• Points along circumference could be calculated by stepping along x-axis:

222 )()( ryyxx cc

22 )( xxryy cc

Page 34: Output primitives in Computer Graphics

Problem (in above method)

• Computational complexity• Spacing:– Non-uniform spacing of

plotted pixels

Page 35: Output primitives in Computer Graphics

Adjustments (To fix problems)• Spacing problem (2 ways):

– Interchange the role of x and y whenever the absolute value of the slope of the circle tangent > 1

– Use polar co-ordinate:

• Equally spaced points are plotted along the circumference with fixed angular step size.

• step size chosen for θ depends on the application and display device.• Computation Problem:

– Use symmetry of circle; i.e calculate for one octant and use symmetry for others.

sin

cos

ryy

rxx

c

c

Page 36: Output primitives in Computer Graphics

Circle Symmetry

Page 37: Output primitives in Computer Graphics

Bresenham’s Algorithm Could Be Adapted ??

• Yes• How ?– Setting decision parameter for finding the closest

pixel to the circumference• And what to do For Non-linear equation of

circle ?– Comparison of squares of the pixel separation

distance avoids square root calculations

Page 38: Output primitives in Computer Graphics

Midpoint Circle Algorithm

• Circle function defined as:

• Any point (x,y) satisfies following conditions

boundarycircletheoutsideisyxif

boundarycircletheonisyxif

boundarycircletheinsideisyxif

yxfcircle),(,0

),(,0

),(,0

),(

222),( ryxyxfcircle

Page 39: Output primitives in Computer Graphics

• Decision parameter is the circle function; evaluated as:

222 )2

1()1(

)2

1,1(

ryx

yxfp

kk

kkcirclek

1)()()1(2

)2

1(]1)1[(

)2

1,1(

122

11

221

2

111

kkkkkkk

kk

kkcirclek

yyyyxpp

ryx

yxfp

Page 40: Output primitives in Computer Graphics

yk+1 = yk if pk<0yk+1 = yk-1 otherwise

• ThusPk+1 = Pk + 2xk+1+1 if pk<0Pk+1 = Pk + 2xk+1+1-2yk+1 otherwise

• Also incremental evaluation of 2xk+1 and 2yk+1

2xk+1 = 2xk + 22yk+1 = 2yk – 2 if pk >0

• At start position (x0,y0) = (0,r)2x0 = 0 and 2y0 = 2r

Page 41: Output primitives in Computer Graphics

• Initial decision parameter

• For r specified as an integer, round p0 to

P0 = 1-r

(because all increments are integers)

r

rr

rfp circle

4

5)2

1(1

)2

1,1(

22

0

Page 42: Output primitives in Computer Graphics

Algorithm1. Input radius r and circle center (xc, yc) and obtain the first point on the circumference of

a circle centered on the origin as(x0,y0) = (0,r)

2. Calculate the initial value of the decision parameter asP0 = 5/4 – r

3. At each xk position, starting at k = 0, perform the following test:If pk < 0, the next point along the circle centered on (0,0) is (xk+1,yk) and

Pk+1 = pk + 2xk+1 + 1Otherwise, the next point along the circle is (xk+1,yK-1) and

Pk+1 = pk + 2xk+1 + 1 -2yk+1

Where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk-24. Determine the symmetry points in the other seven octants.5. Move each calculated pixel position (x,y) onto the circular path

centered on (xc,yc) and plot the co-ordinate values:x = x + xc, y = y+yc

6. Repeat steps 3 through 5 until x ≥ y

Page 43: Output primitives in Computer Graphics

• Given radius =10 use mid-point circle drawing algorithm to determine the pixel in the first quadrant.

Solution:P0 = 1-r = -9(x0 , y0)=(0,10)2x0 =02y0 =20

Page 44: Output primitives in Computer Graphics
Page 45: Output primitives in Computer Graphics
Page 46: Output primitives in Computer Graphics

• Digitize the circle (x-2)^2+(y-3)^2=25 in first quadrant.

Solution:P0=(1-r)= -4

(x0 , y0)=(0,5)2x0 =02y0 =10

Page 47: Output primitives in Computer Graphics
Page 48: Output primitives in Computer Graphics
Page 49: Output primitives in Computer Graphics
Page 50: Output primitives in Computer Graphics

Ellipse Generating Algorithms

• Equation of ellipse:

• F1(x1,y1), F2(x2,y2)

• General Equation

• Simplified Form

• In polar co-ordinate

constantyyxxyyxx 22

22

21

21 )()()()(

constantdd 21

022 FEyDxCxyByAx

1

22

y

c

x

c

r

yy

r

xx

sincosyc

xcryyrxx

Page 51: Output primitives in Computer Graphics

• Ellipse function222222),( yxyellipse rryrxryxf x

boundaryellipsetheoutsideisyxifboundaryellipsetheonisyxifboundaryellipsetheinsideisyxif

yxfellipse),(,0),(,0),(,0

),(

Page 52: Output primitives in Computer Graphics

• From ellipse tangent slope:

• At boundary region (slope = -1):

• Start from (0,ry), take x samples to boundary between 1 and 2

• Switch to sample y from boundary between 1 and 2 (i.e whenever )

yr

xr

dx

dy

x

y

2

2

2

2

yrxr xy22 22

yrxr xy22 22

Slope=-1

Page 53: Output primitives in Computer Graphics

• In the region 1

• For next sample

• Thus increment

222222 )2

1()1(

)2

1,1(1

yxkxky

kkellipsek

rryrxr

yxfp

22

1222

1

2221

222

111

2

1

2

1)1(211

)2

1(1)1(

)2

1,1(1

kkxykykk

yxkxky

kkellipsek

yyrrxrpp

rryrxr

yxfp

01,2201,2

122

12

21

2

kkxyky

kyky

pifyrrxrpifrxr

increment

•For increment calculation; Initially:

•Incrementally:Update x by adding 2ry

2 to first equation and update y by subtracting 2rx

2 to second equation

yxx

y

rryrxr

22

2

2202

Page 54: Output primitives in Computer Graphics

• Initial value

2220

222

22

0

4

11

2

12

1,11

xyxy

yxyxy

yellipse

rrrrp

rrrrr

rfp

(0,ry)

Page 55: Output primitives in Computer Graphics

• In the region 2

• For next sample

• Initially

222222 )1()2

1(

)1,2

1(2

yxkxky

kkellipsek

rryrxr

yxfp

22

1222

1

22222

12

111

2

1

2

1)1(222

112

1

)1,2

1(2

kkyxkxkk

yxkxky

kkellipsek

xxrryrpp

rryrxr

yxfp

2220

22

02

0

000

)1(2

12

1,2

12

yxxy

ellipse

rryrxrp

yxfp

For simplification calculation of p20 can be done by selecting pixel positions in counter clockwise order starting at (rx,0) and unit samples to positive y direction until the boundary between two regions

Page 56: Output primitives in Computer Graphics

Algorithm1. Input rx,ry, and the ellipse center(xc,yc) and obtain the first point on an ellipse centered on the origin as

(x0,y0) = (0,ry)2. Calculate the initial value of the decision parameter in region 1 as

3. At each xk position in region 1, starting at k = 0, perform the following test: If p1k < 0, the next point along the ellipse centered on (0,0) is (xk+1,yk) and

Otherwise, the next point along the ellipse is (xk+1,yk-1) and

With

and continue until

2220 4

11 xyxy rrrrp

21

21 211 ykykk rxrpp

21

21

21 2211 ykxkykk ryrxrpp

221

2221

2 222,222 xkxkxykyky ryryrrxrxr

yrxr xy22 22

Page 57: Output primitives in Computer Graphics

4. Calculate the initial value of decision parameter in region 2 using the last point (x0,y0) calculated in region 1 as

5. At each yk position in region 2, starting at k = 0, perform the following test: If p2k>0, the next point along the ellipse centered on (0,0) is (xk,yk-1) and

Otherwise the next point along the ellipse is (xk+1,yk-1) and

Using the same incremental calculations for x and y as in region 1.6. Determine the symmetry points in the other three quadrants.7. Move each calculated pixel position (x,y) onto the elliptical path centered on (xc,yc) and plot the co-ordinate

values:X = x + xc, y = y+ yc

8. Repeat the steps for region 1 until

2220

22

02

0 )1(2

12 yxxy rryrxrp

21

21 222 xkxkk ryrpp

21

21

21 2222 xkxkykk ryrxrpp

yrxr xy22 22

Page 58: Output primitives in Computer Graphics

• Digitize the ellipse with parameter rx =8 and ry =6 in first quadrant.

Solution:

Page 59: Output primitives in Computer Graphics
Page 60: Output primitives in Computer Graphics
Page 61: Output primitives in Computer Graphics
Page 62: Output primitives in Computer Graphics

Filled Area Primitives

• Two basic approaches for area filling:– By determining overlap intervals for scan lines– Start from given interior position and filling

outwards

Page 63: Output primitives in Computer Graphics

Scan Line Polygon Filled Algorithm

• Intersection points of scan line with the polygon edge are calculated.

• Points are sorted from left to right.• Corresponding frame buffer position between

each intersection pair are set by specified color.

Page 64: Output primitives in Computer Graphics
Page 65: Output primitives in Computer Graphics

• A scan line pass through vertex, intersect two polygon edges.

Page 66: Output primitives in Computer Graphics

• Scan line y’– Intersect even number of edges– 2 pairs of intersection points correctly find the

interior span• Scan line y– Intersect an odd number of edges(5)– Must count the vertex intersection as only one

point

Page 67: Output primitives in Computer Graphics

How to distinguish these cases?

• Scan line y– Intersecting edges are on the opposite side

• Scan line y’– Intersecting edges are on the same side.

• By tracing around the boundary,– If the endpoint y values of two consecutive edges

monotonically increases or decreases count middle vertex as single

Page 68: Output primitives in Computer Graphics

Implementation

Page 69: Output primitives in Computer Graphics
Page 70: Output primitives in Computer Graphics

• In determining edge intersection, we can set up incremental set up calculation using fact that slope of edge is constant.

Page 71: Output primitives in Computer Graphics
Page 72: Output primitives in Computer Graphics
Page 73: Output primitives in Computer Graphics

Inside Outside test

• Odd Even rule:– Odd edge means interior

• Non zero winding number rule– Non zero winding number means interior

Page 74: Output primitives in Computer Graphics
Page 75: Output primitives in Computer Graphics

Scan line fill for Curved Area

• Require more works than polygon filling due to non-linear boundary.

• We calculate the scan line intersection and fill all the interior points.

• Symmetries between the quadrant can be used to reduce the calculation.

Page 76: Output primitives in Computer Graphics

Boundary fill algorithm

• Fill the region starting from the interior point until the appropriate color boundary is reached.

• Two ways:– 4 connected– 8 connected

Page 77: Output primitives in Computer Graphics
Page 78: Output primitives in Computer Graphics

4 connected

Page 79: Output primitives in Computer Graphics

8 connected

Page 80: Output primitives in Computer Graphics

Using 4 connected flood fill algorithm