Introduction to Computer Graphics
Lecture 2
2-D Primitives

Don Herbison-Evans
donherbisonevans@yahoo.com

(updated 23 August 2006)

2D Entities

  • Points Two numbers (x,y) Cartesian coordinates (named after Descartes)
    Need to transform between various coordinate systems:
  • modelling (component space: right handed)
  • world (problem space : right handed)
  • normalised (range 0.0 - 1.0 : left handed)
  • device or screen (eg PAL 780 x 575)
    (often y downward : right handed)
  • Lines two numbers (m,b)
    y = m*x + b

  • Areas : polygons
  • list of points (vertices), and/or
  • list of lines (edges)
  • Angles
  • 3 points, or
  • 2 lines
  • Interconversions

  • points to lines Two points specify a line segment. Find line coefficients using m = (yend - y0)/(xend - x0)
    b = y0 - m*x0

    The finite part of the infinite line is specified using the parameter 't' :

    x = x0 + t*(xend - x0)
    y = y0 + t*(yend - y0)
    then for 0 < t < 1 the (x,y) values lie between (x0,y0) and (xend,yend)
    The length of the line joining these points is { (x0 - xend)2 + (y0 - yend)2 }0.5

  • lines to points Two lines specify a point, where they intersect.
    Find it by solving the two simultaneous equations : y = m1*x + b1
    y = m2*x + b2
    The intersection may be solved for parameters 's' and 't' using the simultaneous equations x1 + s*(x2 - x1) = x3 + t*(x4 - x3)
    y1 + s*(y2 - y1) = y3 + t*(y4 - y3)
    If 0 < s < 1 and 0 < t < 1 then the point is between the ends of both line segments.

  • lines to areas The area of an arbitrary triangle is {s(s-a)(s-b)(s-c)}0.5 where a,b,c are the lengths of the sides,
    and s = (a + b + c)/2

  • lines and points to angles
  • Use the cross or dot product of unit vectors, U and V, along each of the two lines: cos(A) = U.V / |U|.|V|
    sin(A) = UxV / |U|.|V|
  • Drawing a line on a raster display

    Pitfalls overcome by Bresenham's algorithm:

  • division of an integer by an integer
  • inclusion of multiplications and divisions inside the loop
  • stepping along the axis which changes less
  • trying to accommodate all directions rather than just one octant

    Get other octants by combination of

  • swapping x and y
  • negating x
  • negating y

    There is still the problem of intensity being dependent on direction

    Avoid by use of antialiasing, eg
    intensity of a pixel proportional to area of overlap of line corridor with the pixel.

    Inclusion of a point in a polygon

  • Winding algorithm Add the angles which each edge subtends at the point.
    If the sum is zero, the point is outside.
  • Odd-Even (Crossing) algorithm Extend a line from the point to infinity.
    If the number of times this crosses the edges is even, the point is outside.

    Clipping one polygon by another polygon

    Two nested loops are needed, one stepping throught the edges of one polygon, the other loop stepping through the edges of the other polygon, so that every edge of one polygon is intersected with every edge of the other polygon.

    If any intersection is within the segment length of both edges, then a new vertex must be added to the vertices of the output polygon.

    There is a choice of whether the output polygon is initialised to the polygon of the inner or the outer loop.

    The complication is that the edges and vertices of output polygon change as the clipping proceeds.

    There are various simplifications can be made if one polygon is a rectangular viewing window. The algorithms for this have the rectangular window as the inside loop, and the output polygon initialised to the outer loop polygon:

  • Sutherland-Cohen algorithm,
  • Cyrus-Beck algorithm,
  • Liang-Barsky algorithm
  • Nicholl-Lee-Nicholl algorithm,
  • Weiler-Atherton algorithm.
  • Filling a polygon

  • BFI algorithm (Brute Force and Ignorance) Test every pixel on the screen as to whether it is inside or outside the polygon. If inside: then colour it.

  • Flooding algorithm First mark all the boundary pixels with a suitable code.
    Then find any point guaranteed to be inside the polygon.
    Then recursively colour all pixels adjacent to that point if they are not on the boundary.

    Note:

    if the boundary is marked using rooks moves, the the flood can be done with queens moves.

    if the boundary is marked using queens moves, the the flood must be done with rooks moves.

    ~~~~~~~~~~~~~~~~~~~~~~~~