Message-ID: <>
Reply-To: (Anson Tsao)
From: (Anson Tsao)
Date: Wed, 01 Mar 1995 05:58:32 GMT
Subject: Frequently Asked Questions (FAQ)
Lines: 1462

From:     (Anson Tsao)
Subject:           Computer Graphics Algorithms FAQ
Version:           1.18
Last-Modified:     February 20, 1995
Followup-To:       poster
Summary:           This article is a collection of information on
                   common algorithms used in computer graphics

Archive-name:      graphics/algorithms-faq
Posting-Frequency: bi-weekly

Welcome to the FAQ for!

Thanks to all who have contributed.  Corrections and contributions
always welcome.

Changed items are marked with a |.
New items are marked with a +.
Items needing input are marked with a ?.

All ftp    references are of the form ftp://node/path
All Mosaic references are of the form http://node/path

Table of Contents

   0) Charter of
   1) Are the postings to archived?
   2) What are some must have books on graphics algorithms?
   3) Are there any online references?
   4) Where is all the source?
   5) How do I rotate a 2D point?
   6) How do I rotate a 3D point?
   7) How do I find the distance from a point to a line?
   8) How do I find intersections of 2 2D line segments?
   9) How do I find the intersection of a line and a plane?
  10) How do I rotate a bitmap?
  11) How do I display a 24 bit image in 8 bits?
  12) How do I fill the area an arbitrary shape?
  13) How do I find the 'edges' in a bitmap?
? 14) How do I enlarge/sharpen/fuzz a bitmap?
  15) How do I map a texture on to a shape?
  16) How do I find the area/orientation of a polygon?
  17) How do I find if a point lies within a polygon?
? 18) How do I find the intersection of two convex polygons?
  19) How do I detect a 'corner' in a collection of points?
  20) How do I generate a circle through three points?
  21) How do I generate a bezier curve that is parallel to another bezier?
  22) How do I split a bezier at a specific value for t?
  23) How do I find a t value at a specific point on a bezier?
  24) How do I fit a bezier curve to a circle?
  25) What is ARCBALL?
  26) Where can I find ARCBALL source?
  27) How do I clip a polygon against a rectangle?
  28) How do I clip a polygon against another polygon?
? 29) Where can I get source for Weiler/Atherton clipping?
? 30) How do I generate a spline to approximate (insert curve here)?
? 31) Where do I get source to display (raster font format)?
? 32) What is morphing/how is it done?
? 33) How do I draw an anti-aliased line/polygon/ellipse?
  34) How do I determine the intersection between a ray and a polygon?
  35) How do I determine the intersection between a ray and a sphere?
  36) How do I determine the intersection between a ray and a bezier surface?
  37) How do I ray trace caustics?
  38) How do I quickly draw a filled triangle?
  39) Where can I get source for Voronoi/Delaunay triangulation?
  40) Where do I get source for convex hull?
  41) What is the marching cubes algorithm?
  42) What is the status of the patent on the "marching cubes" algorithm?
  43) How do I do a hidden surface test (backface culling) with 3d points?
  44) How do I do a hidden surface test (backface culling) with 2d points?
  45) Where can I find graph layout algorithms?
? 46) Where can I find algorithms for 2D collision detection?
  47) Where can I find algorithms for 3D collision detection?
  48) 3D Noise functions and turbulence in Solid texturing.
  49) How do I generate realistic sythetic textures?
  50) How do I perform basic viewing in 3d?
? 51) How can you contribute to this FAQ?
  52) Contributors.  Who made this all possible.


Subject: 0) Charter of is an unmoderated newsgroup intended
    as a forum for the discussion of the algorithms used in the
    process of generating computer graphics.  These algorithms may
    be recently proposed in published journals or papers, old or
    previously known algorithms, or hacks used incidental to the
    process of computer graphics.  The scope of these algorithms
    may range from an efficient way to multiply matrices, all the
    way to a global illumination method incorporating ray tracing,
    radiosity, infinite spectrum modeling, and perhaps even
    mirrored balls and lime jello.

    It is hoped that this group will serve as a forum for programmers
    and researchers to exchange ideas and ask questions on recent
    papers or current research related to computer graphics. is not:

     - for requests for gifs, or other pictures
     - for requests for image translator software (i.e. gif <--> jpg)


Subject: 1) Are the postings to archived?

    Yes.  The "official" archive is stored at:

    Also available at:

    It is archived in the same manner that all other newsgroups are
    being archived there, namely there is an Index file with all the
    subjects, and all the articles are being kept in a hierarchy based
    on the year and month they are posted.

    FYI, all usenet FAQ's are available with Mosaic via


Subject: 2) What are some must have books on graphics algorithms?

    The keywords in brackets are used to refer to the books in later
    questions.  They generally refer to the first author except where
    it is necessary to resolve ambiguity or in the case of the Gems.

    Basic computer graphics, rendering algorithms,

    Computer Graphics: Principles and Practice (2nd Ed.),
    J.D. Foley, A. van Dam, S.K. Feiner, J.F. Hughes, Addison-Wesley
    1990, ISBN 0-201-12110-7

    Procedural Elements for Computer Graphics,
    David F. Rogers, McGraw Hill 1985, ISBN 0-07-053534-5

    Mathematical Elements for Computer Graphics 2nd Ed.,
    David F. Rogers and J. Alan Adams, McGraw Hill 1990, ISBN

    _3D Computer Graphics, 2nd Edition_,
    Alan Watt, Addison-Wesley 1993, ISBN 0-201-63186-5

    An Introduction to Ray Tracing,
    Andrew Glassner (ed.), Academic Press 1989, ISBN 0-12-286160-4

    [Gems I]
    Graphics Gems,
    Andrew Glassner (ed.), Academic Press 1990, ISBN 0-12-286165-5

    [Gems II]
    Graphics Gems II,
    James Arvo (ed.), Academic Press 1991, ISBN 0-12-64480-0

    [Gems III]
    Graphics Gems III,
    David Kirk (ed.), Academic Press 1992, ISBN 0-12-409670-0 (with
    IBM disk) or 0-12-409671-9 (with Mac disk)

    [Gems IV]
    Graphics Gems IV,
    Paul S. Heckbert (ed.), Academic Press 1994, ISBN 0-12-336155-9
    (with IBM disk) or 0-12-336156-7 (with Mac disk)

    Advanced Animation and Rendering Techniques,
    Alan Watt, Mark Watt, Addison-Wesley 1992, ISBN 0-201-54412-1

    An Introduction to Splines for Use in Computer Graphics and
        Geometric Modeling,
    Richard H. Bartels, John C. Beatty, Brian A. Barsky, 1987, ISBN

    Curves and Surfaces for Computer Aided Geometric Design:
    A Practical Guide, 3rd Edition, Gerald E. Farin, Academic Press
    1993. ISBN 0-12-249052-5

    The Algorithmic Beauty of Plants,
    Przemyslaw W. Prusinkiewicz, Aristid Lindenmayer, Springer-Verlag,
    1990, ISBN 0-387-97297-8, ISBN 3-540-97297-8

    Tricks of the Graphics Gurus,
    Dick Oliver, et al. (2) 3.5 PC disks included, $39.95 SAMS Publishing

    Introduction to computer graphics,
    Hearn & Baker

    Radiosity and Realistic Imange Sythesis,
    Michael F. Cohen, John R. Wallace, Academic Press Professional
    1993, ISBN 0-12-178270-0

    Texturing and Modeling - A Procedural Approach
    David S. Ebert (ed.), F. Kenton Musgrave, Darwyn Peachey, Ken Perlin,
        Setven Worley, Academic Press 1994, ISBN 0-12-228760-6,
        ISBN 0-12-2278761-4 (IBM disk)

    For image processing,

    Fractal Image Compression,
    Michael F. Barnsley and Lyman P. Hurd, AK Peters, Ltd, 1993 ISBN

    Fundamentals of Image Processing,
    Anil K. Jain, Prentice-Hall 1989, ISBN 0-13-336165-9

    Digital Image Processing,
    Kenneth R. Castleman, Prentice-Hall 1979, ISBN 0-13-212365-7

    Digital Image Processing, Second Edition,
    William K. Pratt, Wiley-Interscience 1991, ISBN 0-471-85766-1

    Digital Image Processing (3rd Ed.),
    Rafael C. Gonzalez, Paul Wintz, Addison-Wesley 1992, ISBN

    The Image Processing Handbook,
    John C. Russ, CRC Press 1992, ISBN 0-8493-4233-3

    Digital Image Warping,
    George Wolberg, IEEE Computer Society Press Monograph 1990, ISBN

    Computational geometry,

    A Programmer's Geometry,
    Adrian Bowyer, John Woodwark, Butterworths 1983, ISBN
    0-408-01242-0 Pbk

    [O' Rourke]
    Computational Geometry in C,
    Joseph O'Rourke, Cambridge University Press 1994, ISBN
    0-521-44592-2 Pbk, ISBN 0-521-44034-3 Hdbk

    Geometric Modeling,
    Michael E. Mortenson, Wiley 1985, ISBN 0-471-88279-8

    Computational Geometry: An Introduction,
    Franco P. Preparata, Michael Ian Shamos, Springer-Verlag 1985,
    ISBN 0-387-96131-3


Subject: 3) Are there any online references?

    The computational geometry community maintains its own
    bibliography of publications in or closely related to that
    subject.  Every four months, additions and corrections are
    solicited from users, after which the database is updated and
    released anew.  As of September 1993, it contained 5356 bib-tex
    entries.  It can be retrieved from - bibliography proper     - PostScript format  - overview published
        in '93 in SIGACT News and the Internat. J. Comput.  Geom. Appl.     - detailed retrieval info

    Announcing the ACM SIGGRAPH Online Bibliography Project, by
    Stephen Spencer (

    The database is available for anonymous FTP from the directory.  Please
    download and examine the file READ_ME in that directory for more
    specific information concerning the database.

    'netlib' is a useful source for algorithms, member inquiries for
    SIAM, and bibliographic searches.  For information, send mail to, with "send index" in the body of the mail

    You can also find free sources for numerical computation in C via  In particular, grab
    numcomp-free-c.gz in that directory.

    Check out Nick Fotis's computer graphics resources FAQ -- it's
    packed with pointers to all sorts of great computer graphics
    stuff.  This FAQ is posted biweekly to

    This WWW page contains links to a large number 
    of computer graphic related pages:


Subject: 4) Where is all the source?

    Graphics Gems source code.

    General 'stuff'

    There are a number of interesting items in including:    
    - Code for 2D Voronoi, Delaunay, and Convex hull
    - Mike Hoymeyer's implementation of Raimund Seidel's
      O( d! n ) time linear programming algorithm for
      n constraints in d dimensions
    - geometric models of UC Berkley's new computer science

    You can find useful overviews of a number of computer graphic
    topics in
    - area/orientation of polygons
    - finding if a point lies within a polygon
    - generating a circle through 3 points
    - description and psuedo-code for Delaunay triangulation
    - basic viewing in 3D 


Subject: 5) How do I rotate a 2D point?

    In 2-D, the 2x2 matrix is very simple.  If you want to rotate a
    column vector v by t degrees using matrix M, use

        M = {{cos t, -sin t}, {sin t, cos t}} in M*v.

    If you have a row vector, use the transpose of M (turn rows into
    columns and vice versa).  If you want to combine rotations, in 2-D
    you can just add their angles, but in higher dimensions you must
    multiply their matrices.


Subject: 6) How do I rotate a 3D point?

    Assuming you want to rotate vectors around the origin of your
    coordinate system. (If you want to rotate around some other point,
    subtract its coordinates from the point you are rotating, do the
    rotation, and then add back what you subtracted.) In 3-D, you need
    not only an angle, but also an axis. (In higher dimensions it gets
    much worse, very quickly.)  Actually, you need 3 independent
    numbers, and these come in a variety of flavors.  The flavor I
    recommend is unit quaternions: 4 numbers that square and add up to
    +1.  You can write these as [(x,y,z),w], with 4 real numbers, or
    [v,w], with v, a 3-D vector pointing along the axis. The concept
    of an axis is unique to 3-D. It is a line through the origin
    containing all the points which do not move during the rotation.
    So we know if we are turning forwards or back, we use a vector
    pointing out along the line. Suppose you want to use unit vector u
    as your axis, and rotate by 2t degrees.  (Yes, that's twice t.)
    Make a quaternion [u sin t, cos t]. You can use the quaternion --
    call it q -- directly on a vector v with quaternion
    multiplication, q v q^-1, or just convert the quaternion to a 3x3
    matrix M. If the components of q are {(x,y,z),w], then you want
    the matrix

        M = {{1-2(yy+zz),  2(xy-wz),  2(xz+wy)},
             {  2(xy+wz),1-2(xx+zz),  2(yz-wx)},
             {  2(xz-wy),  2(yz+wx),1-2(xx+yy)}}.

    Rotations, translations, and much more are explained in all basic
    computer graphics texts.  Quaternions are covered briefly in
    [Foley], and more extensively in several Graphics Gems, and the
    SIGGRAPH 85 proceedings.


Subject: 7) How do I find the distance from a point to a line?

    Let the point be C (XC,YC) and the line be AB (XA,YA) to (XB,YB).
    The length of the line segment AB is L:


        r = -----------------------------

        s = -----------------------------

    Let I be the point of perpendicular projection of C onto AB, the


    Distance from A to I = r*L
    Distance from C to I = s*L

    If r<0      I is on backward extension of AB
    If r>1      I is on ahead extension of AB
    If 0<=r<=1  I is on AB

    If s<0      C is left of AB (you can just check the numerator)
    If s>0      C is right of AB
    If s=0      C is on AB


Subject: 8) How do I find intersections of 2 2D line segments?

    This problem can be extremely easy or extremely difficult depends
    on your applications.  If all you want is the intersection point,
    the following should work:

    Let A,B,C,D be 2-space position vectors.  Then the directed line
    segments AB & CD are given by:

        AB=A+r(B-A), r in [0,1]
        CD=C+s(D-C), s in [0,1]

    If AB & CD intersect, then

        A+r(B-A)=C+s(D-C), or

        YA+r(YB-YA)=YC+s(YD-YC)  for some r,s in [0,1]

    Solving the above for r and s yields

        r = -----------------------------  (eqn 1)

        s = -----------------------------  (eqn 2)

    Let I be the position vector of the intersection point, then

        I=A+r(B-A) or


    By examining the values of r & s, you can also determine some
    other limiting conditions:

        If 0<=r<=1 & 0<=s<=1, intersection exists
            r<0 or r>1 or s<0 or s>1 line segments do not intersect

        If the denominator in eqn 1 is zero, AB & CD are parallel
        If the numerator in eqn 1 is also zero, AB & CD are coincident

    If the intersection point of the 2 lines are needed (lines in this
    context mean infinite lines) regardless whether the two line
    segments intersect, then

        If r>1, I is located on extension of AB
        If r<0, I is located on extension of BA
        If s>1, I is located on extension of CD
        If s<0, I is located on extension of DC

    Also note that the denominators of eqn 1 & 2 are identical.


    [O'Rourke] pp. 249-51
    [Gems III] pp. 199-202 "Faster Line Segment Intersection,"


Subject: 9) How do I find the intersection of a line and a plane?

    If the plane is defined as:

        a*x + b*y + c*z + d = 0

    and the line is defined as:

        x = x1 + (x2 - x1)*t = x1 + i*t
        y = y1 + (y2 - y1)*t = y1 + j*t
        z = z1 + (z2 - z1)*t = z1 + k*t

    Then just substitute these into the plane equation. You end up

        t = - (a*x1 + b*y1 + c*z1 + d)/(a*i + b*j + c*k)

    If the denominator is zero, then the vector (a,b,c) and the vector
    (i,j,k) are perpendicular.  Note that (a,b,c) is the normal to the
    plane and (i,j,k) is the direction of the line.  It follows that
    the line is either parallel to the plane or contained in the
    plane. In either case there is no unique intersection point.


Subject: 10) How do I rotate a bitmap?

    The easiest way, according to the faq, is to take
    the rotation transformation and invert it. Then you just iterate
    over the destination image, apply this inverse transformation and
    find which source pixel to copy there.

    A much nicer way comes from the observation that the rotation

        R(T) = { { cos(T), -sin(T) }, { sin(T), cos(T) } }

    is formed my multiplying three matrices, namely:

        R(T) = M1(T) * M2(T) * M3(T)


        M1(T) = { { 1, -tan(T/2) },
                  { 0, 1         } }
        M2(T) = { { 1,      0    },
                  { sin(T), 1    } }
        M3(T) = { { 1, -tan(T/2) },
                  { 0,         1 } }

    Each transformation can be performed in a separate pass, and
    because these transformations are either row-preserving or
    column-preserving, anti-aliasing is quite easy.


    Paeth, A. W., "A Fast Algorithm for General Raster Rotation",
    Proceedings Graphics Interface '89, Canadian Information
    Processing Society, 1986, 77-81
    [Note - e-mail copies of this paper are no longer available]

    [Gems I]


Subject: 11) How do I display a 24 bit image in 8 bits?

    [Gems I] pp. 287-293, "A Simple Method for Color Quantization:
    Octree Quantization"

    B. Kurz.  Optimal Color Quantization for Color Displays.
    Proceedings of the IEEE Conference on Computer Vision and Pattern
    Recognition, 1983, pp. 217-224.

    [Gems II] pp. 116-125, "Efficient Inverse Color Map Computation"

        This describes an efficient technique to
        map actual colors to a reduced color map,
        selected by some other technique described
        in the other papers.

    [Gems II] pp. 126-133, "Efficient Statistical Computations for
    Optimal Color Quantization"

    Xiaolin Wu.  Color Quantization by Dynamic Programming and
    Principal Analysis.  ACM Transactions on Graphics, Vol. 11, No. 4,
    October 1992, pp 348-372.


Subject: 12) How do I fill the area of an arbitrary shape?

    "A Fast Algorithm for the Restoration of Images Based on Chain
        Codes Description and Its Applications", L.W. Chang & K.L. Leu,
        Computer Vision, Graphics, and Image Processing, vol.50,
        pp296-307 (1990)

    "An Introductory Course in Computer Graphics" by Richard Kingslake,
        (2nd edition) published by Chartwell-Bratt ISBN 0-86238-284-X

    [Gems I]


Subject: 13) How do I find the 'edges' in a bitmap?

    A simple method is to put the bitmap through the filter:

        -1    -1    -1
        -1     8    -1
        -1    -1    -1

    This will highlight changes in contrast.  Then any part of the
    picture where the absolute filtered value is higher than some
    threshold is an "edge".


Subject: 14) How do I enlarge/sharpen/fuzz a bitmap?


Subject: 15) How do I map a texture on to a shape?

    Paul S. Heckbert, "Survey of Texture Mapping", IEEE Computer
    Graphics and Applications V6, #11, Nov. 1986, pp 56-67 revised
    from Graphics Interface '86 version

    Eric A. Bier and Kenneth R. Sloan, Jr., "Two-Part Texture
    Mappings", IEEE Computer Graphics and Applications V6 #9, Sept.
    1986, pp 40-53 (projection parameterizations)


Subject: 16) How do I find the area/orientation of a polygon?

    Compute the signed area.  The orientation is counter-clockwise if
    this area is positive.  There's a Gem on computing signed areas.

    A slightly faster method is based on the observation that it isn't
    necessary to compute the area.  One can find the lowest, rightmost
    point of the polygon, and then take the cross product of the edges
    fore and aft of it.  Both methods are O(n) for n vertices, but it
    does seem a waste to add up the total area when a single cross
    product (of just the right edges) suffices.

    The reason that the lowest, rightmost point works is that the
    internal angle at this vertex is necessarily convex, strictly less
    than pi (even if there are several equally-lowest points).

    The key formula is this:

        If the coordinates of vertex v_i are x_i and y_i,
        twice the area of a polygon is given by

        2 A( P ) = sum_{i=0}^{n-1} (x_i y_{i+1} - y_i x_{i+1}).


    [O' Rourke] pp. 18-27.


Subject: 17) How do I find if a point lies within a polygon?

    A quick comment - the code in the Sedgewick book Algorithms is

    The short answer, for the FAQ, could be:

    int pnpoly(int npol, float *xp, float *yp, float x, float y)
      int i, j, c = 0;
      for (i = 0, j = npol-1; i < npol; j = i++) {
        if ((((yp[i]<=y) && (y