CGMB214: Introduction to Computer Graphics Topic 4 Graphics Output Primitives (II)
Objectives To understand circle-drawing algorithms and line function
Other Output Primitives Circle Ellipse Curves
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)
Circles Cartesian Coordinates Midpoint Circle Algorithm Polar Coordinates
Cartesian Coordinates Also known as square root method Distance 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
Cartesian Coordinates y (x, y) r yc x xc
Cartesian Coordinates y = yc r2 – (xc-x)2 Given xc=10, yc=10, r=9 First point is at (10,19) Second point (x): y=10 (92 – (10-9)2) y=18.94, y=1.05 Third point (x): y=10 (92 – (10-8)2) y=18.77, y=1.22 Fourth point (x): y=10 (92 – (10-7)2) y=18.48, y=1.51 Fifth point (x): y=10 (92 – (10-6)2) y=18.06, y=1.93 Sixth point (x): y=10 (92 – (10-5)2) y=17.48, y=2.52 Seventh point (x): y=10 (92 – (10-4)2) y=16.70, y=3.29 Eight point (x): y=10 (92 – (10-3)2) y=15.65, y=4.34 Ninth point (x): y=10 (92 – (10-2)2) y=14.12, y=5.87 Tenth point (x): y=10 (92 – (10-1)2) y=10 The process will repeat for increasing x y 10 x 10
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.
Polar Coordinates Also known as Trigonometric Functions The equation of a circle is written in the polar coordinates r and as x = xc + r.cos y = 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.
Polar Coordinates Example: Given P(2,2) is a center point of a circle, draw the circle with radius = 4 x=2+4(cos 0) = 6 y=2+4(sin 0)= 2 x=2+4(cos 45) = 4.8 y=2+4(sin 45)= 4.8 x=2+4(cos 90) = 2 y=2+4(sin 90)= 6 x=2+4(cos 135) =-0.8 y=2+4(sin 135)= 4.8 x=2+4(cos 180) = -2 y=2+4(sin 180)= 2 x=2+4(cos 225) =-0.8 y=2+4(sin 225)=-0.8 x=2+4(cos 270) = 2 y=2+4(sin 270)= -2 x=2+4(cos 315) = 4.8 y=2+4(sin 315)= -0.8
Polar Coordinates 7 6 3 5 4 2 4 3 2 5 1 1 -1 6 8 -2 7 -2 -1 1 2 3 4 5 -1 6 8 -2 7 -2 -1 1 2 3 4 5 6 7
Bresenham Algorithm (Mid Point Algorithm) 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) 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) 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.
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)
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 axes Major: 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.
Ellipses 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 is Ax2 + By2 + Cxy + Dx + Ey + F = 0 As for circles, algorithms that use Cartesian or polar co-ordinates are computationally expensive.
Mid-Point Ellipses Algorithm A modification of the mid-point circle algorithm Incremental 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 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
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)
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)
Mid-Point Ellipses Algorithm The initial value of the decision parameter in Region 1 p0 = r2y –r2xry + 0.25r2x
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
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)
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.
Other Curves Conic sections such as a and b are constants 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 V Velocity Yo G .gravity Vo Xo
Speed Improvement Parallel Line Algorithms Line Segmentation Algorithm DDA and Bresenham determine pixel positions sequentially and faster with parallel computer Line Segmentation Algorithm n processors Divide line into n segments and assign pixel position calculations of one segment to one processor.
Speed Improvement 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 Parallel Curve Algorithm Circle 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 Ellipse Divide the elliptical arc in the first quadrant into sub-arc segment Assign the pixel calculations for each segment to a different processor