Xref: info.physics.utoronto.ca comp.answers:7731 comp.graphics.algorithms:9057 news.answer
Xref: info.physics.utoronto.ca comp.answers:7731 comp.graphics.algorithms:9057 news.answers:30585
Path: destin!jdstone
From: jdstone@destin.dazixco.ingr.com (Jon Stone)
Newsgroups: comp.graphics.algorithms,comp.answers,news.answers
Subject: comp.graphics.algorithms Frequently Asked Questions (FAQ)
Supersedes:
FollowupTo: comp.graphics.algorithms
Date: 9 Oct 1994 10:00:06 GMT
Organization: Intergraph Corp., Boulder CO
Lines: 1369
Approved: newsanswersrequest@MIT.EDU
Expires: 22 Nov 1994 10:00:03 GMT
MessageID:
ReplyTo: jdstone@ingr.com
NNTPPostingHost: destin.dazixco.ingr.com
Summary: This posting contains a list of Frequently Asked
Questions (and their answers) about computer graphics
algorithms. It should be read by anyone who wishes to
post to the comp.graphics.algorithms newsgroup.
Archivename: graphics/algorithmsfaq
Version: 1.14
LastModified: September 29, 1994
PostingFrequency: monthly
Welcome to the FAQ for comp.graphics.algorithms!
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 comp.graphics.algorithms
 1) Are the postings to comp.graphics.algorithms 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 antialiased 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 perform basic viewing in 3d?
? 50) How can you contribute to this FAQ?
51) Contributors. Who made this all possible.

Subject: 0) Charter of comp.graphics.algorithms
Comp.graphics.algorithms 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.
comp.graphics.algorithms 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 comp.graphics.algorithms archived?
 Yes. The "official" archive is stored at:

 http://www.cis.ohiostate/hypertext/faq/usenet/graphics/algorithmsfaq/faq.html
 ftp://rtfm.mit.edu/pub/usenetbygroup/news.answers/graphics/algorithmsfaq

 Also available at:

 ftp://wuarchive.wustl.edu/graphics/graphics/maillists/comp.graphics.algorithms
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

 http://www.cis.ohiostate/hypertext/faq/usenet/top.html

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,

[Foley]
Computer Graphics: Principles and Practice (2nd Ed.),
J.D. Foley, A. van Dam, S.K. Feiner, J.F. Hughes, AddisonWesley
1990, ISBN 0201121107
[Rogers:Procedural]
Procedural Elements for Computer Graphics,
David F. Rogers, McGraw Hill 1985, ISBN 0070535345
[Rogers:Mathematical]
Mathematical Elements for Computer Graphics 2nd Ed.,
David F. Rogers and J. Alan Adams, McGraw Hill 1990, ISBN
0070535302
[Watt:3D]
_3D Computer Graphics, 2nd Edition_,
Alan Watt, AddisonWesley 1993, ISBN 0201631865
[Glassner:RayTracing]
An Introduction to Ray Tracing,
Andrew Glassner (ed.), Academic Press 1989, ISBN 0122861604
[Gems I]
Graphics Gems,
Andrew Glassner (ed.), Academic Press 1990, ISBN 0122861655
[Gems II]
Graphics Gems II,
James Arvo (ed.), Academic Press 1991, ISBN 012644800
[Gems III]
Graphics Gems III,
David Kirk (ed.), Academic Press 1992, ISBN 0124096700 (with
IBM disk) or 0124096719 (with Mac disk)
[Gems IV]
Graphics Gems IV,
Paul S. Heckbert (ed.), Academic Press 1994, ISBN 0123361559
(with IBM disk) or 0123361567 (with Mac disk)
[Watt:Animation]
Advanced Animation and Rendering Techniques,
Alan Watt, Mark Watt, AddisonWesley 1992, ISBN 0201544121
[Bartels]
An Introduction to Splines for Use in Computer Graphics and
Geometric Modeling,
Richard H. Bartels, John C. Beatty, Brian A. Barsky, 1987, ISBN
0934613273
[Farin]
Curves and Surfaces for Computer Aided Geometric Design:
A Practical Guide, 3rd Edition, Gerald E. Farin, Academic Press
1993. ISBN 0122490525
[Prusinkiewicz]
The Algorithmic Beauty of Plants,
Przemyslaw W. Prusinkiewicz, Aristid Lindenmayer, SpringerVerlag,
1990, ISBN 0387972978, ISBN 3540972978
[Oliver]
Tricks of the Graphics Gurus,
Dick Oliver, et al. (2) 3.5 PC disks included, $39.95 SAMS Publishing
[Hearn]
Introduction to computer graphics,
Hearn & Baker
For image processing,

[Barnsley]
Fractal Image Compression,
Michael F. Barnsley and Lyman P. Hurd, AK Peters, Ltd, 1993 ISBN
1568810008
[Jain]
Fundamentals of Image Processing,
Anil K. Jain, PrenticeHall 1989, ISBN 0133361659
[Castleman]
Digital Image Processing,
Kenneth R. Castleman, PrenticeHall 1979, ISBN 0132123657
[Pratt]
Digital Image Processing, Second Edition,
William K. Pratt, WileyInterscience 1991, ISBN 0471857661
[Gonzalez]
Digital Image Processing (2nd Ed.),
Rafael C. Gonzalez, Paul Wintz, AddisonWesley 1987, ISBN
0201110261
[Russ]
The Image Processing Handbook,
John C. Russ, CRC Press 1992, ISBN 0849342333
[Wolberg]
Digital Image Warping,
George Wolberg, IEEE Computer Society Press Monograph 1990, ISBN
0818689447
Computational geometry,

[Bowyer]
A Programmer's Geometry,
Adrian Bowyer, John Woodwark, Butterworths 1983, ISBN
0408012420 Pbk
[O' Rourke]
Computational Geometry in C,
Joseph O'Rourke, Cambridge University Press 1994, ISBN
0521445922 Pbk, ISBN 0521440343 Hdbk
[Mortenson]
Geometric Modeling,
Michael E. Mortenson, Wiley 1985, ISBN 0471882798
[Preparata]
Computational Geometry: An Introduction,
Franco P. Preparata, Michael Ian Shamos, SpringerVerlag 1985,
ISBN 0387961313

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 bibtex
entries. It can be retrieved from
ftp://cs.usask.ca/pub/geometry/geombib.tar.Z  bibliography proper
ftp://cs.usask.ca/pub/geometry/geom.ps.Z  PostScript format
ftp://cs.usask.ca/pub/geometry/ocgc19.ps.Z  overview published
in '93 in SIGACT News and the Internat. J. Comput. Geom. Appl.
ftp://cs.usask.ca/pub/geometry/ftphints  detailed retrieval info
Announcing the ACM SIGGRAPH Online Bibliography Project, by
Stephen Spencer (biblio@siggraph.org)
The database is available for anonymous FTP from the
ftp://siggraph.org/publications/bibliography 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
netlib@ornl.gov, with "send index" in the body of the mail
message.
You can also find free sources for numerical computation in C via
ftp://usc.edu/pub/Cnumanal. In particular, grab
numcompfreec.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 comp.graphics.

Subject: 4) Where is all the source?
Graphics Gems source code.
ftp://princeton.edu:pub/Graphics/GraphicsGems
General 'stuff'
ftp://wuarchive.wustl.edu/graphics/graphics

Subject: 5) How do I rotate a 2D point?
In 2D, 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 2D
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 3D, 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 3D vector pointing along the axis. The concept
of an axis is unique to 3D. 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 = {{12(yy+zz), 2(xywz), 2(xz+wy)},
{ 2(xy+wz),12(xx+zz), 2(yzwx)},
{ 2(xzwy), 2(yz+wx),12(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:
L=((XBXA)**2+(YBYA)**2)**0.5
and
(YAYC)(YAYB)(XAXC)(XBXA)
r = 
L**2
(YAYC)(XBXA)(XAXC)(YBYA)
s = 
L**2
Let I be the point of perpendicular projection of C onto AB, the
XI=XA+r(XBXA)
YI=YA+r(YBYA)
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 2space position vectors. Then the directed line
segments AB & CD are given by:
AB=A+r(BA), r in [0,1]
CD=C+s(DC), s in [0,1]
If AB & CD intersect, then
A+r(BA)=C+s(DC), or
XA+r(XBXA)=XC+s(XDXC)
YA+r(YBYA)=YC+s(YDYC) for some r,s in [0,1]
Solving the above for r and s yields
(YAYC)(XDXC)(XAXC)(YDYC)
r =  (eqn 1)
(XBXA)(YDYC)(YBYA)(XDXC)
(YAYC)(XBXA)(XAXC)(YBYA)
s =  (eqn 2)
(XBXA)(YDYC)(YBYA)(XDXC)
Let I be the position vector of the intersection point, then
I=A+r(BA) or
XI=XA+r(XBXA)
YI=YA+r(YBYA)
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.
References:
[O'Rourke] pp. 24951
[Gems III] pp. 199202 "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
with:
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 comp.graphics 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
matrix:
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)
where
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 rowpreserving or
columnpreserving, antialiasing is quite easy.
Reference:
Paeth, A. W., "A Fast Algorithm for General Raster Rotation",
Proceedings Graphics Interface '89, Canadian Information
Processing Society, 1986, 7781
[Note  email 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. 287293, "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. 217224.
[Gems II] pp. 116125, "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. 126133, "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 348372.

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,
pp296307 (1990)
"An Introductory Course in Computer Graphics" by Richard Kingslake,
(2nd edition) published by ChartwellBratt ISBN 086238284X
[Gems I]
[Foley]
[Hearn]

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 5667 revised
from Graphics Interface '86 version
Eric A. Bier and Kenneth R. Sloan, Jr., "TwoPart Texture
Mappings", IEEE Computer Graphics and Applications V6 #9, Sept.
1986, pp 4053 (projection parameterizations)

Subject: 16) How do I find the area/orientation of a polygon?
Compute the signed area. The orientation is counterclockwise 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 equallylowest 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}^{n1} (x_i y_{i+1}  y_i x_{i+1}).
Reference:
[O' Rourke] pp. 1827.

Subject: 17) How do I find if a point lies within a polygon?
A quick comment  the code in the Sedgewick book Algorithms is
wrong.
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 = npol1; i < npol; j = i++) {
if ((((yp[i]<=y) && (y
(xvx,yvy,zvz). This includes the viewpoint and the viewplane.
Now we need to rotate so that the z axis points straight at the
viewplane, then scale so it is 1 unit away.
After all this, we may find ourselves looking out upside down. It
is traditional to specify some direction in the world or viewplane
as "up", and rotate so the positive y axis points that way (as
nearly as possible if it's a world vector). Finally, we have acted
so far as if the window was the entire plane instead of a limited
portal. A final shift and scale transforms coordinates in the
plane to coordinates on the screen, so that a rectangular region
of interest (our "window") in the plane fills a rectangular region
of the screen (our "canvas" if you like).
I have left out details of how you define and perform the rotation
of the viewplane, but I'm sure someone else will be happy to
supply those if there is demand. It requires knowing how to
describe a plane, and how to rotate vectors to line up. Neither is
difficult, but this is already using a lot of net space. One
further practical difficulty is the need to clip away parts of the
world behind us, so (x,y,z) doesn't pop up at (x/z,y/z,1).
(Notice the mathematics of projection alone would allow that!) But
all the viewing transformations can be done using translation,
rotation, scale, and a final perspective divide. If a 4x4
homogeneous matrix is used, it can represent everything needed,
which saves a lot of work.

Subject: 50) How can you contribute to this FAQ?
Send email to jdstone@ingr.com with your suggestions, possible
topics, corrections, or pointers to information. Remember, I am
not an expert on many of these topics. I'm the editor.
Here are some possible topics that may interest many of our
readers.
Clipping...
Splines
Nurbs
Image Warping/Transformation/Filtering
Antialiasing
Volume Rendering
Morphing (synonymous with generalized Warping)
MPEG
JPEG
Zbuffer/Abuffer/etc.
interpolation (linear, spline, fft, etc.)
Modeling tricks (fractal mountains, trees, seashells)
Surfaces
Ray Tracing
Reflection/Refraction
1) Computing the minimum bounding boxes of various geometric
elements such as circular arcs, parabolas, clothoids, splines,
etc. What is the most efficient way to do them for the
following cases:
i) The boxes are all orthogonal to the XY plane;
ii) The boxes is oriented such that the smallest area
rectangular bounding boxes are computed.
2) What is the most efficient way to tell if a polygon crosses
itself? i.e. without intersecting every edge with every edge.

Subject: 51) Contributors. Who made this all possible.
andrewfg@aifh.ed.ac.uk (Andrew Fitzgibbon)
atae@spva.ph.ic.ac.uk (Ata Etemadi)
atsao@tkk.win.net (Anson Tsao)
barber@geom.umn.edu (Brad Barber)
bromage@mundil.cs.mu.OZ.AU (Andrew James Bromage)
cek@Princeton.EDU (Craig Kolb)
fritz@riverside.MR.Net (Fritz Lott)
hollasch@kgc.com (Steve Hollasch)
jens_alfke@powertalk.apple.com (Jens Alfke)
karsten@addx.stgt.sub.org (Karsten Weiss)
lhf@visgraf.impa.br (Luiz Henrique de Figueiredo)
orourke@cs.smith.edu (Joseph O'Rourke)
paik@mlo.dec.com (Samuel S. Paik)
sammy@icarus.smds.com (Samuel Murphy)
sanguish@digifix.com (Scott Anguish)
shoemake@graphics.cis.upenn.edu (Ken Shoemake)
slin@esri.com (Sum Lin)
spl@szechuan.ucsd.edu (Steve Lamont)
weilej@rpi.edu (Jason Weiler)
$Id: algorithms,v 1.14 1994/09/29 00:54:31 jdstone Exp $


Jon Stone Intergraph Corporation
jdstone@ingr.com Boulder, CO

EMail Fredric L. Rice / The Skeptic Tank
