31
TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Embed Size (px)

Citation preview

Page 1: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

TOPIC 4GRAPHICS OUTPUT PRIMITIVES ( I I )

CGMB214: Introduction to Computer Graphics

Page 2: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Objectives

To understand circle-drawing algorithms and line function

Page 3: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Other Output Primitives

Circle

Ellipse

Curves

Page 4: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Circles

Is a set of points from a given distance of r from center position (xC, yC).

Is divided into 4 quadrants and 8 octants

Symmetrical is used to reduce the number of calculations

r

(xC, yC)

Page 5: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Circles

Cartesian Coordinat

es

Midpoint Circle

Algorithm

Polar Coordinat

es

Page 6: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Cartesian Coordinates

Also known as square root methodDistance relationship expressed by the

Pythagorean theorem in Cartesian coordinates as:

(x-xc)2 – (y-yc)

2 = r2

So, when stepping along the x axis in units intervals from xc to xc+ r and calculate the corresponding y co-ordinate with

Page 7: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Cartesian Coordinates

r

(x, y)

xc

yc

x

y

Page 8: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

7. Seventh point (x):y=10 (92 – (10-4)2)

y=16.70, y=3.298. Eight point (x):

y=10 (92 – (10-3)2)y=15.65, y=4.34

9. Ninth point (x):y=10 (92 – (10-2)2)y=14.12, y=5.87

10. Tenth point (x):y=10 (92 – (10-1)2)y=10

The process will repeat

for increasing x

Cartesian Coordinates

10

10

x

y

Given xc=10, yc=10, r=91. First point is at

(10,19)2. Second point (x):

y=10 (92 – (10-9)2)y=18.94, y=1.05

3. Third point (x): y=10 (92 – (10-8)2)y=18.77, y=1.22

4. Fourth point (x):y=10 (92 – (10-7)2)y=18.48, y=1.51

5. Fifth point (x):y=10 (92 – (10-6)2)

y=18.06, y=1.936. Sixth point (x):

y=10 (92 – (10-5)2)

y=17.48, y=2.52

y = yc r2 – (xc-x)2

Page 9: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Cartesian Coordinates

Disadvantages: Considerable amount of calculation (squares and square roots) Spacing between pixel is not uniform around the circle Based on the upper octant (between y axis and 45o line), circles

increases faster along x axis compared to y axis. Since we are stepping along x axis, pixels with successive x co-ordinate positions can have the same y coordinate – reduce or no gap

In the lower octant , the circle increases faster along the y axis compared to x axis. Pixels with successive y co-ordinates need to have the same x co ordinates to avoid gaps. Since , we are using x axis in unit of intervals, so each pixel has a unique y-coordinates – bigger gap.

Page 10: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Polar Coordinates

Also known as Trigonometric FunctionsThe equation of a circle is written in the

polar coordinates r and as x = xc + r.cosy = yc + r.sin 

x and y are the coordinates of a point on the circumference, i.e the end point of a line drawn from the centre of the circle to the circumference – the radius.

Page 11: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Polar Coordinates

Example: Given P(2,2) is a center point of a circle, draw the circle with radius = 4x=2+4(cos 0) = 6y=2+4(sin 0)= 2

x=2+4(cos 45) = 4.8y=2+4(sin 45)= 4.8

x=2+4(cos 90) = 2y=2+4(sin 90)= 6

x=2+4(cos 135) =-0.8 y=2+4(sin 135)= 4.8

x=2+4(cos 180) = -2y=2+4(sin 180)= 2

x=2+4(cos 225) =-0.8y=2+4(sin 225)=-0.8

x=2+4(cos 270) = 2y=2+4(sin 270)= -2

x=2+4(cos 315) = 4.8y=2+4(sin 315)= -0.8

Page 12: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Polar Coordinates

3

4 2

5 1

6 8

7

7

6

5

4

3

2

1

0

-1

-2

-2 -1 0 1 2 3 4 5 6 7

Page 13: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

To avoid rounding where incrementing integer for calculation is faster and more efficient.

Center is not always at (0,0)In this algorithm, parameter P is used to determine

the y co-ordinate of the next pixel to plot. Decision parameter P is based on circle function f

that determines whether pixel is inside the boundary on the circle boundary outside the circle boundary

fcircle = x2 + y2 – r2

Bresenham Algorithm (Mid Point Algorithm)

Page 14: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Step along the x axis in unit intervals (k) from k=0 to k=r

Plot the first point (0,r)From (0,r) then we need to decide whether to plot

on (xk+1,yk) or (xk+1, yk-1)The decision parameter P is the circle function

evaluated at the mid-point between these two pixels.

P = fcircle(xk+1,yk – 0.5)

Bresenham Algorithm (Mid Point Algorithm)

Page 15: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Bresenham Algorithm (Mid Point Algorithm)

If P < 0 The mid-point is inside the boundary, so the pixel

at (xk+1,yk) is closer to the circle boundary and should be plotted.

P is then incremented by 2(x) + 1.If P= 0 or P > 0

The mid-point is outside or on the circle boundary. So the pixel at (xk+1,yk-1) is closer to the circle boundary and need to be plotted.

P is then incremented by 2(x-y) + 1.

Page 16: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Bresenham Algorithm (Mid Point Algorithm)

We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0).

Procedure Circle_Points(x,y :Integer);Begin

Plot(x,y);Plot(y,x);Plot(y,-x);Plot(x,-y);Plot(-x,-y);Plot(-y,-x);Plot(-y,x);Plot(-x,y)

End;

(a,b)

(b,a)

(a,-b)

(b,-a)

(-a,-b)

(-a,b)

(-b,-a)

(-b,a)

Page 17: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Ellipses

An ellipses is an elongated circle and can be drawn with modified circle drawing algorithm.

An ellipse has set of fixed points (foci) that will have a constant total of distance from all the points on the boundary.

An ellipse has 2 axesMajor: straight line running through the two foci

and the long axis.Minor: straight line running through the centre

that bisects the major axis – the short axis.

Page 18: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Ellipse symmetry Ellipses are symmetrical between the four quadrants. Ellipses drawing algorithm only need to calculate the

pixels for one quadrant.The general ellipse algorithm in Cartesian co-

ordinates isAx2 + By2 + Cxy + Dx + Ey + F = 0

As for circles, algorithms that use Cartesian or polar co-ordinates are computationally expensive.

Ellipses

Page 19: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

A modification of the mid-point circle algorithmIncremental integer calculations.Calculations are easier if:

the circle is centered on the origin (0,0) points are placed in the correct position by

adding xc to the x co-ordinates and y, to the y co-ordinates. ie performing a translation

the major and minor axes are aligned to be parallel with x and y axes. ellipses are drawn at the correct angle by

rotating the points to their correct position.

Mid-Point Ellipses Algorithm

Page 20: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

A decision parameter P is used to determine the y co-ordinate of the next pixel or plot.

Decision parameter P is based on ellipse function f that determines whether a pixel is inside the ellipse boundary on the ellipse boundary outside the ellipse boundary

Page 21: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

fellipse(x,y) < 0 if(x,y) is inside the ellipse boundary

= 0 if(x,y) is on the ellipse boundary

> 0 if(x,y) is outside the ellipse boundary.

Need to divide the quadrant into two regions and process the pixel in each region separately In region 1 the ellipse increases faster along the x axis than y axis In region 2 the ellipse increases faster along the y axis

than x axis

Plot the first point (0,r)

Page 22: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

Region 1: Step along the x axis in unit intervals (k) until

the boundary between region 1 and region 2. If we have just plotted pixel(xk,yk), the

choices for the next pixel to plot are (xk + 1, yk) and (xk + 1, yk– 1)

The decision parameter P is the ellipse function evaluated at the mid-point between these two pixels.

P=fellipse(xk+1 , yk – 0.5)

Page 23: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

The initial value of the decision parameter in Region 1

p0 = r2y –r2

xry + 0.25r2x

Page 24: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

If Pk < 0 The mid-point is inside the ellipse boundary so the

pixel at (xk + 1, yk) ) is closer to the ellipse boundary and should be plotted.

Pk+1 = Pk + 2r2yxk+1 + r2y

If Pk=0 or Pk>0 The mid-point is outside or on the ellipse boundary

pixel at (xk+1,yk-1) is closer to the ellipse boundary and should be plotted.

Pk+1 = P k + 2r2yxk+1 - 2r2xyk+1 + r2y

Page 25: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Mid-Point Ellipses Algorithm

Region 2: Step along the y axis in unit intervals (k) for the

remainder of the curve until y = 0 If we have just plotted pixel (xk,yk), the choices

for the next pixel to plot are (xk,yk-1) and (xk+1,yk-1)

The decision parameter P is the ellipse function evaluated at the mid-point between these

two pixels.

P = fellipse(xk+0.5 , yk –1)

Page 26: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

(X,0)(-X,0)

(0,Y)

(0,-Y)

(X/¦2, Y/¦2)

(X/¦2, -Y/¦2)(-X/¦2, -Y/¦2)

(-X/¦2, Y/¦2)

Mid-Point Ellipses Algorithm

The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used.

All the points in one quadrant must be computed. Since Bresenham's algorithm is restricted to only one octant, the computation must occur in two stages. The changeover occurs when the point on the ellipse is reached where the tangent line has a slope of ±1. In the first quadrant, this is when the x and y coordinates are both at 0.707 of their maximum.

Page 27: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

G .gravity

V Velocity

Vo

Xo

Yo

Other Curves

Conic sections such as Parabolas

determined by initial velocity v0 and acceleration g (gravity) y = yo + a(x-xo)

2 + b(x-x0)

a and b are constants determined by parameter

t (time – in second) x = x0 + vx0t

y = y0 + vyot – 0.5gt2

Page 28: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Parallel Line Algorithms DDA and Bresenham determine pixel positions

sequentially and faster with parallel computerLine Segmentation Algorithm

n processors Divide line into n segments and assign pixel

position calculations of one segment to one processor.

Speed Improvement

Page 29: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Bounding Rectangle Algorithm Assign 1 processor to 1 pixel inside the

bounding rectangle (needs x. y processors)

Each processor calculates the perpendicular distance of the pixel from the line

Plot point if the distance of the pixel from the line is less than or equal to the line thickness.

Speed Improvement

Page 30: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Parallel Curve Algorithm Like straight lines, parallel curve algorithms use

segmentation and bounding rectangles. Curve segmentation

Circle Divide the circular arc the upper octant into sub-arc

segments Assign the pixel calculations for each segment to a

different processor.

Speed Improvement

Page 31: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (II) CGMB214: Introduction to Computer Graphics

Ellipse Divide the elliptical arc in the first quadrant into sub-

arc segment Assign the pixel calculations for each segment to a

different processor

Speed Improvement