D Graphics Primitives Eye sees Displays - CRT/LCD Frame buffer - Addressable pixel array (D) Graphics processor s main function is to map application model (D) by projection on to D primitives: points, lines, polygons (coloured and shaded) Projection is followed by Scan-conversion. D Parametric Attributes Display List D polygons OpenGL Graphics System Evaluator D polygons Pixel Operations Per- Vertex D points Texture Memory D pixels colours Rasterization Per- Fragment D pixels colours blending clipping Frame Buffer D Graphics Primitives D Primitives: lines, circles... Co-ordinate Conventions Drawing D Primitives Scan Conversion: filled area primitives Attributes of D Graphics Primitives Antialiasing Drawing D Graphics Primitives D Primitives: Lines Circles Ellipses Other Curves Polygons: empty and filled - Characters General Mathematical D Primitives Parametric Equations (x(t), y(t)) Line x(t)=x +t(x x ) y(t)=y +t(y y ) Implicit Equations < inside F(x, y)= > outside f (x,y)=ax + by + c a=y y b=x x c=x y x y Scan Conversion Scan Conversion also known as Rasterization Can be thought of in two equivalent ways: - As a Convolution between an ideal mathematical function (infinitely thin line) and an object the size and shape of a pixel followed by sampling at the pixel positions - Pixels have finite area, output is overlap of pixel with curve
Co-ordinate Conventions What convention is used for coordinate system?. Like square Graph paper: pixels are unit squares that tile the plane with corners at [(x,y),(x +,y + )]. Pixels centred on integer co-ordinates: mathematical. Co-ordinate Conventions Graph Paper I will use this convention, point is at [,] Point at x =, y = Pixel Centred Drawing Lines Basic line drawing Digital Differential Analyser (DDA) Accelerating Basic Line Drawing Uses the simplest expression for a straight line Considers entire line Technique requires floating point and function call in inner-most loop of algorithm which is slow Care needed with end points to get length correct Basic Line Drawing Basic Line Drawing x = # $ " % x = # $ " % ( [ ]) ( [ ]) y i = mx i + c x i, Round y i where m = "y "x and x i, Floor y i +. y i = "y "x x i + c c = y i # "y "x x i
Basic Line Drawing Example x = # $ " % x = # $ " % m = "y "x = c = y "y "x x = Basic Line Drawing Example i x i y i y i +."........ Basic Line Drawing x = # $ " % x = # $ " % Basic Line Drawing Issues NB If we plot the final end point of the line the length would be x = and y = instead of the required x = and y = " Plot pixels interior to the line path Each iteration requires a floating point multiply, add, and the use of the Floor function. Line Drawing: DDA Digital Differential Analyser Solves a parametric equation by solving a differential equation In general the parametric form can be converted to a differential equation using its Taylor expansion: d f = f (t + dt) f (t + dt)= f (t) + f (t)dt + f (t) dt + K DDA for straight lines Polynomial of order n has at most n non-zero derivatives. For straight lines, y(x) = mx + c, this is effectively: dy = y( x + dx) y( x) = y "( x)dx rearranging : y(x + dx) = y(x) + y "(x).dx Remember that: m = y x
DDA for Straight lines DDA Line Drawing If we let dx=, and recognise y (x) as, m, the line gradient: y k+ = mx k + + c = m(x k +) + c = y k + m x = # $ " % x = # $ " % DDA DDA algorithm for arbitrary slopes Result is identical to Basic method Core of calculation requires only an addition + Floor operation Uses only gradient, no explicit use of intercept So far slope has been: m - m : step in x and use: y k+ = y k + m m > : reverse roles of x and y (step in y) and use: x k+ = x k + m FAST Recursive For each point on the line uses: - integer add - or integer increments - integer comparison Uses decision variable Deal with slopes m to start with yk + yk B d A d xk xk + Just drawn shaded Pixel Which pixel do we draw next, A or B?
y co-ordinate of ideal mathematical line at position (xk+) is: - y = m(xk + ) + c Distance to pixel centre below line is: - d = y - yk = m(xk + ) + c - yk Distance to discrete point above line is: - d = (yk + ) - y = (yk + ) - m(xk + ) - c Difference (d - d) of distances: - d - d = m(xk + ) - yk + c - By substituting, m = "y/"x, and rearranging, we can get and integer only expression DEFINE Decision variable to be: - pk = "x (d - d) Substituting for (d - d) : - pk = "y xk - "x yk + K where K is a constant. Decision variable: - pk = "y xk - "x yk + K pk has the same sign as (d - d), since "x is +ve. pk < # choose yk+ = yk pk $ # choose yk+ = yk + Decision variable at next point: - pk+ = "y xk+ - "x yk+ + K Take difference of decision variable at current and next point (eliminates K): - pk+ - pk = "y (xk+ - xk) - "x (yk+ - yk) Giving recurrence relation for pk+: - pk+ = pk + "y - "x (yk+ - yk), - since (xk+ - xk) = Recurrence relation for decision pk+ = pk + "y - "x (yk+ - yk) Which can take on two forms, either: - If (pk < ) # choose yk+ = yk - pk+ = pk + "y (since (yk+ - yk)=) Or - - If pk $ # choose yk+ = yk + pk+ = pk + "y - "x (since (yk+ - yk)=) Initialising Decision Variable: Recall from earlier pk = "y xk - "x yk + K where K = "y + "x(c - ) Since: - c = y - x "y/"x# c "x = "x y - x "y We can substitute for c "x above with k= p = "y - "x
Setup. Input endpoints,. Store left endpoint in (x, y). Draw pixel at (x, y). Calculate constants: "x, "y, ( "y), ( "y- "x). Initialise decision variable: p = "y - "x Loop For each xk, from k = to k = "x- - If ( pk < ) Draw pixel at (xk +, yk) - pk+ = pk + ( "y) - Else Draw pixel at (xk +, yk+) - pk+ = pk + ( "y - "x) Bresenham Line Example Bresenham Line Example x = # $ " % x = # $ " % x = # $ " % x = # $ " % Bresenham Line Example x = # $ " % x = # $ " % Generalising Line to Arbitrary Orientations So far have considered lines with slope between and + (first octant) Need to consider implications for algorithm of arbitrary slopes of lines
Slope > Interchange roles of x and y axes Calculate x values of line points Step along y direction in unit increments Could also revise algorithm to plot points in either direction - Both x and y decrease as line is generated - Decide on convention for d = d (plot lower or upper, but must be consistent) Procedure is similar Slope < One co-ordinate decreases as the other increases For negative slope < - again axes are swapped - Decrement y axis co-ordinate - Calculate new x co-ordinate Special Cases Horizontal lines and Vertical lines can be detected at the initialisation stage (before the Bresenham inner loop) and written directly into the Frame Buffer Drawing Circles Basic Circle drawing Accelerating Circle s by exploiting symmetry Midpoint () Circle Details Circle Drawing Techniques Circle is defined by: - (x - xc) + (y - yc) = r Solving this gives: - y = yc ± %(r - (x - xc)) Computation of square root Step in x Also need to swap axes to step in y rather than x Step in y Circle Drawing Techniques Calculate points along a circular boundary by stepping in equal increments of angle: Polar parametric form: - x = xc + r cos - y = yc + r sin Again considerable computation of trigonometric expressions
Reducing computation by Symmetry of circles Can work in quadrants Even more efficient to exploit octants Need only compute one eighth of circle points: Work in single octant Variation of algorithm more easily extended to ellipses and other curves Need to set up a decision variable to determine which pixel is next plotted around circular path Use implicit circle function: f (x,y) = x + y r f (x,y) = x + y r Implicit function for a circle: f(x, y) <, inside circle f(x, y) =, on the circle - f(x, y) >, outside circle These tests are performed on the midpoint between two candidate next points y k y k - x k x k +?? Work in third quadrant Need to keep points plotted by algorithm internal to circle in graph paper coordinate convention Work in sixth Octant and exploit symmetry yk + yk A B?? Work in single octant xk - xk
: Decision parameter In previous slide have just drawn pixel at (xk, yk), which is next pixel B is at (xk -, yk), or pixel A is at (xk -, yk + )? Decision variable is circle function evaluated at the midpoint: p k = f (x k +.,y k + ) = f (x k.,y k + ) p k = (x k.) + (y k + ) r Decision Parameter If p k < - Midpoint is INSIDE circle - Plot point B Else - Midpoint is OUTSIDE circle - Plot point A Decision Parameter We have an expression for already, then p k+ = (x k+.) + (y k+ + ) r And because x k+ = x k p k+ = (x k.) + (y k+ + ) r p k Decision Parameter Get a recurrence relation for the decision parameter by forming: p k+ p k We then rearrange this to get an expression for: p k+ =p k (x k + ) + (y k+ + y k )(y k+ y k ) + (y k+ y k ) NB (y k+ y k ) will either be + or Initialising Decision Parameter p = f (, r) =(.) + ( r + ) r =. r For integer radius we can take the floor of this to be: p = r To write down the algorithm we have noted that when: y k+ = y k + p k+ =p k (x k + ) + (y k+ + y k )(y k+ y k ) + (y k+ y k ) Simplifies to: p k+ = p k + (y k x k ) + Since: y k+ = y k + and y k+ y k =
IF pk < midpoint INSIDE xk+ = xk - yk+ = yk SetCirclePoints(xk+, yk +) pk+ = pk - xk + ELSE midpoint OUTSIDE xk+ = xk - yk+ = yk + SetCirclePoints(xk+, yk+) pk+ = pk + (yk - xk) + - - - Circle R= - - - - - Circle R = - - - - - - - - - - - - - - - - - - - Circle R= - - - - - - - - - - - Conics Circles are a member of a group of functions, the conics, their general form is: Ax + By +Cxy + Dx + Ey + F = Particular conic is given by: - B - AC < Ellipse or Circle - B - AC = Parabola - B - AC > Hyperbola Hearn and Baker, p -