Sunteți pe pagina 1din 666

G GG GGRAPHICS RAPHICS RAPHICS RAPHICS RAPHICS

G GG GGEMS EMS EMS EMS EMS


edit ed by
A AA AANDREW NDREW NDREW NDREW NDREW S. G S. G S. G S. G S. GLASSNER LASSNER LASSNER LASSNER LASSNER
Xerox Palo Alt o Research Cent er
Palo Alt o, Califor nia
AP PROFESSIONAL
Bost on San Diego New Yor k
Lond on Syd ney Tokyo Toront o
Copyright (c) 1995 by Academic Press, Inc.
GRAPHICS GEMS copyright (c) 1990 by Academic Press, Inc.
GRAPHICS GEMS II copyright (c) 1991 by Academic Press, Inc.
GRAPHICS GEMS III copyright (c) 1992 by Academic Press, Inc.
QUICK REFERENCE TO COMPUTER GRAPHICS TERMS
copyright (c) 1993 by Academic Press, Inc.
RADIOSITY AND REALISTIC IMAGE SYNTHESIS
copyright (c) 1993 by Academic Press Inc.
VIRTUAL REALITY APPLICATIONS AND EXPLORATIONS
copyright (c) 1993 by Academic Press Inc.
All rights reserved.
No part of this product may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including input into or storage in any information
system, other than for uses specified in the License Agreement, without permission
in writing from the publisher.
Except where credited to another source, the C and C++ Code may be used freely to
modify or create programs that are for personal use or commercial distribution.
Produced in the United States of America
ISBN 0-12-059756-X
To t he spirit s of creat ivit y and sharing t hat
imagine new invent ions and urge t heir
communicat ion
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER vi
CONTENTS
About t he Cover About t he Cover About t he Cover About t he Cover About t he Cover
The cover p ict u re was d esigned and p rod u ced by Thad Beier at Pacific Dat a images. The book insp ired
t he p ict u re, alt hou gh t here are only 74 gems on t he cover and over 100 in t he book. There are fou r
gem shap es rep eat ed many t imes in many color s. The bag is mod eled aft er t he bag a cer t ainScot ch comes
in. While t he p ict u re was creat ed over a t wo-mont h p er iod , it of cou r se came d own t ot he last minu t e
t o get it creat ed , so it was r ay-t r aced on 18 Silicon Gr ap hics wor kst at ions in abou t an hou r and a half.
The t ext behind t he gems and t he t it le is from t he animat ion scr ip t t hat p laces t he gems and t he r ay-
t r acing p rogr am t hat creat ed t he p ict u re from t hat scr ip t .
Thad Beier
Pacific Dat a Images

When And rew showed u s t he image d esigned for t he cover of Gr ap hics Gems and asked if we
wereint er est ed in d igit ally conver t ing it t o p r int able for m, we said What a wond er fu l image! It s going
t obe t r icky, bu t it will be fu n. It was bot h. The image color s were d esigned wit h resp ect t o a color
monit or, p r od u cing red , green, and blu e p ixels. For p r int ing, we need ed t o conver t t hese p ixels t o cyan,
magent a, yellow and black color sep ar at ions.
Whet her color s are d efined for a p r int er or a monit or, t hey can be d efined wit h resp ect t o a d evice-
ind ep end ent st and ard based on t he Commission Int er nat ionale d e lclair age (CIE) st and ard s for color
measu rement . Given su ch a d efinit ion, we can d efine t he gamu t , or set of all p ossible color s t hat can
be rep rod u ced by each d evice. Color s ou t sid e of t he d evice gamu t cannot be rep rod u ced . The figu re
shows a p lot of t he monit or, p r int er and image gamu t s over laid . It is easy t o see t hat t he monit or and
image gamu t s are mu ch larger t han t he p r int er gamu t , and t hat t he image near ly fills t he monit or gamu t .
To make t he best rep rod u ct ion of t he p ict u re, we had t o squ eeze t he image color s int o t he p r int er gamu t
in a way t hat maint ained t he ap p ear ance of t he image. We d id t his wit h a p iecewise, non-linear 3D
t r ansfor mat ion t hat collap sed t he monit or gamu t int o t he p r int er gamu t . The const r aint s on t his
t r ansfor mat ion were t hat color s shou ld move r ad ially t oward s t he light ness axis of t he color sp ace
~p r eser ves hu e an~ light ness at t he cost of sat u r at ion) and t hat color s ou t sid e of t he gamu t shou ld move
more t han color s insid e t he gamu t (p reser ves over all sat u r at ion).
Find ing t he best t r ansfor mat ion is st ill a resear ch p roblem. The image was p ar t icu lar ly challenging
becau se it filled t he monit or gamu t , so we had t o comp ress in all d irect ions at once. The t ext u re on
t he bag and t he sp ar kle of t he gems were ver y sensit ive t o var iat ions in t he t r ansfor mat ion; many
at t emp t s p rod u ced d u ll, p last ic looking gems or an u nt ext u red bag, even t hou gh t he absolu t e color
fid elit y was bet t er t han t he one chosen for final rep rod u ct ion (p roving t here is mu ch more t o good color
rep r od u ct ion t han t he color s).
Mau reen St one and Bill Wallace
Xer ox Palo Alt o Research Cent er
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER vii
CONTENTS
CONTENTS
It alic page numbers refer t o locat ion of corresponding C implement at ion.
Preface xv
Int roduct ion xvii
Mat hemat ical Not at ion xix
Pseudo-Code xxi
Cont ribut ors xxvi
1
2D GEOMETRY
Useful 2D Geomet r y 3
Tr igonomet r y Summar y 12
Useful Tr igonomet r y 13
Tr igonomet r ic Funct ions at Select Point s 18
Tr iangles 20
Gener at ing Rand om Point s in Tr iangles (649) 24
Fast LineEd ge Int er sect ions on a Unifor m Gr id (651) 29
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER viii
CONTENTS
Ant i-Aliasing Summar y 37
Area of Int er sect ion: Circle and a Half-Plane 38
Area of Int er sect ion: Circle and a Thick Line 40
Area of Int er sect ion: Two Circles 43
Ver t ical Dist ance from a Point t o a Line 47
A Fast 2D Point -on-Line Test (654) 49
Fast CircleRect angle Int er sect ion Checking (656) 51
2
2D RENDERING
Circles of Int egr al Rad ius on Int eger Lat t ices 57
Nice Number s for Gr aph Labels (657) 61
Efcient Gener at ion of Sampling Jit t er Using Look-up
Tables (660) 64
Scan Conver sion Summar y 75
Fast Ant i-Aliasing Polygon Scan Conver sion (662) 76
Gener ic Convex Polygon Scan Conver sion and Clipping (667) 84
ConcavePolygon Scan Conver sion (681) 87
Fast Scan Conver sion of Ar bit r ar y Polygons 92
Line-Dr awing Summar y 98
Digit al Line Dr awing (685) 99
Symmet r ic Double St ep Line Algor it hm (686) 101
Rend er ing Ant i-Aliased Lines (690) 105
An Algor it hm for Filling in 2D Wid e Line Bevel Joint s 107
Rend er ing Fat Lines on a Rast er Gr id 114
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER i x
CONTENTS
Two-Dimensional Clipping: A Vect or-Based Approach (694) 121
Per iod ic Tilings of t he Plane on a Rast er 129
3
IMAGE PROCESSING
Ant i-Aliasing Filt er s Summar y 143
Convenient Ant i-Aliasing Filt er s That Minimize
Bumpy Sampling 144
Filt er s for Common Resampling Tasks 147
Smoot hing Enlarged Monochrome Images 166
Med ian Find ing on a 3 3 Gr id (711) 171
Ord ered Dit her ing (713) 176
A Fast Algor it hm for Gener al Rast er Rot at ion 179
Useful 1-t o-1 Pixel Tr ansfor ms 196
Alpha Blend ing 210
4
FRAME BUFFER TECHNIQUES
Fr ame Buffer s and Color Maps 215
Read ing a Wr it e-Only Wr it e Mask 219
A Digit al Dissolve Effect (715) 221
Mapping RGB Tr iples ont o Four Bit s (718) 233
What Are t he Coord inat es of a Pixel? 246
Proper Treat ment of Pixels as Int eger s (719) 249
Nor mal Cod ing 257
Record ing Animat ion in Binar y Ord er for Progressive
Tempor al Renement (720) 265
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER x
CONTENTS
1-t o-1 Pixel Tr ansfor ms Opt imized t hrough
Color-Map Manipulat ion 270
A Seed Fill Algor it hm (721) 275
Filling a Region in a Fr ame Buffer 278
Precalculat ing Ad d resses for Fast Fills, Circles,
and Lines 285
A Simple Met hod for Color Quant izat ion:
Oct ree Quant izat ion 287
5
3D GEOMETRY
Useful 3D Geomet r y 297
An Efcient Bound ing Sphere (723) 301
Int er sect ion of Two Lines in Three-Space 304
Int er sect ion of Three Planes 305
Mapping Summar y 306
Digit al Car t ogr aphy for Comput er Gr aphics 307
Alber s Equal-Area Conic Map Project ion. (726) 321
Boxes and Spheres Summar y 326
Spheres-t o-Voxels Conver sion 327
A Simple Met hod for Box-Sphere Int er sect ion Test ing (730) 335
6
3D RENDERING
3D Gr id Hashing Funct ion (733) 343
Backface Culling 346
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xi
CONTENTS
Fast Dot Prod uct s for Shad ing 348
Scanline Dept h Gr ad ient of a Z-Buffered Tr iangle 361
Simulat ing Fog and Haze 364
Int er pret at ion of Text ure Map Ind ices 366
Mult id imensional Sum Tables 376
7
RAY TRACING
A Simple Ray Reject ion Test 385
RayObject Int er sect ion Summar y 387
Int er sect ion of a Ray wit h a Sphere 388
An Efcient RayPolygon Int er sect ion (735) 390
Fast RayPolygon Int er sect ion 394
Fast RayBox Int er sect ion (736) 395
Shad ow At t enuat ion for Ray Tr acing
Tr ansparent Object s 397
8
NUMERICAL AND PROGRAMMING
TECHNIQUES
Root Find ing Summar y 403
Cubic and Quar t ic Root s (738) 404
A Bzier Cur ve-Based Root -Find er (787) 408
Using St ur m Sequences t o Br acket Real Root s
of Polynomial Equat ions (743) 416
Dist ance Measures Summar y 423
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xii
CONTENTS
A High-Speed , Low Precision Square Root (756) 424
A Fast Approximat ion t o t he Hypot enuse (758) 427
A Fast Approximat ion t o 3D Euclid ean Dist ance 432
Full-Precision Const ant s 434
Conver t ing bet ween Bit s and Digit s 435
St or age-free Swapping 436
Gener at ing Rand om Int eger s 438
Fast 2D3D Rot at ion 440
Bit Pat t er ns for Encod ing Angles 442
Bit Int er leaving for Quad - or Oct rees (759) 443
A Fast HSL-t o-RGB Tr ansfor m (763) 448
9
MATRIX TECHNIQUES
Mat r ix Id ent it ies 453
Rot at ion Mat r ix Met hod s Summar y 455
Tr ansfor ming Axes 456
Fast Mat r ix Mult iplicat ion 460
A Vir t ual Tr ackball 462
Mat r ix Or t hogonalizat ion (765) 464
Rot at ion Tools 465
Mat r ix Inver sion (766) 470
Mat r ices and Tr ansfor mat ions 472
Efcient Post -Concat enat ion of Tr ansfor mat ion Mat r ices (770) 476
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xiii
CONTENTS
10
MODELING AND TRANSFORMATIONS
Tr ansfor mat ion Id ent it ies 485
Fixed -Point Tr igonomet r y wit h CORDIC It er at ions (773) 494
Using Quat er nions for Cod ing 3D Tr ansfor mat ions (775) 498
3D Viewing and Rot at ion Using Or t honor mal Bases (778) 516
The Use of Coord inat e Fr ames in Comput er Gr aphics 522
For ms, Vect or s, and Tr ansfor ms (780) 533
Proper t ies of Sur face-Nor mal Tr ansfor mat ions 539
Tr ansfor ming Axis-Aligned Bound ing Boxes (785) 548
Const ruct ing Shapes Summar y 551
Dening Sur faces from Sampled Dat a 552
Dening Sur faces from Cont our Dat a 558
Comput ing Sur face Nor mals for 3D Mod els 562
Calculat ion of Reference Fr ames along a Space Cur ve 567
11
CURVES AND SURFACES
Planar Cubic Cur ves 575
Explicit Cubic Spline Int er polat ion For mulas 579
Fast Spline Dr awing 585
Some Proper t ies of Bzier Cur ves 587
Tut or ial on For ward Differencing 594
Int egr at ion of Ber nst ein Basis Funct ions 604
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xiv
CONTENTS
Solving t he Nearest -Point -on-Cur ve Problem (787) 607
An Algorithm for Automatically Fitting Digitized Curves (797) 612
References 808
Index 822
xv GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xv
PREFACE
P PP PPREFACE REFACE REFACE REFACE REFACE
Welcome t o Graphics Gems: a collect ion of algor it hms, progr ams, and
mat hemat ical t echniques for t he comput er gr aphics progr ammer.
I have wanted a book like this for a long time. I have written tens of
thousands of lines of computer graphics code in the last few years, and I
know that much of it could have been better. I even knew that when I
wrote it. But often I didnt have the time to nd the best data structure or
d esign t he most elegant or robust algor it hm. Somet imes I only realized
how t o d o somet hing well aft er d oing it t he wrong way r st .
As time went on I found myself sharing my experiences and tricks with
friends and colleagues, who offered their insights in return. Though we
were trading our hard-earned lessons with each other, there was no more
gener al or public forum where we could d ocument t hese id eas per ma-
nent ly. And I somet imes wond ered what insight s I was missing simply
because I could nt t alk wit h ever yone in t he eld .
Thus Graphics Gems was bor n. This book was creat ed for t he wor king
gr aphics progr ammer. It s prod uct ion conclud es one t ur n of a cycle of
discovery, documentation, editing, publishing, and reading, which will
lead to new discoveries. The articles in this book are not research papers.
The formal publication process in journals and conferences works well for
d isseminat ing t he archit ect ure of large, new id eas. Rat her, t his book
focuses on the nuts-and-bolts of programming and implementation, sup-
plying t he d et ails oft en left out of t echnical paper s.
How Thi s Book Came t o Be How Thi s Book Came t o Be How Thi s Book Came t o Be How Thi s Book Came t o Be How Thi s Book Came t o Be
In the spring of 1989 I decided that there was probably enough informal
(and unpublished) community wisdom in the graphics eld that we could
xvi GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xvi
PREFACE
put together a little book of clever ideas. The books title was inspired
by Jon Bentleys excellent Programming Pearls column published in
the Communications of the ACM. At Siggraph 89 in Boston I handed
out the rst call for contributions, which was followed up at other
conferences and in graphics publications. I asked for tools that belong in
a graphics programmer s toolbox, yet dont appear in the standard
literature.
I expected about 25 or 30 contributions; by the deadline in January
1990 over 110 Gems had been submitted. As contributions arrived I let
the scope of the book grow slightly, and accepted a few short tutorials. I
accepted these longer pieces because they were in tune with the philoso-
phy of the book, presenting useful information not easily accessible in the
current literature.
Most of the contributions went through at least one revision step after
submission. I have attempted to make the book consistent in presentation
by asking everyone to use a uniform mathematical notation and pseudo-
code. I hope that most of the Gems in this book are accessible to most
readers.
I originally planned to include a set of appendices providing source
code in various programming languages. But, except for one short assem-
bly-language routine, all the code submitted was in C! Thus there is one
substantial appendix containing C implementations of many Gems. This
source code is public domainit is yours to use, study, modify, and
share. By the time you read this, all the code in the appendix should be
available on many of the popular networks, so you need not type it in
yourself.
I would like to thank my employer, the Xerox Corporation, for its
support of this project as part of my work in the Electronic Documents
Lab at its Palo Alto Research Center (PARC).
It gives me great pleasure to offer you a book that I have always wanted
to have myself. Through the efforts of over 50 contributors, you are
holding many valuable nuggets of knowledge and craft earned by experi-
ence over many years. We hope that you will nd these Gems useful in
your work, enhancing your programs and extending your reach.
Enjoy!
Andrew S. Glassner
February, 1990
Palo Alt o, California
xvii GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xvii
INTRODUCTION
I II IINTRODUCTION NTRODUCTION NTRODUCTION NTRODUCTION NTRODUCTION
This int rod uct ion is d esigned t o help you get t he most out of t his book.
I will d iscuss some aspect s of t he books st ruct ure, and t hen summar ize
t he mat hemat ical not at ion and t he pseud o-cod e used in many of t he
Gems.
Some of the Gems originally submitted to this book presented different
solut ions t o t he same problem. Ive includ ed some of t hese mult iple
approaches when I felt t hey d emonst r at ed int erest ing alt er nat ives t hat
were useful eit her for t heir pr act ical d ifferences or ed ucat ional value.
Some Gems place a high premium on speed ; ot her s emphasize algor it h-
mic clar it y. Similar ly, some Gems t ake slight ly d ifferent views of t he
same problem: For example, t here are many ways t o d r aw a line, but
your need s are quit e d ifferent for t hick lines, t hin lines, ant i-aliased
lines, and so on.
I have ind icat ed connect ions bet ween Gems in t his book in t wo ways.
When relat ed Gems are all in t he same chapt er, I have grouped t hem
t oget her and wr it t en a shor t summar y t hat appear s at t he st ar t of t he
group. If you refer t o one of t he Gems in such a group you should
at least t ake a look at t he ot her s. When relat ed Gems are not sequent ial
I have includ ed a list ing of ot her relevant Gems und er t he head ing
See also at t he end of t he Gem. The See also list s are not
exhaust ive, but t hey should point you in t he r ight d irect ions.
To make t he most of t he connect ions in t his book, I suggest you skim
br iey all t he Gems once. I somet imes nd t hat I can apply an
algor it hm in a set t ing complet ely d ifferent from t he one for which it
was or iginally d esigned ; knowing what t he book cont ains will help
you make t hese leaps of int er pret at ion.
xviii GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xviii
INTRODUCTION
All of t he references are collect ed t oget her int o a single bibliogr aphy.
Each reference ent r y cont ains back-point er s t o all t he Gems t hat refer-
ence it . You may d r aw fur t her connect ions bet ween Gems by following
t hese point er s.
Except for some of t he t ut or ials, most Gems d o not provid e t he
background mathematics for their discussions. Usually this math does not
go beyond 3-dimensional vector geometry; you can nd good summaries
of this topic in Kindle, or in virtually any modern textbook on introduc-
t or y calculus and analyt ic geomet r y, such as Fland er s. Many gr aphics
progr ammer s have a copy of Beyer on t heir shelves; t his st and ard
reference work distills many important mathematical results into a form
t hat is easy t o access.
Some Gems use matrix techniques for geometric transformations. These
t echniques are d escr ibed in d et ail in t he st and ard gr aphics t ext s. Our
convent ion is t hat point s are represent ed by row vect or s, and are t r ans-
for med by post -mult iplicat ion wit h a mat r ix. You must be careful when
transferring the results in this volume to other systems, for they may use
a d ifferent convent ion. For example, t he PHIGS st and ard and t he Dore
rendering system use pre-multiplication of column vectors. You can make
the switch between conventions simply by transposing the transformation
mat r ix.
Most of t he Gems assume t hat you are familiar wit h most of t he
fundamental material of computer graphics. If you nd that youre left
behind somewhere, you may wish t o consult t he classic st and ard t ext s,
Newman and Foley, or one of t he more mod er n t ext books t hat have
appeared recently; some of these references are listed in the bibliography.
Beyer, W. B. CRC Standard Mathematical Tables, CRC Press, Inc., Boca Raton, Florida.
(Upd at ed year ly.)
Flanders, H. and Price, J. (1978). Calculus with Analytic Geometry. Academic Press,
New Yor k.
Foley, J., van Dam, A., Feiner, S., and Hughes, J. (1990). Fundamentals of Interactive
Computer Graphics, Addison-Wesley, Read ing, MA.
Kind le, J.H. (1950). Plane and Solid Analyt ic Geomet ry Schaums Out line Series.
McGr aw-Hill, New Yor k.
Newman, W.M., and Sproull, R..F. (1979). Principles of Interactive Computer Graph-
ics, 2nd edit ion. McGr aw-Hill, New Yor k.
xix GRAPHICS GEMS III Edit ed by ANDREW S. GLASSNER xix
MATHEMATICAL NOTATION
M MM MMATHEMATICAL ATHEMATICAL ATHEMATICAL ATHEMATICAL ATHEMATICAL
N NN NNOTATION OTATION OTATION OTATION OTATION
Geomet r i c Obj ect s Geomet r i c Obj ect s Geomet r i c Obj ect s Geomet r i c Obj ect s Geomet r i c Obj ect s
0 t he number 0, t he zero vect or, t he point (0, 0), t he
point (0, 0, 0)
a, b, c t he real number s (lower case it alics)
P, Q point s (upper-case it alics)
l , m lines (lower-case bold )
A, B vect or s (upper-case bold )(component s A
i
)
M mat r ix (upper-case bold )
, angles (lower-case greek)
Der i ved Obj ect s Der i ved Obj ect s Der i ved Obj ect s Der i ved Obj ect s Der i ved Obj ect s
A

the vector perpendicular to A (valid only in 2D, where


A

= (A
y
, A
x
)
M
-1
t he inver se of mat r ix M
M
T
t he t r anspose of mat r ix M
M
*
t he ad joint of mat r ix M

M
1

det M ( )

_
,

| M| d et er minant of M
d et (M) same as above
M
i,j
element from row i, column j of matrix M (top-left is
(0, 0)
M
i,
all of row i of mat r ix M
xx GRAPHICS GEMS III Edit ed by ANDREW S. GLASSNER xx
MATHEMATICAL NOTATION
M
,j
all of column j of Mat r ix

ABC t r iangle for med by point s A, B, C


ABC angle for med by point s A, B, C wit h ver t ex at B
Basi c Oper at or s Basi c Oper at or s Basi c Oper at or s Basi c Oper at or s Basi c Oper at or s

+, , / ,
st and ard mat h oper at or s

t he d ot (or inner or scalar ) prod uct

t he cross (or out er or vect or ) prod uct


Basi c Expr essi ons and Funct i ons Basi c Expr essi ons and Funct i ons Basi c Expr essi ons and Funct i ons Basi c Expr essi ons and Funct i ons Basi c Expr essi ons and Funct i ons

x
]
floor of x (largest int eger not great er t han x)

x
1
ceiling of x (smallest int eger not smaller t han x)
a| b mod ulo ar it hmet ic; remaind er of a b
a mod b same as above

B
i
n
t ( ) Ber nst ein polynomial =

n
i

_
,

t
i
1 t ( )
n i
, i 0Ln

n
i

_
,
binomial coefficient

n!
n i ( )!i!
xxi
PSEUDO-CODE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxi
P PP PPSEUDO-CODE SEUDO-CODE SEUDO-CODE SEUDO-CODE SEUDO-CODE
Decl ar at i ons ( not r equi r ed) Decl ar at i ons ( not r equi r ed) Decl ar at i ons ( not r equi r ed) Decl ar at i ons ( not r equi r ed) Decl ar at i ons ( not r equi r ed)
name: TYPE init ialValue;
examples:

:real 3.14159;
v: array [0..3] of i nteger [0, 1, 2, 3];
Pr i mi t i ve Dat a Types Pr i mi t i ve Dat a Types Pr i mi t i ve Dat a Types Pr i mi t i ve Dat a Types Pr i mi t i ve Dat a Types
array [lower Bound ..upper Bound ] of TYPE;
bool ean
char
i nteger
real
doubl e
poi nt
vector
matri x3
equivalent t o:
mat rix3: record [array [0..2] of array [0..2] of real;];
example: m:Matrix3 [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
m[2][1] is 8.0
m[0][2] 3.3; assigns 3.3 t o upper-right corner of mat rix
xxii
PSEUDO-CODE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxii
matri x4
equivalent t o:
mat rix4: record [array [0..3] of array [0..3] of real;];
example: m: Mat rix4 [
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]];
m[3][1] is 14.0
m[0][3] 3.3; assigns 3.3 t o upper-right corner of mat rix
Recor ds ( St r uct ur es) Recor ds ( St r uct ur es) Recor ds ( St r uct ur es) Recor ds ( St r uct ur es) Recor ds ( St r uct ur es)
Record denit ion:
Box: record [
left , r ight , t op, bot t om: i nteger;
];
newBox: Box new[Box];
dynamically allocate a new instance of Box and return a pointer to it
newBox.left 10;
t his same not at ion is appropriat e whet her newBox is a point er or
st ruct ure
Ar r ays Ar r ays Ar r ays Ar r ays Ar r ays
v: array [0..3] of integer [0, 1, 2, 3]; v is a four-element array of integers
v[2] 5; assign t o t hird element of v
Comment s Comment s Comment s Comment s Comment s
A comment may appear anywhereit is indicat ed by it alics
xxiii
PSEUDO-CODE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxiii
Bl ocks Bl ocks Bl ocks Bl ocks Bl ocks
begi n
St at ement ;
St at ement ;

L
end;
Condi t i onal s and Sel ect i ons Condi t i onal s and Sel ect i ons Condi t i onal s and Sel ect i ons Condi t i onal s and Sel ect i ons Condi t i onal s and Sel ect i ons
i f Test
then St at ement ;
[el se St at ement ]; else clause is opt ional
result = sel ect It em from
inst ance: St at ement ;
endcase: St at ement ;
Fl ow Cont r ol Fl ow Cont r ol Fl ow Cont r ol Fl ow Cont r ol Fl ow Cont r ol
for Cont rolVar iable: Type Init ialExpr, Next Expr do
St at ement ;
endl oop;
unti l Test do
St at ement ;
endl oop;
whi l e Test do
St at ement ;
endl oop;
l oop; go direct ly t o t he next endloop
exi t; go direct ly t o t he rst st at ement aft er t he next endloop
return[value] ret urn value as t he result of t his funct ion call
xxiv
PSEUDO-CODE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxiv
Logi cal Connect i ves Logi cal Connect i ves Logi cal Connect i ves Logi cal Connect i ves Logi cal Connect i ves
or, and, not, xor
Bi t wi se Oper at or s Bi t wi se Oper at or s Bi t wi se Oper at or s Bi t wi se Oper at or s Bi t wi se Oper at or s
bi t-or, bi t-and, bi t-xor
Rel at i ons Rel at i ons Rel at i ons Rel at i ons Rel at i ons
=, , >, , <,
Assi gnment Symbol Assi gnment Symbol Assi gnment Symbol Assi gnment Symbol Assi gnment Symbol

(not e: t he t est for equalit y is = )


Avai l abl e Funct i ons Avai l abl e Funct i ons Avai l abl e Funct i ons Avai l abl e Funct i ons Avai l abl e Funct i ons
These funct ions are d ened on all d at a t ypes
min(a, b) ret urns minimum of a and b
max(a, b) ret urns maximum of a and b
abs(a) ret urns absolut e value of a
sin(x) sin(x)
cos(x) cos(x)
t an(x) t an(x)
arct an(y) arct an(y)
arct an2(y, x) arct an(y/x), defined for all values of x and y
arcsin(y) arcsin(y)
arccos(y) arccos(y)
r shift (x, b) shift x right b bit s
lshift (x, b) shift x left b bit s
swap(a, b) swap a and b
lerp(, l, h) linear interpolation: ((1 )*l) +(*h) = l + ((h l))
xxv
PSEUDO-CODE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxv
clamp(v, l, h) return l if v < l, else h if v > h, else v: min(h,max(l,v))
oor (x) or

x
]
round x t owards 0 t o rst int eger
ceiling(x) or

x
1
round x away from 0 t o rst int eger
round(x) round x to nearest integer, if frac(x) = .5, round towards
0
fr ac(x) fract ional part of x
CONTRIBUTORS
xxvi GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxvi
CONTRIBUTORS
Numbers in parent heses indicat e pages on which aut hors Gems begin
James Ar vo (335, 548), Apollo Syst ems Division of Hewlet t -Packard, 330 Billerica
Road, Chelmsford, Massachuset t s 01824
Did ier Bad ouel (390), IRISA /INRIA, Campus Universit aire Beaulieu, 35042 Rennes
Cdex, France
Paul D. Bame (321), Hewlet t -Packard, P.0. Box 617, Colorado Springs, Colorado
80901-0617
Jules Blooment hal (567), Xerox PARC, 3333 Coyot e Hill Road, Palo Alt o, California
94304
Richard Car ling (470), 13 Overlook Drive, Bedford, Massachuset t s 01730
St eve Cu nningham (516), Depart ment of Comput er Science, California St at e
Universit y, St anislaus, Turlock, California 95380
Joseph M. Cychosz (64, 476), Purdue Universit y CADLAB, Pot t er Engineering Cent er,
West Lafayet t e, Indiana 47907
Rober t Dawson (424), Dalhousie Universit y, 1179 Tower Road, Halifax, Nova Scot ia
B3H 2Y7, Canada
Ken Fishkin (278, 448), Pixar, Inc., 3240 Kerner Boulevard, San Rafael, California
94901
Michael Ger vaut z (287), Technische Universit t Wien, Inst it ut fur Prakt ische Infor-
mat ik, Karlsplat z 13/180, A-1040 Wien, Aust ria
And rew S. Glassner (3, 13, 215, 257, 297, 364, 366, 376, 438, 562, 575) Xerox PARC,
3333 Coyot e Hill Road, Palo Alt o, California 94304
CONTRIBUTORS
xxvii GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxvii
Ronald Gold man (20, 304, 305, 472, 587, 604), Depart ment of Comput er Science,
Universit y of Wat erloo, Wat erloo, Ont ario N2L 3G1, Canada
Julian Gomez (585), MacroMind, Inc., 410 Townsend St ., Suit e 408, San Francisco,
California 94107
Ned Greene (485), Apple Comput er, Inc., 20705 Valley Green Drive, MS 60-W,
Cupert ino, California 95014
Mar k Hall (552, 558), Comput er Science Depart ment , Rice Universit y, P.0. Box 1892,
Houst on, Texas 77251-1892
St ephen Hawley (176), 13 Cat herine Lane #2, Morrist own, New Jersey 07960
Paul S. Heckber t (61, 84, 87, 99, 246, 265, 275), 1415 Arch St reet , Berkeley, California
94708
D. G. Hook (416), Depart ment of Engineering Comput er Resources, Facult y of Engi-
neering, The Universit y of Melbourne, Melbourne, Aust ralia
Jeff Hult quist (346, 388, 462), Mailst op T-045-1, NASA Ames Research Cent er, Moffet t
Field, California 94035
Paul Lalond e (424), Dalhousie Universit y, 1179 Tower Road, Halifax, Nova Scot ia
B3H 2Y7, Canada
Greg Lee (129), Weit ek Corporat ion, 1060 East Arques Avenue, Sunnyvale, California
94086
Mar k Lee (348), Amoco Product ion Company, Tulsa Research Cent er, P.0. Box 3385,
Tulsa, Oklahoma 74102
Pat r ick-Gilles Maillot (498), Sun Microsyst ems, Inc., Deskt op and Graphics Develop-
ment Organizat ion, 2550 Garcia Avenue, MS 21-04, Mount ain View, California
94043
P. R. McAr ee (416), Depart ment of Engineering Comput er Resources, Facult y of
Engineering, The Universit y of Melbourne, Melbourne, Aust ralia
Claud io Mont ani (327), Ist it ut o di Elaborazione delInformazione, Consiglio Nazionale
delle Ricerche, Via Sant a Maria 46, 56100 Pisa, It aly
Jack C. Mor r ison (76), 5654 Sout h Jackpine Road, Evergreen, Colorado 80439
Mike Mor t on (221), P.0. Box 11299, Honolulu, Hawaii 96828
John Olsen (166), Hewlet t -Packard, Mail St op 73, 3404 E. Harmony Road, Fort
Collins, Colorado 80525
Alan W. Paet h (18, 49, 57, 171, 179, 219, 233, 249, 307, 427), Comput er Graphics
Laborat ory, Depart ment of Comput er Science, Universit y of Wat erloo, Wat erloo,
Ont ario N2L 3G1, Canada
Mar k J. Pavicic (144), Depart ment of Comput er Science, Nort h Dakot a St at e Univer-
sit y, 300 Minard Hall, SU St at ion, P.0. Box 5075, Fargo, Nort h Dakot a 58105-5075
CONTRIBUTORS
xxviii GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxviii
And rew Pearce (397), Alias Research, Inc., 110 Richmond, St reet East #550, Toront o,
Ont ario M5C IPI, Canada
Mike Penk (129), 525 Sout h East 15t h St reet , Apart ment #2, Port land, Oregon 97214
Michael E. Pique (465), Research Inst it ut e of Scripps Clinic, MB-5, 10666 Nort h
Torrey Pines Road, La Jolla, California 92037
Wer ner Purgat hofer (287), Technische Universit t Wien, Inst it ut fur Prakt ische Infor-
mat ik, Karlsplat z 13 / 180, A-1040 Wien, Aust ria
Er ic Raible (464), 1591 Ensenada Drive, Campbell, California 95008
Richard Rasala (579), Nort heast ern Universit y, 117 Cullinane Hall, Bost on, Mas-
sachuset t s 02115
Jack Ritter (107, 301, 385, 432, 440), Versat ec, Inc., MS 1-7, 2710 Walsh Avenue, P.0.
Box 58091, Sant a Clara, California 95052-8091
Philip J. Schneid er (408, 607, 612), Universit y of Geneva CUI 12 rue du Lac, Geneva
CH-1207, Swit zerland
Dale Schumacher (196, 270), 399 Beacon Avenue, St . Paul, Minnesot a 55104-3527
Jochen Schwar ze (404), ISA GmbH, Azenberst rasse 35, 7000 St ut t gart 1, Federal
Republic of Germany
Rober t o Scopigno (327), Ist it ut o di Elaborazione delllnformazione, Consiglio
Nazionale delle Ricerche, Via Sant a Maria 46, 56100 Pisa, It aly
Clifford A. Shaffer (51, 443), Depart ment of Comput er Science, Virginia Technical
Universit y, Blacksburg, Virginia 24061
And rew Shapir a (29), ECSE Depart ment , Rensselaer Polyt echnic Inst it ut e, Troy, New
York, 12180
Ken Shoemake (442), Xerox PARC, 3333 Coyot e Hill Road, Palo Alt o, California
94304
Hans J. W. Spoelder (121), Physics Applied Comput er Science, Facult y of Physics and
Ast ronomy, Vrije Universit eit , De Boelelaan 1081, 1081 HV Amst erdam, The
Net herlands
Kelvin Thompson (38, 40, 43, 47, 105, 210, 361, 434, 435, 453, 456, 460), 903 Romeria
#204, Aust in, Texas 78757-3435
Greg Tur k (24), Depart ment of Comput er Science, Sit t erson Hall, UNC-Chapel Hill,
Chapel Hill, Nort h Carolina 27599-3175
Ken Tur kowski (147, 494, 522, 539), Apple Comput er, Inc, 20705 Valley Green Drive,
MS 60-W, Cupert ino, California 95014
Fons H. Ullings (121), Physics Applied Comput er Science, Facult y of Physics and
Ast ronomy, Vrije Universit eit , De Boelelaan 1081, 1081 HV Amst erdam, The
Net herlands
CONTRIBUTORS
xxix GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER xxix
Bill Wallace (285), 101 Earl Grey Road, Toront o, Ont ario M4J 3L6, Canada
Bob Wallis (92, 114, 129, 533, 594), Weit ek Corporat ion, 1060 East Arques Avenue,
Sunnyvale, California 94086
And rew Woo (394, 395), Alias Research, Inc., 110 Richmond St reet East , Toront o,
Ont ario M5C 1P1 Canada
Br ian Wyvill (101, 343, 436), Universit y of Calgary, Comput er Science Depart ment ,
2500 Universit y Drive N.W., Calgary, Albert a T2N 1N4, Canada
2D GEOMETRY
1
3
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3
I. 1 I. 1 I. 1 I. 1 I. 1
U UU UUSEFUL SEFUL SEFUL SEFUL SEFUL
2 22 22D GEOMETRY D GEOMETRY D GEOMETRY D GEOMETRY D GEOMETRY
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
Many of t he for mulae in t his sect ion are 2D specializat ions of a more
gener al solut ion. Why d ont we bot her giving t he mult id imensional solu-
t ion in it s full gener alit y? There are at least t wo good reasons t hat t he
equat ions in t his sect ion are most ly valid only in 2D: t hey eit her prod uce
a unique answer, or t hey require less comput at ion. These ad vant ages are
usually relat ed t o t he fact s t hat in 2D Euclid ean geomet r y, nonpar allel
lines int er sect and t here is exact ly one line per pend icular t o a given line.
Some of t hese for mulae are valid in higher d imensions, and are re-
peat ed in t he Gem on Useful 3D Geomet ry wit hout much change in t he
not at ion. Ot her s are gener alized in t hat Gem, when appropr iat e.
I will use a progr ammer s not at ion t o express t he for mulae. This allows
us t o express some comput at ions in t er ms of previous result s. I use t he
prex V2 t o d ist inguish t he t echniques for 2D vect or geomet r y in t his
sect ion from t echniques wit h similar names in t he 3D sect ion. In t his
sect ion t he d ot (or inner or scalar ) prod uct of t wo vect or s A and B will be
wr it t en A B; t his may be t r anslat ed for implement at ion as V2 Dot (A, B).
I will somet imes t reat point s as vect or s; t his will be allowed wit h t he
und er st and ing t hat a point P will represent a vect or P wit h t ail at t he
or igin and head at P; t hus, t he coord inat e d escr ipt ions of bot h ent it ies
will have t he same values.
Record Line:[
implicit : N, c; Point s P sat isfy N P + c = 0 (see Fig. 1a)
explicit: U, V; Points P satisfy P = U + Vt for some scalar t (see
Fig. 1b)
normalized: BOOL FALSE True if and only if N = V = 1
]
4
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4
Figur e. 1
Line st ruct ures of t his for m will be represent ed in it alic (e.g., l, m).
Record Circle: [
cent er : C;
r ad ius: r
]
Ci r cl es of t h i s for m wi l l be r ep r esen t ed by ca p i t a l r oma n l et t er s
(e.g., A, B) (see Fig. 2).
Figure 2. Figure 3.
5
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5
V2 Normal i ze
A V2 Nor malize (A)

A
A
V2Length(A)
V2 Dot
d V2 Dot (A, B)
d A
x
B
x
+ A
y
B
y
V2 Impl i ci t to Expl i ci t
l V2 Implicit t o Explicit (l)
l
U
V2 Point on Line (l) Nearest t he Or igin
l
V
V2 Per pend icular (V2 Reect (l
N
))
V2 Expl i ci t to Impl i ci t
l V2 Explicit t o Implicit (l)
l
N
V2 Per pend icular (l
V
)
l
c
l
N
l
U
V2 Li ne Tangent to a Ci rcl e at a Poi nt
l V2 Line Tangent t o a Circle (C) at a Point (P) (see Fig. 3)
l
N
P C
C
l
c
(l
N
P)
V2 Perpendi cul ar
N V2 Per pend icular (V) (see Fig. 4)
N ( V
x
, V
y
)
V2 Reect
N V2 Reect (V) (see Fig. 5)
N ( V
y
, V
x
)
V2 Length
d V2 Lengt h (A)

d A A
V2 Intersecti on of a Ci rcl e and a Li ne
P1, P2 V2 Int er sect ion of a Circle (C) and a Line (l) (see Fig. 6)
G l
U
C
C
6
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6
Figure 4. Figure 5.
Figure 6. Figure 7.
a l
V
l
V
b 2(l
V
G)
c (G G)

C
r
2
d b
2
4ac
i f d < 0
then no int er sect ion
el se
P1 ( b +

d )/ 2a
P2 ( b

d )/ 2a
V2 Li nes Tangent to Two Ci rcl es Meeti ng Outsi de
l, m V2 Lines Tangent to Two Circles (A, B) Meeting Outside (see Fig. 7)
To make life easier, we label circles A and B so t hat A
r
B
r
.
7
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7
We assume t hat A
C
is t he origin, and B
C
is on t he X axis.
We build a line parallel t o t he X axis t hrough H; it int ersect s line A
C
G at
point J. Thus by const ruct ion,

JG = A
r
B
r
Not e from t he gure t hat cos =

JG
JH
.

arccos
A
r
B
r
A
C
B
C

_
,
G A
C
+ A
r
(cos, sin)
G A
C
+ A
r
(cos , sin )
H B
C
+ B
r
(cos, sin)
H B
C
+ B
r
(cos , sin )
l V2 Line t hrough 2 Point s(G,H)
m V2 Line t hrough 2 Point s(G, H)
V2 Li nes Tangent to Two Ci rcl es Meeti ng Insi de
l, m V2 Lines Tangent to Two Circles (A, B) Meeting Inside (see Fig. 8 )
From similar t riangles, observe t hat

A
r
x

B
r
d x
.
d is t he dist ance bet ween t he cent ers: d B
C
A
C
.

x B
C
A
C
( )
A
r
A
r
+ B
r

_
,

M A
C
+ x
Observe from t he gure t hat cos =

A
r
x
.

arccos
A
r
x

_
,
G (A
r
cos, A
r
sin) + A
C
H (B
r
cos, B
r
Sin) + B
C
l V2 Line t hrough 2 Point s (G, M)
m V2 Line t hrough 2 Point s (H, M)
V2 Li nes Tangent to Ci rcl e and Perpendi cul ar to Li ne
l, m V2 Lines Tangent t o Circle (C) and Per pend icular t o Line (k) (see
Fig. 9)
P C
C
+ C
r
k
V
Q C
C
C
r
k
V
l
N
m
N
k
V
l
c
(l
N
P)
8
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8
Figur e 8.
m
c
(l
N
Q)
l V2 Implicit t o Explicit (l)
m V2 Implicit t o Explicit (m)
Poi nt on Ci rcl e Nearest Poi nt
Q Point on Circle (C) Nearest Point (P) (see Fig. 10)
Q C
C
+ C
r
V2 Nor malize(P C
C
)
Figur e 9.
9
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9
Figure 10. Figure 11.
V2 Li ne Through Two Poi nts
l V2 Line Through Two Point s (A, B)
l
U
A
l
V
V2 Nor malize(B A)
l V2 Explicit t o Implicit (l)
V2 Normal i ze
l V2 Nor malize(l)
l
c
l
c
/ V2 Lengt h (l
N
)
l
N
V2 Nor malize(l
N
)
l
V
V2 Nor malize(l
V
)
l
nor malized
TRUE
V2 Di stance from Poi nt to Li ne
d V2 Dist ance fr om Point (P) t o Line (l) (see Fig . 11)
Q V2 Point on Line (l) Nearest t o Point (P)
d V2 Dist ance bet ween Point (P) t o Point (Q)
V2 Poi nt on Li ne Nearest Ori gi n
P V2 Point on Line (l) Nearest t he Or igin
d V2 Point on Line (l) Nearest Point (0)
V2 Poi nt on Li ne Nearest Poi nt
Q V2 Point on Line (l) Nearest Point (P)
Fornot at ionalconveniencein t his discussion,we writ e N for l
N
and c
for l
c
.
Observat ion 1: Since Q is on l, t hen (N Q) + c = 0.
Observat ion 2: Thest raight line t hat joins Pand Q is perpendicular t ol,
so P = Q + qN, for some value of q. Rewrit e t his as Q = P qN.
1 0
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 0
Plug t his expression for Q int o t he line equat ion, dist ribut e t he dot
product , and solve for q:
N (P qN) + c = 0
N P q(N N) + c = 0
q = (c + N P)/ (N N).
We now plug t his value for q back int o t he equat ion in observat ion 2 t o
nd Q:
q l
c
+ (l
N
P )
i f not l
nor malized
then q

q
V2 Length l
N
( )
Q P ql
N
.
V2 Di stance between Poi nt and Poi nt
d V2 Dist ance bet ween Point (P) and Point (Q)
d V2 Lengt h(P Q))
V2 Li ne Perpendi cul ar to Li ne through Poi nt
m V2 Line Per pend icular t o Line (l) t hrough Point (P)
General Sol uti on
Q V2 Point on Line (l) Nearest Point (P)
m V2 Line t hrough Point (P) and Point (Q)
Di rect Sol uti ons (A

= V2 Per pend icular (A))
V2 Cosi ne of Angl e between Li ne and Li ne
d V2 Cosine of Angle bet ween Line (l) and Line (m)
d (l
V
m
V
)
i f not l
nor malize
and m
nor malized
then d

d
V2 Length(l
V
) V2 Length(m
V
)
INPUT
explicit implicit
explicit m
U
P m
U
P
m
V
l
V

m
V
l
N
OUTPUT
implicit m
N
l
V
m
N
l
N

m
c
l
V
P m
c
l
N


P
1 1
I.1 USEFUL 2D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 1
V2 Poi nt of Intersecti on between Li ne and Li ne
P V2 Point of Int er sect ion bet ween Line (l) and Line (m)
The point P must be on bot h lines, so it sat ises bot h line equat ions. Writ e
one explicit ly and one implicit ly: l
N
P + l
c
= 0, and P = m
U
+ t m
V
Since bot h are t rue at t he same t ime, plug t he explicit int o t he implicit ,
dist ribut e t he dot product , and solve for t :
l
N
(m
U
+ t m
V
) + l
c
= 0
(l
N
m
U
) + t (l
N
m
V
) + l
c
= 0

t
l
c
+ (l
N
m
U
)
l
N
m
V
Now it can happen t hat l
N
m
V
= 0. This indicat es t hat t he t wo lines are
parallel, and t here is no int ersect ion at all. Ot herwise, we plug t his ualue
of t back int o t he explicit form t o nd t he point of int ersect ion:
d l
N
m
V
i f d = 0
then Er ror [no point of int er sect ion]
el se

Pm
U

(l
N
m
U
)+l
c
d
m
V
V2 Parameter of Poi nt on Li ne from Poi nt to Poi nt
a V2 Par amet er of Point (P) on Line from Point (Q) t o Point (R)

a
V2 Distance between Point P ( ) to Point Q ( )
V2 Distance between Point P ( ) to Point Q + R ( )
V2 Area of Pol ygon
a V2 Area of Polygon (P)
polygon has n point s, P0, P1, . . . Pn 1

a
1
2
x
i
y
i +1 ( ) mod n
y
i
x
i +1 ( ) mod n
( )
i 1
n1

See also Useful 3D Geomet r y (297); Useful Tr igonomet r y (13)


1 2
TRIG SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 2
I II II
T TT TTRIG SUMMARY RIG SUMMARY RIG SUMMARY RIG SUMMARY RIG SUMMARY
Th e fol l owi n g t wo Gems p r ovi d e some r el a t i on sh i p s t h a t ma y
p r ove u sefu l wh en wor ki n g on t r i gon omet r y p r obl ems. Th e r st
Gem i n cl u d es some r el a t i on sh i p s ba sed on t h e geomet r y of
p l a n a r t r i a n gl es; for mor e d i scu ssi on on t h i s t op i c see Trian-
gles. Th e secon d Gem p r ovi d es si mp l e cl osed -for m va l u es for
t h e ma jor t r i g fu n ct i on s a t a n u mber of sp eci a l a n gl es. Th ese
va l u es ca n be h el p fu l wh en you a r e d oi n g symbol i c ca l cu l a t i on s
pr ior t o wr it ing a progr am.
See also Fi xed -Poi n t Tr i gon omet r y wi t h CORDIC It er a t i on s
(494); Tr iangles (20)
1 3
I.2 USEFUL TRIGONOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 3

b c
a

sin
B C
2
cos
A
2
I. 2 I. 2 I. 2 I. 2 I. 2
U UU UUSEFUL SEFUL SEFUL SEFUL SEFUL
TRIGONOMETRY TRIGONOMETRY TRIGONOMETRY TRIGONOMETRY TRIGONOMETRY
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
Law of Cosines
a
2
= b
2
+ c
2
2bc cos A
Law of Tangent s

a b
a + b

tan
A B
2
tan
A + B
2
Law of Sines

a
sin A

b
sinB

c
sinC
Mollweid es For mula
1 4
I.2 USEFUL TRIGONOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 4
Newt ons For mula
Inver se Tr ig Funct ions in Ter ms of Inver se Tangent
sin
l
(x) = t an
1

x
1 x
2

_
,

cos
1
(x) =

2
t an
1

x
1 x
2

_
,

Funct ions Sums and Differences


sin + sin = 2 sin

+
2
cos


2
sin sin = 2 cos

+
2
sin


2
cos + cos = 2 cos

+
2
cos


2
cos cos = 2 sin

+
2
sin


2
t an + t an =

sin + ( )
cos cos
t an t an =

sin ( )
cos cos

b + c
a

cos
B C
2
sin
A
2

1 5
I.2 USEFUL TRIGONOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 5
DeMoivres Theorem
(cos + i sin )
n
= cos n + i sin n; where i =

1
Sines and Cosines in Exponent ials
e
i
= cos + i sin ; i =

1
sin =

e
i
e
i
2i
cos =

e
i
+ e
i
2
t an = i

e
i
e
i
e
i
+ e
i

_
,

= i

e
2i
1
e
2i
+ 1

_
,

Power Relat ions


sin
2
=

1
2
(1 cos 2)
sin
3
=

1
4
(3 sin sin 3)
sin
4
=

1
8
(3 4 cos 2 + cos 4 )
cos
2
=

1
2
(1 + cos 2)
cos
3
=

1
4
(3 cos + cos 3)
cos
4
=

1
8
(3 + 4 cos 2 + cos 4)
t an
2
=

1 cos 2
1 + cos 2
1 6
I.2 USEFUL TRIGONOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 6
Prod uct Relat ions
sin sin = cos


2
cos

+
2
cos cos = cos


2
+ cos

+
2
sin cos = sin

+
2
+ sin


2
Half-Angle Relat ions
sin

2
=

t
1 cos
2
cos

2
=

t
1 + cos
2
t an

2
=

t
1 cos
1 + cos

1 cos
sin

sin
1 + cos
Angle Sum and Difference Relat ions
sin ( + ) = sin cos + cos sin
sin ( ) = sin cos cos sin
cos ( + ) = cos cos sin sin
cos ( ) = cos cos + sin sin
t an ( + ) =

tan + tan
1 tan tan
t an ( ) =

tan tan
1 + t a n t a n
1 7
I.2 USEFUL TRIGONOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 7
Double-Angle Relat ions
sin 2 = 2 sin cos =

2 tan
1 + tan
2

cos 2 = cos
2
sin
2
= 2 cos
2
1 = 1 2 sin
2
=

1 tan
2

1 + tan
2

t an 2 =

2 tan
1 t an
2

Mult iple-Angle Relat ions
sin n = 2 sin(n 1) cos sin(n 2)
cos n = 2 cos(n 1) cos cos(n 2)
t an n =

tan n 1 ( ) + tan
1 tan n 1 ( ) tan
1 8
I.3 TRIGONOMETRIC FUNCTIONS AT SELECT POINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 8
I. 3 I. 3 I. 3 I. 3 I. 3
T TT TTRIGONOMETRIC FUNCTIONS RIGONOMETRIC FUNCTIONS RIGONOMETRIC FUNCTIONS RIGONOMETRIC FUNCTIONS RIGONOMETRIC FUNCTIONS
A AA AAT SELECT POINTS T SELECT POINTS T SELECT POINTS T SELECT POINTS T SELECT POINTS
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Br ief t r igonomet r ic t ables as appear ing in high-school t ext s usually pre-
sent values of sine, cosine and t angent for a small number of argument s.
Most oft en t he values coincid e wit h a subset of ver t ex locat ions on t he
regular d od ecagon (a t welve-sid ed n-gon). This implicit choice relat es t o
t hat polygons und er lying t hree- and four-fold symmet r ies, for which t he
relat ed t r igonomet r ic values are easily d er ived .
Alt hough t r ig funct ions have t r anscend ent al value for most argument s,
ot her n-gons yield up coord inat es expressible in simple algebr aic for ms.
This is a consequence of Gausss seminal wor k on t he compass const r uc-
t ion of t he 17-gon. Gener ally, any n-gon is const r uct ible if ns fact or s are
member s of t h e set (2 3 5 17 257 65537), i n wh i ch ea ch od d
fact or appear s at most once. The fact or s are t he known pr ime Fer mat
number s of t he for m

2
2
n
+ 1. (Euler found t he fact or 641 in 2
32
+ 1, and
no fur t her Fer mat pr imes have been found t hrough n = 20, a number
cont aining a t hird of a million d igit s; cur rent research suggest s t hat t he
above set is complet e.) Sur pr isingly, 2
32
1 = 4, 294, 967, 295known
t o lover s of comput er t r ivia as t he largest unsigned t hir t y-t wo bit int eger
is also t he largest known const r uct ible polygon having od d sid es.
By consid er ing t he regular pent agon and oct agon, a mor e useful t able
may be d er ived . This has value in t he symbolic comput at ion of ver t ex
locat ions for t hese n-gons, which are commonplace. As an example, t he
t went y-faced icosahed ron, which und er lies many geod esic d omes, rest s
heavily on t he cosine for an argument of t hir t y-six d egrees, a value t hat
is easily represent ed . Here =

1
2
(

5 + 1) 1.618 (t he gold en mean) wit h
t he useful proper t ies
1
= 1 and
2
= + 1. By t he Fibonacci
ser ies,
n1
+
n
=
n+1
, making a ser ies of ar bit r ar y power s easily
1 9
I.3 TRIGONOMETRIC FUNCTIONS AT SELECT POINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 1 9
Table 1. Select Rat ional Tr igonomet r ic Values.
d er ived from t he t hree t er ms given. Values out sid e of t he r st oct ant may
r ely on t h e id ent it ies cos((/ 2) x) = sin (x) or t an ((/ 2) x) =
1/ t an (x ). Hal f-an gl e i d en t i t i es u si n g cos(/ 2) =

1
2
1 + cos ( ) an d
sin(/ 2) =

1
2
1 cos ( ) allow t he const ruct ion of higher ord er even-
sid ed n-gons by angle bisect ion, t hou gh t he symbolic for ms qu ickly
become awkward .
2 0
I.4 TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 0
I. 4 I. 4 I. 4 I. 4 I. 4
T TT TTRIANGLES RIANGLES RIANGLES RIANGLES RIANGLES
Ronald Goldman
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Most of t he physical proper t ies and d ist inguished point s of a t r iangle
P
1
P
2
P
3
can be wr it t en as simple, symmet r ic, closed -for m expressions
in t he ver t ices P
1
, P
2
, P
3
. We provid e examples below.
Per imet er
Per imet er { P
1
P
2
P
3
} = P
1
P
2
+ P
2
P
3
+ P
3
P
1

Area
Area{ P
1
P
2
P
3
} = P
1
P
2
+ P
2
P
3
+ P
3
P
1
/2
Cent er of Gr avit y (Int er sect ion of t he Med ianssee Fig. 1)
C
G
= (P
1
+ P
2
+ P
3
)/ 3
In Rad i u s an d In Cen t er (In t er sect i on of t h e An gl e Bi sect or ssee
Fig. 2)
r
In
= 2 Area{ P
1
P
2
P
3
}/ Per imet er { P
1
P
2
P
3
}
C
In
= {P
2
P
3
P
1
+ P
3
P
1
P
2
+ P
1
P
2
P
3
}/ Per imet er { P
1
P
2
P
3
}
Ci r cu mr ad i u s an d Ci r cu mcen t er (In t er sect i on of t h e Per p en d i cu l ar
Bisect or ssee Fig. 3).
2 1
I.4 TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 1
Figur e 1. Cent er of gr avit y: Int er sect ion of t he med ians.
Figure 2. The in radius and in center: Intersection of the angle bisectors.
Figure 3. The circumradius and circumcenter: Intersection of the perpendicular bisect or s.
2 2
I.4 TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 2
Figure 4. Int er sect ion of t he alt it ud es.
Fir st d ene scalar s d
1
, d
2
, d
3
and c
1
, c
2
, c
3
, c by set t ing
d
1
= (P
3
P
1
) (P
2
P
1
)
d
2
= (P
3
P
2
) (P
1
P
2
)
d
3
= (P
1
P
3
) (P
2
P
3
)
c
1
= d
2
d
3
c
2
= d
3
d
1
c
3
= d
1
d
2
c = c
1
+ c
2
+ c
3
.
Then we can comput e t he circumr ad ius and circumcent er by set t ing
r
Cir
= l/ 2

d
1
+ d
2
( ) d
2
+ d
3
( ) d
3
+ d
1
( ) / c
C
Cir c
= {(c
2
+ c
3
)P
1
+ (c
3
+ c
1
)P
2
+ (c
1
+ c
2
)P
3
}/ 2c.
Int er sect ion of t he Alt it ud es of P
1
P
2
P
3
(see Fig. 4)
Let c
1
, c
2
, c
3
, c be as above. Then
P
Alt
= {c
1
P
1
+ c
2
P
2
+ c
3
P
3
)/ c.
The for mulas for t he int er sect ion of t he per pend icular bisect or s (C
Circ
)
and t he int er sect ion of t he alt it ud es (P
Alt
) are r elat ed because t he alt i-
t ud es of t he t r iangle Q
1
Q
2
Q
3
for med by t he mid point s of t he sid es of
P
1
,P
2
,P
3
are id ent ical t o t he per pend icular bisect or s of P
1
P
2
P
3
(see
2 3
I.4 TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 3
Figure 5. Alt it ud es and per pend icular bisect or s.
Fig. 5). That is,
Q
Alt
= P
Circ
,
where
Q
k
= (P
i
+ P
j
)/ 2
P
k
= Q
i
+ Q
j
Q
k
,
and t he ind ices i, j, k represent a per mut at ion of t he int eger s 1, 2, 3.
Thus we can nd Q
Alt
by solving for P
1
, P
2
, P
3
and using t he for mula for
t he circumcent er of P
1
P
2
P
3
. Similar ly, we can nd P
Circ
by solving for
Q
1
, Q
2
, Q
3
and using t he for mula for t he int er sect ion of t he alt it ud es of
Q
1
Q
2
Q
3
.
Obser ve t hat t he circumcent er and circumr ad ius solve t he problem of
nd ing t he circle t hrough t hree given point s P
1
, P
2
, P
3
. Similar ly, we can
use t he in cent er and in r ad ius t o solve t he problem of nd ing t he circle
t angent t o t hree given lines. To nd t his t angent circle, r st nd t he
pair wise int er sect ions P
1
, P
2
, P
3
of t he t hree given lines. Then simply
comput e t he in cent er and in r ad ius of P
1
P
2
P
3
. The r esult s are t he
cent er and t he r ad ius of t he circle t angent t o t he or iginal t hree lines.
See also Gener at ing Rand om Point s in Tr iangles (24)
2 4
I.5 GENERATING RANDOM POINTS IN TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 4
I. 5 I. 5 I. 5 I. 5 I. 5
G GG GGENERATING RANDOM ENERATING RANDOM ENERATING RANDOM ENERATING RANDOM ENERATING RANDOM
P PP PPOINTS IN TRIANGLES OINTS IN TRIANGLES OINTS IN TRIANGLES OINTS IN TRIANGLES OINTS IN TRIANGLES
Greg Turk
Universit y of Nort h Carolina
Chapel Hill
Pr obl em Pr obl em Pr obl em Pr obl em Pr obl em
Given t hree point s A, B and C t hat d escr ibe a t r iangle, pick a r and om
point in t he t r iangle. When many such point s are picked t he d ist r ibut ion
of t he point s should be unifor m across t he t r iangle.
Met hod 1 Met hod 1 Met hod 1 Met hod 1 Met hod 1
Let s and t be t wo nu mber s chosen from a u nifor m d ist r ibu t ion of
r and om number s in t he int er val [0, 1]. Then t he point Q given below is a
r and om point in t he t r iangle wit h ver t ices A, B and C.
a 1

t ;
b (1 s)

t ;
c

s t
;
Q aA + bB + cC;
This amount s t o having t d et er mine a line segment par allel t o BC t hat
joins a point on AB wit h a point on AC, and t hen picking a point on t his
segment based on t he value of s (see Fig. 1). Taking t he square root of t
is necessar y t o weight all por t ions of t he t r iangle equally. The values a, b
and c are t he bar ycent r ic coord inat es for t he point in t he t r iangle.
2 5
I.5 GENERATING RANDOM POINTS IN TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 5
Figur e 1. Rand om point in polygon using met hod 1.
Met hod 2 Met hod 2 Met hod 2 Met hod 2 Met hod 2
Let s and t be r and om number s in [0, 1]. A r and om point Q in t he t r iangle
is given by t he following:
i f s + t > 1 then
begi n
s 1 s;
t 1 t ;
end;
a 1 s t ;
b s;
c t ;
Q aA + bB + cC
Wit hout t he if st at ement , t he point Q will be a r and om point in t he
par allelogr am wit h ver t ices A, B, C and (B + C A) (see Fig. 2). A point
t hat land s in t he t r iangle B, C, (B + C A ) is moved int o t he t r iangle
A, B, C by reect ing it about t he cent er of t he par allelogr am.
2 6
I.5 GENERATING RANDOM POINTS IN TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 6
Figure 2a. Met hod 2 when s + t 1.
Figure 2b. Met hod 2 when s + t > 1.
Gener al i zat i ons Gener al i zat i ons Gener al i zat i ons Gener al i zat i ons Gener al i zat i ons
Met hod 1 can be ext end ed t o higher-d imensional shapes in a st r aight for-
ward manner. For example, a r and om point in a t et r ahed ron can be found
by using t hree r and om number s: t he cube root of t he r st number is used
t o pick a t r iangle t hat is par allel t o t he base of t he t et r ahed ron, and t hen
t he t wo remaining number s are used t o pick a r and om point on t hat
t r iangle.
2 7
I.5 GENERATING RANDOM POINTS IN TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 7
Met hod 2 d oes not gr acefully gener alize t o higher d imensions. For
picking a r and om point in a t et r ahed ron, t he analog of met hod 2 will use
t hree r and om number s r, s and t t o give a r and om point in a par al-
lelepiped . This par allelepiped cannot be easily d issect ed int o par t s t hat
are all congruent t o t he d esired t et r ahed ron, so it is d ifcult t o t ake point s
t hat fall out sid e t he t et r ahed ron and map t hem back int o t he t et r ahed ron.
The simplest t hing t o d o is t hrow out point s in t he par allelepiped t hat are
not also in t he t et r ahed ron, and t his can be accomplished by reject ing
t r iples r, s, t when r + s + t > 1. As t his met hod is ext end ed beyond
t et r ahed r a, a higher propor t ion of t he r and om values must be reject ed .
Eit her met hod can be used t o pick r and om point s in a polygon by
breaking t he polygon int o t r iangles and using a r and om number t o choose
a t r iangle in which t o pick a r and om point . The t r iangle must be select ed
t aking int o account t he relat ive areas of t he subt r iangles. Given r and om
number s s and t in [0, 1], here is how t o pick a r and om point in a convex
polygon d escr ibed by t he ver t ices V
l
, V
2
, . . . , V
n
:
area_sum 0;
for k 1 to n 2 do nd area of t riangles radiat ing from V
1
area
k
(V
k+1
V
k
) (V
k+2
V
1
); half area of t riangles
area_sum area_sum + area
k
; nd t ot al area of polygon
endl oop;
sum 0;
for k 1 to n 2 do pick a t riangle based on relat ive areas
sum sum + area
k
; keep running area subt ot al
i f sum s*area_sum then exi t; see if were wit hin proper range
endl oop;
s 1 + (s*area_sum sum/ area
k
); maps int o [0,1]
pick random point in t he sub-t riangle wit h vert ices V
1
, V
k + 1
, V
k + 2
Q (1

t
)V
1
+ (1 s)

t V
k+1
+ s

t V
k+2
;
The above cod e ext end s met hod 1 t o give a mapping from t he unit square
[0, 1] x [0, 1] int o t he given polygon. This mapping is cont inuous, one-t o-
2 8
I.5 GENERATING RANDOM POINTS IN TRIANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 8
one and ont o. We can use t his mapping t o d ene a Peano (area-lling)
cur ve for a convex polygon. If we have a Peano cur ve t hat maps t he
int er val [0, 1] int o [0, 1] [0, 1], we can compose t his wit h our mapping
from above t o give a mapping fr om [0, 1] int o t he polygon. Met hod 2 d oes
not give a one-t o-one mapping from [0, 1] [0, 1] int o a t r iangle, so a
Peano cur ve const ruct ed using met hod 2 for mapping ont o t he t r iangles
would fold on t op of it self, which is probably und esir able.
Acknowl edgement s Acknowl edgement s Acknowl edgement s Acknowl edgement s Acknowl edgement s
Some of t hese id eas were wor ked out d ur ing conver sat ions I had wit h
John Airey and David Banks.
See also Tr iangles (20)
See Append ix 2 for C Implement at ion (649)
2 9
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 2 9
I. 6 I. 6 I. 6 I. 6 I. 6
F FF FFAST LIN AST LIN AST LIN AST LIN AST LINE EE EEEDGE EDGE EDGE EDGE EDGE
I II IINTERSECTIONS ON A NTERSECTIONS ON A NTERSECTIONS ON A NTERSECTIONS ON A NTERSECTIONS ON A
U UU UUNIFORM GRID NIFORM GRID NIFORM GRID NIFORM GRID NIFORM GRID
Andrew Shapira
Rensselaer Polyt echnic Inst it ut e
Troy, New York
This paper present s an algor it hm t hat uses only int eger ad d it ion and
su bt r act ion t o nd int er sect ions bet ween a u nifor m gr id and a line
segment having gr id ver t ices as it s end point s. The out put of t he algor it hm
is a list of gr id ver t ices and ed ges t hat int er sect t he line segment ; t he
precise point s of int er sect ion are not found . The algor it hm is ver y similar
t o Bresenhams algor it hm for d r awing line segment s on a r ast er d evice.
The problem is st at ed below.
Given: (1) A 2D unifor m gr id G wit h square cells of unit sid e lengt h
(2) Two d ist inct ver t ices of G, P = (P
x
, P
y
) and Q = (Q
x
, Q
y
)
Find : Al l ed ges a n d ver t i ces of G, excl u d i n g P a n d Q, t h a t
int er sect PQ
The solut ion of t his problem was mot ivat ed by t he need t o comput e
visibilit y in a gr id -based t er r ain. An implement at ion of t he line-ed ge
algor it hm present ed in t his paper was used as a plat for m by a gr id
visibilit y algor it hm. The result ing gr id visibilit y d at a have been used for
sever al applicat ions, includ ing t er r ain labelling, pat h planning, line-of-
sight commu nicat ion, visu alizat ion, visibilit y t heor y exper iment s, and
object recognit ion in images. Ot her possible visibilit y applicat ions includ e
t er r ain or ient at ion, t er r ain navigat ion, and represent at ion of t er r ain phys-
iogr aphy.
The t er r ain mod el ment ioned above was select ed because d igit al t er r ain
d at a are oft en packaged in a for m t hat mat ches t his mod el. The t er r ain
mod el is as follows. Each ver t ex in a 2D unifor m gr id has an int eger-val-
3 0
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 0
Figure 1. Det er mining whet her or not t wo point s are mut ually visible. The upper
d iagr am shows a t er r ain as seen from above, wit h elevat ions given next t o each ver t ex.
The project ion of line-of-sight PQ int er sect s t he gr id at point s I1, I2, and I3. The t able
shows the heights of PQ and the terrain at each intersection. Since the line of sight is
above t he t er r ain at all int er sect ions, point s P and Q are mut ually visible.
ued elevat ion associat ed wit h it ; each of t he result ing point s in 3-space is
called a dat a value. Ter r ain elevat ions above gr id ed ges are obt ained by
linear int er polat ion bet ween t he appropr iat e d at a valu es. The t er r ain
above t he int er ior of all gr id cells is d ened in such a way so as not t o
int er fere wit h t he int er visibilit y of d at a values.
Visibilit y wit hin t his simple t er r ain mod el approximat es visibilit y wit hin
more complicat ed mod els such as t r iangulat ed t er r ain mod els, but is
simpler t o calculat e (see Fig. 1). To d et er mine whet her t wo d at a point s U
and V are mut ually visible, a t est is per for med ever ywher e t hat t he 2D
project ion of UV int er sect s a gr id ed ge or ver t ex. The t est d et er mines
3 1
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 1
whet her t he line-of-sight UV is above t he t er r ain at t he point of int er sec-
t ion. If UV always t ur ns out t o be above t he t er r ain, t hen U and V are
visible. If any t est shows t hat UV is below t he t er r ain, t hen U and V are
mut ually invisible, and t est ing t er minat es.
This comput at ion is ver y efcient . The ent ire visibilit y calculat ion can
be d one using only int eger ad d it ions, subt r act ions, and mult iplicat ions. If
d esired , t he calculat ion for many pair s of d at a valu es can easily be
ad apt ed t o execut e in par allel on a coar se-gr ained machine.
The visibilit y algor it hm was implement ed in C; int er sect ions are gener-
at ed using a slight ly mod ied ver sion of t he line-ed ge int er sect ion algo-
r it hm in t his paper. On a SUN 3/ 60 comput er running SUN Oper at ing
Syst em 3.4 wit h 12 megabyt es of memor y, t he progr am t ook roughly 11
hour s of CPU t ime t o comput e t he 100 million visibilit y pair s of a 100 by
100 t er r ain t aken from Unit ed St at es Geological Sur vey d at a.
The line-ed ge int er sect ion algor it hm is d er ived below using pseud o-C.
Includ ed are t wo int er med iat e ver sions t hat use oat ing point . Because of
round ing problems, t hese ver sions may not wor k on machines wit h nit e
oat ing point precision. They are used only t o d er ive t he nal ver sion.
Fir st we will d iscuss a few miscellaneous it ems. We will assume for t he
t ime being t hat Q lies bet ween 0 and 45 from P, inclusive. If PQ for ms
an angle wit h t he x-axis t hat is a mult iple of 45, t hen PQ will be
consid ered t o int er sect t he t er r ain only at gr id ver t ices. In Algor it hms
14, t he symbols x and y are used as abbreviat ions for (Q
x
P
x
) and
(Q
y
P
y
) respect ively, and m d enot es (y/ x). All var iables are local
and t ype int eger unless ot her wise ind icat ed . The unar y oper at or (real )
conver t s it s oper and t o t ype real . In any expression cont aining one or
more oat ing point oper and s, all oper at ions are per for med using oat ing
point . The value of an ar bit r ar y var iable t d ur ing loop it er at ion i is
d enot ed t
i
;t
0
t o d enot es t he value of t just before t he r st loop it er at ion.
To d er ive t he r st ver sion of t he algor it hm, consid er each point (x, y(x))
along PQ su ch t hat x {P
x
+ 1, P
x
+ 2, . . .,Q
x
1}, and y(x) =
P
y
+ m(x P
x
). It is apparent from Fig. 2 t hat if y(x) = y(x)] , t hen
PQ int er sect s a ver t ex at (x, y(x)). If y(x) y(x)] , t hen PQ int er sect s
t he ver t ical ed ge connect ing ( x, y(x)] ) and (x, y(x)1); if, in ad d it ion,
y(x 1) < y(x)] , t hen PQ also int er sect s t he hor izont al ed ge connect -
ing (x 1, y(x)] ) and ( x, y(x)] ). An algor it hm based on t hese id eas is
given below.
]
3 2
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 2
Figure 2. The intersection of line segment PQ and the uniform grid can be expressed as
{up(2, l), left(3, 2), up(3, 2), left(4, 3), up(4, 3), vertex(5, 4), up(6, 4), left(7, 5), up (7, 5),
left(8, 6), up(8, 6)}. Highlighted grid edges indicate intersections with PQ. Circles indicate
points traversed by Algorithm l; squares indicate points traversed by Algorithms 24.
3 3
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 3
Al gor i t hm I Al gor i t hm I Al gor i t hm I Al gor i t hm I Al gor i t hm I
oy, fy: real
cx P
x
+ 1
oy P
y
whi l e cx < Q
x
do
begi n
fy P
y
+ (cx P
x
)*( y/ (real )x)
i f fy fy] then
begi n
i f oy < fy] then left (cx, fy] )
up (cx, fy] )
end
el se ver t ex (cx, fy] )
oy fy
cx cx + 1
end
endl oop
Inst ead of t r aver sing point s (cx, fy) as in Algor it hm 1, we can t r aver se
point s (cx, cy), where cy = fy] , and use a new var iable r t hat cont ains
as it s value fy fy]. The value of r can be comput ed ind uct ively as
follows:
r
0
= 0

r
i

r
i 1
+ m if r
i 1
+ m < 1
r
i 1
+ m 1 otherwise

'

The int er sect ion t est s of Algor it hm 1 can be rewr it t en as follows: at


(cx
i
, cy
i
), PQ int er sect s a ver t ex if r
i
= 0, an upward ed ge if r
i
0, and
a left ward ed ge if r
i
0 and r
i 1
+ m > 1. The result ing algor it hm is
given below.
3 4
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 4
Al gor i t hm 2 Al gor i t hm 2 Al gor i t hm 2 Al gor i t hm 2 Al gor i t hm 2
r, or, m: real
C: IntPoi nt2
C
x
P
x
+ 1
C
y
P
y
r 0
whi l e C
x
< Q
x
do
begi n
or r
i f r + m < 1 then r r + m
el se
begi n
r r + m 1
C
y
C
y
+ 1
end
i f r 0 then
begi n
i f or + m > 1 then left (C)
up(C)
end
el se ver t ex (C)
C
x
C
x
+1
end
endl oop
All oat ing point oper at ions in Algor it hm 2 can be eliminat ed by int rod uc-
ing a new var iable nr = rx. The Algor it hm 2 oper at ions on r can be
expressed in t er ms of nr as list ed below, yield ing Algor it hm 3.
In t er ms of r In t er ms of nr
r = 0 nr = 0
r + m < 1 nr + y < x
r = r + m nr = nr + y
r = r + m 1 nr = nr + y x
3 5
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 5
Al gor i t hm 3 Al gor i t hm 3 Al gor i t hm 3 Al gor i t hm 3 Al gor i t hm 3
C: IntPoi nt2
C
x
P
x
+ 1
C
y
P
y
nr 0
whi l e C
x
< Q
x
do
begi n
onr nr
i f nr + y < x then nr nr + y
el se
begi n
nr nr + y x
C
y
C
y
+1
end
i f nr 0 then
begi n
i f onr + y > x then left (C)
up (C)
end
el se ver t ex (C)
C
x
C
x
+ 1
end
endl oop
Sever al simplicat ions can be mad e. Int rod ucing a const ant const =
x y moves some comput at ion out of t he loop. The check for a left
ed ge need only be mad e when C
y
is increment ed . Wit h t his check moved
t o it s new locat ion, it is sufcient t o check for nr 0. This leaves onr
unused , so it can be removed . Finally, a red und ant check of nr can be
eliminat ed by copying and combining i f st at ement s. The nal ver sion of
t he single-oct ant algor it hm is given below.
3 6
I.6 FAST LINE-EDGE INTERSECTIONS ON A UNIFORM GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 6
Al gor i t hm 4 Al gor i t hm 4 Al gor i t hm 4 Al gor i t hm 4 Al gor i t hm 4
C: IntPoi nt2
C
x
P
x
+ 1
C
y
P
y
nr 0
const x y
whi l e C
x
< Q
x
do
begi n
i f nr < const then
begi n
nr nr + y
i f nr 0 then up (C)
el se ver t ex (C)
end
el se
begi n
C
y
C
y
+1
nr nr const
i f nr 0 then
begi n
left (C)
up (C)
end
el se ver t ex (C)
end
C
x
C
x
+ 1
end
endl oop
Al gor i t h ms 14 assu me t h at Q l i es bet ween 0 an d 45 fr om P,
inclusive. To hand le t he ot her eight oct ant s wit hout slowing d own t he
algor it hm, separ at e cod e segment s are used for each oct ant of t he plane
(see Append ix).
See Append ix 2 for C Implement at ion (651)
3 7 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 7
I II II
A AA AANTI-ALIASING SUMMARY NTI-ALIASING SUMMARY NTI-ALIASING SUMMARY NTI-ALIASING SUMMARY NTI-ALIASING SUMMARY
Th e fol l owi n g fou r Gems a r e u sefu l for a n t i -a l i a si n g ca l cu l a -
t ion s. On e t ech n iqu e for exact an t i-aliasin g is t o n d t h e ar ea of
over l ap bet ween a p i ece of geomet r y t o be r en d er ed an d a l t er
fu n ct i on . Oft en t h i s l t er i s a u n i t -h ei gh t box over a p i xel , bu t
l ar ger, mor e symmet r i c l t er s (su ch as a Gau ssi an wi t h a r ad i u s
of 1. 5 p i xel s) wi l l u su a l l y gi ve bet t er r esu l t s. Si n ce su ch l t er s
a r e r a d i a l l y symmet r i c, t h ey h a ve a ci r cu l a r foot p r i n t , a n d on e
n eed s t o n d t h e r egion of t h is circle occu p ied by t h e geomet r y;
a wei gh t i n g fa ct or i s t h en u su a l l y i n cl u d ed t o a ccou n t for t h e
changing height of t he lt er.
Th e r st Gem d et er mi n es t h e a r ea of over l a p bet ween a ci r cl e
a n d a h a l f-p l a n e. Th i s t ech n i qu e ma y be a d a p t ed for p ol ygon
a n t i -a l i a si n g by obser vi n g t h a t a con vex p ol ygon ma y be r ep r e-
sen t ed a s t h e i n t er sect i on of a col l ect i on of h a l f-p l a n es. Th e
secon d Gem ap p l i es t h i s t ech n i qu e t o t h i ck l i n es. Th e t h i r d Gem
is u sefu l wh en an t i-aliasin g cir cles. Th e n al Gem may be u sefu l
t o d et er mi n e wh et h er a p a r t i cu l a r p i ece of geomet r y i s a ca n d i -
d a t e for a n t i -a l i a si n g i n a p a r t i cu l a r si t u a t i on by exa mi n i n g
some point s (for example, t he ver t ices of a polygon).
See also Line Dr awing Gems; Polygon Scan Conver sion Gems;
Filt er ing Gems; Ant i-Aliasing Filt er s Su mmar y (143); A Fast 2D
Point -on-Line Test (49)
3 8
I.7 AREA OF INTERSECTION: CIRCLE AND A HALF-PLANE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 8
I. 7 I. 7 I. 7 I. 7 I. 7
A AA AAREA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION: :: ::
C CC CCIRCLE AND A HALF-PLANE IRCLE AND A HALF-PLANE IRCLE AND A HALF-PLANE IRCLE AND A HALF-PLANE IRCLE AND A HALF-PLANE
Kelvin Thompson
Nt h Graphics, Lt d.
Aust in, Texas
Given a circle of r ad ius r whose cent er is a d ist ance d from t he ed ge of a
half-p lane (see Fig. 1), t he fr act ion of t he cir cle t hat int er sect s t he
half-plane is
and t he area of int er sect ion is r
2
cov(d, r). If t he cent er of t he circle
is insid e t he half-plane, t hen t he fr act ional cover age is 1 cov(d, r) and
t he area r
2
(1 cov(d, r)).
The fu nct ion cov(d, r) can be u sefu l when ant i-aliasing lines and
polygon ed gessee Gupt a and Sproull (1981) and Area of Int er sect ion:
Circle and a Thick Line in t his volume.
Pr oof Pr oof Pr oof Pr oof Pr oof
We will t ake t he int egr al of par t of a semicircle, and t hen d ouble t hat t o
get t he area of int er sect ion; t he area of int er sect ion d ivid ed by t he area of
t he circle gives us t he fr act ional cover age. We know t he equat ion for a
semicircle is y =

r
2
x
2
, and a t able of int egr als t ells us

a
2
u
2

du
u
2
a
2
u
2
+
a
2
2
arcsin
u
a
.
cov(d,r ) =

d r,
1
2

d r
2
d
2
r
2
d r, 0

'

arcsin
d
r
3 9
I.7 AREA OF INTERSECTION: CIRCLE AND A HALF-PLANE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 3 9
Figur e 1.
To get t he area of int er sect ion wit h t he semicircle we int egr at e fr om d t o
t he ed ge of t he circle, r. Aft er a lit t le bit of algebr a t his gives us

d
r
r
2
x
2

dx
1
2

2
r
2
d r
2
d
2
r
2
arcsin
d
r

1
]
1
.
When we d ou ble t his and d ivid e by r
2
, we get t he exp ression for
cov(d, r) shown above.
4 0
I.8 AREA OF INTERSECTION: CIRCLE AND A THICK LINE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 0
I. 8 I. 8 I. 8 I. 8 I. 8
A AA AAREA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION: :: ::
C CC CCIRCLE AND A THICK LINE IRCLE AND A THICK LINE IRCLE AND A THICK LINE IRCLE AND A THICK LINE IRCLE AND A THICK LINE
Kelvin Thompson
Nt h Graphics, Lt d
Aust in, Texas
Given a circle of r ad ius r at a d ist ance p from t he cent er of a line of
t hickness 2w (see Fig. 1), t he fr act ion of t he circle t hat over laps t he line
is d ened in t er ms of t he funct ion cov(d), which in t ur n is d ened in
t er ms of t he cover age funct ion found in Area of Int er sect ion: Circle and
a Half-Plane in t his volume:
cov(d) cov(d, r).
Our use of cov(d) d epend s on whet her t he line is t hinner t han t he pixel:
For w < r (t he line is t hinner t han t he pixel):
Range of p Coverage
0 p w 1 cov(w p) cov(w + p)
w p r w cov(p w) cov(p + w)
r w p cov(p w)
For w r (t he line is t hicker t han t he pixel):
Range of p Coverage
0 p w 1 cov(w p)
w p cov(p w)
The area of int er sect ion is t he cover age shown above mult iplied by t he
area of t he circle r
2
.
4 1
I.8 AREA OF INTERSECTION: CIRCLE AND A THICK LINE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 1
Figur e 1.
Pr oof , by Obser vat i on of t he Geomet r y Pr oof , by Obser vat i on of t he Geomet r y Pr oof , by Obser vat i on of t he Geomet r y Pr oof , by Obser vat i on of t he Geomet r y Pr oof , by Obser vat i on of t he Geomet r y
For skinny lines we have:
Geomet r y: See Fig. 2. See Fig. 3.
Cover age: 1 cov(w p,) cov(w + p) cov(p w) cov(p + w)
Range: 0 p w w p r w
Figure 2. Figure 3
4 2
I.8 AREA OF INTERSECTION: CIRCLE AND A THICK LINE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 2
Figure 4. Figure 5.
And if w > r, we invoke cov(d) once for each p:
Geomet r y: See Fig. 4. See Fig. 5.
Cover age: 1 cov(w p) cov(p w)
Range: 0 p w w p r + w
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER
4 3
I.9 AREA OF INTERSECTION: TWO CIRCLES
I. 9 I. 9 I. 9 I. 9 I. 9
A AA AAREA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION REA OF INTERSECTION: :: ::
T TT TTWO CIRCLES WO CIRCLES WO CIRCLES WO CIRCLES WO CIRCLES
Kelvin Thompson
The Universit y of Texas at Aust in
Given t wo lled circles (d iscs) wit h r ad ii r
1
r
2
whose cent er s are a
d ist ance d ap ar t (see Fig. 1), and some exp ressions r elat ing t o t his
geomet r y,
(1)
t hen t he area of int er sect ion is given by
o(r
1
, r
2
, d)

d r
2
r
1
, r
1
2
d r
2
+ r
1
, 0

'

Pr oof Pr oof Pr oof Pr oof Pr oof


Equat ions 3a and 3b are obvious by inspect ion, since t hey occur when (a)
d isc 1 is complet ely insid e d isc 2, and (b) t he t wo circles d o not int er sect .
a (x, r)
(2)


1
2
r
2
x r
2
x
2
r
2
arcsin
x
r
( )
,
(3a)
(3b)

d
2
< r
2
2
r
1
2
, r
1
2
a s, r
1
( ) + a s + d, r
2
( )
d
2
r
2
2
r
1
2
, a x
1
, r
1
( ) + a x
2
, r
2
( ).

'

(3c)
(3d )

x
1

d
2
+ r
1
2
r
2
2
2d
, x
2

d
2
+ r
2
2
r
1
2
2d
, s
r
2
2
r
1
2
d
2
2d
,
ot her wise
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER
4 4
I.9 AREA OF INTERSECTION: TWO CIRCLES
We now prove t he remaining expressions in Equat ion 3.
Figure 1
Fir st we not e t hat t he ar ea of int er sect ion can be sp lit int o t wo
cr escent like shap es, wher e each of t hese shap es is t he int er sect ion
bet ween a d isc and half-plane. The t wo regionsd enot ed C
1
, and C
2
in
Fig. 2are always separ at ed by t he chord connect ing t he t wo point s
where t he bou nd ar ies of t he d iscs int er sect (we call t his t he shared
chord).
From Area of Int er sect ion: Circle and a Half-Plane in t his volume we
know t hat t he area of int er sect ion bet ween a d isc and a half-plane is given
by a(x, r) in Equat ion 2, where x is t he d ist ance bet ween t he cent er of
t he d isc and t he ed ge of t he half-plane, and r is t he r ad ius of t he d isc.
Thus, once we know t he d ist ance bet ween t he cent er of each d isc and t he
shared chord , we can d et er mine t he area of int er sect ion. Now let us d r aw
a t r iangle (see Fig. 3) whose ver t ices are at t he cent er s of t he t wo circles
and at one of t he end point s of t he shared chord . When r
1
2
+ d
2
= r
2
2
, t he
t r iangle is a r ight t r iangle (wit h r
2
t he hypot enuse), and t he d ist ances x
1
,
an d x
2
ar e 0 an d d by in sp ect ion . However, if we h old r
1
an d d
const ant , t hen as r
2
get s larger and smaller, we encount er t wo d iffer ing
Figur e 2.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER
4 5
I.9 AREA OF INTERSECTION: TWO CIRCLES
Figur e 3.
geomet r ies. When r
2
2
is less t han r
1
2
+ d
2
we get t he geomet r y shown
above, and t his gives us t he simult aneous equat ions
r
1
2
x
1
2
= r
2
2
x
2
2
, and x
1
+ x
2
= d.
Wit h a lit t le bit of algebr a we get t he exp ressions for x
l
and x
2
in
Equat ion 1. Since x
l
and x
2
are t he d ist ances bet ween t he cent er s of t he
d iscs and t he shared chord , we immed iat ely get Equat ion 3d .
Now we let r
2
grow larger t han r
2
1
+ d
2
, and we get t he geomet r y in
Fig. 4. This, in t ur n, gives us t he relat ion
r
2
2
(s + d)
2
= r
2
1
s
2
.
Again, we apply a lit t le algebr a t o get t he expression for s in Equat ion 1.
Here, however, t he geomet r y is a lit t le more complicat ed . The d ist ance
from t he cent er of d isc 2 t o t he shared chor d is d + s, and t he d ist ance
Figure 4.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER
4 6
I.9 AREA OF INTERSECTION: TWO CIRCLES
Figure 5.
from t he cent er of d isc 1 is s. However, t he cent er of d isc 1 is insid e t he
half-plane wit h which we are int er sect ing it , so Equat ion 2 is not valid .
To get t he cor rect ar ea for C
l
, we use t he expression r
2
1
a(s, r
1
),
which lead s us t o Equat ion 3c.
4 7
I.10 VERTICAL DISTANCE FROM A POINT TO A LINE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 7
I.10 I.10 I.10 I.10 I.10
V VV VVERTICAL DISTANCE ERTICAL DISTANCE ERTICAL DISTANCE ERTICAL DISTANCE ERTICAL DISTANCE
F FF FFROM A POINT ROM A POINT ROM A POINT ROM A POINT ROM A POINT
T TT TTO A LINE O A LINE O A LINE O A LINE O A LINE
Kelvin Thompson
Nt h Graphics, Lt d.
Aust in, Texas
Given a line wit h slope m = dy/ dx, and per pend icular and ver t ical
d ist ances p and v bet ween a point and t he line in Fig. 1, t he r at io k
relat ing p and v is
k =

p
v
=

1
1 + m
2
.
(The ver t ical d ist ance v is t he lengt h of t he shor t est ver t ical line
segment bet ween t he point and t he line.) Fur t her, if 1 m 1, t hen
1/

2 k 1.
This relat ionship can be useful for rend er ing ant i-aliased lines and
polygon ed gessee Gupt a and Sproull (1981), and Area of Int er sect ion:
Circle and Half-Plane in t his volume.
Pr oof Pr oof Pr oof Pr oof Pr oof
By element ar y geomet r y, t he t hree r ight t r iangles (t wo insid e t he t hird ) in
Fig. 2 are similar.
Figure 1.
4 8
I.10 VERTICAL DISTANCE FROM A POINT TO A LINE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 8
Figur e 2.
Hence,
k =

p
v
=

v / m
v
2
+ v
2
/ m
2
=

1
m 1 + 1/ m
2
=

1
1 + m
2
.
4 9 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 4 9
I.11 I.11 I.11 I.11 I.11
A AA AA FAST FAST FAST FAST FAST
2 22 22 D POINT-ON-LINE D POINT-ON-LINE D POINT-ON-LINE D POINT-ON-LINE D POINT-ON-LINE
T TT TTEST EST EST EST EST
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Fast proximit y t est ing bet ween a point and a line on a t wo-d imensional
plane nd s common applicat ion, par t icular ly in gr aphics ed it or s. Unfor t u-
n at ely, t he r st p r incip les p er p en d icu lar d ist an ce t est requ ir es a
square-root oper at ion t o for m a nor malized (Euclid ean) vect or of unit
l en gt h . Th i s st ep i s exp en si ve a n d oft en u n n ecessa r y. For si mp l e
hit / miss select ion, t he d ist ance inequalit y may be squared t o yield a
for m requir ing only mult iplicat ion, yield ing fast er cod e, which oper at es
on int eger var iables while maint aining mat hemat ical consist ency. Subst i-
t ut ion of an alt er nat e vect or nor m red uces mult iplicat ion count s and in
some cases yield s a more useful proximit y t est . For inst ance, a vect or
r ast er ized using convent ional DDA t echniques gener at es a pixel set of
on point s, whose d ist ances t o t he und er lying vect or backbone all fall
wit hin a common innit y-nor m d ist ance ind epend ent of line slope, t hough
an out er, br acket ing Euclid ean d ist ance may always be t t ed .
The cod e present ed below was or iginally wr it t en t o merge chains of
shor t vect or s having similar slope int o a larger const it uent vect or (Paet h,
1988), p r ovid ed t hat all int er med iat e ver t ices lie along t he common
parent an applicat ion for which t he Euclid ean nor m is bot h slow and
inappropr iat e. An example of t est d ist ances ret ur ned by t he cod e appear s
in t he comment s prefacing t he C source cod e in t he append ix.
5 0
I.11A FAST 2D POINT-ON-LINE TEST
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 0
Pseudo- Code Pseudo- Code Pseudo- Code Pseudo- Code Pseudo- Code
Given t wo point s P and Q and a t est point T
ret urn 0 if T is not on t he (innit e) line PQ
1 if T is on t he open ray

r
P
2 if T is wit hin t he line segment PQ
3 if T is on t he open ray

r
Q
i f ABS((Qy Py) (Tx Px) (Ty Py) (Qx Px))
MAX(ABS(Qx Px), ABS(Qy Py)) return[0];
i f (Qx < Px and Px < Tx) or (Qy < Py and Py < Ty) return[1];
i f (Tx < Px and Px < Qx) or (Ty < Py and Py < Qy) return[1];
i f (Px < Qx and Qx < Tx) or (Py < Qy and Qy < Ty) return[3];
i f (Tx < Qx and Qx < Px) or (Ty < Qy and Qy < Py) return[3];
return[2];
See also Solving t he Near est -Point -on-Cu r ve Pr oblem (607); A
Fa st Ap p r oxi ma t i on t o t h e Hyp ot en u se (427); Li n e Dr a wi n g
Summar y (98)
See Append ix 2 for C Implement at ion (654)

5 1
I.12 FAST CIRCLE-RECTANGLE INTERSECTION CHECKING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 1
I.12 I.12 I.12 I.12 I.12
F FF FFAST CIRCLERECTANGLE AST CIRCLERECTANGLE AST CIRCLERECTANGLE AST CIRCLERECTANGLE AST CIRCLERECTANGLE
INTERSECTION CHECKING INTERSECTION CHECKING INTERSECTION CHECKING INTERSECTION CHECKING INTERSECTION CHECKING
Clif f ord A. Shaf f er
Virginia Tech
Blacksburg, Virginia
If you d o a lot of gr aphics or spat ial d at a progr amming, sooner or lat er
you will want t o know if a circle and a rect angle int er sect , or if a sphere
and a box int er sect . This is even more likely if you use quad t ree or oct ree
met hod s. (For example, you may want t o nd all nod es of t he t ree wit hin
a cer t ain Euclid ean d ist ance of a point ). Unfor t unat ely, t his pr oblem is
not as easy t o solve as it appear s. The r st approach t hat nor mally comes
t o mind is t o check if any cor ner of t he rect angle falls wit hin t he circle
(using a simple d ist ance check). Unfor t unat ely, t his approach will some-
t imes give false negat ive result s. There are t hree anomalous cases t o
wat ch out for. Fir st , while no cor ner of t he rect angle may be in t he circle,
a chord of t he circle may over lap one ed ge of t he rect angle (see Fig. 1).
Second , t he rect angle might fall insid e a bound ing box placed around t he
circle, but st ill be out sid e t he cir cle (see Fig. 2). Third , t he cir cle might lie
ent irely insid e t he rect angle (see Fig. 3).
A fast algor it hm is present ed , for d et er mining if a circle and a rect angle
int er sect . The 3D case can easily be d er ived from t he 2D case; alt hough it
is a lit t le longer, it requires only slight ly more execut ion t ime. The 2D
ver sion of t his algor it hm requires at most ve compar isons (all but one
t est against 0), t hree mult iplies, ve ad d / subt r act s (four of which are for
nor malizat ion) and one absolut e-value funct ion. It basically wor ks by
d et er mining where t he rect angle falls wit h respect t o t he cent er of t he
circle. There are nine possibilit ies in 2D (27 in 3D): t he rect angle can be
ent irely t o t he NW, NE, SW, or SE of t he circles cent er point (four cases),
d irect ly N, E, S, or W of t he circles cent er point (four cases) or in t he
cen t er (t h at i s, con t ai n i n g t h e ci r cl es cen t er p oi n t ). Th e al gor i t h m
5 2
I.12 FAST CIRCLE-RECTANGLE INTERSECTION CHECKING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 2
Figur e 1. Figure 2.
Figure 3
enumer at es t hese cases and d et er mines t he d ist ance bet ween t he single
closest point on t he bord er of t he rect angle and t he cent er of t he circle.
bool ean Check_Int er sect (R, C, Rad )
Return TRUE iff rectangle R intersects circle with centerpoint C and radius Rad.
begi n Comment s assume origin is at lower left corner
Translat e coordinat es, placing C at t he origin
R.max R.max C; R.min R.min C;
i f (R.max.x < 0) R t o left of circle cent er
then i f (R.max.y < 0) R in lower left corner
then return (R.max.x
2
+ R.max.y
2
< Rad
2
);
el se i f (R.min.y > 0) R in upper left corner
then return (R.max.x
2
+ R.min.y
2
< Rad
2
);
el se R due West of circle
return (R.max.x< Rad ):
5 3
I.12 FAST CIRCLE-RECTANGLE INTERSECTION CHECKING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 3
el se i f (R.min.x > 0) R t o right of circle cent er
then i f (R.max.y < 0) R in lower right corner
then return (R.min.x
2
+ R.max.y
2
< Rad
2
);
el se i f (R min.y > 0) R in upper right corner
then return (R.min.x
2
+ R.min.y
2
< Rad
2
);
el se R due EAST of circle
return (R.min.x < Rad )
el se R on circle vert ical cent erline
i f (R.max.y < 0) R due Sout h of circle
then return (R.max.y < Rad );
el se i f (R.min.y > 0) R due Nort h of circle
then return (R.min.y < Rad );
el se R cont ains circle cent erpoint
return (TRUE);
end; Check_int ersect
See also Fa st Ra y-Box In t er sect i on (395); Sp h er es-t o-Voxel s
Con ver si on (327); A Si mp l e Met h od for Box-Sp h er e In t er sec-
t ion Test ing (335); Ray Tr acing (383)
See Append ix 2 for C Implement at ion (656)
2
2D RENDERING
II.1 CIRCLES OF INTEGRAL RADIUS ON INTEGER LATTICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 7
II.1 II.1 II.1 II.1 II.1
C CC CCIRCLES OF IRCLES OF IRCLES OF IRCLES OF IRCLES OF
I II IINTEGRAL RADIUS ON NTEGRAL RADIUS ON NTEGRAL RADIUS ON NTEGRAL RADIUS ON NTEGRAL RADIUS ON
I II IINTEGER LATTICES NTEGER LATTICES NTEGER LATTICES NTEGER LATTICES NTEGER LATTICES
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Consid er t he quest ion of d r awing circles of int egr al r ad ius R on an
int eger point lat t ice (i,j) so t hat select point s on t he lat t ice fall exact ly
along t he circles per imet er. This sit uat ion occur s implicit ly when rend er-
ing circles of int egr al size on a r ast er d isplay. Here t he pixels represent
t he point lat t ice. The quest ion ar ises explicit ly when we represent a circle
by an int er polat ing cur ve: a d esir able cont rol polygon places knot s at
locat ions having exact represent at ions. When t he circles are small t he
int er polat ion cur ve may d egener at e t o simple line segment s, and a convex
polygon of ir regular ed ge lengt hs (but precise circumferent ial ver t ices) is
rend ered .
Set s of point s (i,j) lying at a const ant d ist ance R solve t he well-known
Pyt hagorean relat ion i
2
+ j
2
= R
2
. What values of R yield up large set s
of int egr al sid es and what are t heir proper t ies? A brut e-force search of t he
solu t ion sp ace yield s t hose t r iangles wit h hyp ot enu se less t han one
hund red . Rest r ict ing R t o pr ime number s assures t r iangles in lowest
t er ms; t he gener at ion of ad d it ional t r iangles wit h composit e, relat ively
pr ime, ed ge lengt hs is explained lat er :
It is unfor t unat e t hat in lowest t er ms t he hypot enuse must be an od d
lengt h. To show t his, not e t hat an even number (of for m 2n and congru-
ent t o 0 mod 2) is congr uent t o 0 mod 4 aft er squar ing as (2n)
2
= 4n
2
.
Similar ly, squar ing an od d yield s (2n + 1)
2
= 4(n
2
+ n) + 1, leaving it
congruent t o 1 mod 4. In par t icular, even/ od d par it y is preser ved und er
squar ing.
Par it y implies t hat a t r iangle of even hypot enuse must be t he sum of
eit her t wo even or t wo od d legs. The r st case is immed iat ely d iscard ed
II.1 CIRCLES OF INTEGRAL RADIUS ON INTEGER LATTICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 8
as it is not in lowest t er ms, as st at ed . In t he remaining case, t he sum of
t wo od d legs gives a hypot enuse lengt h congruent t o 2 mod 4, which
cannot be represent ed as a per fect square. Thus, even hypot enuse lengt hs
are r uled out for t r iangles in lowest t er ms. Wor se, a hypot enuse wit h
lengt h a power of t wo can have no (od d ) fact or s in common wit h eit her
od d legt he for m is necessar ily in lowest t er ms. Thus, no Pyt hagorean
t r iangles exist whose hypot enuse lengt h is a power of t wo.
By d ivid ing ed ge lengt hs by t he hypot enuse, a unit vect or is for med
wit h r at ional coefcient s. For inst ance, binar y oat ing point hard ware
approximat es real values by using scaled r at ionals, in which bot h t he
imp licit mant issa d enominat or and scaling exp onent ar e p ower s of
t wo. Thu s, t her e exist (u nd er r ad ix-2 or r ad ix-16 oat ing p oint ) no
values 0 < x < 1 and 0 < y < 1 such t hat x
2
+ y
2
= 1 when evaluat ed
in full precision. This fur t her implies t hat t he univer sal id ent it y sin
2
x +
cos
2
x = 1.0 hold s only as round -off allows.
For t unat ely, t he sit uat ion is not as severe in base t en. By happenst ance
t his base possesses an od d fact or (5), which appear s in Tab. 1 in t he
(3, 4, 5) t r iangle known t o t he Egypt ians. This allows Car t esian pair s
possessing exact int egr al lengt h in nit e d igit s. For inst ance, scaling t he
above ont o (6, 8, 10) and d ivid ing gives (0.6, 0.8) as a unit vect or. To
show t hat t here are ad d it ional point s of higher precision, ad d it ional
t r iangles are for med whose hypot enuse lengt hs are t he prod uct of t wo
Table 1. Pr ime Pyt hagorean Tr iangles.
II.1 CIRCLES OF INTEGRAL RADIUS ON INTEGER LATTICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 5 9
(possibly id ent ical) hypot enuse values appear ing in t he t able. This yield s
t r iangles of t wo t ypes: t he scaled ver sions of t he or iginal parent s, plus
ad d it ional t r iangles which, sur pr isingly, are alread y in lowest t er ms (t he
algebr aic number t heor y is left as a recr eat ion t o t he int erest ed read er ).
For inst ance, scaling (3, 4, 5) by ve yield s t wo t r iangles of common
hypot enuse: t he expect ed (15, 20, 25), found d irect ly, plus t he unex-
pect ed (7, 24, 25) in lowest t er ms, found by searching ed ge lengt hs for
R = 25. Similar ly, t he prod uct of t able ent r ies t wo and t hree (hypo-
t enuses of lengt h 13 and 17) yield four int egr al t r iangles of hypot enuse
221: t he or iginals scaled by 17 and 13, plus t he ad d it ional member s
(21, 220, 221), and (140, 171, 221).
This proper t y may be applied t o gener at e coord inat e pair s wit h exact
d ecimal represent at ion in a xed number of d igit s. For any vect or of
lengt h I0
D
a lowest -t er m t r iangle of lengt h 5
D
exist s; nor malizat ion
yield s a Car t esian pair of D d ecimal d igit s. Searching t he set of power s
(5 25 125 625 3125 15625) for ed ge p air s t hr ou gh six d igit s yield s
t hese unit vect or s:
(0.6 0.8)
(0.28 0.96)
(0.352 0.936)
(0.5376 0.8432)
(0.07584 0.99712)
(0.658944 0.752192)
As a circle is symmet r ic about any line t hrough t he or igin, t he eight
symmet r y axes implicit in t he Car t esian coord inat e syst em may be used
t o map t he point (a, b) int o all eight oct ant s, (ta, t b) and (tb, t a),
but not e t hat (1,0) gives r ise t o only t hree new point s. Taking t he r st
t wo ent r ies above, a t went y-point polygonal approximat ion t o a circle
may be for med , in which all ver t ices are exact and required only t wo
signicant d igit s for specicat ion. Present ed in count erclockwise d irec-
t ion beginning on t he x-axis t hese are as follows:

1.0,
(
0.0
)
, 0.96,
(
0. 28
)
, 0.80,
(
0.60
)
, 0.60,
(
0.80
)
, 0. 28,
(
0.96
)
0.0,
(
1.0
)
, 0. 28,
(
0.96
)
, 0.60,
(
0.80
)
, 0.80,
(
0.60
)
, 0.96,
(
0. 28
)
1.0,
(
0.0
)
, 0.96,
(
0. 28
)
, 0.80,
(
0.60
)
, 0.60,
(
0.80
)
, 0. 28,
(
0.96
)
0.0,
(
1.0
)
, 0. 28
(
, 0.96
)
, 0.60,
(
0.80
)
, 0.80,
(
0.60
)
, 0.96,
(
0. 28
)
II.1 CIRCLES OF INTEGRAL RADIUS ON INTEGER LATTICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 0
Alt er nat ely, t he met hod is useful in provid ing large t r iangle families wit h
common hypot enuse lengt hs by choosing values r ich in t able fact or s.
For i n st an ce, t h e easi l y r emember ed 16385 = (2
14
+ l ) h as fact or s
(5 29 113) all wit hin t he pr ime t able and yield s t hir t een t r iangles wit h
sid es r anging fr om (256, 16383) t hrou gh (11484, 11687). Mu ch lar ger
t r iangle set s are possible. For inst ance, a hypot enuse lengt h of 27625,
fact ored as (5 5 5 13 17) yield s up t hir t y-one d ist inct t r iangles, making
possible a polygon coincid ent wit h a circle at t wo hund red ft y-t wo
r at ional ver t ices.
What can be said of a circle d r awn on a convent ional fr ame buffer ? In
t his set t ing, t he met hod p rovid es an excellent accu r acy t est for any
circle-d r awing algor it hmfor select int eger r ad ii many pixels should be
always visit ed regard less of implement at ion specics, as t hey lie precisely
on t he circles p er imet er. Two not ewor t hy valu es ar e R= 325 (seven
t r iangles, sixt y ver t ices in circu lar agreement ) and R= 1105 (t hir t een
t r iangles, one hund red eight ver t ices). Searching for t he ed ge set s is
st r aight for ward since t he hypot enuse lengt hs are given, and may be sped
using t he id ent it y (i
2
j
2
)
2
+ (2ij)
2
= (i
2
+ j
2
)
2
. Row n of t he d ecimal
coord inat e t able cont ains an ent r y

T
n
0.6
( )
. This Chebyshev polynomial
may be evalu at ed u sing t he recu r sive for m T
n+1
= 1.2T
n
T
n1
wit h
T
0
= 1 and T
l
= 0.6.
See also What Ar e t he Coor d inat es of a Pixel? (246); Pecalcu -
lat ing Ad d resses for Fast Fills, Circles, and Lines (285)
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 1
II.2 NICE NUMBERS FOR GRAPH LABELS
II.2 II.2 II.2 II.2 II.2
N NN NNICE NUMBERS FOR ICE NUMBERS FOR ICE NUMBERS FOR ICE NUMBERS FOR ICE NUMBERS FOR
G GG GGRAPH LABELS RAPH LABELS RAPH LABELS RAPH LABELS RAPH LABELS
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
When creat ing a gr aph by comput er, it is d esir able t o label t he x and y
axes wit h nice number s: simple d ecimal number s. For example, if t he
d at a r ange is 105 t o 543, wed probably want t o plot t he r ange from 100
t o 600 and put t ick mar ks ever y 100 unit s (see Fig. 1). Or if t he d at a r ange
is 2.03 t o 2.17, wed probably plot a r ange from 2.00 t o 2.20 wit h a t ick
spacing of .05. Humans are good at choosing such nice number s, but
simplist ic algor it hms are not . The naive label-select ion algor it hm t akes
t he d at a r ange and d ivid es it int o n equal int er vals, but t his usually
result s in ugly t ick labels. We here d escr ibe a simple met hod for gener at -
ing nice gr aph labels.
The pr imar y obser vat ion is t hat t he nicest number s in d ecimal are 1,
2, and 5, and all power-of-t en mult iples of t hese number s. We will use
only such number s for t he t ick spacing, and place t ick mar ks at mult iples
of t he t ick sp acing. We choose t he minimu m and maximu m of t he
gr aphed r ange in eit her of t wo ways: (a) loose: round t he d at a minimum
Figure 1.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 2
II.2 NICE NUMBERS FOR GRAPH LABELS
d own, and t he d at a maximum up, t o comput e t he gr aph minimum and
maximum, respect ively, or (b) t ight : use t he d at a minimum and maximum
for t he gr aph minimum and maximum. The relat ive mer it s of t hese t wo
approaches are d iscussed in Tuft e (1983). Below is some pseud o-cod e for
t he loose met hod :
const nt ick 5; desired number of t ick marks
l oose_l abel : label t he dat a range from min t o max loosely.
(t ight met hod is similar )
procedure loose_label(min, max: real );
nfr ac: i nt;
d : real ; t ick mark spacing
gr aphmin, gr aphmax: real ; graph range min and max
r ange, x: real ;
begi n
r ange nicenum(max min, f al se);
d nicenum(r ange/ (nt ick 1), true);
gr aphmin oor (min/ d )*d ;
gr aphmax ceiling(max/ d )*d ;
nfr ac max( oor (logl0(d )), 0); number of fract ional digit s t o show
f or x gr aphmin to gr aphmax + .5*d step d do
put t ick mark at x, wit h a numerical label showing nfr ac fract ion digit s
endl oop;
endproc loose_label;
ni cenum: nd a nice number approximat ely equal t o x.
Round t he number if r ound = true, t ake ceiling if round = f al se.
f uncti on nicenum(x: real ; round : bool ean): real ;
exp: i nt; exponent of x
f: real ; fract ional part of x
nf: real ; nice, rounded fract ion
begi n
exp oor (logl0(x));
f x/ expt (10., exp); bet ween 1 and 10
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 3
II.2 NICE NUMBERS FOR GRAPH LABELS
i f round then
i f f < 1.5 then nf 1.;
el se i f f < 3. then nf 2.;
el se i f f < 7. then nf 5.;
el se nf 10.;
el se
i f f 1. then nf 1.;
el se i f f 2. then nf 2.;
el se i f f 5. then nf 5.;
el se nf 10.;
return nf*expt (10., exp);
endf unc nicenum;
We assume in t he above t hat logl0(z) is log base 10 of z.
We also assume expt (a, n) = a
n
for int eger n. But not e t hat t he exponen-
t iat ion rout ines in some mat h libr ar ies are inexact for int eger argument s,
and such er ror s can cause t he above cod e t o fail. On ear ly UNIX syst ems
I found t hat pow(10.,2.) 100 exact ly, so I wrot e my own expt funct ion by
mult iplying or d ivid ing in a loop. The pow rout ine in cur rent (BSD 4.3)
UNIX is t r ust wor t hy, however.
See Append ix 2 for C Implement at ion (657)
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 4
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
II.3 II.3 II.3 II.3 II.3
E EE EEFFICIENT GENERATION OF FFICIENT GENERATION OF FFICIENT GENERATION OF FFICIENT GENERATION OF FFICIENT GENERATION OF
S SS SSAMPLING JITTER USING AMPLING JITTER USING AMPLING JITTER USING AMPLING JITTER USING AMPLING JITTER USING
L LL LLOOK-UP TABLES OOK-UP TABLES OOK-UP TABLES OOK-UP TABLES OOK-UP TABLES
Joseph M. Cychosz
Purdue Universit y
W. Laf ayet t e, Indiana
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Present ed in t his paper is a met hod for gener at ing sampling jit t er using a
limit ed number of r and om number s. The proposed jit t er funct ion is a
funct ion of t hree var iables, namely t he sample locat ion (x,y) and t he
sample number (s) for t he locat ion. Fur t her more, t he met hod will pro-
d uce repeat able jit t er for all x and y wit hout requir ing eit her t he st or age
of a large number of r and om number s, or t he consist ent access of x, y,
and s. This paper examines t he applicat ion of t his jit t er funct ion t o t he
r ay-t r acing algor it hm.
Recent ad vances in t he r ay-t r acing algor it hm have u sed st ochast ic
sampling t echniques as a met hod of rend er ing ant i-aliased images. Cook
et al. (1984) pioneered t his approach in t heir wor k on d ist r ibut ed r ay
t r acing. Ot her wor ks by Cook (1983, 1986) examined t he use of st ochas-
t ic sampling in r ay t r acing from a t heoret ical per spect ive. Lee et al.
(1985) and Dipp and Wold (1985) also examined t he use of st ochast ic
sampling in r ay t r acing. Kajiya (1986) in a lat er wor k used st ochast ic
sampling as a basis for evaluat ing t he rend er ing equat ion. The t hree
wor ks by Cook, Lee, and Dipp used a jit t er funct ion t o simulat e Mont e
Car lo int egr at ion, in which r and om sampling point s for t he image plane
are gener at ed . The gener at ed jit t er is t hen used t o per t ur b t he sampling
r ays as t hey pass t hrough t he image plane. Cook, however, (1985, 1986)
id ent ied t hat t he d ist r ibut ion of samples should approximat e a Poisson
d isk based on Yellot s st ud y (1983) of t he d ist r ibut ion of cones in t he
ret ina of Rhesus monkeys.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 5
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
Alt hough it is not d esir able t o have a met hod t hat can prod uce a
consist ent set of sample point s for each fr ame in an animat ion, it may be
d esir able t o have a met hod t hat can prod uce a consist ent set of sample
point s d ur ing t he rend er ing of an ind ivid ual fr ame. For example, in a
scan-conver sion environment using a z-buffer such as Reyes (Cook et al.,
1987) each polygon should be sampled wit h t he same set of point s for
each pixel being processed . Wit hout consist ency, t empor al ar t ifact s may
occur as t he sample point s move wit hin t he pixels. A basic approach t o
t his problem would be t o save all of t he sample point s used in t he
gener at ion of an image for reuse at a lat er t ime. For a 512 512 image
wit h 16 samples per pixel, more t han eight million number s would have
t o be st ored . An alt er nat ive approach might be t o use a congruent ial
r and om-number gener at or t o gener at e t he sample point s as each pixel is
processed . Prod uct ion of a consist ent set of sample point s would not only
require t hat t he init ial seed of t he r and om-number funct ion be t he same,
but also t hat t he pixels be accessed in a consist ent manner. An opt imiza-
t ion algor it hm, such as screen space bound ing, may eliminat e t he need t o
examine cer t ain pixels, t hereby d ist ur bing t he r and om-number sequence.
Ot her opt imizat ion algor it hms may alt er t he number of samples required
for par t icular pixels. Mult iprocessor implement at ions would exper ience
similar problems in t he gener at ion of consist ent jit t er.
In r ay t r acing only t he efciency aspect of gener at ing sampling jit t er is
of concer n. Mult iprocessor implement at ions (Dipp and Swensen, 1984;
Clear y et al., 1983; Nishimu r a et al., 1983) and comp u t at ionally d is-
t r ibut ed approaches (Ar vo and Kir k, 1987; Ar vo and Kir k, 1987 Film
Show Cont r ibut ion) would exper ience similar problems, only t his t ime in
t he gener at ion of jit t er wit hout spat ial regular it y.
A Ji t t er Funct i on Usi ng Look- up Tabl es A Ji t t er Funct i on Usi ng Look- up Tabl es A Ji t t er Funct i on Usi ng Look- up Tabl es A Ji t t er Funct i on Usi ng Look- up Tabl es A Ji t t er Funct i on Usi ng Look- up Tabl es
Wit h st ochast ic sampling, t he sampling locat ions wit hin t he area of a
pixel are per t ur bed , or jit t ered. To implement t his met hod of ant i-alias-
ing, an efcient met hod for gener at ing jit t er is necessar y. As st at ed
ear lier, t he gener at ed r and om number s must be a funct ion of bot h t he
pixel locat ion and t he sample number for t hat pixel. Alt hough it is not
necessar y t hat t he funct ion have a long per iod (t hat is, t he t ime it t akes
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 6
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
for t he number sequence t o repeat ), it is necessar y t hat t he funct ion not
exhibit any pat t er ns of spat ial or t empor al regular it y wit h respect t o t he
image. Shown below is an implement at ion of t he proposed jit t er funct ion:
x
j
= URAND(mod (x + 2y + IRAND(mod (x + s,n)), n)
y
j
= URAND(mod (y + 2x + IRAND(mod (y + s + l,n)), n),
where
x, y = t he locat ion of t he given pixel,
s = t he sample number for t he given pixel,
x
j
= t he amount of jit t er in x for t he sample point ,
y
j
= t he amount of jit t er in y for t he sample point ,
URAND= a t able of unifor m r and om number s,
IRAND = a t able of r and om int eger s of t he r ange 0 t o n,
n = t he number of element s in t he r and om-number t ables.
The jit t er funct ion uses t wo look-up t ables. One t able, URAND, simply
cont ains a unifor mly d ist r ibut ed set of r and om number s, which may be
prescaled t o t he size of t he jit t er for nal use in t he sampling process. The
number s in t hese t ables may be gener at ed using st and ard , r and om-num-
ber gener at ion t echniques, such as t hose found in Knut h (1981) in press
(1988), or in LEcuyer (1988). The second t able, IRAND, cont ains a set of
int eger s, which is used as a shufe generat or t o prevent t he funct ion
from exhibit ing any spat ial pat t er ns. Wit hout t his t able, t he sampling
pat t er n gener at ed by t he funct ion would repeat , t hus causing a low-
frequency ar t ifact t o appear from t he result ing cor relat ion of t he sam-
pling pat t er n. The y + s + l component of t he equat ion for y-jit t er is
used t o ensure t hat t he x and y ind ices int o URAND are nonequal for all
values of x and y. Should t hey become equal, only t he d iagonal of t he
pixel will be sampled . This component can be simplied t o y + s by
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 7
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
eit her ext end ing IRAND t o n + 1 element s and by repeat ing t he r st
element , or by subst it ut ing t wo ind epend ent t ables, one for x and one for
y, for URAND. The lat t er approach is more d esir able, especially if t he
scaling of t he jit t er is nonunifor m in x and y.
A slight ly more comput at ionally efcient jit t er funct ion can be imple-
ment ed by using a bit wise or oper at ion t o comput e t he sums x + 2y and
y + 2x. Shown below is an implement at ion of t he jit t er funct ion using t he
or oper at ions and ind epend ent t ables for URAND:
x
j
= URANX(mod (or (x,2y) + IRAND(mod (x + s,n)),n)
y
j
= URANY(mod (or (y,2x) + IRAND(mod (y + s,n)),n).
Eval uat i on of t he Comput at i onal Cost Eval uat i on of t he Comput at i onal Cost Eval uat i on of t he Comput at i onal Cost Eval uat i on of t he Comput at i onal Cost Eval uat i on of t he Comput at i onal Cost
If t he size of t he look-up t ables is a power of t wo, t hen t he mod funct ions
can be replaced wit h bit wise and oper at ions. Mult iplicat ions by 2 may be
replaced wit h shift oper at ions. This allows t he funct ion t o be imple-
ment ed fair ly efcient ly, r equ ir ing only fou r addit ions, fou r indexed
loads, four and oper at ions, and t wo shift oper at ions.
Typical pseud o-r and om-number gener at or s use a feed back approach,
in which each number gener at ed is used as a seed t o gener at e t he next
number in t he sequence. Rand om-number gener at or s of t his t ype, ot her-
wise known as linear congruent ial generat ors (LCG), have t he follow-
ing for m:
R
i+1
= mod (R
i
s + c, m),
where R
i+1
is t he next r and om number in t he sequence, and R
i
is t he
cur rent seed . S, c, and m are t he mult iplier, and ad d it ive and mod ulo
t er ms of t he gener at or. While t his met hod seems t o be comput at ionally
simpler t han t he proposed approach, t his comput at ion oft en requires t he
use of eit her d ouble-precision or mult iple-precision int eger ar it hmet ic,
t hus requir ing sever al mult iplicat ions and ad d it ions. Fur t her more, t his
approach d oes not exhibit t he d esir able repeat abilit y t hat t he jit t er func-
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 8
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
t ions d o. Table 1 compares t he unweight ed oper at ional cost s of t he
jit t er-gener at ion met hod s, and Table 2 compares t he measured comput a-
t ional cost s of t he met hod s for a var iet y of comput er s. The t imes repor t ed
in Table 2 are t he CPU t imes in second s required t o gener at e jit t er for a
512 by 512 image wit h 16 samp les p er p ixel. The r and om-nu mber
gen er at or u sed was R
i + 1
= R
i
1629 + 1 mod u lu s 1048576, wit h an
init ial seed of 98531.
Table 2. Comput at ional Compar ison for Var ious CPUs.
Table 1. Oper at ional Cost Compar ison for Jit t er Gener at ions.
Oper at ion Jit t er 1 Jit t er 2 LCG
mult iplies 0 0 4
ad d s 3 2 2
ind exed load s 2 2 0
and / or s 2 3 0
shift s 1 1 0
Tot al 8 8 6
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 6 9
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
E EE EEval uat i on of t he Sampl i ng Pr oper t i es val uat i on of t he Sampl i ng Pr oper t i es val uat i on of t he Sampl i ng Pr oper t i es val uat i on of t he Sampl i ng Pr oper t i es val uat i on of t he Sampl i ng Pr oper t i es
The sampling proper t ies of t he t wo jit t er funct ions are evaluat ed using
approaches similar t o t hose used by Cook. The visual result s of t hese
t est s are present ed in Figs. 1 and 2. The r st t est (shown in t he upper left
of each gure), examines t he sampling pat t er n gener at ed by t he func-
t ions. A good sampling pat t er n will have t he point s r and omly d ist r ibut ed
wit h ver y lit t le clust er ing of t he point s. Once again, t o simulat e a Poisson
d isk d ist r ibut ion, t he sample point s should be r and omly d ist r ibut ed wit h
Figure 1. Jit t er funct ion 1 result s. Upper left : sampling pat t er n; upper right : 2D FFT
of t he pat t er n; lower left : image of single-point / pixel sampled comb; lower right :
image of 16-point s/ pixel sampled comb.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 0
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
Figure 2. Jit t er funct ion 2 result s. Upper left : sampling pat t er n; upper right : 2D FFT
of t he pat t er n, lower left : image of single-point / pixel-sampled comb; lower right :
image of 16-point / pixel-sampled comb.
some minimal separ at ion. In t he t est , t he pixels are 8 8 wit h 1 sample
per pixel. The 2D Four ier t r ansfor m of t he sample pat t er n is shown in t he
upper r ight of each gure. For compar ison, t he 2D Four ier t r ansfor ms for
u nifor mly sp aced samp ling and for cor relat ed r and om samp ling ar e
shown in Fig. 3.
The second t est examines t he image result ing from t he sampling of a
comb of sliver s, in which each sliver is 1.01 pixels wid e and 50 pixels
high. The result s for jit t ered single-point sampling are shown in t he lower
left of Figs. 1 and 2. The lower r ight shows t he resu lt s for jit t ered
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 1
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
Figure 3. Left : 2D FFT of unifor mly spaced sampling; right : 2D FFT of cor relat ed
sampling using 128 by 128 t iles.
Figure 4. Compar ison images. Upper Left : single-point / pixel unifor m sampling;
lower left : 16-point / pixel unifor m sampling; upper right : 16-point / pixel r and om
sampling; lower right : id eal image.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 2
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
Table 3. Er ror Analysis Result s for Single- and 16-Point Sampling.
16-p oint su bp ixel samp ling. For comp ar ison, Fig. 4 shows resu lt s of
unifor mly spaced sampling for single-point and 16-point sampling, and
16-point r and om sampling, as well as t he id eal image.
Table 3 compares t he er ror of t he var ious sampling met hod s for single-
and 16-point sampling. The er ror is d et er mined by compar ing t he pixel
values of t he result ing image for t he sampling met hod wit h t he pixel
values of an id eal square-aper t ure sampled image. Bot h jit t er funct ions
prod uce er ror levels compar able t o t hat of complet ely r and om sampling
(t hat is, t he jit t er is gener at ed using t he LCG r and om-number gener at or
p resent ed ear lier ) for all t hree cat egor ies of samp ling. Sixt een p oint
subpixel r and om sampling prod uces t he least er ror, followed closely by
16-point subpixel sampling using jit t er funct ions 1 and 2.
Use of t he Ji t t er Funct i on i n Ray Tr aci ng Use of t he Ji t t er Funct i on i n Ray Tr aci ng Use of t he Ji t t er Funct i on i n Ray Tr aci ng Use of t he Ji t t er Funct i on i n Ray Tr aci ng Use of t he Ji t t er Funct i on i n Ray Tr aci ng
In r ay t r acing, t he jit t er funct ion is used t o per t ur b t he d irect ion of t he
sampling r ays as t hey pass t hrough t he image plane. Ot her sampling r ays
may be jit t ered also, such as t he r ays used t o sample an area light source
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 3
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
t o prod uce soft shad ows, or t he locat ion of t he eye t o simulat e d ept h of
focus (Cook et al., 1984). To ant i-alias a given pixel locat ed at x, y, t he
following equat ions may be used t o per t ur b t he point where t he r ay
passes t hrough t he image plane:
x
s
= x
c
+ w
x
(x
j
.5)
y
s
= y
c
+ w
y
(y
j
.5),
where
x
s
, y
s
= t he sampling locat ion for t he pixel,
x
c
, y
c
= t he locat ion of t he cent er for t he pixel,
w
x
, w
y
= t he wid t h and height of t he pixel,
x
j
, y
j
= t he amount of jit t er (0. t o 1.).
Tr ad it ional r ay t r acing passes t he r ay t hrough t he cent er of t he pixel for
single r ay sampling, or t hrough unifor mly spaced point s wit hin t he pixel
for mult iple r ay sampling. To jit t er a square set of regular ly spaced
sample point s t he following equat ions may be used :

w
xs

x
x
n
,

w
ys

w
y
n

x
s
x
c

w
x
2
+ w
xs
mod s 1, n
( )
+ w
xs
y
j
,

y
s
y
c

w
y
2
+ w
ys
floor
s 1
n

_
,
+ w
ys
y
j
,
where
s = t he sample number for t he pixel,
n = t he number of samples per pixel (1 t o n).
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 4
II.3 EFFICIENT GENERATION OF SAMPLING JITTER USING LOOK-UP TABLES
A nal value for t he pixel may be found by comput ing t he aver age of t he
samples for t he pixel (t hat is, applying a box lt er ). Ot her lt er funct ions
may be used by weight ing t he samples by t he shape of t he lt er funct ion.
Concl usi ons Concl usi ons Concl usi ons Concl usi ons Concl usi ons
Present ed is a jit t er ing met hod t hat is not only comput at ionally efcient
(shown in Tables 1 and 2), but t hat also prod uces image-sampling result s
compar able t o r and om jit t er ing (shown gr aphically in Figures 1, 2, and 4,
and analyt ically in Table 3). The met hod can also gener at e reprod ucible
jit t er t hat is a funct ion of pixel locat ion and sample number. To aid in
provid ing insight int o t he const r uct ion of a jit t er funct ion, t wo jit t er ing
met hod s using look-up t ables are present ed for compar ison. Funct ion 1
exhibit s a good sampling pat t er n wit h a Poisson d ist r ibut ion; funct ion 2,
on t he ot her hand , has a less d esir able sampling pat t er n wit h some d egree
of spat ial regular it y.
See also Ray Tr acing (383); Scan Conver sion Summar y (75)
See Append ix 2 for C Implement at ion (660)
7 5 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 5
II II II II II
S SS SSCAN CONVERSION CAN CONVERSION CAN CONVERSION CAN CONVERSION CAN CONVERSION
S SS SSUMMARY UMMARY UMMARY UMMARY UMMARY
Scan con ver si on i s t h e gen er al t ech n i qu e of r en d er i n g a p i ece of
2D geomet r y i n t o a d i scr et e mesh . Typ i ca l l y i n gr a p h i cs we u se
sca n con ver si on t o r en d er p ol ygon s, l i n es, a l p h a n u mer i c ch a r -
a ct er s, a n d ot h er su ch i ma ge el emen t s i n t o a r ect a n gu l a r gr i d .
Usu al l y t h i s gr i d i s ei t h er a fr ame bu ffer or a p i ece of st an d ar d
comp u t er memor y, t h ou gh va r i ou s ot h er a p p r oa ch es a r e p ossi -
bl e. (Imagi n e a Logo t u r t l e car r yi n g can s of p ai n t , t r ai l i n g p ai n t
behind it as it cr awls.)
Th e n ext fou r Gems d emon st r a t e some d i ffer en t wa ys t o
sca n -con ver t p ol ygon s. Th ey sh ow some d i ffer en t a p p r oa ch es t o
t r a d i n g off sp eed , si mp l i ci t y, efci en cy, su p p or t of a n t i -a l i a si n g,
and gener alit y.
See also An t i -Al i a si n g Gems; Sca n l i n e Dep t h Gr a d i en t of a
Z-Buffered Tr iangle (361)
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 6
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
II.4 II.4 II.4 II.4 II.4
F FF FFAST ANTI-ALIASING AST ANTI-ALIASING AST ANTI-ALIASING AST ANTI-ALIASING AST ANTI-ALIASING
P PP PPOLYGON SCAN OLYGON SCAN OLYGON SCAN OLYGON SCAN OLYGON SCAN
C CC CCONVERSION ONVERSION ONVERSION ONVERSION ONVERSION
Jack C. Morrison
Evergreen, Colorado
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
This algor it hm scan-conver t s polygons int o r ast er lines at subpixel res-
olut ion, provid ing some ant i-aliasing benet s wit hout t he expense of
polygon-t o-pixel clipping or brut e-force subpixel scan conver sion. The
result ing d at a may be used wit h d ept h-buffer or scanline ant i-aliasing
hid d en-sur face met hod s.
Backgr ound Backgr ound Backgr ound Backgr ound Backgr ound
One approach t o red ucing aliasing ar t ifact s (for example, jaggies) in
comput er-gener at ed pict ures is t o rend er t he image at a high resolut ion,
t hen aver age t he result ing pixel d at a t o a lower-resolut ion d isplay. Al-
t hough t his met hod requires no new algor it hms, t he ad d it ional memor y
and execut ion t ime required t o prevent aliasing effect ively is high.
Mor e sop h i st i cat ed an t i -al i asi n g h i d d en -su r face met h od s, su ch as
Car p ent er s A-Bu ffer (1984), t yp ically requ ir e infor mat ion abou t t he
cover age of a pixel by t he polygon being rend ered . The usual met hod for
ext r act ing t his su bp ixel d et ail is t o ap p ly r ep eat ed ly a 2D clip p ing
algor it hm t o clip t he or iginal polygon t o t he bound ar ies of each pixel, and
comput e t he exact fr act ion of t he pixel area covered by t he polygon. This
area is t hen u sed t o scale t he polygon color int ensit y. The A-Bu ffer
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 7
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
met hod also conver t s t he clipped polygon int o a bit mask for visible
sur face d et er minat ion wit hin t he pixel.
For smoot h-shad ed and t ext ured images, a signicant amount of d at a is
maint ained at each polygon ver t ex, inclu d ing mod el coord inat es (for
t ext ure comput at ion), wor ld coord inat es and nor mal vect or (for shad ing),
and d isplay coord inat es (for pixel cover age and d ept h-pr ior it izing). All
t his infor mat ion must be int er polat ed at each clipping bound ar y, making
t he pixel-clipping met hod expensive. Since ult imat ely only a single shad -
ing r esu lt is need ed at each p ixel, a fast er ap p r oximat e met hod is
possible.
The Scan Conver si on Al gor i t hm The Scan Conver si on Al gor i t hm The Scan Conver si on Al gor i t hm The Scan Conver si on Al gor i t hm The Scan Conver si on Al gor i t hm
The following algor it hm efcient ly d et er mines approximat e pixel cover-
ages from a polygon represent ed by it s ver t ex coord inat es. Cover age area
and a subpixel bit mask can bot h be read ily comput ed .
It is assumed t hat t he polygon t o be conver t ed is convex, and t hat t he
ver t ices ar e consist ent ly ord er ed (for examp le, clockwise). For each
ver t ex, int eger x (hor izont al) and y (ver t ical) subpixel coord inat es are
comput ed from t r ansfor med oat ing point image coord inat es. In fact ,
only t he y-coor d inat es need be st ored for each ver t ex; x-coord inat es are
refer enced only once and can t her efor e be comp u t ed on t he y. X
resolut ion can be increased at no cost , up t o t he number of bit s available
for an int eger. Increasing y resolut ion requires t wo int eger s and a small
t ime incr ease p er su bp ixel y-coor d inat e. Power s of t wo ar e always
convenient , and it seems wise t o keep t he t wo resolut ions on t he same
ord er of magnit ud e. For clar it y, t he pseud o-cod e assumes a subpixel
resolut ion of eight t imes t he nal rend er ing resolut ion.
In t his algor it hm, x and y refer t o subpixel coord inat es, while pixel
and scanline refer t o t heir cor resp ond ing low-r esolu t ion cou nt er p ar t s.
Bot h refer t o t he d isplay coord inat e syst em (see Fig. 1). Polygon x and y
coord inat es are int er polat ed at subpixel resolut ion, but all ot her ver t ex
infor mat ion is int er polat ed only at d isplay pixel resolut ion. To comput e
pixel cover age, only t he left and r ight x-coord inat es of t he polygon at
each y-coord inat e wit hin t he cur rent scanline need t o be saved .
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 8
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
Ver t ex: record [ polygon vert ex informat ion
mod el, wor ld ,
nor mal, image: vector; geomet ric informat ion
x, y: i nteger; subpixel display coordinat es
];
Vleft , Vnext Left : Ver t ex; limit s of current left polygon edge
Vr ight , Vnext Right : Ver t ex; limit s of current right polygon edge
VscanLeft , VscanRight : Ver t ex; int erpolat ed vert ices at scanline
subpixel x-coordinat es of polygon wit hin current scanline
xLeft , xRight : array [0..7] of i nteger;
Figur e 1. Subpixel d isplay coord inat es.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 7 9
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
Scan convert one polygon. For each scanline, build xLeft [] and xRight []
wit h subpixel x edges at each of eight subpixel ys, and int erpolat e ot her
polygon vert ex informat ion once. See Fig. 2.
begi n
Vleft Polygon ver t ex V wit h minimum V.y;
Vnext Left (Vleft + 1) mod numVer t ex;
Vr ight Vleft ;
Vnext Right (Vr ight 1) mod numVer t ex;
for each subpixel y covered by polygon
for y Vleft .y by 1 do
updat e edge dat a if reached next vert ex
i f y = Vnext Left .y
then begi n
Vleft Vnext Left ;
Vnext Left (Vleft + 1) mod numVer t ex;
end;
i f y = Vnext Right .y
then begi n
Vr ight Vnext Right ;
Vnext Right (Vr ight 1) mod numVer t ex;
end;
Figure 2. Polygon ver t ices d ur ing scan conver sion.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 0
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
i f y > Vnext Left .y or y > Vnext Right .y
then begi n last scanline
cal l rend er Scanline(Vleft , Vr ight ,

y
8
);
return;
end;
int erpolat e subpixel x endpoint s at t his subpixel y
xLeft [y mod 8] ler p(Vleft .x, Vnext Left .x at y);
xRight [y mod 8] ler p(Vr ight .x, Vnext Right .x at y);
i f (y mod 8) = 7
then begi n end of scanline
VscanLeft ler p(Vleft , Vnext Left at y);
VscanRight ler p(Vr ight , Vnext Right at y);
cal l rend er Scanline(VscanLeft , VscanRight ,

y
8
);
end;
endl oop;
end;
Render one scanline of t he polygon from t he subpixel informat ion. The
shad ing and rend er Pixel procedures are beyond t he scope of t his Gem!
rend er Scanline: procedure (VscanLeft , VscanRight , scanLine);
begi n
for each pixel in scanline overlapped by polygon
for pixel

min xLeft
( )
8
to

max xRight
( )
+ 7
8
do
area comput ePixelCover age(pixel);
Vpixel ler p(VscanLeft , VscanRight at pixel);
color

area
256

shad ing(Vpixel);
mask comput ePixelMask(pixel); (if needed)
insert ant i-aliased pixel dat a int o hidden surface rout ine
rend er Pixel(scanLine, pixel, color, mask);
endl oop;
end;
Comput e fract ion of t he pixel (act ually, number of subpixels) covered by
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 1
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
t he polygon. See Fig. 3.
comput ePixelCoverage: procedure (pixel);
begi n
area 0;
pixelLeft pixel 8; subpixel edges of pixel
pixelRight pixelLeft + 7;
for y 0 to 7 do
par t ialArea min(xRight [y], pixelRight )
max(xLeft [y], pixelLeft ) + 1;
i f par t ialArea > 0 polygon overlaps t his pixel
then area area + par t ialAr ea;
endl oop;
return area;
end;
Figure 3. Example pixel cover age comput at ion.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 2
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
Comput e subpixel bit mask indicat ing which part s of t he pixel are covered by
t he polygon. Look-up t ables speed up mask comput at ion when t he polygon
edge occurs inside t he pixel. See Fig. 3.
comput ePixelMask: procedure (pixel);
left MaskTable: array[0..7] of char
[0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01];
r ight MaskTable: array[0..7] of char
[0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF];
begi n
pixelLeft pixel 8; subpixel edges of pixel
pixelRight pixelLeft + 7;
for y 0 to 7 do
i f xLeft [y] < pixelLeft
then left Mask 0xFF;
el se i f xLeft [y] > pixelRight
then left Mask 0;
el se left edge of polygon is inside pixel
left Mask left MaskTable[xLeft [y] pixelLeft ];
i f xRight [y] > pixelRight
then r ight Mask 0xFF;
el se i f xRight [y] < pixelLeft
then r ight Mask 0;
el se right edge of polygon is inside pixel
r ight Mask r ight MaskTable[xRight [y] pixelLeft ];
mask[y] left Mask bi t-and r ight Mask;
endl oop;
return mask;
end;
Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es
Care must be t aken at t he r st and last scanline of a polygon, where some
y-coord inat es may not be covered . Set t ing t he u ncovered xLeft [] and
xRight [] values t o 1 is sufcient , making sure renderScanline ignor es
such ed ges.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 3
II.4 FAST ANTI-ALIASING POLYGON SCAN CONVERSION
Pixel area (and bit mask) comput at ion can be read ily opt imized t o make
use of pixel-t o-pixel coherence, since most pixels wit hin a scanline are
complet ely covered by t he polygon. One approach is t o d et er mine t he
maximu m xLeft [] and minimu m xRight [], for all su bp ixel ys, at t he
begi n n i n g of renderScanline. Comput ePix elCov erage ca n t h en i mme-
d iat ely ret ur n maximum area for pixels bet ween t hese limit s.
If t he pixel bit mask is t o be comput ed at coar ser resolut ion t han t he
area, t he left and r ight limit s should be aver aged over each group of
y-coord inat es t o comput e par t ial bit masks. The lookup t ables can ac-
count for red ucing x resolut ion aut omat ically. If bot h bit masks and areas
are d esired , t hey can be comput ed t oget her t o red uce overhead , or t he
area d et er mined from t he bit mask. (See Car pent er ' s A-Buffer ar t icle for
t ips on comput ing area from a bit mask.)
As d escr ibed , t he scan conver sion algor it hm is suit able for d ept h-buffer
hid d en-sur face met hod s, where one polygon at a t ime is rend ered . For
scanline met hod s, where one scanline at a t ime is r end ered (for all
polygons), st and ard int er polat ion or clipping proced ures can be used t o
ext r act polygon ver t ex d at a at t he scanline limit s, wit h t he above algo-
r it hm used wit hin t he scanline t o comput e subpixel d et ail.
For an RGBaZ-st yle hid d en-sur face met hod , such as t he one d escr ibed
by Duff (1985), it may be useful t o int er polat e Z-coord inat es (d ept h) at
high resolut ion also, so t hat d ept h at each cor ner of t he pixel can be
d et er mined more accur at ely.
For relat ed infor mat ion see Cat mull (1978), Crow (1977), and Sut her-
land (1974).
See Append ix 2 for C Implement at ion (662)
8 4
II.5 GENERIC CONVEX POLYGON SCAN CONVERSION AND CLIPPING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 4
II.5 II.5 II.5 II.5 II.5
G GG GGENERIC ENERIC ENERIC ENERIC ENERIC
C CC CCONVEX POLYGON SCAN ONVEX POLYGON SCAN ONVEX POLYGON SCAN ONVEX POLYGON SCAN ONVEX POLYGON SCAN
C CC CCONVERSION AND CLIPPING ONVERSION AND CLIPPING ONVERSION AND CLIPPING ONVERSION AND CLIPPING ONVERSION AND CLIPPING
Paul S. Heckbert
Universit y of Ca!if ornia
Berkeley, Calif ornia
When d oing facet ed shad ing, Gour aud shad ing, Phong shad ing, or t ext ure
mapping in a paint er s or z-buffer hid d en-sur face algor it hm, t ypically a
small set of oat ing point at t r ibut es need t o be int er polat ed across a
polygon. For example, when d oing z-buffered color Gour aud shad ing, t he
fou r at t r ibu t es r, g, b, and z are u sed : t hey ar e init ialized at each
polygon ver t ex and int er polat ed at each pixel wit hin t he polygon. If linear
int er polat ion is used for all at t r ibut es, t hen t he cod e for int er polat ing
each at t r ibut e is ver y similar. Maint aining separ at e source cod e for scan
conver t ing each d ifferent at t r ibut e set becomes ver y t ed ious.
I see t hree gener al t echniques for red ucing t he cod e red und ancy: (1)
use a ver t ex st r uct ure wit h d ynamic size and layout cont aining only t he
at t r ibut e set of int erest ; (2) use a st at ic ver t ex st ruct ure t hat includ es all
at t r ibut es, and int er polat e ever yt hing; and (3) use a st at ic ver t ex st ruct ure
but int er polat e only t he at t r ibut es of int erest . Alt er nat ive (1) is t he most
space-efcient , but at t r ibut e offset s must be comput ed at run-t ime, so it is
slow. Met hod (2) red uces access t ime because t he xed st ruct ure offset s
allow compile-t ime opt imizat ion, but it is less efcient over all because
unused at t r ibut es would be allocat ed and int er polat ed . Met hod (3) is t he
fast est , as offset s are xed at compile-t ime, and only relevant at t r ibut es
are int er polat ed . It uses more st or age t han t he r st met hod , however.
The following is C cod e t hat I have evolved over t he year s t o per for m
scan conver sion and clipping of gener ic convex, planar polygons. Use of
met hod (3) allows a var iet y of at t r ibut e set s t o be hand led efcient ly
wit hout changing t he scan conver t er or clipper cod e, as t hose rout ines
are d evice-ind epend ent and ignor ant of t he semant ics of t he at t r ibut es
8 5
II.5 GENERIC CONVEX POLYGON SCAN CONVERSION AND CLIPPING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 5
being int er p olat ed . The scan conver t er is based on an algor it hm by
Fr ank Crow at NYIT, and t he clipper is based on an implement at ion of
Sut her land -Hod gman by Ed Cat mull and Alvy Ray Smit h (Sut her land and
Hod gman, 1974). I rewrot e and evolved t he cod e sever al t imes at Pacic
Dat a Images, Pixar, and UC Ber keley. I have been careful wit h round off;
consequent ly, polygons shar ing an ed ge abut per fect ly wit h no gaps or
over lap. Lance Williams suggest ed int er polat ion of gener ic at t r ibut es, and
int er polat ion masks were inspired by Whit t ed and Weimer (1982). Henr y
Moret on suggest ed t he t ext ure coord inat e int er polat ion t r ick involving
d ivision by sw.
The gener al met hod for using t hese rout ines is as follows: load d at a
int o t he ver t ices, set t he polygons mask t o ind icat e which at t r ibut es are
in use, call t he clipper, mod ify t he ver t ex at t r ibut es (t ypically a homoge-
neous d ivid e), set t he polygons mask t o ind icat e which at t r ibut es are
st ill in use, call t he scan conver t er, supplying a callback proced ure t hat is
called by poly_scan at each pixel, and in t he pixel r out ine, unload d at a
from t he int er polat ed point , using it t o d r aw int o a r ast er image.
There are four les of gener ic cod e,
poly.h Polygon d at a st ruct ure.
poly.c Ut ilit y subrout ines t o pr int polygons.
poly_scan.c Scan convert a convex, planar polygon by unifor m
sampling at pixel cent er s.
poly_clip.c Clip a convex, planar polygon t o a screen space
par allelepiped ,
and t wo les giving simple examples of t heir use:
scant est .c Gour aud shad ing wit h z-buffer using poly_scan.
fancyt est .c Phong shading and texture mapping using poly_clip
and poly scan.
You can change anyt hing in t he Poly_vert st r uct ure d enit ion except t he
screen space posit ion eld s sx, sy, sz, and sw, which are required by
8 6
II.5 GENERIC CONVEX POLYGON SCAN CONVERSION AND CLIPPING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 6
poly_scan and poly_clip. All eld s of Poly_vert shou ld be d ou bles.
Not e t hat incor r ect set t ings of t he int er p olat ion mask can resu lt in
meaningless at t r ibut e values or wast ed comput e t ime. For C environ-
ment s t hat d ont have t he bcopy rout ine, use #dene bcopy (fr om, t o,
nbyt es) memcpy(t o, from, nbyt es).
Not e t hat linear int er polat ion is not cor rect for all at t r ibu t es; it is
appropr iat e only when t he mapping bet ween screen space x, y and t he
at t r ibut e is afne (linear plus a const ant ). Incid ent ally, linear int er polat ion
for Gour aud and Phong shad ing on polygons wit h more t han t hree sid es
gives result s t hat are, in gener al, not rot at ion-invar iant , so in t his sense
linear int er p olat ion is not cor r ect for t hose p u r p oses. The er r or s
caused by linear-int er polat ed Gour aud and Phong shad ing are invisible in
most images, however, unlike t he er ror s of linear-int er polat ed per spect ive
t ext ure coord inat es, which t ypically cause a d ist r act ing rubber sheet
effect . A d iscussion of afne and project ive mappings and t heir efcient
comput at ion is given in Heckber t (1989).
See also Concave Polygon Scan Conver sion (87)
See Append ix 2 for C Implement at ion (667)
8 7
II.6 CONCAVE POLYGON SCAN CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 7
II.6 II.6 II.6 II.6 II.6
C CC CCONCAVE POLYGON ONCAVE POLYGON ONCAVE POLYGON ONCAVE POLYGON ONCAVE POLYGON
SCAN CONVERSION SCAN CONVERSION SCAN CONVERSION SCAN CONVERSION SCAN CONVERSION
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
Scan conver sion of concave polygons or nonsimple polygons is more
complex t han scan conver sion of convex polygons because each scan line
can int er sect t he polygon in more t han one int er val or span of pixels. (A
polygon is simple if it d oes not int er sect it self.) When scan-conver t ing
concave polygons one must keep t r ack of a var iable-lengt h act ive edge
list of polygon ed ges int er sect ing t he cur rent scan line. This is not as
d ifcult as it may seem. Many published algor it hms for concave (or
convex) polygon scan conver sion employ voluminous cod e t o hand le t he
special cases of hor izont al ed ges. In fact , su ch cases d o not r equ ire
special t reat ment if care is t aken in t he inequalit ies of t he cond it ionals.
The progr am result ing from t his approach, list ed below, is sur pr isingly
simple. It is ver y similar t o t hat of Roger s (1985), p. 76.
This progr am assumes t he polygon is d escr ibed by a single cyclic loop
of point s. To d escr ibe polygons wit h holes using t his d at a st ruct ure,
const r uct br id ges joining t he holes t o t he out er ver t ices. For example,
if t he out er polygon has ver t ices p[i] for 0 i < np, and t he inner
polygon has ver t ices q[i] for 0 i < nq, const r uct a single polygon
co n s i s t i n g o f t h e v e r t i ce s : p[ 0] . . . p[ n p 1] , p [ 0] , q [ 0] . . . q[ n q l ] ,
q[0]. The t wo new br id ge ed ges connect ver t ices p[0] and q[0] in bot h
d irect ions.
Depend ing on t he sor t ing algor it hm used and t he shape of t he polygon,
t he complexit y of t his algor it hm will be bet ween O(n) and O(n
2
).
8 8
II.6 CONCAVE POLYGON SCAN CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 8
Figure 1. Out put for a r and om 25-sid ed polygon.
The progr am follows:
concave: scan convert n-sided concave nonsimple polygon wit h vert ices at
(pt [i].x, pt [i].y) for i in [0..n 1] wit hin t he window win by calling
drawproc for each visible span of pixels.
Polygon can be clockwise or count erclockwise.
Algorit hm does uniform point sampling at pixel cent ers.
Insideoutside test done by Jordans rule: a point is considered inside if an
emanat ing ray int ersect s t he polygon an odd number of t imes.
drawproc should ll in pixels from xl t o xr inclusiue on scanline y, e.g:
procedure drawproc(y, xl, xr: int );
x: int ;
begin
for x xl t o xr do
pixel_writ e(x, y, pixelvalue);
endloop;
endproc;
8 9
II.6 CONCAVE POLYGON SCAN CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 8 9
Not e: lines enclosed in angle bracket s<, > should be replaced wit h t he
code described.
Point : type record [x, y: real ]; 2D point
Wind ow: type record [xmin, ymin, xmax, ymax: i nt]: inclusive window
Spanproc: type procedure(y, xl, xr : i nt);
procedure concave(
n: i nt; number of vert ices
pt : array [0..n 1] of Point ; vert ices of polygon
win: Wind ow; screen clipping window
d r awproc: Spanproc; called for each span of pixels
);
Ed ge: type record [ a polygon edge
x: real ; x-coordinat e of edges int ersect ion wit h
current scanline
d x: real ; change in x wit h respect t o y
i: i nt; edge number: edge i goes from pt [i] t o
pt [i + 1]
];
nact : i nt; number of act ive edges
act ive: array [0..n 1] of Ed ge; act ive edge list : edges crossing scanline y
ind : array [0..n 1] of i nt; list of vert ex indices
k, y0, yl, y, i, j, xl, xr : i nt;
begi n procedure concave
creat e y-sort ed array of indices ind [k] int o vert ex list
for k 0 to n 1 do
ind [k] k;
endl oop;
<sor t ind by pt [ind [k]].y>
nact 0; st art wit h empt y act ive list
k 0; ind[k] is next vert ex t o process
y0 max(win.ymin, ceiling(pt [ind [0]].y .5)); ymin of polygon
y1 min(win.ymax, oor (pt [ind [nl]].y .5)); ymax of polygon
9 0
II.6 CONCAVE POLYGON SCAN CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 0
for y y0 to y1 do st ep t hrough scanlines
scanline y is at y + .5 in cont inuous coordinat es
check vert ices bet ween previous scanline and current one, if any
whi l e k < n and pt [ind [k]].y

y + .5 do
invariant : y .5 < pt [i].y

y + .5
i ind [k];
insert or delet e edges before and aft er vert ex i (i 1 t o i, and
i t o i + 1) from act ive list if t hey cross scanline y
j i f i > 0 then i 1 el se n 1; vert ex previous t o i
i f pt [j].y

y .5 then old edge, remove from act ive list


d elet e(j);
el se i f pt [j].y > y + .5 then new edge, add t o act ive list
inser t (j, y);
j i f i < n 1 then i + 1 el se 0; vert ex next aft er i
i f pt [j].y

y .5 then old edge, remove from act ive list


d elet e(i);
el se i f pt [j].y > y + .5 then new edge, add t o act ive list
inser t (i, y);
k k+ 1;
endl oop;
sort act ive edge list by act ive [j].x
draw horizont al segment s for scanline y
for j 0 t o nact 1 step 2 do
span bet ween j and j + 1 is inside, span from j + 1 t o j + 2 is out side
x1 ceiling(act ive[j].x .5); left end of span
i f x1 < win.xmin then xl win.xmin;
xr oor (act ive[j + 1].x .5); right end of span
i f xr > win.xmax then xr win.xmax;
i f x1

xr then
d r awproc(y, xl, xr ); draw pixels in span
increment edge coords
act ive[j].x act ive[j].x + act ive[j].d x;
act ive[j + 1].x act ive[j + 1].x + act ive[j + 1].d x;
endl oop;
endl oop; y loop
9 1
II.6 CONCAVE POLYGON SCAN CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 1
procedure d elet e(i: i nt); remove edge i from act ive list
j, k: i nt;
begi n
for j 0 to nact 1 do
i f act ive[j].i = i then begi n edge found
nact nact 1;
for k j to nact 1 do shift remainder of array down
act ive[k].x act ive[k + 1].x;
act ive[k].d x act ive[k + 1].d x;
act ive[k].i act ive[k + 1].i;
endl oop;
return;
end;
endl oop;
edge not found; t his can happen at win.ymin
endproc d elet e;
procedure inser t (i, y: i nt); append edge i t o end of
act ive list
j, p, q: i nt;
begi n
j i f i<n 1 then i+1 el se 0;
i f pt [i].y < pt [j].y then begi n p i; q j; end;
el se begi n p j; q i; end;
init ialize x posit ion at int ersect ion of edge wit h scanline y
act ive[nact ].d x (pt [q].x pt [p].x)/ (pt [q].y pt [p].y);
act ive[nact ].x act ive[nact ].d x*(y + .5 pt [p].y) + pt [p].x;
act ive[nact ].i i;
nact nact + 1;
endproc inser t ;
endproc concave;
See also Gener ic Convex Polygon Scan Conver sion and Clip-
ping (84)
See Append ix 2 for C Implement at ion (681)
9 2
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 2
II.7 II.7 II.7 II.7 II.7
F FF FFAST SCAN AST SCAN AST SCAN AST SCAN AST SCAN
C CC CCONVERSION OF ONVERSION OF ONVERSION OF ONVERSION OF ONVERSION OF
A AA AARBITRARY POLYGONS RBITRARY POLYGONS RBITRARY POLYGONS RBITRARY POLYGONS RBITRARY POLYGONS
Bob Wallis
Weit ek Corporat ion
Sunnyvale, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Many of t he t r ad it ional scan-conver sion met hod s d escr ibed in t he lit er a-
t ure are not well suit ed t o implement at ion on RISC processor s. A fast and
ext remely simple algor it hr m, which maximizes usage of machine regist er s
but sacr ices memor y t o gain speed , is d escr ibed .
Keepi ng t he Regi st er s Ful l Keepi ng t he Regi st er s Ful l Keepi ng t he Regi st er s Ful l Keepi ng t he Regi st er s Ful l Keepi ng t he Regi st er s Ful l
In d evising algor it hms for implement at ion on RISC processor s, a major
goal is minimizing t he amount of load / st ore t r afc bet ween machine
regist er s an(i memor y. This is par t icular ly t r ue in cost -sensit ive plat for ms
employing slow ext er nal memor y in which wait -st at e penalt ies are in-
cur red . On CISC processor s which are less regist er or ient ed , t his is not
as much of a concer n (or at least , t here is less oppor t unit y t o d o anyt hing
about it ).
Act i ve Edge Li st s Act i ve Edge Li st s Act i ve Edge Li st s Act i ve Edge Li st s Act i ve Edge Li st s
The st and ard scan-conver sion schemes t ypically ll a polygonal pat h by
emp l oyi n g a d at a st r u ct u r e cal l ed an act ive edge list (Fol ey an d
Van Dam, 1982). As t he scan line scrolls t hrough t he y d irect ion, t he
list s ed ge element s are upd at ed by ad d ing new line segment s t hat have
9 3
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 3
just begun st r ad d ling t he act ive scan, and by d elet ing lines t hat no longer
st r ad d le y. Each x element of t he list st ruct ure cont ains t he par amet er s
required t o prod uce x values for t hat given line segment of t he polygon.
By maint aining t he list s element s in x sor t ed ord er, t he x spans for a
given y are prod uced d irect ly.
A problem wit h t his scheme is t hat it is d ifcult t o t ake ad vant age of t he
relat ive sp eed of r egist er -t o-r egist er op er at ions. Each element of an
act ive ed ge list cor respond s t o a line of a different slope wit h d ifferent
DDA par amet er s; t hus, t here is a gr eat d eal of load / st ore t hr ashing
involved , wit h a signicant loss of efciency in t he nor mally ver y efcient
DDA int er polat or. Fur t her more, t here is a fair amount of sor t ing involved
in set t ing t hings up.
The following is a d escr ipt ion of an alt er nat e met hod , which scan-con-
ver t s t he line segment s of t he polygon one at a t ime so t hat t he same DDA
coefcient s may be kept in regist er s and used for t he ent ire d ur at ion of
t he line segment . The pr ice t o be paid is t hat t he met hod uses more
memor y t han t he act ive ed ge scheme. However, t he memor y require-
ment s are quit e mod est for applicat ions such as scan conver sion of font s,
which are t ypically represent ed as small polygons.
The X- Tr ansi t i on Tabl e The X- Tr ansi t i on Tabl e The X- Tr ansi t i on Tabl e The X- Tr ansi t i on Tabl e The X- Tr ansi t i on Tabl e
The basic concept is t o eliminat e explicit y-sor t ing and act ive ed ges
alt oget her. A polygon is t reat ed as just a succession of chained line
segment s, and each line segment is r ast er ized separ at ely as t he per imet er
of t he polygon is t r aver sed . As wit h all scan-conver sion algor it hms, if a
scan line is per mit t ed t o go d irect ly t hr ough a ver t ex t here are messy
special cases t o be d ealt wit h, and one has t o wor r y about half-open
int er vals and ot her ugly d et ails. The most exped ient way t o d eal wit h t his
problem is t o avoid it by using a coord inat e syst em t hat is much ner t han
t he pixel gr id for example, eight bit s of binar y fr act ion for an x or y
coord inat e. On a CPU wit h 32 bit regist er s, t his leaves plent y of room t o
t he left of t he d ecimal point . If t he least signicant bit of t he fr act ion is
always set t o 1, t his amount s t o ad d ing a t iny amount of ad d it ional
round off er ror, but a scan line never hit s a ver t ex (see Rend er ing Fat
Lines on a Rast er Gr id in t his volume).
9 4
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 4
Each line segment [x1, y1]

[x2, y2] is processed separ at ely, pro-


d ucing an x value for ever y scan line t hat it st r ad d les. This is d one wit h
a slight ly mod ied Bresenham DDA t hat suppor t s subpixel resolut ion. The
x values t hat are prod uced go int o a rect angular ar r ay refer red t o as t he
t ransit ion t able for t he p olygon. The ar r ay is high enou gh in t he y
d irect ion t o accommod at e all required scan lines for t he given polygon,
and wid e enou gh in t he x d ir ect ion t o accommod at e t he maximu m
possible number of line segment s t hat can st r ad d le a scan line. Consid er
t he example in Fig. 1, which has a t able 8 columns wid e and 11 scan lines
high. The line segment s of t he W polygon are scan-conver t ed in t he ord er
0-l, 1-2, and so on. As t he DDA walks d own t he scan lines, each x
prod uced is load ed int o t he next available column in t he yt h row of t he
t able. The sign of y2 y1 is also record ed in a reser ved bit of t he x
value.
Once t he t able has been complet ed , it is a simple mat t er t o visit each
row, sor t t he x values, and prod uce int er ior spans t o be lled for t hat
scan line. The x-sor t ing required from one row of t he map t o t he next
row is highly coher ent , making it ad vant ageous t o use t he sor t per mut a-
t ion required on t he previous row as an init ial guess on how t o x-sor t t he
cur rent row. A ver y gener al way t o d et er mine t he int er ior regions of t he
p olygon is t o u se t he signs of y2y1 t o t r ack t he winding number
t r ansit ions (Newell and Sequin, 1980) at each x int er sect ion (see Fig. 2).
The wind ing number st ar t s at zero out sid e t he polygon and is incre-
ment ed t 1 d epend ing on whet her t he line segment being crossed is
r ising or falling. Hor izont al lines are not encount ered because scans never
hit ver t ices. In t he example above, t he wind ing number r ises t o 3 in t he
Figur e 1.
9 5
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 5
inner most loop. If t he sense of t he polygon had been count erclockwise,
t he wind ing number s would have been negat ive but nonzero in bot h
cases. The shad ed areas in t he gure illust r at e t he st and ard even/ od d
par it y r ule, which d eclares a span t o be int er ior whenever t he least
signicant bit of t he wind ing number is 1. An alt er nat e d enit ion, suit able
for self-int er sect ing polygons, is t o d eclare any span wit h a nonzero
wind ing number as int er ior.
Y Ext r ema and Memor y Requi r ement s Y Ext r ema and Memor y Requi r ement s Y Ext r ema and Memor y Requi r ement s Y Ext r ema and Memor y Requi r ement s Y Ext r ema and Memor y Requi r ement s
One issue was glossed over ear lier : d et er mining how many colu mns
should be allocat ed for t he t r ansit ion t able. This should be t he wor st -case
number of x t r ansit ions possible for any scan line (maximum number of
Figure 2.
9 6
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 6
act ive ed ges). This can be easily d et er mined by count ing t he number of
t imes t hat t he polar it y of y2 y1 changes as we t r aver se t he polygon.
This count is easily obt ained at t he same t ime as t he polygons bound ing
box is calculat ed . Each of t he point s where t he slope of y changes is a y
ext rema. These are ind icat ed for Xs in Fig. 2. It has six ext rema, and
t hus has a maximum of six x t r ansit ions possible. The W polygon in Fig.
1 has eight ext rema, so it s t r ansit ion t able need s eight columns.
To be able t o scan-conver t polygons of ar bit r ar y size and complexit y, it
is necessar y t hat t he algor it hm be able t o break polygons t hat are t oo big
for t he t r ansit ion t able int o hor izont al swat hs, which are scan-conver t ed
separ at ely. The amount of memor y available for t he t able should be large
enou gh so t hat most p olygons can be hand led wit hou t r esor t ing t o
st r ipping.
Int er sect i ng Pol ygons Int er sect i ng Pol ygons Int er sect i ng Pol ygons Int er sect i ng Pol ygons Int er sect i ng Pol ygons
A simple mod icat ion of t he previous algor it hm per mit s it t o d et er mine
t he int er sect ion of t wo or more polygons. For example, t he int er sect ion
of a W and E is shown below. The t r ick is t o feed bot h polygons t o t he
scan conver t er as if t hey were one single polygon, but t o keep t heir
wind ing number s separ at e. For a given scan line, t he x regions wit hin
which bot h wind ing number s are nonzero are int er ior t o bot h polygons.
The int er sect ion cor respond s t o t he Boolean AND of bot h wind ing num-
ber s, but any ot her logical funct ion could be used .
Figure 3.
9 7
II.7 FAST SCAN CONVERSION OF ARBITRARY POLYGONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 7
Figur e 4.
Convex Decomposi t i ons Convex Decomposi t i ons Convex Decomposi t i ons Convex Decomposi t i ons Convex Decomposi t i ons
Some applicat ions require t hat ar bit r ar y polygons be fr act ured int o con-
vex gures, such as t r apezoid s. By ad d ing a lit t le more d at a t o t he x
element s st ored in t he t r ansit ion t able, t he algor it hm can ser ve as a front
en d for a convex d ecomp oser. It is n ecessar y t h at t h e x elemen t s
remember which line segment s t hey came from, so t hat segment s wit h
mat ching left and r ight sid es from successive scan lines can be merged
and grown int o convex polygons. Figure 4 shows a convex d ecomposit ion
of t he E/W int er sect ion.
9 8 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 8
II LINE-DRAWING SUMMARY
II II II II II
L LL LLINE-DRAWING INE-DRAWING INE-DRAWING INE-DRAWING INE-DRAWING
S SS SSUMMARY UMMARY UMMARY UMMARY UMMARY
Vect or gr a p h i cs r ema i n s a n i mp or t a n t a p p l i ca t i on a r ea of com-
p u t er gr a p h i cs. Al t h ou gh sh a d ed i ma ges h a ve mu ch t o offer,
lin es r emain a valu able p r imit ive for man y t yp es of images. Fast
and efcient line-d r awing is not a simp le t ask, p ar t icu lar ly if you
want t o avoid aliasing ar t ifact s.
Th e ba si c l i n e-d r a wi n g a l gor i t h m i s Br esen h a ms a l gor i t h m,
d evel op ed or i gi n a l l y for d i gi t a l p l ot t er s. Th e r st t h r ee of t h e
fol l owi n g a l gor i t h ms p r esen t t h e ba si c Br esen h a m t ech n i qu e
a n d t h en ext en d i t for i n cr ea sed sp eed , or for i n cl u si on of
a n t i -a l i a si n g i n for ma t i on . Th e n ext t wo Gems d i scu ss on l y l i n es
t h i cker t h a n on e p i xel , sh owi n g h ow t o p u t bevel s on t h e
cor n er s for med by t wo a t l i n e en d s, a n d h ow efci en t l y t o
rend er fat lines in a rect angular gr id .
See also An t i -Al i a si n g Gems; Fa st Sp l i n e Dr a wi n g (585); Tu t o-
r ial on For ward Differencing (594)
9 9
II.8 DIGITAL LINE DRAWING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 9 9
II.8 II.8 II.8 II.8 II.8
D DD DDIGITAL LINE DRAWING IGITAL LINE DRAWING IGITAL LINE DRAWING IGITAL LINE DRAWING IGITAL LINE DRAWING
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
A d igit al line-d r awing algor it hm is one of t he most basic t ools in a
comp u t er gr ap h i ci st s t ool box. Th e fol l owi n g i s skel et on cod e for
Bresenhams algor it hm (1965). The cod e as list ed calls a user-supplied
p r oced u r e at each p i xel , bu t for efci en cy you may p r efer t o d o
inline/ macro subst it ut ion for t he pixel proced ure.
di gl i ne: draw digit al line from (x1, y1)t o(x2,y2),
calling a user-supplied procedure at each pixel.
Does no clipping. Uses Bresenhams algorit hm.
Pixelproc: type procedure(x, y: i nt);
procedure d igline(x1, y1, x2, y2: i nt; d ot proc: Pixelproc);
d , x, y, ax, ay, sx, sy, d x, d y: i nt;
begi n
d x x2 x1; ax abs(d x)*2; sx sgn(d x);
d y y2 y1; ay abs(d y)*2; sy sgn(d y);
x x1;
y y1;
100
II.8 DIGITAL LINE DRAWING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 100
i f ax > ay then begi n x dominant
d ay ax/ 2;
whi l e true do
d ot proc(x, y);
i f x = x2 then return;
i f d 0 then begi n
y y + sy;
d d ax;
end;
x x +sx;
d d + ay;
endl oop;
end;
el se begi n y dominant
d ax ay/ 2;
whi l e true do
d ot proc(x, y);
i f y = y2 then return;
i f d 0 then begi n
x x + sx;
d d ay;
end;
y y + sy;
d d + ax;
endl oop;
end;
endproc d igline;
functi on sgn (x: i nt) : i nt;
begi n return i f x > 0 then 1 el se 1; endfunc;
See Append ix 2 for C Implement at ion (685)
101 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 101
II.9 SYMMETRIC DOUBLE STEP LINE ALGORITHM
II.9 II.9 II.9 II.9 II.9
S SS SSYMMETRIC DOUBLE STEP YMMETRIC DOUBLE STEP YMMETRIC DOUBLE STEP YMMETRIC DOUBLE STEP YMMETRIC DOUBLE STEP
LlNE LlNE LlNE LlNE LlNE ALGORITH ALGORITH ALGORITH ALGORITH ALGORITHM MM MM
Brian Wyvill
Universit y of Calgary
Albert a, Canada
Li ne Dr awi ng Li ne Dr awi ng Li ne Dr awi ng Li ne Dr awi ng Li ne Dr awi ng
Dr awing st r aight lines on a r ast er d evice, such as an increment al gr aph
plot t er or fr ame st ore is an old problem. Jack Bresenham (1965) pro-
d uced a simple and efcient algor it hm t hat lent it self t o hard ware imple-
ment at ion. Bresenhams algor it hm wor ks by keeping t r ack of t he er ror
bet ween t he act ual line and t he nearest pixel. This value is called a
discriminat or. The met hod has t o nd t he next pixel closest t o t he t rue
line. Once t he d irect ion of t he line is d et er mined , all t he algor it hm has t o
d o is d ecid e bet ween t wo alt er nat ive pixels at each st ep. To d o t his t he
d iscr iminat or is t est ed t o nd t he next pixel and t hen (and t his is t he
clever bit ) increment ed by a const ant amount read y for t he next t est . This
basic algor it hm was not great ly improved in over t went y year s. Many
resear cher s became int erest ed in line d r awing, par t icular ly t he hard ware
manufact urer s, who relied on prod ucing fast er line d r awing for compar i-
son wit h t heir compet it or s. Tod ay Bresenhams algor it hm is at t he hear t
of sever al fast line d r awing chips.
Doubl e Speed Br esenham s Doubl e Speed Br esenham s Doubl e Speed Br esenham s Doubl e Speed Br esenham s Doubl e Speed Br esenham s
A few year s ago one of my st ud ent s, Xialon Wu, approached me wit h an
excit ing new line d r awing algor it hm. At t he t ime his English was bad , his
claims out r ageous, and I was busy. Event ually Wu d eveloped his d ouble
st ep algor it hm wit h Prof. Jon Rokne and I realized what a good id ea he
102 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 102
II.9 SYMMETRIC DOUBLE STEP LINE ALGORITHM
Figure 1. Double st ep pixel pat t er ns (Wu, 1987).
had had (see Wu and Rokne, 1987). Like all good id eas it is ver y simple:
inst ead of using a d iscr iminat or t o choose t he next pixel, Wu chooses t he
next pat t er n of t wo pixels (see Fig. 1). Since t here are four d ist inct
pat t er ns, how d oes t he algor it hm red uce t o a simple binar y d ecision? Let
us for t he moment call pat t er ns 2 and 3 one pat t er n, 2(3). This could be
t he case on a mult ilevel d isplay since bot h pat t er ns could be shown as
one wit h t he cent er p ixels at half int ensit y t o achieve a d egree of
ant i-aliasing. It can be shown t hat for lines whose slope is less t han 1/ 2
t hat pat t er n 4 d oes not occur ; t he choice is t hen bet ween pat t er n 1 and
2(3). Similar ly, for lines wit h slope great er t han or equal t o 1/ 2, t he
choice is bet ween pat t er n 2(3) and pat t er n 4 (pat t er n 1 cannot occur ).
Simp ly by t est ing t he slop e ou t sid e t he p lot t ing loop t he algor it hm
red uces t o a single d iscr iminat or. To d ist inguish bet ween pat t er ns 2 and 3
also t ur ns out t o be easy, requir ing one more t est but using t he same
d iscr iminat or. In t his way t he algor it hm d oes only slight ly mor e wor k t o
prod uce t wo pixels inst ead of one per st ep, vir t ually d oubling t he speed
of Bresenhams or iginal. (A similar, but much mor e complex algor it hm
also exist s for quad ruple st ep pat t er ns (Bao and Rokne, 1990).
Usi ng Symmet r y Usi ng Symmet r y Usi ng Symmet r y Usi ng Symmet r y Usi ng Symmet r y
So impressed was I wit h t his breakt hrough t hat I cod ed t he algor it hm and
ad d ed a small change of my own. Since lines are symmet r ic about t he
cent er, it makes sense t o use t his symmet r y t o plot from bot h end s
103 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 103
II.9 SYMMETRIC DOUBLE STEP LINE ALGORITHM
symwuline(a1, b1, a2, b2) i nt a1, b1, a2, b2;
drawline from a1, b1 t o a2, b2
The algorit hm is described for slopes bet ween 0 and 1/2
The C version given lat er is generalized t o all quadrant s
begi n
d x

a2a1; This may be generalized t o


d y

b2b1; axis of great est movement


xend

(d x1) / 4;
pixelsLeft

(d x1) mod 4;
incr 2

4*d y2*d x;
plot rst t wo point s
set pixel(a1, b1);
set pixel(a2, b2);
c

2*d y;
incr l

2*c;
D

incr ld x;
plot t ing loop
f or i :i nt

0, i < xend , i

i + 1 d o
a1

a1 + 1;
a2

a21;
i f (D < 0) then
begi n
d r awPat t er n1For ward s;
d r awPat t er n1Backward s;
D = D + incr 1;
end;
el se begi n
i f (D < c) then
begi n
pat t er n2For ward s;
pat t er n2Backward s;
end;
el se begi n
pat t er n3For ward s;
pat t er n3Backward s;
end;
D = D + incr 2;
end;
endl oop;
i f pixelsLeft > 0 then
begi n
d r awTwoFor war d Pixels;
d r awTwoBackwar d Pixels;
end;
end;
Figure 2. Pseud o-cod e for symmet r ical d ouble st ep line algor it hm.
104 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 104
II.9 SYMMETRIC DOUBLE STEP LINE ALGORITHM
simult aneously using half t he number of st eps. Wu was not pleased t o see
t hat I had d oubled t he speed of his algor it hm over night ! It t ur ns out t hat
using t he symmet r y was not a new id ea; probably Bresenham himself
t hought of it or iginally. The symmet r ic d ouble st ep algor it hm is bet ween
t hree and four t imes fast er t han t he or iginal Bresenhams (see Rokne
et al., 1990). The hard war e manufact urer s were not par t icular ly int er-
est ed in Wus id ea. The bot t leneck (cur rent ly) in line d r awing is not
choosing t he pixels, but get t ing t he infor mat ion t o t he d isplay, t he pixel
wr it e oper at ions. Wu went on t o d evelop a similar id ea for d r awing conics
and Jon Rokne and Paul Bao cont inued wit h t he pat t er n id ea t o prod uce
a quad r uple st ep ver sion of t he line algor it hm. Pseud o cod e for lines wit h
slopes from 0 t o 1/ 2 is set out in Fig. 2. C cod e for lines of any slope is
given in t he append ix.
See Append ix 2 for C Implement at ion (686)
105
II.10 RENDERING ANTI-ALIASED LINES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 105
II.10 II.10 II.10 II.10 II.10
R RR RRENDERING ENDERING ENDERING ENDERING ENDERING
A AA AANTI-ALIASED LINES NTI-ALIASED LINES NTI-ALIASED LINES NTI-ALIASED LINES NTI-ALIASED LINES
Kelvin Thompson
Nt h Graphics, Lt d.
Aust in, Texas
Pr obl em Pr obl em Pr obl em Pr obl em Pr obl em
Rend er an ant i-aliased line segment .
Sol ut i on I Sol ut i on I Sol ut i on I Sol ut i on I Sol ut i on I
Mod el t he line segment as having a nit e t hickness and set each pixels
int ensit y accord ing t o how much it over laps t he line. We accomplish t his
wi t h a n ext en si on t o t h e t r a d i t i on a l Br esen h a m l i n e a l gor i t h m
(Bresenham,1965). Wit h each it er at ion, t he usual algor it hm moves by one
pixel along a major axis and by zero or one pixel along a minor axis (for
example, if t he lines slope is in t he r ange [1, 1], t hen t he major axis is
X and t he minor is Y). To expand t he algor it hm we ad d t wo loopscalled
ort hogonal loopsin sequ ence insid e t he t r ad it ional loop. Immed iat ely
aft er t he t r ad it ional algor it hm chooses t he cent r al pixel of t he line, t he
r st or t hogonal loop examines ad jacent pixels in t he posit ive d irect ion
along t he minor axis, t hen t he second or t hogonal loop examines ad jacent
pixels in t he negat ive d irect ion.
At each pixel (includ ing t he cent r al pixel) t he algor it hm upd at es a
var iable t hat cont ains t he d ist ance bet ween t he cent er of t he pixel and t he
mid d le of t he t hick line; t his dist ance variable can be used t o calculat e
(usually via a look-up t able) how much t he pixel over laps t he t hick line.
106
II.10 RENDERING ANTI-ALIASED LINES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 106
Also see Gupt a and Sproull (1981) for a more d et ailed d escr ipt ion of t he
algor it hm; Ver t ical Dist ance from a Point t o a Line (in t his volume) for
t he mapping bet ween t he ver t ical and t rue d ist ances bet ween a point at
a line; Area of Int er sect ion: Circle and a Thick Line (in t his volume) for
t he over lap calculat ion; and subrout ine Ant i_Line for example cod e.
Sol ut i on 2 Sol ut i on 2 Sol ut i on 2 Sol ut i on 2 Sol ut i on 2
Rend er sever al slight ly offset lines using t he t r ad it ional Bresenham line
algor it hm, but use alpha blend ing wit h progressively smaller cover age
values (for example, 1,

1
2
,

1
3
,

1
4
, , . . .; see Alpha Blend ing in t his volume).
The lines should all be par allel wit h slight ly d ifferent st ar t ing posit ions.
You can change t he subpixel st ar t ing posit ion in Bresenhams line algo-
r it hm by ad d ing values in t he r ange [0, 2 dx] t o t he init ial d ecision
var iable.
See Append ix 2 for C Implement at ion (690)
Figur e 1.
107
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 107
II.11 II.11 II.11 II.11 II.11
A AA AAN ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR
FILLING IN 2D WIDE FILLING IN 2D WIDE FILLING IN 2D WIDE FILLING IN 2D WIDE FILLING IN 2D WIDE
LINE BEVEL JOINTS LINE BEVEL JOINTS LINE BEVEL JOINTS LINE BEVEL JOINTS LINE BEVEL JOINTS
Jack Rit t er
Versat ec Inc.
Sant a Clara, Calif ornia
Typical 2D gr aphics packages allow for wid e lines t o be joined end t o
end , where t heir end cent er point s ar e coincid ent . Bevelling lls in t he
t r iangle bet ween t he out er cor ner s. This is an ar bit r ar y isosceles t r iangle.
From Fig. 1, t his bevel t r iangle is mad e of t he t wo out er point s 01 and
02, and t he cent er point C.
This algor it hm is an alt er nat ive t o breaking t he bevel t r iangle int o t wo
t r iangles wit h a hor izont al line, t hen gener at ing bot h t r iangles, t hus d oing
Bresenham walking along all t hr ee ed ges of t he bevel t r iangle. This
algor it hm lls in t he bevel t r iangle area wit h one nonrot at ed r ight t r ian-
gle, and zero, one, or t wo or t hogonal rect angles. The ad vant age is t hat
only t he out er ed ge is walked (as par t of d r awing t he r ight t r iangle), and
so t he amount of Bresenham st ep calculat ion is red uced t o about one
t hird . The ot her ad vant age is t hat it wont leave st iches (holes) along
t he t wo int er ior ed ges, where t he old met hod might , if t he d r awing of t he
wid e line and t he d r awing of t he t r iangles d id not meet ush. This
algor it hm may be easily implement ed in int eger ar it hmet ic.
The r ight t r iangle is (01, 02, R), where t he out er ed ge (01, 02) is it s
hypot enuse, shown as a dashed line in all gures. The t r iangles t wo
int er ior legs are shown as dot t ed lines. It s insid e 90-d egree cor ner point
will be one of t he t wo opposit e point s t o t he hypot enuse. Not e in Figs.
2, 3, and 4: t hat one opposit e point is R, and t he ot her is d epict ed by X.
Bet ween R and X, how d o we choose t he insid e point ? We pick t he one
closest t o c. Closeness can be d et er mined by Manhat t an d ist ance,
which is t he number of blocks you would walk in a cit y gr id t o get from
one point t o anot her. Euclid ian d ist ance need not be used .
108
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 108
Figure 1. Gener al Case.
Now we have t wo t r iangles: t he or iginal bevel t r iangle (01, 02, C), and
t he r ight t r iangle (01, 02, R). They share t he out er ed ge. The sit uat ion can
now be broken d own int o one of t hree st at es, d epend ing on t he t opological
r el a t i on sh i p bet ween t h ese t wo t r i a n gl es. On e of t h e fol l owi n g
will be the case:
case 1 (Fig. 2): C is insid e t he r ight t r iangle
case 2 (Fig. 3): R is insid e t he bevel t r iangle
case 3 (Fig. 4): neit her point is insid e t he ot her t r iangle.
For case 1, all we need d r aw is t he r ight t r iangle. For case 2, we d r aw t he
109
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 109
Figure 2. Case 1.
r ight t r iangle, p lu s t he t wo rect angles whose d iagonals ar e t he t wo
int er ior ed ges. For case 3, we d r aw t he r ight t r iangle, plus a rect angle
whose out er ed ge is t he longest leg of t he r ight t r iangle, and whose
opposit e ed ge goes t hrough C. C can be left of, r ight of, below, or above
t his out er ed ge. This rect angle can be t hought of as t he area swept out if
we push t he out er ed ge up t o C.
Here is t he pseud o-cod e for t his algor it hm:
01 is specied as (O1x, O1y),
02 is specied as (02x, 02y),
C is specied as (Cx, Cy),
R is specied as (Rx, Ry).
110
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 110
Figure 3. Case 2.
The main rout ine is r st , and t he called rout ines are d escr ibed
aft er ward s.
Input paramet ers are t he 3 point s 01, 02, & C
bevel_ll (01x, 01y, 02x, 02y, Cx, Cy)
{
First Anomoly
If all t hree point s are collinear, t he t wo wide lines are parallel.
This means t he bevel t riangle has collapsed t o not hing.
i f (01, 02, and C are colinear )
then
return;
end;
Second anomoly
If t he bevel edge (01, 02) is horizont al or vert ical
(Fig. 4 comes close t o being vert ical), t hen t he right
t riangle has collapsed t o not hing. In t his case, we simply
draw t he push rect angle of case 3.
111
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 111
i f ((0x1 = 0x2) or (0y1 = 0y2))
then
push line (01, 02) t o point C.
emit _push_rec(01, 02, C);
return;
end;
set R (eg, Rx, Ry) t o 1 of t he 2 hypot enuse opposit e point s,
whichever one is closest t o C.
The Ist point passed t o manhat t an_dist is C, t he 2nd is a pair of
coordinat es
i f (manhat t an_d ist (C, 01x, 02y) < manhat t an_d ist (C, 02x, 01y))
then
begi n
Rx 01x;
Ry 02y;
Figur e 4. Case 3.
112
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 112
end;
el se
begi n
Rx 02x;
Ry 0ly;
end;
emit _r ight _t r i(R, 01, 02); for all 3 cases
i f (pt _in_t r i(C, 01, 02, R) )
then
CASE 1
C is inside t he t riangle (01, 02, R).
Above emit t ed t riangle has covered t he full bevel t riangle.
return;
end;
i f (pt _in_t r i(R, 01, 02, C))
then
CASE 2
R is inside t he t riangle (01, 02, C).
Draw t he t wo rect angles whose diagonals are t he int erior edges
(C, 01) and (C, 02).
emit _d iag_rec(C, 01);
emit d iag_rec(C, 02);
return
end;
else CASE 3
Neit her point is inside t he ot hers t riangle
Draw t he rect angle swept by pushing an edge perpendicularly t o C.
The edge t o be pushed will be t he longest leg of t he right t riangle:
i f (manhat t an_d ist (R, 01x, 01y) > manhat t an_d ist (R, 02x, 02y) )
then emit _push_rec (R, 01, C);
el se emit _push_rec (R, 02, C);
end of bevel_ll( )
Rout ines called from above
emit t _r ight _t r i(P1, P2, P3)
draws t he right t riangle made of t he t hree point s passed, P1 is t he
t riangles inside point .
113
II.11 AN ALGORITHM FOR FILLING IN 2D WIDE LINE JOINTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 113
emit _d iag_rec(P1, P2)
draws t he rect angle whose diagonal is (P1, P2).
emit _push_rec(P1, P2, P3)
d et er mines if P3 is left of, r ight of, above, or below t he
ed ge (P1, P2), and d r aws t he rect angle t hat would be swept out if t he ed ge
wewere pushed d ir ect ly t o t he point C.
pt _in_t r i(P, T1, T2, T3)
Ret urns TRUE if t he point P is in t he t riangle (T1, T2, T3).
This is done by comparing t he signs of crossproduct s:
Here, sign_of( ) is act ually t he sign of t he z component of t he cross vect or
get winding direct ion of t riangle:
Let t ri_wind = sign_of [(T1, T2) (T1, T3)]
V3Cross ( (T1, T2), (T1, T3), WIND);
t r i_wind = sign_of(WIND);
Now cross P wit h each side
V3Cross ( (T1, P), (T1, T3), WIND);
sign1 sign_of(WIND);
V3Cross ( (T2, P), (T2, T1), WIND);
sign2 sign_of(WIND);
V3Cross((T3, P), (T3, T2), WIND);
sign3 sign_of(WIND);
i f (
sign1 = t r i_wind
and
sign2 = t r i_wind
and
sign3 = t r i_wind
)
then return(TRUE);
el se return(FALSE);
NOTE: all calls t o pt _in_t r i( ) have 01 before 02,
for consist ent wind ing.
114 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 114
(1)
(2)
II.12 II.12 II.12 II.12 II.12
R RR RRENDERING ENDERING ENDERING ENDERING ENDERING
F FF FFAT LINES ON A AT LINES ON A AT LINES ON A AT LINES ON A AT LINES ON A
R RR RRASTER GRID ASTER GRID ASTER GRID ASTER GRID ASTER GRID
Bob Wallis
Weit ek Corporat ion
Sunnyvale, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
If mult ipixel wid e lines are rend ered improper ly, unat t r act ive beat fre-
quencies wit h t he r ast er gr id may result . A pr act ical met hod for prod uc-
ing aest het ically pleasing fat lines is present ed .
The st and ard t ext books in comput er gr aphics d o not appear t o cover
algor it hms for rend er ing mult ipixel wid e lines on a r ast er gr id . On d evices
such as laser pr int er s, t he st and ard skinny lines gener at ed by t he conven-
t ional Bresenham algor it hm might be t oo nar row t o be accept able. A good
met hod for gener at ing unifor m-looking wid e is known, but t he approach
hasnt received t he exposure t hat it d eser ves in t he gr aphics communit y.
It is based on t he polygonal pens used by Hobby (1985).
Hobby s Pol ygonal Pens Hobby s Pol ygonal Pens Hobby s Pol ygonal Pens Hobby s Pol ygonal Pens Hobby s Pol ygonal Pens
Denot ing pixel cent er s by t he int eger coord inat es [x, y], we may d ene
t he int er ior of a line t o be t hose pixels t hat sat isfy t he cr it er ion
d < (ax + by + c) d.
This implies a line whose wid t h in pixel space is roughly

w 2d/ a
2
+ b
2
.
The p roblem is t hat t he act u al wid t h can change wit h t he p hasing
(c offset ) of t he line in pixel space. For example, using a =

1, b = 3,
115
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 115
c = {0,

20. 5}, d = 4.01, we obt ain d ifferent -looking lines for t he t wo
d ifferent c offset s (see Fig. 1).
Since t he phasing of a line relat ive t o t he pixel gr id may be r and om,
t his behavior should be avoid ed if possible. What is going on can be
analyzed by examining t he set of pixels t ur ned on by Eq. 1 as we
change t he line equat ion offset c. For t he t ime being, assume t hat t he
coefficient s a, b are mut ually pr ime, t hat is, t hey have been scaled by
1 /gcd (a,b). Using a =

1, b = 3, c = 0, d = 4, we have t he sit uat ion
d epict ed in Fig. 2. The number s above each pixel are t he values of
ax + by + c, which will be d ened t o be class numbers for t hat pixel
Figure 1.
Figur e 2.
116
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 116
(Hobbys nomenclat ure). The d iagonal lines t hrough t he pixel cent er s are
int end ed t o show t hat all p ixels having t he same class nu mber are
equivalent in a sense; each class is a d iagonal row of pixels, which st ack
t oget her t o for m our fat line. Since gcd(a, b) = 1, pixel cent er s of t he
same class along a given d iagonal line are separ at ed by t he d isplacement
vect or [b, a ]=[3, 1]. Ea ch d i a gon a l of t h e st a ck con t r i bu t es a p i xel
d ensit y of

1/ a
2
+ b
2
pixels per unit lengt h, and t here are precisely d
classes, so ou r line wid t h expressed in Eq. 2 is ind eed t he aver age
number of pixels per unit lengt h.
Not e t hat as we slid e t he int er ior region up and d own by changing t he
c coefcient , t he fact t hat we are using an open and closed int er val for t he
per missible class number s in Eq. 1 and an exact int eger for 2d, ensures
t hat t he number of pixels t ur ned on in a given column d oesnt behave
d iscont inu ou sly. If ou r a, b coefcient s were ir r at ional, t he behavior
would be similar t o using r at ional a, b coefcient s, but gr ad ually chang-
ing t he c offset . Therefore, keeping t he line wid t h invar iant wit h c is
required for unifor mit y.
In one d imension, t he analogous quest ion is how many int eger s lie in
t he semiopen int er val of wid t h 2W,
W< (x + c) W c = arbit rary phase shift , (3a)
as we slid e t he wind ow left or r ight by changing t he c phasing fact or ?
Since t he phase fact or is ar bit r ar y, we can merge t he W t er ms by using a
d ifferent phase fact or :
c = c + W different phase fact or
The answer can be for mulat ed in t er ms of oor funct ions (Gr aham et al.,
1989):


n 2W c'
]
c'
]
# of int egers in int erval . (4)
c < x 2W c combine wit h Eq. 3a. (3b)
117
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 117
If 2W is an int eger, it can be t aken out of t he oor funct ion, red ucing t he
value of n t o
n = 2W+

c'
]
c'
]
= 2W invariant wit h phase. (5)
If 2W is not an int eger, it cannot be t aken out of t he oor funct ions, and
it will cause var iat ions in n as we shift c.
In t he 2D line d r awing case, t he count er par t of 2W is 2d. To achieve
an int eger value of 2d for an ar bit r ar y line, Hobby has proposed using
polygonal pens, d ened by int eger offset vect ors. Relat ive t o t he cent er
of t he polygon, t he vect or s need only have coefcient s t hat are mult iples
of

1
2
. The symmet r y will guar ant ee t hat t he value of 2d is t hen an int eger.
For a line of a given slope, t he value of d in Eq. 1 is obt ained by d ot t ing
t he vect or [a, b] wit h t he int eger offset vect or t hat is closest t o being
nor mal t o t he line. This is easily accomplished wit h some abs and max
funct ions. An example is shown in Fig. 3. The envelope may be consid -
ered t he convolut ion of a line wit h a polygonal brush.
If we had for mulat ed t he line geomet r y using t he obvious Euclid ean
met r ic for line wid t h (a per fect ly circular pen inst ead of a polygon wit h
int eger d iamet er s), not only wou ld t he calcu lat ion of ou r x-sp ans
require square root s, but t he result s would act ually be infer ior t o t he
more efcient , int eger based , polygonal pen approach.
Sof t war e Impl ement at i on Sof t war e Impl ement at i on Sof t war e Impl ement at i on Sof t war e Impl ement at i on Sof t war e Impl ement at i on
Wr it ing efcient cod e t o imp lement p olygonal p en fat lines is qu it e
st r aight for ward . In a bit mapped environment , we would like t o lay d own
an ent ire scan line (x-span) at a t ime as we walk up our fat line in t he y
d ir ect ion. This requ ires a slight mod icat ion (d escr ibed here) t o t he
classic one-pixel-at -a-t ime, Bresenham DDA algor it hm. Some of t he t r icks
d escr ibed fall in t he cat egor y of exp ed iencies, and t hu s may offend
pur ist s.
The easiest way t o achieve subpixel accur acy is t o use a pixel coord i-
nat e syst em wit h a binar y fr act ion. Eight bit s of pixel fr act ion (1/ 256t h
of a pixel) wor ks quit e well. This provid es a simple way t o implement
oor / ceiling funct ions, half-open int er vals, and so on. If t he lsb of t he
118
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 118
fr act ion is always set , t his prevent s ver t ices from ever hit t ing an exact
scan line and eliminat es an ent ire class of ugly special cases t hat are
usually required in scan conver sion algor it hms. This effect ively red uces
t he accur acy t o 1/ 128t h of a pixel.
The basic t r ick in Bresenham-t ype linear int er polat or s is t o express t he
dx/dy p ar t as an (exact ) int eger p ar t and a p osit ive fr act ional p ar t ,
Figure 3.
119
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 119
Figure 4.
inst ead of t r ying t o approximat e it by a single number. This eliminat es
accumulat ed round off er ror :
dx/dy = I + f where 0 f < 1 DDA increment .
That is, 7/ 3 = 2 1/ 3 = 3 + 2/ 3 (I = 3; f = 2/ 3). Assuming
f = p/q, we insist t hat
0 f < 1 fract ional part
0 p < q numerat or.
In t he st and ard (one-pixel-at -a-t ime) algor it hm, I = 0. The mod ied
algor it hm is as follows:
P p0
unti l nished do
X X + I
p p + d p
i f (p q) then begi n
init frac t erm
keep 0 p q
int eger part
bump numerat or of fract ion
overow
rest ore legalit y of fract ion
carry int o int part
p p q
X X + I
end
endl oop
120
II.12 RENDERING FAT LINES ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 120
We can simplify t his wit h t he usual t r ick of biasing t he p t er m so it
over ows by crossing 0 inst ead of q. This usually saves one inst ruct ion in
t he inner loop.
One pr ice t o be paid for t he subpixel accur acy is t hat t he init ializat ion
of t he DDA coefcient s requires a d ivid e and remaind er oper at ion. If t he
pixel coord inat es wit h t heir binar y fr act ions exceed 16 bit s, t hen t he
int er med iat e prod uct will require 64 bit s.
Once t he d t er m of Eq. 1 has been d et er mined , using t he polygonal
pen, t he wid t h of t he x-span is xed . Consequent ly, t he DDA need s t o
t r ack only t he left sid e of t he line, and may d et er mine t he r ight sid e of t he
x-span by knowing t he wid t h. All of t his can be d one wit h exact r at ional
ar it hmet ic. Some ad d it ional cod e is required t o d eal wit h mit er joint s at
t he end s of line segment s.
121
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 121
II.13 II.13 II.13 II.13 II.13
T TT TTWO-DIMENSIONAL CLIPPING WO-DIMENSIONAL CLIPPING WO-DIMENSIONAL CLIPPING WO-DIMENSIONAL CLIPPING WO-DIMENSIONAL CLIPPING: :: ::
A AA AA VECTOR-BASED APPROACH VECTOR-BASED APPROACH VECTOR-BASED APPROACH VECTOR-BASED APPROACH VECTOR-BASED APPROACH
Hans J. W. Spoelder, Fons H. Ullings
Vrije Universit eit
Amst erdam, The Net herlands
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
The problem we ad d ress here is t he int er sect ion of lines wit h polygons.
Alt hough numerous t ext books on comput er gr aphics give par t ial solu-
t ions and hint s (Sed gewick, 1983; Su t her land and Hod geman, 1974;
Nicholl et al., 1987; Van Wyck, 1984; Cheng and Jiaan, 1986), it is hard
t o nd a complet e algor it hm. The algor it hm d escr ibed here provid es a
robust solut ion of t he pr oblem of clipping a vect or against a polygon. The
same algor it hm can wit h minor ext ensions be used for applicat ions such
as area shad ing and polygon lling.
Repr esent at i on: Int eger s and Vect or s Repr esent at i on: Int eger s and Vect or s Repr esent at i on: Int eger s and Vect or s Repr esent at i on: Int eger s and Vect or s Repr esent at i on: Int eger s and Vect or s
We will assume t hat t he algor it hms will be used for d r awing gr aphics on
some kind of gr aphics d evice wit h a possibly high but nit e resolut ion.
Consequent ly, an int eger not at ion of t he coord inat es involved seems most
appropr iat e. Since t he user coord inat es will gener ally consist of oat ing
point quant it ies, t hey will have t o be conver t ed int o int eger s. This can be
d one by mult iplying t he oat ing point quant it ies by a well-chosen int eger
const ant and by round ing t he result t o an int eger. Wit h t his, one ent er s
t he eld of xed point ar it hmet ic. Alt hough precaut ions have t o be t aken
t o ensure sufcient accur acy (and avoid over ows) no fund ament al prob-
lems are involved . We summar ize t he most r elevant feat ures here.
Let (x
p
, y
p
) d enot e t he oat ing point quant it y t o be conver t ed and let
SCALE d enot e t he int eger used for upscaling. Not e t hat t he number of
122
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 122
d ecimals preser ved is equal t o

log
10
(SCALE). The int eger represent a-
t ion cor respond ing t o (x
p
,y
p
) is t hen given by
iy
p
=

y
p
SCALE

1
]
1
Ad d it ion and subt r act ion d o not pose a problem. For mult iplicat ion and
d ivision a rescaling has t o be per for med . The cor rect way t o per for m t he
last t wo oper at ions is
mult iplicat ion: a = (b c)/ SCALE
division: a = (b SCALE)/ c.
Fur t her more, one should bear in mind t hat d ivisions should be avoid ed as
much as possible. So r at her t han evaluat ing, for inst ance, t he inequalit y
(a/ b) > (c/ d),
one should evaluat e
ad cb > 0.
These slight ar it hmet ic inconveniences are more t han mat ched by t he
ad vant ages. Not only d o int eger oper at ions per for m fast er t han t heir
oat ing point count er par t s on almost any machine, but above all t he
calculat ions are now d one in st and ard int eger innit e precision.
Anot her fund ament al d ecision is concer ned wit h t he represent at ion. In
t he algor it hm d escr ibed here we will use a so-called vect or not at ion, in
which t he end point s of t he line segment s are specied . This implies t hat
t he line segment s t hemselves are not d igit ized . Alt hough t his complicat es
somewhat t he algor it hm used , t he ad vant age is t hat at ever y st age t he
resolut ion is mer ely d et er mined by t he value of SCALE and not by t he
resolut ion wit h which t he vect or s are d igit ized . This is t he case in t he nal
st age of p ixel-based algor it hmsfor examp le t he Post -Scr ip t st encil
oper at ion.
ix
p
=

x
p
SCALE

1
]
1
123
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 123
Some Basi c Consi der at i ons Some Basi c Consi der at i ons Some Basi c Consi der at i ons Some Basi c Consi der at i ons Some Basi c Consi der at i ons
Let

P
i
{ }
i i
n
d enot e a set of n point s (x
i,p
, y
i,p
). The polygon P is t hen
d ened by t he n line segment s connect ing t wo consecut ive point s. Not e
t hat t his implies t hat t he cont our is always closed . We will fur t her more
assume t hat no t hree consecut ive point s of t he cont our are colinear ; t his
const r aint can be easily accommod at ed . As a gener alized not at ion for t he
point s of t he j
t h
segment of t he cont our, we will use
Not e t hat we use a half-open int er val for s t o avoid d ouble use of t he
end point of a segment . Some basic algebr a sufces t o d et er mine t he
int er sect ion bet ween a line segment l and t he segment s of t he cont our
(see Append ix). The pr oblem of clipping a line segment against a polygon
can now easily be solved . It involves t wo major st eps: t he calculat ion of
t he int er sect ions of t he line segment l wit h t he polygon and t he d et er mi-
nat ion of t he st at us of an ar bit r ar y point of l. By st at us we mean in t his
cont ext whet her t he point und er invest igat ion lies insid e or out sid e t he
polygon (not e t hat t he polygon is always closed ). Wit hin t he set of
possible int er sect ion of t he line segment l wit h t he cont ou r P t hree
d ifferent classes must be consid ered :
1. int er sect ions coincid ing wit h begin (or end ) point s of t he line seg-
ment s of t he polygon (see Fig. 1).
Figure 1. Two illust r at ions of begin point s of t he polygon coincid ing wit h t he line
segment l.
P(s) = P
j
+ s(P
j + 1
P
j
), s [0, 1).
1
124
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 124
Figure 2. Two illust r at ions of line segment s l, which (par t ially) coincid e wit h t he
cont our P.
2. line segment l, which coincid es (par t ially) wit h line segment s of t he
polygon, t hat is, an innit e number of int er sect ions, (see Fig. 2).
3. int er sect ions not belonging t o 1 or 2. We will refer t o t hese as
st andard.
It will be clear from t he examples given t hat it d oes not sufce t o merely
calculat e t he possible int er sect ion, but t hat ad d it ional infor mat ion has t o
be comput ed and st ored . One fur t her ingred ient is need ed for a complet e
d escr ipt ion of t he algor it hm. This is t o d et er mine t he relat ive posit ion of
t wo point s wit h respect t o a given line segment l. Again some basic
algebr a sufces. Let (x
1
, y
1
) and (x
2
, y
2
) d enot e t he end point s of t he line
segment l and let P wit h coord inat es (x
p
, y
p
) d enot e t he given point s.
Then compar ison of t he slopes of line segment l wit h t he line segment
d ened by one of t he end point s of l and P will result in a quant it y of
which t he sign d et er mines t he relat ive posit ion of P wit h respect t o l.
This quant it y S is given by:
> 0: P lies on one sid e of l
< 0: P lies on ot her sid e of l
Al gor i t hm Al gor i t hm Al gor i t hm Al gor i t hm Al gor i t hm
Aft er t hese basic consid er at ions, t he complet e algor it hm for nd ing t he
int er sect ions can now be st at ed as follows:
1. Test if t here is a simple int er sect ion bet ween line segment 2 and t he
polygon segment P
i
P
i+1
. If not , got o 5.
= 0: P lies on l S = (x
2
x
1
)(y
p
y
1
) (y
2
y
1
)(x
p
x
1
)
125
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 125
2. Test if t he int er sect ion found coincid es wit h P
i
or P
i+l
If not , ad d
t he int er sect ion t o t he list mar ked st andard and got o 8.
3. Test if t he int er sect ion is P
i1
. If so, got o 8.
4. Test if t he polygon point s P
i l
and P
i + 1
lie at opposit e sid es of l. If
so, ad d t he int er sect ion t o t he list mar ked st andard. Got o 8.
5. Test if t he line segment l coincid es wit h t he p olygon segment
P
i
P
i+ 1
. If not , got o 8
6. Test if bot h P
i
and P
i+l
lie on l; if not , got o 8.
7. Test if P
i1
and P
i+2
lie at opposit e sid es of l. If so, ad d bot h
point s t o t he int er sect ion list mar ked delayed.
8. Test if more polygon segments have to be investigated. If so, goto 1.
9. Add the begin and endpoints of l to the intersection list and sort it.
10. Scan t he list for t wo successive point s mar ked delayed. If present ,
r e mov e t h e r s t p oi n t f r om t h e l i s t a n d r e ma r k t h e s e con d a s
st andard.
If t he number of int er sect ions found is t wo, t he segment l d oes not
int er sect t he polygon (not e t hat t he end point s of t he l have been ad d ed t o
t he list !) Ot her wise t he st at u s of an ar bit r ar y p oint of l has t o be
d et er mined .
This can be d one using t he same algor it hm albeit wit h a slight mod i-
cat ion. The id ea behind t his calculat ion is t hat in going from t he insid e
of t he polygon t o t he out sid e, one will encount er an od d number of
int er sect ions. Not e t hat it is essent ial t o have a point t hat lies out sid e t he
polygon. This can be found easily by r st calculat ing t he smallest rect an-
gular box enclosing all t he point s of t he polygon. Any point out sid e t his
box will clear ly lie out sid e t he polygon. Let (x
0
, y
0
) d enot e such a point
and let (x
l
, y
l
) d enot e an ar bit r ar y point of 1. We can t hen d et er mine t he
number of int er sect ions k of t his line segment wit h t he polygon using
st eps 1 t o 6 of t he algor it hm. The int er pret at ion of k is not complet ely
st r aight for ward since it is possible t hat (x
l
, y
l
) lies on t he polygon. In
t hat case (x
l
, y
l
) need not be t aken int o account and consequent ly k has
126
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 126
Figure 3. Det er minat ion of t he st at us of point (x
1
, y
1
). The d ashed line represent s
t he box enclosing t he polygon P.
t o be d ecreased by 1. If t he result ing k is od d , (x
l
, y
l
) lies insid e t he
polygon; ot her wise it lies out sid e t he polygon.
Each line segment of t he int er sect ion list , d ened by t wo successive
point s of t he list , can now be mar ked as out side or inside. If we want t o
clip t he line segment l against t he polygon, only t he segment s mar ked
out side have t o be t aken int o account , whereas t he rever se is t rue for
lling t he polygon. It will be clear t hat t his scheme can be recur sively
repeat ed for m polygons.
Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on
We have implement ed a clipping pr oced ure, based on t his algor it hm in
st and ard C. The polygons are st ored in a linked list . Each element of t his
linked list hold s infor mat ion about a specic polygon and cont ains among
ot her t hings an id ent icat ion, a segment count , t he coord inat es of t he
smallest rect angular box enclosing t he polygon, some st at us infor mat ion,
and a point er t o a circular linked list . The element s of t his circular list
cont ain t he end point s of t he segment s of t he polygon (see Fig. 4).
Up on ent er ing t hese rou t ines, u ser coor d inat es are t r ansfor med t o
int eger represent at ion. Dur ing t he d enit ion of a cont our, t he cont our is
also st ret ched t o avoid colinear it y of t hree or more point s. For clipping
p u r p oses t he algor it hm d escr ibed in t he p reviou s sect ion is ap p lied
recur sively t o t he linked list of polygons. The specic act ions d epend on
t he st at us infor mat ion of t he polygon. When a line segment is clipped
against a specic polygon t his will result in a (new) set as line segment ,
which can eit her be plot t ed d irect ly or clipped subsequent ly against ot her
polygons.
127
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 127
Figure 4. Symbolic r epresent at ion of t he st or age st ruct ure of t he polygons.
Our implement at ion t ook approximat ely 500 lines of C cod e. Among
ot her s, t he following ut ilit ies were includ ed : d enit ion of polygons, re-
moval of polygons, t empor ar ily on and off swit ching of polygons. SCALE
was set t o 1024. The efciency of t he algor it hm was t est ed by clipping
r and om gener at ed line segment s I against p olygons wit h a var iable
nu mber of segment s. Using a SUN3/ 60 (MC68020, 20 MHz, Su nOS
4.0 C-compiler ) wor kst at ion we nd t hat t he aver age t ime need ed t o
hand le one segment of t he polygon is approximat ely 125 microsecond s.
Consequent ly, for a rect angular-shaped polygon one can process about
2000 vect or s per second .
Appendi x: Pol ygon St r et chi ng and Appendi x: Pol ygon St r et chi ng and Appendi x: Pol ygon St r et chi ng and Appendi x: Pol ygon St r et chi ng and Appendi x: Pol ygon St r et chi ng and
Int er sect i on Cal cul at i on Int er sect i on Cal cul at i on Int er sect i on Cal cul at i on Int er sect i on Cal cul at i on Int er sect i on Cal cul at i on
The problem of polygon st ret ching can be hand led ver y easily. Consid er
t he t hree consecu t ive p oint s P
i 1
, P
i
, and P
i + 1
. Since t he t wo line
segment s d ened by t hese t hree point s have one point in common (P
i
),
128
II.13 TWO-DIMENSIONAL CLIPPING: A VECTOR-BASED APPROACH
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 128
one need s merely t o compare t he slopes of t he t wo line segment s t o
t est for colinear it y. If t he relat ion
(y
i
y
i 1
)(x
i + 1
x
i
) = (y
i + 1
y
i
)(x
i
x
i 1
)
hold s, t he t hree point s are colinear. In t hat case it sufces t o consid er t he
segment P
i 1
P
i + l
r at her t han t he t wo segment s P
i 1
P
i
and P
i
P
i + 1.
The problem of int er sect ing t he t wo line segment s d et er mined by t he
point s P
i
P
i + 1
, and Q
i
Q
i + 1
, respect ively, can be solved as follows. Let
P(s) = P
j
+ s(P
j + 1
P
j
), s [0, 1)
Q(t ) = Q
j
+ t (Q
j + 1
, Q
j
), t [0, 1)
d ene t he line segment s und er consid er at ion. Then t he quant it ies a, b,
and c can be d ened as
a = (x
i + 1, p
x
i, P
)(y
i, Q
y
i + 1, Q
) (x
i, Q
x
i + l, Q
)(y
i + l, P
y
i, P
)
b = (x
i, Q
x
i, p
)(y
i, Q
y
i + 1, Q
) (x
i, Q
x
i + 1, Q
)(y
i, Q
y
i, P
)
c = (x
i + 1, p
x
i, p
)(y
i, Q
y
i, P
) (y
i + 1, P
y
i, P
)(x
i,Q
x
i, P
)
and s = b/ a and t = c/ a.
The following possibilit ies t hen exist :
a = 0 and b = 0: P
i
P
i+1
and Q
i
Q
i + l
coincid e
a = 0 and b 0: P
i
P
i+l
and Q
i
Q
i + l
are par allel
Ot her wise, t hey int er sect if s [0, 1) and t [0, 1), t hat is c < a and
b < a. Not e t hat t he pr oper way t o evaluat e s (and t ) is
s = (bSCALE)/ a.
See Append ix 2 for C Implement at ion (694)
129
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 129
II.14 II.14 II.14 II.14 II.14
P PP PPERIODIC TILINGS OF THE ERIODIC TILINGS OF THE ERIODIC TILINGS OF THE ERIODIC TILINGS OF THE ERIODIC TILINGS OF THE
PLANE ON A RASTER GRID PLANE ON A RASTER GRID PLANE ON A RASTER GRID PLANE ON A RASTER GRID PLANE ON A RASTER GRID
Greg Lee, Mike Penk, Bob Wallis
Weit ek Corporat ion
Sunnyvale, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Cer t ain result s from group and number t heor y are id eally suit ed for
analyzing and manipulat ing per iod ic pat t er ns on r ast er gr id s. Conver sely,
per iod ic plane t ilings may be used t o illuminat e some ot her wise abst r act
mat hemat ical concep t s in a concret e manner. A gener al met hod for
bit -blit t ing any per iod ic pat t er n int o a r ast er gr id will be d er ived .
Wal l paper Gr oups Wal l paper Gr oups Wal l paper Gr oups Wal l paper Gr oups Wal l paper Gr oups
Consid er t he per iod ic plane t esselat ion of Fig. 1. It is basically a jigsaw
puzzle composed of id ent ical but t er ies (d iseased mot hs?) in a hexagonal
ar r ay. The st and ard manner in which such t ilings are cat egor ized is by
examining t he t yp es of symmet r ies t hat leave t he p at t er n invar iant .
Infor mally, t his p rocess can be t hou ght of as over laying an innit e
t r anslucent t r acing of t he pat t er n over t he or iginal (also ext end ed t o
innit y), and examining t he nat ure of t he t r anslat ions, rot at ions, and ips
(mir ror images) of t he t r acing t hat result in an exact realignment of t he
t r acing wit h t he und er lying image. If an x, y coord inat e of t he plane is
viewed as a complex number, t he oper at ions of ipping t he t r acing over,
shift ing it , and rot at ing may be mod elled as composit ions of complex
conjugat ion, ad d it ion, and mult iplicat ion.
130
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 130
Figure 1.
If symmet r y-preser ving oper at ions are combined t oget her, t he element s
are found t o prod uce yet ot her symmet r y-preser ving oper at ions. Alge-
br aically, t he set of manipulat ions obey closure, possess inver ses, are
associat ive, and have an id ent it y element . Therefore, t hey for m a mat he-
mat ical group st ruct ure. The group proper t ies of a per iod ic t iling may be
used as a means of classifying it . It t ur ns out t hat t here are exact ly 17
t ypes of t hese, and t hey are known as t he plane cr yst allogr aphic groups,
or wallpaper groups (Bur n, 1985). The r st organized collect ion of (most
of) t hese ap p ear s t o be in t he or nament al d ecor at ions u sed in t he
Gr anad as Alhambr a cat hed r al, which d at es back t o t he t hir t eent h cen-
t ur y.
Consid er t he t iling d epict ed in Fig. 1. The par allelogr ams and boxes
d epict t hree d ifferent t ypes of regions, each of which may be used t o st ep
and repeat a t emplat e of t he pat t er n t o for m an ext end ed region of
wallpaper. The rect angular boxes are quit e at t r act ive from an implemen-
131
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 131
t at ion st and point , since once we ll one box, we can repeat it over t he
plane wit h bit -blit oper at ions t o prod uce t he d esired t iling. The next
sect ion will d escr ibe how t o d o t his.
The wallpaper group represent ed in Fig. 1 is of t ype p6, char act er ized
by sixfold and t hreefold rot at ional symmet r ies about t wo d ifferent xed
point s. The six-cent er is at t he r ight wingt ip, while t he t hree-cent er is at
t he t r ailing ed ge of t he left wing. If we t ake t he or igin of t he coord inat e
syst em as one of t he six-cent er s (t he cent er of Fig. 1), and call t he
coor d inat e of t he t hree-cent er z
3
, t hen we can d ene t wo symmet r y
oper at or s

and as


: z ze
j 2/ 6
sixfold rot at ion about z = 0


: z z z
3
( )
e
j 2/ 3
+ z
3
t hreefold rot at ion about z
3
.
These t wo group element s are sufcient t o gener at e t he ent ire st ruct ure.
The following will show how var ious combinat ions of t hese t wo gener a-
t or s may be used t o ext r act ot her symmet r ies. There have t o be pure
t r anslat ions lur king about in t he set of symmet r y oper at ions. We can
fer ret one of t hem out by combining t he

and oper at or s in such a way


as t o neu t r alize t he rot at ion component , leaving only a t r anslat ional
resid ue. One way t o d o t his is t o combine t wo rot at ions of 60 d egrees
about z = 0 wit h one rot at ion of 120 d egrees about z
3
. The result is

2
: z e
j 2 / 3
z z
3
( )
e
j 2/ 3
+ z
3 [ ]
,
which red uces t o


: z z + z
3
(e
j 2/ 3
1) z z
3
3e
2(1/ 12)
.
The

t r anslat ion vect or is p recisely a shift fr om one six-cent er t o


anot her. Combining and yield s ot her t r anslat ions, which are just
60-d egree rot at ions of one anot her. Any t wo ad jacent t r anslat ions for m
t he legs of a fund ament al par allelogr am (t wo t ypes of which are shown in
Fig. 1). Not e t hat each cont ains exact ly six but t er ies (if we cut and past e
wr apped -around fr agment s t oget her ). We can st ep and repeat one of
t hese par allelogr ams t o ll t he ent ire plane wit h t he per iod ic pat t er n.
132
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 132
There are of cour se many ot her symmet r ies wait ing t o be d iscovered by
combining power s of and . For example, a 240 rot at ion about z
3
followed by a 60 about 0 result s in a net rot at ion of 180. This must be a
t wo-cent er,

a
1

2
:z e
j 2/ 6
z z
3
( )
e
j 8/ 6
+ z
3 [ ]
,
which red uces t o

:z z + z
3
(e
j 2/ 6
+1)
which is a 180-d egree rot at ion about t he mid d le lead ing ed ge of t he left
wing. The xed point of t his t wo-cent er is locat ed at


z
2
z
3
3e
j 2/ 12
t wo-cent er.
If we join but t er ies paired t oget her by and color t he pair s whit e, gr ay,
and black, we get Fig. 2.
Figur e 2.
133
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 133
If we prod uce t r iplet s of but t er lies by combining t he t r iad prod uced by
t he rot at ion, and color t hese d ist or t ed t r iangles black and whit e, we get
Fig. 3.
Figs. 2 and 3 d epict groups wit hin groups. In t he case of Fig. 3, if t he
t r iad s are viewed as t he basic element , t he t iling is an examp le of
wallpaper group p3. The group t heoret ic d escr ipt ion of t his embed d ing is
a fact or group or quot ient group (Bloch, 1987; Bur n, 1985). This concept
proves useful in t he d esign of color t ilings t hat preser ve t he inherent
symmet r y of t he embed d ing group (Coxet er et al., 1987).
Tiling in Rast er Grids
In t his sect ion, we are int erest ed in d evising an algor it hm t o t ile per iod ic
pat t er ns efcient ly int o a pixel ar r ay. Clear ly, t here must be a way t o
exploit t he st ep-and -repeat t iling suggest ed by t he int er locking par allelo-
Figur e 3.
134
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 134
gr ams. If we can nd rect angular br icks or t hogonal t o t he coord inat es of
our r ast er, such as t hose in t he lower r ight area of Fig. 1, we could load
up one copy of t he br ick and t hen st ep and repeat it wit h a bit -blit
oper at ion over what ever area of t he plane we wish t o cover.
It will be shown t hat t his is always possible t o d o. The basic mat hemat i-
cal t ools for d oing t his come from t he eld of number t heor y, which d eals
wit h t he proper t ies of int eger s. We have t o d eal wit h exact int eger s
because we want a bit -blit t emplat e whose cor ner s are aligned exact ly
wit h pixel cent er s. All we need is a par allelogr am basis wit h one compo-
nent t hat is exact ly hor izont al. Refer r ing again t o Fig. 1, not e t hat t he
rect angles can be d er ived from t he hor izont al par allelogr ams by slicing
off t he t r iangle from t he left ed ge of t he par allelogr am and gluing it t o t he
r ight ed ge.
Assume t hat we st ar t wit h a pair of valid basis vect or s [a, b] and [c, d].
Topologically, we t reat t he ent ire plane as if it were just one (fund amen-
t al) par allelogr am cell t hat wr aps around it self in t oroid al fashion. A pixel
in t he plane t r anslat ed by any int eger combinat ion of t hese basis vect or s
will land at t he same place relat ive t o t he par allelogr am cell. It is easy t o
show t hat we can t r ansfor m a set of basis vect or s t o anot her set of valid
basis vect or s as long as we use int eger weight s and preser ve t he area of
t he par allelogr am. We wish t o d o t his in a fashion t hat will prod uce a new
basis wit h an exact ly hor izont al component (in ord er t o make a hor izont al
block, which is more suit able for bit -blit t ing). Denot ing t he new basis
vect or s as [A, 0], [C, D],
[A,0] = i[a, b] + j[c, d] t he new horizont al basis vect or
[C, D] = k[a, b] + l[c, d] il jk = 1 preserve area.
Fir st we want t o nd t he smallest i and j t hat sat isfy:
0 = ib + jd y component of horizont al vect or.
The solut ion is
i = d/ g
j = b/ g
g = gcd (b, d) great est common denominat or.
135
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 135
The remaining k, l component s must sat isfy:
1 = il jk = (d/ g)l + (b/ g)k unit det erminant
g = dl + bk solve for l, k.
This is exact ly t he problem solved by t he ext end ed ver sion of Euclid s
gcd algor it hm (Knut h, 1981b), and a solut ion is guar ant eed t o exist . The
nal result s are
A = ai + cj
C = ak + cl
D = bk + dl
You can always put [C, D] in t he upper half-plane and t hen ad d any
int eger combinat ion of t he [A, 0] vect or t o t he [C, D] vect or, so you can
always ad just [C, D] such t hat
0 C < A
0 < D.
This represent s a red uced canonical represent at ion of t he lat t ice. That is,
any t wo lat t ices t hat red u ce t o t he same set of A, B, C valu es are
equivalent . The mat r ices wit h int egr al weight s and unit d et er minant s t hat
r el at e equ i val en t l at t i ces ar e kn own as unimodular t ransforms, an d
for m a group t hemselves.
As an example, consid er t he par allelogr am lat t ice wit h basis vect or s
[9, 9] and [ 3, 6]. We have
g = gcd(b, d) = gcd(9, 6) = 3
A = ia + jc = (ad bc)/ g = 27
3 = dl + bk = 6l + 9k,
whose solut ion is l = 1, k = 1.
So t he new basis is [27, 0] and [12, 3].
The old basis is shown in Fig. 4, over laying a per iod ic ar r ay of 81
number s ar r anged t o show t he pat t er n t hat we wish t o t ile by a st ep and
136
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 136
Figur e 4.
137
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 137
repeat process. The + signs represent pixel cent er s. In t he language of
group t heor y, t he set of pixels mar ked 0 is t he subgroup of t he plane
gener at ed by t he basis vect or s. All t he pixels mar ked as 1 represent one
of 81 possible coset s of t he subgroup.
The same d iagr am wit h t he new basis is shown in Fig. 5.
Not e t hat in bot h cases t he same number s appear in t he same places in
each par allelogr am, which is precisely what we want . Our bit -blit rect an-
gle is 27 pixels wid e by 3 pixels high, and each row of br icks should be
r ight -shift ed by 12 pixels relat ive t o t he previous r ow.
An alt er nat e way t o see what is happening is t o view t he basis vect or s
as t he axes of a new [u, v] coord inat e syst em.
x = au + cv [u, v] = [1, 0] [x, y] = [a, b]
y = bu + dv [u, v] = [0, 1] [x, y] = [c, d]
M = ad bc number of pixels in cell
If we t r y t o solve for [u, v] in t er ms of [x, y] by inver t ing t his relat ion-
ship, we seem t o be faced by a d ivision wit h t he d et er minant , which
would d est roy t he exact int eger relat ionships we wish t o maint ain. How-
ever, if we scale up t he [u, v] coord inat es so t hat t hey lie in t he int er val
[0, M 1] inst ead of [0, 1], and enforce wr ap cond it ions on t he bound ar y
of our fund ament al cell, we obt ain
u = d x cy
M
generat ors for group st ruct ure
v = bx + ay
M
of t iling pat t ern,
where

x
M
d enot es t aking t he least posit ive resid ue mod M.
The resid ue red uct ion ensures t hat an [x, y] from anywhere in t he
plane maps t o a [u, v] in t he int er ior of t he fund ament al cell. This d ual
basis is exact ly equivalent t o t he reciprocal lat t ice used in cr yst allogr a-
phy. In t he case of our example in Fig. 5, we have
u = 3x 12y
81
v = 0x + 27y
81
138
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 138
Figur e 5.
139
II.14 PERIODIC TILINGS OF THE PLANE ON A RASTER GRID
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 139
If we r un t hrough all possible values of [x, y], number t heor y t ells us t hat
u can only t ake on 81/ gcd (3, 12) = 27 d ifferent values, all of which will
be mult iples of 3. The v component can only t ake on t he t hree values
{0, 27, 54}. The geomet r ical signicance of t his is t hat t he u basis vect or
hit s t hree pixel cent er s, and t he v vect or hit s 27 (not inclu d ing t he
or igin). Consequent ly, our exact int eger relat ionships will be maint ained
if we shr ink t he basis vect or s (group gener at or s) by 3 and 27, t hat is,
u = u/ 3 = x 4y
27
u in range {0, 1 ,... 26}
v = v/ 27 = y
3
v in range {0, 1, 2}
This means t hat t he group st r uct ure is essent ially Z
27

Z
3
. One way t hat
we can use t he above t o prod uce a 1D funct ion t hat assigns a unique
number t o ever y pixel in t he cell is t o for m a map Z
27

Z
3
Z
81
wit h
z(x, y) = u + 27v = x 4y
27
+ 27 y
3
.
This is exact ly t he funct ion used t o prod uce t he pixel ind ices in Figs. 4
and 5.
The ut ilit y of t he above ind exing met hod is t hat we can load up all
possible M element s of t he cell int o a linear ar r ay, and t hen for each
[x, y] in t he plane, pick out t he one select ed by z(x, y).
Anot her pr act ical applicat ion of t he above t echnique is in gener at ing
d igit al pat t er ns for halft one screens t hat are rot at ed relat ive t o t he r ast er
gr id (Hollad ay, 1980). In t his case, t he pixels are usually one bit d eep,
and t he t iling is accomplished by bit -blit t ing an ent ire rect angle.
3
IMAGE
PROCESSING
143
III ANTI-ALIASING FILTERS SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 143
III III III III III
ANTI-ALIASING ANTI-ALIASING ANTI-ALIASING ANTI-ALIASING ANTI-ALIASING
FILTERS SUMMARY FILTERS SUMMARY FILTERS SUMMARY FILTERS SUMMARY FILTERS SUMMARY
Al i asi n g i s a fact of l i fe wh en u si n g d i gi t al comp u t er s t o r ep r e-
sen t con t i n u ou s si gn a l s. On e mu st be con st a n t l y a wa r e of t h e
i mp l i ca t i on s of sa mp l i n g a n d qu a n t i z a t i on , a n d a t t emp t t o a voi d
or su p p r ess i ma ge a r t i fa ct s r esu l t i n g fr om t h ese p r ocesses. An
essen t i a l st ep i n a l l a l gor i t h ms t h a t su p p r ess or el i mi n a t e a l i a s-
i n g i s t h e ch oi ce of l t er s u sed i n t h e samp l i n g an d r econ st r u c-
t i on p r ocesses. Th e sea r ch for good l t er s (a ccor d i n g t o
d ifferent cr it er ia) is an act ive research area.
Th e fol l owi n g Gems p r esen t some d i ffer en t l t er i n g st r a t e-
gi es, r a n gi n g fr om t h e exp l i ci t t o t h e i mp l i ci t . Th ese l t er i n g
t ech n i qu es ma y be a d a p t ed for u se i n a l most a n y a n t i -a l i a si n g
t echnique.
See also An t i -Al i a si n g Su mma r y (37); Sca n Con ver si on Su m-
mar y (75); Line-Dr awing Summar y (98)
144 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 144
III.1 III.1 III.1 III.1 III.1
C CC CCONVENIENT ANTI-ALIASING ONVENIENT ANTI-ALIASING ONVENIENT ANTI-ALIASING ONVENIENT ANTI-ALIASING ONVENIENT ANTI-ALIASING
F FF FFILTERS THAT MINIMIZE ILTERS THAT MINIMIZE ILTERS THAT MINIMIZE ILTERS THAT MINIMIZE ILTERS THAT MINIMIZE
B B B B BUMPY SAMPLING UMPY SAMPLING UMPY SAMPLING UMPY SAMPLING UMPY SAMPLING
Mark J. Pavicic
Nort h Dakot a St at e Universit y
Fargo, Nort h Dakot a
A solut ion t o t he spat ial aliasing problem is t o convolve t he image wit h
an appropr iat e lt er funct ion. Two commonly used funct ions are t he cube
and t he Gaussian. Ad vant ages of t he cube are t hat it is a simple funct ion
and it evenly weight s t he image plane. Uneven weight ing cont r ibut es t o
problems such as lines t hat appear t o have var ying t hickness. Ad vant ages
of t he Gaussian are t hat it is a closer approximat ion t o a CRT d ot and it
is r ad ially symmet r ic. The r ad ial symmet r y makes it easy t o const ruct a
look-up t able, since it is ind exed by a single var iable, r, which is t he
d ist ance of an ed ge from t he cent er of a pixel.
An id eal funct ion would be r ad ially symmet r ic and would evenly sample
t he image plane. A proper ly chosen Gaussian can come close t o t his id eal.
Figure 1 shows how a par t icular Gaussian weight s t he square region
whose cor ner s are t he cent er s of four pixels. The square region for ms t he
base of t he cube. The t op sur face result s from t aking t he sum of four
Gaussian mount ains. The par t ial out line of one of t he Gaussian moun-
t ains is shown on t he front face of t he cube. This lt er has t he equat ion

f r ( ) a 1
1 exp r
2
( )
1 exp ( )

1
]
1
,
where = 1 and = 2.378. It sat ises t he cr it er ia t hat it have a unit
volume, t hat f(0) = 1, and t hat f(l) = 0. It is d ened t o be zero for
r > 1. Not e t hat f(0) = = 1 is not a xed requirement . In fact , a more
even weight ing can be achieved if 1. Figure 2 shows t he opt imal
result , which occur s when = 0.918 and = 1.953. The unit -volume
and f(1) = 0 cr it er ia are st ill sat ised .
An int erest ing alt er nat ive is t he sum of a cone and a cosine. This
composit e funct ion sat ises all t he cr it er ia imposed on t he Gaussian in
145 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 145
III.1CONVENIENT ANTI-ALIASING FILTERS THAT MINIMIZE BUMPY SAMPLING
Figure 1. Unit height Gaussian. Figure 2. Opt imized Gaussian.
Figure 3. Cone plus cosine. Figure 4. A t abular solut ion.
Fi g. 1, yet s a mp l es t h e i ma ge p l a n e n ea r l y a s even l y a s t h e op t i mi z ed
Gaussian in Fig. 2. This lt er has t he equat ion
f(r) = (1 r) + (1 )(1 + cos(r))/ 2
for r < 1, where = (1 v2)/ (v1 v2), vl = / 3, and v2 = / 2
2/ . It was used t o gener at e Fig. 3. Not e t hat t he lt er s sum t o 1 along
t he t op ed ges of t he cube.
To get a quant it at ive compar ison of t hese t hree cases, t he percent age
of t he volume above and below a unit height was calculat ed and found t o
be 3.25, 1.03, and 1.13 for Figs. 1, 2, and 3, respect ively. Thus, t hese
measu res ver ify what is alread y evid ent in t he gu res, namely t hat
carefully chosen lt er funct ions can d o a super ior job of minimizing
bumpy sampling while st ill ret aining t he convenient char act er ist ic of
r ad ial symmet r y.
146 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 146
III.1CONVENIENT ANTI-ALIASING FILTERS THAT MINIMIZE BUMPY SAMPLING
Table I. A Tabular Solut ion.
At t his point one might ask, What is t he opt imum? To get some id ea,
a t able of 41 values was ad just ed t o minimize t he volume d isplaced by
uneven sampling. The result is shown in Fig. 4 and t he values are list ed
in Tab. 1. The percent age volume d isplaced in t his case is 0.43. This level
of per for mance can also be approximat ed by r ad ially symmet r ic lt er s
whose shapes are d escr ibed by polynomials or spline cur ves. As a nal
example, Tab. 2 list s t he ve cont rol point s for a nonu nifor m cu bic
B-spline t hat has a bump fact or of 0.60.
Table 2. Cont rol Point s.
147 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 147
III.2 FILTERS FOR COMMON RESAMPLING TASKS
III.2 III.2 III.2 III.2 III.2
F FF FFILTERS FOR COMMON ILTERS FOR COMMON ILTERS FOR COMMON ILTERS FOR COMMON ILTERS FOR COMMON
R RR RRESAMPLING TASKS ESAMPLING TASKS ESAMPLING TASKS ESAMPLING TASKS ESAMPLING TASKS
Ken Turkowski
Apple Comput er
Cupert ino, Calif ornia
Cont i nuous, Sampl ed, and Di scr et e Si gnal s Cont i nuous, Sampl ed, and Di scr et e Si gnal s Cont i nuous, Sampl ed, and Di scr et e Si gnal s Cont i nuous, Sampl ed, and Di scr et e Si gnal s Cont i nuous, Sampl ed, and Di scr et e Si gnal s
Signals or funct ions t hat are cont inuous are d ened at all values on an
int er val. When t hese are t hen sampled, t hey are d ened only at a given
set of point s, regular ly spaced or not . When t he values at t hese sample
point s are t hen quant ized t o a cer t ain number of bit s, t hey are called
discret e. A sampled funct ion may or may not be d iscret e.
In comput er gr aphics, we d eal wit h all t hree of t hese represent at ions, at
least in our mod els of comput at ion. A funct ion such as sin(x) is consid -
ered cont inuous. A sequence of oat ing-point values may be consid ered
t o represent a sampled funct ion, whereas a sequence of int eger s (espe-
cially 8-bit int eger s) represent a d iscret e funct ion.
Int er pol at i on and Deci mat i on Int er pol at i on and Deci mat i on Int er pol at i on and Deci mat i on Int er pol at i on and Deci mat i on Int er pol at i on and Deci mat i on
Even t hou gh a signal is samp led , we may have cer t ain ru les abou t
infer r ing t he valu es bet ween t he samp le p oint s. The most common
assumpt ion mad e in signal processing is t hat t he signal is band limit ed t o
an ext ent consist ent wit h t he sampling r at e, t hat is, t he values change
smoot hly bet ween samples. The Sampling Theorem guar ant ees t hat a
cont inuous signal can be reconst ruct ed per fect ly from it s samples if t he
signal was appropr iat ely band limit ed pr ior t o sampling (Oppenheim and
Schaeffer, 1975). Pr act ically speaking, signals are never per fect ly band -
limit ed , nor can we const ruct a per fect reconst ruct ion lt er, but we can
get as close as we want in a prescr ibed manner.
148 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 148
III.2 FILTERS FOR COMMON RESAMPLING TASKS
We oft en want t o change from one samp ling r at e t o anot her. The
process of represent ing a signal wit h more samples is called int erpola-
t ion, whereas represent ing it wit h less is called decimat ion. Examples of
int er polat ion are zooming u p on an image; cor rect ing for nonsqu are
p ixels; and conver t ing an image from 72 d p i t o 300 d p i t o feed a
high-resolut ion out put d evice. Applicat ions of d ecimat ion are red ucing
t he jaggies on an super sampled image; and cor rect ing for nonsquare
pixels.
Choi ces of Fi l t er s Choi ces of Fi l t er s Choi ces of Fi l t er s Choi ces of Fi l t er s Choi ces of Fi l t er s
Sever al t ypes of lt er s are more popular t han ot her s: box, t ent , Gaussian,
and sinc. In Fig. 1, we show t he frequency response of a few of t he
cont inuous ver sions of t hese lt er s. The id eal lt er would have a gain of
0 d B bet ween frequencies of 0 and 1 (t he passband ), and beyond 1 (t he
st opband ). The rolloff in t he passband is responsible for blur r iness, and
t he leakage in t he st opband is responsible for aliasing (jaggies). One
gener ally has t o make t he t r ad eoff bet ween shar pness and aliasing in
choosing a lt er. We will be sampling some of t hese lt er s, specically for
use in int er polat ion and d ecimat ion r at ios of int eger amount s, such as 2,
3, and 4.
Figure 1.
149 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 149
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Box Box Box Box Box
Figure 2.
The box lt er for int er p olat ion merely p icks t he closest valu e. For
d ecimat ion, it is simply an aver age of t he input samples. Wit h an even
number of samples, t he lt er prod uces an out put t hat is sit uat ed bet ween
t wo input samples (half phase), whereas wit h an od d number, it is sit uat ed
at t he same locat ion as t he mid d le sample (zero phase). Wit h ot her lt er s,
you can select t he phase of t he lt er, but not so for t he box lt er. In Fig.
2, we show t he half-phase box lt er for d ecimat ion by 2. Higher d ecima-
t ion r at io lt er s just have coefcient s wit h weight s t hat sum t o 1.
Tent Tent Tent Tent Tent
Figure 3. Decimat ion by a fact or of t wo wit h t he t ent funct ion.
150 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 150
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 4. Decimat ion by a fact or of t hree wit h t he t ent funct ion.
Figure 5. Decimat ion by a fact or of four wit h t he t ent funct ion.
The t ent lt er is a gener alizat ion of linear int er polat ion, and is so when
int er polat ing. Unlike t he box lt er, t his can accommod at e ar bit r ar y lt er
phases; we show t he zero-phase and half-phase lt er for d ecimat ion by
t wo, t hree, and four (see Figs. 3, 4, and 5).
Gaussi an Gaussi an Gaussi an Gaussi an Gaussi an
The Gaussian funct ion is popular for it s many elegant analyt ical proper-
t ies; it is ent irely posit ive, it is t he limit of probabilit y d ensit y funct ions,
and it is it s own Four ier t r ansfor m. Here, we give a r at ionale for choosing
an appropr iat e wid t h, or var iance, or lt er ing in gr aphics.
We choose Gaussian lt er s here whose var iances have physical and
compu t at ional signicance. The r st is t he nar rowest t hat we wou ld
151 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 151
III.2 FILTERS FOR COMMON RESAMPLING TASKS
probably ever want t o use, and has a half-amplit ud e wid t h of

1
2
, t hat is,
it has t he value

1
2
at a d ist ance

1
2
from it s cent er. It s value get s negligible
1

1
2
samples away from t he cent er, so it can be consid ered t o have a suppor t
of 3.
Energy, in gener al t er ms, is t he square of t he magnit ud e. If t he eye is
more linear in energy t han in magnit ud e, t hen a more appropr iat e Gauss-
ian might be one in which t he square of t he magnit ud e is

1
2
at a d ist ance

1
2
from t he cent er, or t hat t he magnit ud e it self has a value of

1/ 2 at t hat
point . This is a wid er Gaussian t han t he r st , and it s magnit ud e d oesnt
become negligible unt il 2 samples from t he cent er, so t hat it may be
consid ered a lt er wit h suppor t 4.
In Fig. 1, we compare t he box, t ent , and t hese t wo Gaussians. The box
lt er capt ures more of t he passband (freq < 1) t han t he ot her s, but it also
let s t hrough more of t he st opband (freq > 1). It is t he leakage in t he
st opband t hat is responsible for aliasing ar t ifact s, or jaggies. The t ent
lt er is 15 d B bet t er at eliminat ing aliasing in t he st opband , but d oes so
at t he expense of losing more feat ures in t he passband . The Gaussian

1
2
lt er
mat ches t he t ent for a good por t ion of t he passband , but cont inues t o
at t enuat e t he st opband . The Gaussian

1/ 2 lt er d oes an even bet t er job
at at t enuat ing t he aliases, but d oes so at t he expense of losing ad d it ional
d et ail in t he passband .
A compar ison of t he t ent and t he nar row Gaussian in t he t ime (space)
d omain will show t hat t hey look ver y similar, except t hat t he Gaussian is
Figure 6.
152 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 152
III.2 FILTERS FOR COMMON RESAMPLING TASKS
smoot h at t he peak and t he base, whereas t he t ent has slope d iscont inu-
it ies t here. It is t hese d iscont inuit ies t hat cause t he r inging and ineffect ive
alias suppression in t he st opband .
One of t he sid e effect s of our par t icular choices of Gaussian var iance is
t hat many of t heir coefcient s at int erest ing locat ions are scaled power s
of t wo, which makes way for fast er comput at ion. We will see t his in t he
following lt er s, sp ecialized for cer t ain int er p olat ion and d ecimat ion
t asks.
Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an

1
2
Fi l t er Fi l t er Fi l t er Fi l t er Fi l t er
Figure 7. Int er polat ion wit h t he Gaussian

1
2
lt er.
In Fig. 7, we give t he lt er coefcient s for a set of lt er s t o int er polat e
bet ween t wo given samples: halfway bet ween, and a quar t er of t he way t o
eit her sid e of a sample. Not ice t he nice r at ional coefcient s t hat are
scaled power s of t wo.
153 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 153
III.2 FILTERS FOR COMMON RESAMPLING TASKS
To d et er mine t he coefcient s for a lt er t o prod uce t he value at any
ot her point bet ween t wo samples, we merely sample t he Gaussian at a
ser ies of locat ions one sample apar t , and nor malize t hem so t hat t heir
sum equals one. Even t hough t he Gaussian is zero nowhere, we consid er
t his lt er s value t o be negligible great er t han 1.5 samples away from it s
cent er.
Deci mat i on wi t h t he Gaussi an Deci mat i on wi t h t he Gaussi an Deci mat i on wi t h t he Gaussi an Deci mat i on wi t h t he Gaussi an Deci mat i on wi t h t he Gaussi an

1
2
Fi l t er Fi l t er Fi l t er Fi l t er Fi l t er
Figure 8. Decimat ion by a fact or of t wo wit h t he Gaussian

1
2
lt er.
Figure 9. Decimat ion by a fact or of t hree wit h t he Gaussian

1
2
lt er.
154 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 154
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 10. Decimat ion by a fact or of four wit h t he Gaussian

1
2
lt er.
Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an Int er pol at i on wi t h t he Gaussi an

1
2 Fi l t er Fi l t er Fi l t er Fi l t er Fi l t er
This wid er Gaussian becomes negligible great er t han t wo samples away
from t he cent er (see Fig. 11).
Figure 11. Int er polat ion wit h t he Gaussian

1
2
filt er.
155 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 155
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 11. (Cont inued)
Figure 12. Decimat ion by a fact or of t wo wit h t he Gaussian

1
2
lt er.
Figure 13. Decimat ion by a fact or of t hree wit h t he Gaussian

1
2
lt er.
156 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 156
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 14. Decimat ion by a fact or of four wit h t he Gaussian

1
2
lt er.
The Si nc Funct i on The Si nc Funct i on The Si nc Funct i on The Si nc Funct i on The Si nc Funct i on
The sinc funct ion (see Fig. 15) is t he id eal low-pass lt er (Oppenheim and
Schaeffer, 1975).
Figure 15.
The Lanczos- Wi ndowed Si nc Funct i ons The Lanczos- Wi ndowed Si nc Funct i ons The Lanczos- Wi ndowed Si nc Funct i ons The Lanczos- Wi ndowed Si nc Funct i ons The Lanczos- Wi ndowed Si nc Funct i ons
Since t he sinc funct ion never goes t o zero but approaches it slowly, we
mult iply it by an appropr iat e wind owing funct ion. The t wo-lobed Lanczos-
157 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 157
III.2 FILTERS FOR COMMON RESAMPLING TASKS
wind owed sinc funct ion is one such wind owed sinc funct ion, and is
d ened as follows (see Fig. 16):

Lanczos2 x ( )
sin x ( )
x
sin
x
2

_
,

x
2
, x < 2
0, x 2.

'

Figure 16.
The t hree-lobed Lanczos-wind owed sinc funct ion is d ened similar ly
(see Fig. 17):
Figure 17.
158 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 158
III.2 FILTERS FOR COMMON RESAMPLING TASKS

Lanczos 3 (x) =
sin x ( )
x

sin
x
3

_
,

x
3
, x < 3
0, x 3

'

The Lanczos-wind owed sinc fu nct ion lt er s have been shown t o be


par t icular ly useful for gr aphics applicat ions.
1
We will concer n our selves
here mainly wit h t he t wo-lobed ver sion, because of it s smaller ker nel.
Int er pol at i on by a Fact or of Two wi t h t he Int er pol at i on by a Fact or of Two wi t h t he Int er pol at i on by a Fact or of Two wi t h t he Int er pol at i on by a Fact or of Two wi t h t he Int er pol at i on by a Fact or of Two wi t h t he
Lanczos2 Si nc Funct i on Lanczos2 Si nc Funct i on Lanczos2 Si nc Funct i on Lanczos2 Si nc Funct i on Lanczos2 Si nc Funct i on
Not e t hat in Fig. 18, wit h a zero-phase lt er, t he cont r ibut ions from ot her
t han t he cent r al pixel are zero, so t hat only t he cent r al pixel is used .
Figure 18. Interpolation by a factor of two with the Lanczos2 sinc function
1
Tu r kows ki , Ken a n d Ga br i el , St ev e, 1979. Con cl u s i on s of exp er i men t s d on e a t Amp ex,
comp a r i n g box, Ga u s s i a n , t r u n ca t ed -s i n c, a n d s ev er a l wi n d owed -s i n c l t er s (Ba r t l et t ,
cos i n e, Ha n n i n g, La n cz os ) for d eci ma t i on a n d i n t er p ol a t i on of 2-d i men s i on a l i ma ge d a t a .
Th e La n cz os -wi n d owed s i n c fu n ct i on s offer ed t h e bes t comp r omi s e i n t er ms of r ed u ct i on
of aliasing, shar p ness, and minimal r inging.
Figure 18. (Cont inued)
Deci mat i on wi t h t he Lanczos2 Si nc Funct i on Deci mat i on wi t h t he Lanczos2 Si nc Funct i on Deci mat i on wi t h t he Lanczos2 Si nc Funct i on Deci mat i on wi t h t he Lanczos2 Si nc Funct i on Deci mat i on wi t h t he Lanczos2 Si nc Funct i on
Figure 19. Decimation by a factor of two with the Lanczos2 sinc function.
The zero-phase lt er (Fig. 19) has coefcient s t hat are near ly r at ional. If
t he negat ive coefcient s are scaled so t hat t hey are equal t o 1, t hen t he
remaining coefcient s are 9 and 15.7024. This inspired a search for such
lt er s wit h r at ional coefcient s. This yield ed t he t wo zero-phase lt er s in
Fig. 20.
GRAPHIC GEMS I edit ed by DAVID KIRK 159
160 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 160
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 20.
Compar at i ve Fr equency Responses Compar at i ve Fr equency Responses Compar at i ve Fr equency Responses Compar at i ve Fr equency Responses Compar at i ve Fr equency Responses
Filt er s are evaluat ed on t heir abilit y t o ret ain d et ail in t he passband
(shar pness is valued more t han blur r iness) and t o eliminat e aliasing in t he
st opband (smoot hness is valu ed more t han jagginess). The frequ ency
response of a sampled lt er is quit e d ifferent t han t he cont inuous one
Figure 21. Decimation by a factor of three with the Lanczos2 sinc funct ion.
161 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 161
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 22. Decimat ion by a fact or of four wit h t he Lanczos2 funct ion.
By t he way, one bit cor respond s t o about 6 d B, so t hat at t enuat ion
beyond 48 d B is ir relevant when wor king wit h 8-bit pixels from which it
wa s d er i ved . In st ea d of t a ki n g t h e Fou r i er t r a n sfor m a s wi t h
cont inuous lt er s, we t ake t he z-t r ansfor m and sample on t he unit circle.
In Figs. 23 and 24 we see t hat t he lt er d er ived from t he Gaussian 1/ 2
lt er d oesnt per for m as well as t he one d er ived from t he t ent , alt hough
we know t hat in t he cont inuous case, t he Gaussian is much bet t er. What
happened ? We sampled t he lt er funct ions, t hat s what happened . In t he
process, we changed t he char act er ist ics of t he lt er. In fact , t here are
sever al cont inuous lt er s t hat give r ise t o t he same sampled lt er s. The
labels on each of t he lt er s are act ually misnomer s, since t he sampled
lt er s are not t he same as t he cont inuous ones.
The box lt er seems t o ret ain a large por t ion of t he passband , but let s
t hrough a t remend ous amount of energy in t he st opband , result ing in
not iceable aliasing. The Lanczos lt er s keep more of t he passband t han
t he ot her s (except for maybe t he box), and t hey cut off more of t he
st opband (except for maybe t he Gaussian

1/ 2), wit h t he Lanczos3 lt er
coming closest t o t he id eal lt er shape of all t he lt er s evaluat ed . The
Gau ssian

1/ 2 lt er is comp et it ive wit h t he Lanczos3 for st op band
response, but d oes so at t he expense of excessive at t enuat ion on t he
passband .
162 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 162
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Figure 23. Frequency response of t he zero-phase lt er s for d ecimat ion by 2.
Figure 24. Frequency response of t he half-phase lt er s for d ecimat ion by 2.
163 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 163
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Fr equency Response of t he Gaussi an Fi l t er s f or Fr equency Response of t he Gaussi an Fi l t er s f or Fr equency Response of t he Gaussi an Fi l t er s f or Fr equency Response of t he Gaussi an Fi l t er s f or Fr equency Response of t he Gaussi an Fi l t er s f or
Sever al Deci mat i on Rat i os Sever al Deci mat i on Rat i os Sever al Deci mat i on Rat i os Sever al Deci mat i on Rat i os Sever al Deci mat i on Rat i os
The cut off frequencies are 0.5 for t he 2 lt er, 0.333 for t he 3, 0.25
for t he 4. Not e t hat t he zero-p hase and t he half-p hase lt er s for
d ecimat ion by 2 d iverge, whereas t he higher-d ecimat ion lt er s d o not .
Figure 25. Frequency response of t he Gaussian lt er for sever al d ecimat ion r at ios.
Figure 26. Frequency response of t he Gaussian

1
2
lt er for sever al d ecimat ion r at ios.

1
2
164 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 164
III.2 FILTERS FOR COMMON RESAMPLING TASKS
Fr equency Response of t he Lanczos2 Si nc Fr equency Response of t he Lanczos2 Si nc Fr equency Response of t he Lanczos2 Si nc Fr equency Response of t he Lanczos2 Si nc Fr equency Response of t he Lanczos2 Si nc
Funct i ons Funct i ons Funct i ons Funct i ons Funct i ons
Figure 27. Frequency response of t he Lanczos2 sinc funct ions.
Figure 28. Frequency response of t he Lanczos2 sinc funct ions for sever al d ecimat ion
r at ios.
165 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 165
III.2 FILTERS FOR COMMON RESAMPLING TASKS
We sh ow t h e r esp on ses of t h e d ecimat e-by-2 lt er s r elat ed t o t h e
Lanczos2 lt er in Fig. 27. Not e t hat t he Gabr iel d ecimat or let s more of
t he passband t hrough and has a shar per cut off in t he st opband , but also
bounces back in t he st opband at a higher level t han t hat of t he Lanczos2.
The Tur kowski d ecimat or, however, d oes not bounce back and eliminat es
mor e of t he highest fr equ encies t han t he ot her t wo. They all have
approximat ely t he same passband response and aliasing energy, but t he
aliasing energy is d ist r ibut ed d ifferent ly t hroughout t he spect rum, so t hey
can be consid ered about equivalent .
166 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 166
III.3 SMOOTHING ENLARGED MONOCHROME IMAGES
III.3 III.3 III.3 III.3 III.3
S SS SSMOOTHING ENLARGED MOOTHING ENLARGED MOOTHING ENLARGED MOOTHING ENLARGED MOOTHING ENLARGED
MONOCHROME IMAGES MONOCHROME IMAGES MONOCHROME IMAGES MONOCHROME IMAGES MONOCHROME IMAGES
John Olsen
Hewlet t -Packard
Fort Collins, Colorado
Typical met hod s of enlarging a monochrome image result in a ver y blocky
prod uct because of pixel replicat ion or repeat ing a pixel t o ll in t he area
ad d ed bet ween pixels when an image is increased in size. The enlarging
met hod t hat follows will preser ve many of t he angles t hat are missed by
simple enlargement schemes such as pixel replicat ion, and will gener at e a
much more int uit ive result . This met hod of image enlargement is espe-
cially effect ive on images such as circles, d isks, and ot her cont inuous or
smoot h cur ves.
Only monochrome images will be d iscussed here because t here are
many comp licat ions t hat ar ise when d ealing wit h color or gr ayscale
images. This t echnique of enlarging bit maps will not be ext end ed t o color
images because t here is no obvious or int uit ive scheme for d oing so. The
d ifcult y ar ises because t he result image can var y, d epend ing on which
color s are smoot hed r st .
The monochrome enlarging process is implement ed as a search over a
ser ies of rules t hat gover n how t he enlarged pixels are lled in. These
rules consist of an ar r ay of source pixels and of d at a ind icat ing what
ad d it ional areas are lled wit h t he foreground color, as is seen in t he rules
shown in Fig. 1.
Scaling by int eger mult iples gives t he most unifor m result s, but t his
scaling t echnique is equally applicable t o nonint eger increases in size.
The cod e required for nonint eger scaling will of cour se be more complex.
Each of t he specied rules is t o be rot at ed by 90-d egree increment s
and mir rored in ord er t o gener at e a complet e set of eight nal rules per
167 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 167
III.3 SMOOTHING ENLARGED MONOCHROME IMAGES
Figure 1. The rules used t o creat e smoot h monochrome enlargement s.
init ial rule. Fig. 2 shows how one of t he rules from Fig. 1 is used t o
gener at e a set of eight rules.
By using all t hree r ules in Fig. 1 and applying t he mir ror ing in Fig. 2
you will obt ain a set of 24 rules t hat will each preser ve a unique angle. A
simplied set of rules is obt ained by using only rules 1 and 2 wit h t heir
associat ed mir ror ing, and rot at ing t o obt ain a set of 16 rules. The
Figure 2. The or iginal rule 2 wit h it s rot at ions and reect ions.
168 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 168
III.3 SMOOTHING ENLARGED MONOCHROME IMAGES
simplied set is well-behaved because t he preser ved angles are evenly
d ist r ibut ed , whereas wit h t he set of 24 t he ad d it ional preser ved angles
t end t o be close t o mult iples of 90 d egrees.
The init ial st ep used t o creat e a smoot hed image is t o gener at e a blown
up image, just as wit h pixel replicat ion. Next , t he rules are applied t o t he
image from t he most complex t o t he least complex, which will par t ially
Figure 3. Result s of smoot hing var ious pat t er ns.
169 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 169
III.3 SMOOTHING ENLARGED MONOCHROME IMAGES
ll some of t he previously empt y blocks wit hin t he result image. The r ules
need t o be sequent ially checked from complex t o simple only unt il a
mat ch is found because t he simpler rules will ll only a subset of t he area
lled by t he more complex rules.
There is a d enit e pat t er n wit hin t he rules: each successive rule is a
cop y of t he p reviou s one, wit h one colu mn ad d ed t o gener at e t he
following more complex rule. This means t hat t he process of gener at ing
rules can cont inue as far as you would like t o t ake it , but it is gener ally
not pr act ical t o go past rule 3 as specied in Fig. 1 because of t he
increased CPU t ime required for t he more complex rules.
These rule set s will preser ve many pat t er ns (such as t he int er ior and
ed ges of checker board s and bot h insid e and ou t sid e squ are cor ner s)
inst ead of causing t hem t o be smoot hed in d ist r act ing or unexpect ed
ways, yet will d o a ver y good job of smoot hing cur ves on object s where
smoot hing makes more sense. Fig. 3 shows a few t ypical pat t er ns and
how t he smoot hing algor it hm will mod ify each.
Figure 4. Smoot hing causes cer t ain areas t o be lled by mult iple rules.
170 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 170
III.3 SMOOTHING ENLARGED MONOCHROME IMAGES
These result s are obt ained by t aking each of t he or iginal rules and it s
seven rot at ed and mir rored rules and compar ing t hem t o each locat ion on
t he image. Wherever a mat ch occur s, t he shad ed areas are lled in. Best
result s are obt ained by skipping pixels t hat appear exact ly at t he ed ge of
t he area t o be lled . This is d emonst r at ed by d oubling t he scale of a
simple object t hat uses only rule 1, and not ing t hat hor izont al and ver t ical
ed ges are emphasized when t he bord er pixels are d r awn. Care must also
be t aken in applying t he rules t o gener at e t he nal out put , since d est ina-
t ion blocks of pixels can be mod ied by more t han a single rule, as seen
in Fig. 4.
Ad mit t ed ly, t his smoot hing algor it hm d oes not t ake all possible angles
int o consid er at ion, but it is meant t o prod uce a much bet t er nal result
t han simple scaling of bit mapped images wit h pixel replicat ion. The
great est gain in u sing t his t echniqu e is t hat t he resu lt ing smoot hed
images will always be gener at ed in a pred ict able and int uit ive manner.
171 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 171
III.4 MEDIAN FINDING ON A 3 3 GRID
III.4 III.4 III.4 III.4 III.4
M MM MMEDIAN FINDING EDIAN FINDING EDIAN FINDING EDIAN FINDING EDIAN FINDING
ON A ON A ON A ON A ON A 3 33 333 3 3 3 3 GRID GRID GRID GRID GRID
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Over vi ew Over vi ew Over vi ew Over vi ew Over vi ew
A fast implement at ion for nd ing t he med ian of nine element s is pre-
sent ed . This su p p or t s med ian lt er ing on 2D r ast er imagesou t p u t
pixels are d ened as t he med ian value cont ained by a cor respond ing
3 3 pixel region on t he input . Filt er ing by t his nonlinear t echnique
removes shot noise, p ixel d rop ou t , and ot her sp u r iou s feat u res of
single pixel ext ent (Fig. 1) while preser ving over all image qualit y (Huang,
1981). The t echnique is also at t r act ive in t hat med ian is d ened by a
logical r anking of element values, not by ar it hmet ic comput at ion, yield ing
t ools t hat oper at e on images of ar bit r ar y pixel precision (Paet h, 1986a,
1986b; Paet h, 1987). Fast med ian evalu at ion is essent ial as oper at ion
count s on images quickly approach one million.
The med ian on n element s is t hat value having r ank

1
2
(n + 1), where
r ank is each element s relat ive sor t ed posit ion wit hin t he ar r ay. That is,
t he med ian element is t he cent r al element on t he sor t ed d at a set . The
comput at ion t ime for med ian nd ing is known t o increase linear ly wit h
input lengt h but t he relat ed met hod s are unsuit able for small ar r ays.
Br u t e-for ce sor t ing p er for ms u nnecessar y comp u t at ion becau se all n
element s become r anked . The approach present ed here requires t went y
compar isons and is close t o t he minimum exchange-net wor k bound for
med ian nd ing on nine element s (ninet een compar isons) and a clear win
over a bubble-sor t feat ur ing ear ly complet ion aft er sor t ing has r anked t he
r st ve element s (t hir t y compar isons).
172 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 172
III.4 MEDIAN FINDING ON A 3 3 GRID
Figure 1. Med ian lt er ing (3 3).
El ement Exchange El ement Exchange El ement Exchange El ement Exchange El ement Exchange
Ranking of t he d at a is achieved using simple element exchanges. This
suppor t s st r aight -line cod e implement at ion lacking cond it ional logic or
subrout ine calls. This approach also allows for par allel execut ion or reuse
of par t ial t er ms for larger 2D lt er ker nels. Exchanging may be d one in a
manner reminiscent of d at a swapping wit hout a t empor ar y var iable using
t hree bit -wise exclusive ORs:
A: a B: b
a a bi t-xor b A: a xor b B: b
b b bi t-xor a A: a xor b B: b xor (a xor b) = a
a a bi t-xor b A: a xor b xor a = b, B: a.
Su bt r act ion behaves in a self-comp lement ar y fashion id ent ical t o xor
because bot h bit complement at ion and ar it hmet ic negat ion are t heir own
inver ses. The basic t wo-element exchange t hat u nd er p ins t he ent ire
algor it hm shares t he subt r act ion implicit in any ar it hmet ic compar ison.
This red uces t hree progr am st at ement s t o t wo; cod ing as a macro re-
173 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 173
III.4 MEDIAN FINDING ON A 3 3 GRID
moves t he cost of subrout ine invocat ion:
s2(a, b) place a and b in ascending order
macro s2(a, b) i f (z b a) < 0 then begi n a a + z; b b z; end.
The s2 sor t ing oper at or is a special case of t he minnax
k
oper at ion wit h
k = 2. The gener al oper at ion nd s t he over all winner and loser (element s
of r ank 1 and k) on a k element set . Find ing t he minimum and maximum
on larger set s using t wo element compar isons may be d one by way of a
t our nament . Here element s and compar isons are represent ed by player s
and mat ches (which may t ake place in par allel), respect ively. The t our na-
ment commences by pair ing off all player s; in subsequent round s only
mat ches bet ween remaining winner s (or loser s) occur. Play end s when
bot h an all-t ime winner and an all-t ime loser have been fou nd . For
t our nament s whose member ship is not a power of t wo some player s will
d r aw byes and will (re)ent er lat er round s. A k-player t our nament end s
aft er complet ion of

1
2
3n 4 ( )
1
mat ches. An illust r at ion of minmax on six
element s using seven compar isons is d iagr amed in Fig. 2, in which circles
represent player s and ar rows represent mat ches wit h an ar rowhead ind i-
cat ing t he winner.
The med ian oper at ion on a nine-element ar r ay par t it ions t he ar r ay int o
t hr ee set s: a set of fou r element s wit h rank < 5, t he med ian wit h
rank = 5, and a set of four element s wit h rank > 5. The med ian may be
found by placing element s pair wise int o t he nonmed ian set s unt il t he
med ian remains. Any six-element subset of t he nine-element input ar r ay
cont ains an element whose r ank is at least six (as when t he su bset
happens t o cont ain t he six smallest element s of t he ar r ay); conver sely,
Figure 2. Six-player min-max t our nament .
174 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 174
III.4 MEDIAN FINDING ON A 3 3 GRID
Figure 3. Red ucing t he med ian.
t his subset cont ains a smallest element whose r ank can be no great er t han
four (see Fig. 3).
These out er most t wo element s br acket t he med ian and belong wit hin
t he remaining t wo par t it ions: d iscard ing t hem red uces t he input d at a t o
seven element s. The med ian (r ank 4) on t he remaining element s is t hen
found by nd ing and d iscard ing minimum and maximum element s using
a subset of four ; t he proced ure cont inues t hrough minmax
3
. At t his point ,
eight d iscard values have been par t it ioned and t he med ian remains. The
complet e pseud o-cod e requires t went y compar isons and is list ed here:
macro s2(a, b) i f (z b a) < 0 then begi n a a + z; b b z; end
macro mn3(a, b, c) s2(a, b); s2(a, c);
macro mx3(a, b, c) s2(b, c); s2(a, c);
macro mnmx3(a, b, c) mx3(a, b, c); s2(a, b);
macro mnmx4(a, b, c, d ) s2(a, b); s2(c, d ); s2(a, c); s2(b, d );
macro mnmx5(a, b, c, d , e) s2(a, b); s2(c, d ); mn3(a, c, e); mx3(b, d , e);
macro mnmx6(a, b, c, d , e, f) s2(a, d ); s2(b, e); s2(c, f); mn3(a, b, c,); mx3(d , e, f);
i nteger f uncti on med ian9 (v)
v:array [1..9] of i nteger Subscript ing from one for clarit y,
begi n v is of t ype real or i nteger.
mnmx6(v[1], v[2], v[3], v[4], v[5], v[6]); Cast off t wo element s each t ime.
mnmx5(v[2], v[3], v[4], v[5], v[7]);
mnmx4(v[3], v[4], v[5], v[8]);
mnmx3(v[4], v[5], v[9]);
The median is now in v[5]; v[1..4] and v[6..9] are t he ranked part it ions.
return[v[5]];
end;
175 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 175
III.4 MEDIAN FINDING ON A 3 3 GRID
Alt hough t he macro expansion for m suggest s und er lying machine cod e
wit h many cost ly ind exing oper at ions (as compared t o t he point er ar it h-
met ic possible wit h sor t ing), t his is not t he case. Because t he ar r ay size is
small, most ar r ay element s can be cached in local regist er s. The for m
present ed here is par t icular ly useful because d iscard ing of min/ max pair s
may commence immed iat ely aft er t he r st six element s have been read in
consecut ive ord er ; subsequent progr am st at ement s d rop t wo element s
and fet ch only one ad d it ional input so t he number of act ive regist er s
st ead ily d eclines. This suggest s C cod e (see t he Append ix) in which t he
regist er source cod e d irect ivewhich provid es t he compiler a list of
suggest ed cand id at es for regist er var iableslead s t o fur t her speed -ups.
For larger od d -ord er boxes t he compar ison t echnique is st ill useful.
The above approach cost s 1/ 16[3n
4
+ 10n
2
13] int egr al compar isons
for boxes of n
2
element s and od d ed ge lengt h n, giving 132 compar isons
for a 5 5 box. An alt er nat e net wor k for med ian on t went y-ve element s
(Paet h, 1990), requir ing less t han one hund red compar isons, is includ ed
in t he C cod e. It has been exhaust ively checked against all possible input
per mut at ions and t r anscr ibed d irect ly from t he sources. For larger sam-
p ling boxes (ker nels) reu se of common element s su ggest s t r ad it ional
med ian nd ing met hod s on large ar r ays. In t he 2D spat ial case, t hese
includ e t he use of sor t ed d at a st ruct ures in which t he t r ailing and lead ing
element columns are d elet ed and ad d ed while ind exing across a scan-line.
See also St or age-free Swapping (436)
See Append ix 2 for C Implement at ion (711)
176 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 176
III.5 ORDERED DITHERING
III.5 III.5 III.5 III.5 III.5
O OO OORDERED RDERED RDERED RDERED RDERED
D DD DDITHERING ITHERING ITHERING ITHERING ITHERING
St ephen Hawley
Morrist own, New Jersey
Ord ered d it her ing is a t echnique of red ucing high-precision, t wo-d imen-
sional d at a t o a lower precision using posit ional infor mat ion t o ret ain as
much infor mat ion as possible. In t he cont ext of image processing, or-
d ered d it her ing can be used t o red uce mult ilevel gr ayscale images t o
fewer levels, or in t he simple case, t o black and whit e. This can be used
t o red uce t he st or age space of an image, while ret aining a fair amount of
t he image cont ent s or t o d isplay mult ilevel gr ayscale images on a black-
and -whit e d isplay.
Ord ered d it her ing is a ver y fast , efcient t echnique. Dit her ing t ypically
requires one compar ison, one ar r ay lookup, and t wo mod ulo oper at ions
per pixel and can be implement ed as a macro or inline funct ion. In most
cases, t he mod ulo oper at ions can be d one wit h bit -and s.
The whole id ea behind ord ered d it her ing is t o d ist r ibut e evenly t he
expect ed r ange of gr ay levels t hroughout a mat r ix, which will act as a
screen. A gr ay level is compared t o a value in t he mat r ix using t he
coord inat es of t he gr ay level as ind ices int o t he mat r ix. Gr ay levels t hat
are great er t han t he cor respond ing value in t he d it her ing mat r ix are
lt ered out as represent ing black. All ot her values represent whit e. This is
almost exact ly t he same as t he halft oning process used in newspaper s.
The real t r ick is t o come up wit h a way t o d esign t he mat r ix so t hat
each gr ay level will prod uce an even pat t er n in t he out put . For example,
a 50% gr ay level should come out as a checker board pat t er n. If we ad opt
a recur sive d enit ion of t he mat r ix, we get t his for free.
177 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 177
III.5 ORDERED DITHERING
To st ar t off, heres a base case for a d it her ing mat r ix for 256 gr ay
levels:

M
0
0
[ ]
.
This is t he zerot h d it her ing mat r ix. It s d imensions are 2
0
by 2
0
. It
cont ains t he r ange from 0 t o 255, but is not ver y useful by it self. M
1
looks like t his:

M
1

0, 192
128, 64

1
]
1
1
.
M
1
is t he r st d it her ing mat r ix. It s d imensions are 2
1
by 2
1
. It cont ains
t he r ange from 0 t o 255, but wit h only four number s. This means t hat M
1
can only gener at e four d ist inct halft one pat t er ns. If we use larger Ms
well get more pat t er ns, and bet t er image qualit y.
M
1
can be t hought of as four copies of M
0
wit h const ant s ad d ed :

M
1

M
0
+ 0, M
0
+ 192
M
0
+ 128, M
0
+ 64

1
]
1
1
.
Similar ly, M
2
appear s as follows:

M
1

0, 192, 48, 240
128, 64, 176, 112
32, 224, 16, 208
160, 96, 144, 80

1
]
1
1
1
1
1
1
1
.
In t his case, M
2
is just four copies of M
l
wit h const ant s ad d ed :

M
2

M
1
+ 0, M
1
+ 48
M
1
+ 32, M
0
+ 16

1
]
1
.
In gener al, M
K
can be d ened as follows:

M
k

M
k 1
+ 02
n2k
, M
k 1
+ 32
n2k
M
k 1
+ 22
n2k
, M
k 1
+ 12
n2k

1
]
1
1
,
178 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 178
III.5 ORDERED DITHERING
where 2
n
is t he upper limit of t he r ange t hat is being d it hered over (in t he
above examples, n is 8) and k such t hat n 2k. Gener at ing d it her ing
mat r ices by hand is t ime-consuming and prone t o er ror. It is far easier t o
use a progr am t o gener at e t hem. For an example of such a progr am,
please refer t o t he Append ix.
In t he following example, youll see how t o red uce a mult ilevel image t o
a bilevel image. The funct ion reduce loops over t he ent ire input bit map
d it her ing each pixel. To st ress t he size-ind epend ence of t he cod e, neit her
t he d it her ing mat r ix nor it s d imensions are specied in t he pseud o-cod e.
Dit her Size: i nteger;
Dit her Mat r ix: array [0..Dit her Size 1] of array [0..Dit her Size 1] of i nteger;
Mult iMap: record [
array [0..m] of array [0..n] of i nteger;
];
BiMap: record [
array [0..m] of array [0..n] of bool ean;
];
macro d it her (x, y, level)
(level > Dit her Mat r ix[x mod Dit her Size] [y mod Dit her Size]);
This macro compares t he given level t o an element in t he dit hering mat rix.
The element is locat ed by mapping t he coordinat es of t he pixel int o t he dit hering
mat rix.
f uncti on red uce (in: Mult iMap; out : BiMap;)
begi n
f or i: i nteger 0, i i + 1 whi l e i m do
f or j: i nteger 0, j j + 1 whi l e j n do
out [i][j] d it her (i, j, in[i][j]);
Since t he dit her macro is a boolean expression, it reduces t he
mult ilevel input t o binary out put .
endl oop;
endl oop;
end;
See Append ix 2 for C Implement at ion (713)
179
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 179
A AA AA FAST ALGORITHM FOR FAST ALGORITHM FOR FAST ALGORITHM FOR FAST ALGORITHM FOR FAST ALGORITHM FOR
G GG GGENERAL RASTER ROTATION ENERAL RASTER ROTATION ENERAL RASTER ROTATION ENERAL RASTER ROTATION ENERAL RASTER ROTATION
1 11 11
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
The rot at ion of a d igit ized r ast er by an ar bit r ar y angle is an essent ial
funct ion for many r ast er manipulat ion syst ems. We d er ive and implement
a par t icular ly fast algor it hm which rot at es (wit h scaling invar iance) r ast er s
ar bit r ar ily; skewing and t r anslat ion of t he r ast er is also mad e possible by
t he implement at ion. This oper at ion is concept ually simple and is a good
cand id at e for inclusion in d igit al paint or ot her int er act ive syst ems, where
near real-t ime per for mance is required .
We d er ive a high-speed r ast er algor it hm based on t he d ecomposit ion of
a 2D rot at ion mat r ix int o t he prod uct of t hree shear mat r ices. Rast er
shear ing is d one on a scan-line basis, and is par t icular ly efcient . A useful
shear ing approximat ion is t o aver age ad jacent pixels, where t he blend ing
r at ios remain const ant for each scan-line. Taken t oget her, our t echnique
(wit h ant i-aliasing) rot at es r ast er s fast er t han previou s met hod s. The
gener al d er ivat ion of rot at ion also shed s light on t wo common t ech-
niques: small-angle rot at ion using a t wo-pass algor it hm, and t hree-pass
90-d egree rot at ion. We also provid e a compar at ive analysis of Cat mull
and Smit hs met hod (1980) and a d iscussion of implement at ion st r at egies
on fr ame buffer hard ware.
1
Th i s p a p er r ev i s es a n d u p d a t es t h e jou r n a l a r t i cl e (Pa et h , 1986a ), wh i ch r s t d es cr i bed
gen er al r ast er r ot at i on u si n g t h r ee sh ear i n g p asses. Mi n or er r or s h ave been cor r ect ed , t h e
r efer en ces h a v e been a u gmen t ed , a n d a n a d d en d u m h a s been i n cl u d ed , wh i ch p r ov i d es
ad d it ional backgrou nd and ap p licat ion not es.
III. 6 III. 6 III. 6 III. 6 III. 6
180
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 180
St at ement of t he Pr obl em St at ement of t he Pr obl em St at ement of t he Pr obl em St at ement of t he Pr obl em St at ement of t he Pr obl em
A gener al 2D count erclockwise rot at ion of t he point (x, y) ont o (x, y)
by angle t het a is per for med by mult iplying t he point vect or (x, y) by t he
rot at ion mat r ix

M =
cos sin
sin cos

1
]
1
1
The mat r ix is or t hogonal: it s inver se is it s t r anspose, rows and columns
are unit vect or s, and t he d et er minant is one. To rot at e a r ast er image, we
consid er mapping t he unit cell wit h cent er at locat ion (i, j) ont o a new
locat ion (i, j).
The image of t he input cell on t he out put gr id is a cell wit h (usually) a
nonint egr al cent er, and wit h a rot at ion angle t het a ( ). We ad opt a
box-lt er sampling cr it er ion, so t he value of t he out put pixel is t he sum
of t he int ensit ies of t he covered pixels, wit h each cont r ibut ing pixels
int ensit y weight ed in d irect propor t ion t o it s cover age (see Fig. 1). Not e
t hat t he out put pixel may t ake int ensit ies from as many as six input
p ixels. Wor se, t he ou t p u t p ixel cover age of ad jacent inp u t p ixels is
nonper iod ic; t his is d irect ly relat ed t o t he presence of ir r at ional values in
t he rot at ion mat r ix. Clear ly, t he d irect mapping of a r ast er by a gener al
2 2 mat r ix is comput at ionally d ifcult : many int er sect ion t est s result ,
usually wit h no coherence or per iod icit y t o speed progr am loops.
Figure 1. Rot at ion by r ast er sampling.
181
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 181
Rot at i on t hr ough Shear i ng Rot at i on t hr ough Shear i ng Rot at i on t hr ough Shear i ng Rot at i on t hr ough Shear i ng Rot at i on t hr ough Shear i ng
Now consid er t he simplest 2 2 mat r ices t hat may oper at e on a r ast er.
These are shear mat r ices:

x shear =
1
0 1

1
]
1
1


y shear =
1 0
1

1
]
1
1
.
Shear mat r ices closely r esemble t h e id en t it y mat r ix: bot h h ave a
d et er minant of one. They share no ot her p rop er t ies wit h or t hogonal
mat r ices. To build more gener al mat r ices, we for m prod uct s of shear
mat r icest hese cor respond t o a sequence of shear oper at ions on t he
r ast er. Int uit ively, consecut ive shear ing along t he same axis prod uces a
confor ming shear. This follows d irect ly:

1 a
0 1

1
]
1
1

1 a
0 1

1
]
1
1

1 a+ a
0 1

1
]
1
1
.
Thus, shear prod uct s may be rest r ict ed t o prod uct s of alt er nat ing x
and y shear s, wit hout loss of gener alit y. The prod uct of t hree shear s
gives r ise t o a gener al 2 2 mat r ix in which t hree ar bit r ar y element s may
be specied . The four t h element will t ake on a value t hat ensures t hat t he
d et er minant of t he mat r ix remains one. This falls ou t becau se t he
d et er minant of t he prod uct is t he prod uct of t he d et er minant s (which are
always one for each shear mat r ix). Or t hogonal 2 2 mat r ices also have
unit d et er minant s, and may t hus be d ecomposed int o a prod uct of no
more t han t hree shear s:

1
0 1

1
]
1
1

1 0
1

1
]
1
1

1
0 1

1
]
1
1

cos sin
sin cos

1
]
1
1
.
Solving t he gener al equat ion, we have = y = (cos 1)/ sin ; =
sin . The r st equat ion is numer ically unst able near zero, but can be
r ep l a ced by su bst i t u t i n g t h e h a l f-a n gl e i d en t i t y for t h e t a n gen t :

tan

2

sin
1+ cos

1 cos
sin
182
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 182
yield ing = t an(/ 2). Count erclockwise rot at ion by is t hus t he
shear prod uct :

1 tan / 2
0 1

1
]
1
1

1 0
sin 1

1
]
1
1

1 tan / 2
0 1

1
]
1
1
.
Not ice t hat and have opposing signs for nonzero . As will be shown
lat er, = 2/ (1 +
2
). Progr am cod e t o shear and upd at e t he point
(x,y) wit h (x,y) is t hen:
x shear y shear
x x t an(/ 2) y; x x;
y y; y y + sin() x;
When t he out put vect or replaces t he input , x x and y y, so t he
second line of t he sequence may be opt imized out . Consecut ive shear s
yield sequent ial progr am st eps. Thus, a t hree-shear rot at ion is achieved
by t he t hree progr am st at ement s:
x x + y; x shear (1)
x y + y; y shear (2)
x x + y; x shear (3).
Wit h 0, Cohen (Newman and Sproull, 1979) uses st eps (1) and (2)
t o gener at e circles by plot t ing point s increment ally. His d er ivat ion begins
by choosing and t o approximat e t he convent ional rot at ion mat r ix
and t hen point s out t hat by reassigning x + y t o t he or iginal var iable
x in (1), and not t o a t empor ar y value x, t he d et er minant becomes one,
and t he circle event ually closes. Our analysis d emonst r at es for mally why
t his is t r ue: rewr it ing t he var iables const it ut es a shear, and t he sequence
of shear s always maint ains a d et er minant of one. Augment ing t he cod e
wit h line (3) would conver t t he t wo-axis shear in a t rue rot at ion: t he circle
gener at or would t hen prod uce point s rot at ed t hrough a const ant angle
relat ive t o t he preced ing point . This is impor t ant should t he algor it hm be
183
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 183
used t o prod uce circle approximat ions as n-gons (and not point d r aw-
ings), where = 360/ n is no longer small.
Rast er Shear i ng Rast er Shear i ng Rast er Shear i ng Rast er Shear i ng Rast er Shear i ng
Rast er shear ing d iffer s from point t r ansfor mat ion in t hat we must con-
sid er t he area of t he unit cell t hat represent s each pixel. For t unat ely, t he
shear oper at ion mod ies t he pixel locat ion wit h respect t o only one axis,
so t he shear can be represent ed by skewing pixels along each scan-line.
This simplies t he int er sect ion t est ing t hat must go on t o recomput e t he
int ensit y of each new out put pixel.
In gener al, t he u nit squ are P(i, j) on row i is rewr it t en as a u nit
par allelogr am wit h sid e of slope 1/ on row i, wit h t he for mer d isplaced
by i pixel wid t hs. This d isplacement is not usually int egr al, but
remains invar iant for all pixels on t he it h scan-line. For illust r at ion and
Figure 2. Rast er shear ing along t he x-axis.
Figure 3. The par allelogr am approximat ion.
184
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 184
implement at ion, it is represent ed as t he sum of an int egr al and a fr ac-
t ional par t (f in Fig. 3; skewf on p. 187). Those pixels covered by t his
par allelogr am are wr it t en wit h fr act ional int ensit ies propor t ional t o t heir
cover age by t he par allelogr am. The sum of all t hese pixels must equal t he
value of t he or iginal input pixel, as t hey represent t his input pixel aft er
shear ing.
We next approximat e t his par allelogr am of unit area wit h a unit square.
Placing t he ed ges of t he square t hrough t he mid point s of t he par allelo-
gr am, we prod uce an exact approximat ion when t he par allelogr am cover s
t wo pixels, but not when it cover s t hree. This approximat ion is t he basis
for our rot at ion algor it hm. As we shall see, it can be implement ed as a
ver y efcient inner most pixel blend ing loop, t hus offset t ing t he cost of
making t hree shear ing passes, as compared wit h previous t echniques,
which employ t wo less efcient (t hough more gener al) passes.
Based on t his lt er ing st r at egy, we consid er t wo approaches t o rot a-
t ion. Fir st , we seek angles for which t he lt er ing is exact . Second , we
analyze t he lt er for ar bit r ar y values of

where t he lt er may not be


exact .
Rat i onal Rot at i on Rat i onal Rot at i on Rat i onal Rot at i on Rat i onal Rot at i on Rat i onal Rot at i on
Filt er ing is exact when all par allelogr ams over lap no more t han t wo
pixels. This will always occur when t he shear offset is of lengt h 1/ n,
because a per iod ic cycle of n par allelogr ams result s in which each spans
exact ly t wo pixels. Choosing t his id eal lt er for t he r st and t hird passes,
we d er ive t he second pass shear value. This requires t he gener al solut ion
of in t er ms of . Since = t an(/ 2), = 2 t an
1
as t an x =
t a n x . Su bst i t u t i on yi el d s = si n = si n (2 t a n
1
); si mi l a r l y
sin x = sin x. Given a r ight t r iangle of ad jacent sid e 1 and opposit e
sid e it is clear t hat

tan
1
= sin
1

2
+1
= cos
1
1

2
+1
.
Also, sin(2 ) = 2 sin cos . By expressing arct angent successively in
185
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 185
t er ms of arcsine and arccosine, we have

= 2 sin sin
1

2
+1

_
,

cos cos
1
1

2
+1

_
,

=
2
1+
2
.
In t he case at hand , we choose in t he for m

1
n
yield ing ,


2n
1+ n
2
.
An all-int eger case occur s when set t ing n = 1, which yield s = 1,
= 1. Thus, rot at ions wit h = 90 are exact , a feat ure not possible
when using t he t wo-pass approach of Cat mull-Smit h. Because no fr ac-
t ional pixel values are gener at ed , rot at ion by 90 d egrees may be cod ed as
a high-speed t hree pass shufe for use on eit her soft ware or hard ware
(Kor nfeld , 1987).
We may consid er rot at ion by r at ional values gener ally, as t hese specic
for ms allow loop unrolling for fast rot at ions. pass gener at es small
er ror s on a per iod ic basis. When and are small r at ionals of t he for m
i/j, t hen t he shear values (which are used as blend ing coefcient s by our
algor it hm) will recur ever y j scan-lines. In par t icular, t he jt h scan-line
will have no fr act ional remaind er and can be blit t ed d irect ly int o t he
out put buffer. Solving for gener al r at ional values of and , we nd t hat
= i/ j and = 2ij/ (i
2
+ j
2
). These t abu lat ed valu es give r ise t o
highly efcient lt er s, wit h approximat ion er ror s minimized (see Fig. 4).
Gr aphically, t hese rot at ion angles are relat ed t o solut ions of Pyt hago-
r as t heorem i
2
+ j
2
= k
2
wherein t he coord inat es of select input pixels
and mat ching out put pixels remain int egr al. As an example, rot at ion by

1 1 90.00
3/ 4 24/ 25 73.74
2/ 3 12/ 13 67.38
1/ 2 4/ 5 53.13
1/ 3 3/ 5 36.87
1/ 4 8/ 17 28.07
1/ 5 5/ 13 22.62
Figure 4. Rot at ion by a r at ional shear.
186
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 186
= 53.13 map s all inp u t p ixels at coord inat es (5i, 5j) ont o ou t p u t
pixels at (3i, 4j) exact ly. (See also Circles of Int egr al Rad ius on Int eger
Lat t ices beginning on page 57.)
Ar bi t r ar y Rot at i on Ar bi t r ar y Rot at i on Ar bi t r ar y Rot at i on Ar bi t r ar y Rot at i on Ar bi t r ar y Rot at i on
We now consid er ar bit r ar y choices of and t hen t he precision of t he
rot at ion. For > 90, our shear par allelogr am may span four pixels and
t he lt er ing r apid ly breaks d own. Based on t he four-fold symmet r y of a
r ast er, we may rest r ict our at t ent ion t o rot at ions of no more t han 45
d egrees, where our approximat ion has wor st -case per for mance (because
and grow monot onically wit h 0 < < 90). Here = 1

2
.4142; and =

2 / 2 .7071. The second pass is t he most er ror-
prone.
Probabilist ically, it s lt er is exact 29.3% of t he t ime. Ot her wise, t he
par allelogr am spans t hree pixels, and t he er ror, as a funct ion of fr act ional
pixel skew, grows quad r at ically t o a cusp, reaching it s wor st -case er ror
when t he par allelogr am is symmet r ic about t he out put pixel. This er ror is

2 / 8 or 17.7%. However, t he sampling t ile shift s as t he shear value
changes wit h each scan-line, so aver age d egr ad at ion t o t he sheared r ast er
is comput ed by int egr at ing over par allelogr ams of all possible skew.
Solving t he equat ions, we nd t hat t he wor st -case shear lt er approxi-
mat es int ensit ies t o wit hin 4.2% of t he act ual int ensit y. For rot at ions less
t han 45 d egrees, t he approximat ion is even closer, as t he probabilit y of
Figure 5. Approximat ion er ror.
187
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 187
t he par allelogr am spanning t hree pixels d ecreases. Where it d oes, t he
er ror t er ms are also smaller.
The nat ure of t he er ror is t o concent r at e int ensit ies from a cent er pixel,
whereas t he t rue box-lt er approximat ion calls for cont r ibut ing cover ages
from t wo neighbor ing pixels. Thus, t he approach peaks t he d at a: t he
nat ure of t he d egr ad at ion is not r and om. Fur t her, a reasonable implemen-
t at ion of t he lt er guar ant ees t hat when any scan-line is skew-sheared by
a fr act ional amount , t he cont r ibut ing int ensit ies of each input pixel sum
t o 1.0t he lt er par allelogr ams never over lap. If we consid er t he sum of
t he pixel int ensit ies along any scan-line, t his sum remains unchanged
aft er t he shear oper at ion. Thus, t he algor it hm prod uces no visible shift s
in int ensit y and int rod uces no holes d ur ing rot at ionall pixel ux is
account ed for. The only rot at ion ar t ifact s d iscer nible appear wit h high-
frequency d at a (such as lines of single pixel wid t h), and even t hen only
aft er magnicat ion. This proper t y is shared gener ally wit h rot at ion and
t r anslat ion algor it hms, which must resample such shar p r ast er s ont o
nonint egr al pixel bound ar ies.
Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on
Scan-line shear ing is approximat ed by a blend ing of ad jacent pixels. In
t he following cod e segment , t he pixmult funct ion ret ur ns a pixel scaled
by a value skewf, where 0 < skewf < 1 is a const ant par amet er for all
wid t h passes t hrough t he inner most loop.
procedure xshear (shear, wid t h, height )
begi n
for y 0 to height 1 do begi n
skew shear x (y + 0.5);
skewi oor(skew);
skewf frac(skew); (see ad d end a)
oleft 0;
for x 0 to wid t h 1 do begi n
pixel P[wid t h x, y];
left pixmult (pixel, skewf); pixel = left + right
pixel pixel left + oleft ; pixel left = right
188
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 188
P[wid t h x + skewi, y] pixel;
oleft left ;
endl oop;
P[skewi, y] oleft ;
endl oop;
end
functi on pixmult (pix, fr ac)
begi n
pix
r
pix
r
fr ac
pix
g
pix
g
fr ac
pix
b
pix
b
fr ac
end.
This oper at ion is a shear ing algor it hm for t he x-axis; it shear s a r ast er
of size (wid t h, height ) by t he value present in shear, so t he d at a mat r ix P
must be of sufcient wid t h t o accommod at e t he shift ed out put d at a. Not e
t hat only widt h out put ent r ies are wr it t en, so t he skewed out put line may
be wr it t en t o fr ame buffer memor y mod ulo t he fr ame buffer scan-line
wid t h, t hus requir ing no ad d it ional memor y but ; complicat ing t he speci-
cat ion of d at a t o t he t hree shear passes. A vir t ual fr ame buffer implemen-
t at ion, which provid ed a not ion of margins t o act ive pict ure d et ail, can
maint ain t his offset infor mat ion implicit ly.
A shear oper at ion always has an axis of shear invar iance (it is in fact an
eigenvect or ). In t his implement at ion, t he axis is t he pixel bound ar y below
t he nal row of pixel d at a at a d ist ance height . This gives r ise t o rot at ion
about t he int er st ices bet ween pixel cent er s. To rot at e r ast er s about pixel
cent er s, t he 0.5 half-pixel offset may be removed .
The cod e sp lit s each p ixel int o a left and right valu e u sing one
mult iply per pixel; left and right always sum exact ly t o t he or iginal pixel
value, regard less of machine round ing consid er at ions. The out put pixel is
t hen t he sum of t he remaind er of t he left hand pixel, plus t he comput ed
fr act ional value for t he present (r ight hand ) pixel. The pixmult funct ion
r ed u ces t o a fr a ct i on a l mu l t i p l y or t a bl e l ook-u p op er a t i on wi t h
monochromat ic images. More gener ally, it may oper at e on an aggregat e
pixel, which might cont ain t hree color component s or an opt ional cover-
age fact or (Por t er and Duff, 1984). Because read and wr it e references t o
189
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 189
P occur at ad jacent pixel locat ions d ur ing t he cour se of t he inner most
loop, pixel ind exing can be great ly opt imized .
On machines lacking hard ware mult iply, cod e t o shear a large (512
512) image may build a mult iply t able at t he beginning of each scan-line
and t hen use t able look-up t o mult iply. By skew symmet r y, x-shear ing of
line n and line n are id ent ical, save for shear d irect ion, so one t able
may be used for t wo scan-lines, or for ever y 1024 pixels. Wit h a pixel
consist ing of t hree 8-bit component s, t he t able lengt h is 256, and t able
fet ches will exceed t able load s by a fact or of 12. Since t he t able can be
built wit h one ad d it ion per (consecut ive) ent r y, it s amor t ized cost per
look-up is low, and d ecreases linear ly wit h r ast er size.
Many fr ame buffer s now incor por at e int eger mult iply hard ware, oft en
t arget ed t o pixel blend ing applicat ions (The Ad age/ Ikonas fr ame buffer s
at Wat er loos Comput er Gr aphics Labor at or y provid e a 16-bit int eger
mult iply in hard ware). This speed s t he evaluat ion of t he pixel blend ing;
t he major it y of t he inner-loop overhead is in (un)packing t he 24-bit RGB
pixel t o provid e suit able input for t he mult iplier. For t unat ely, t he ad d it ion
used t o complet e t he blend may be d one as a 24-bit par allel ad d , because
t he values t o be summed , left and right , have been scaled by frac and
1-ffrac respect ively. Thus, t he blend ing oper at ion is closed , and no car r y
can over ow from one pixel component int o t he next .
Finally, t he shear cod e may more gener ally be used t o int rod uce spat ial
t r anslat ion of t he r ast er. By int rod ucing an out put offset in t he shear
cod e, a Bit Blt -st yle oper at ion (Ingalls, 1978) may be includ ed at no ext r a
cost . In t his set t ing, skewi and skewf would have int egr al and fr act ional
offset s ad d ed t o t hem t o accommod at e t he lat er al d isplacement of t he
r ast er. Displacement d ur ing d at a passes t wo and t hree provid es ar bit r ar y
d isplacement on t he plane, wit h or t hogonal specicat ion of t he d isplace-
ment par amet er s.
More gener ally, when t he cod e is incor por at ed int o a larger package,
which provid es ar bit r ar y (afne) mat r ix oper at ions on a r ast er, t he com-
posit e of all int er med iat e image t r ansfor mat ions are represent ed in one
mat r ix. This avoid s unnecessar y oper at ions t o t he image. Event ually, t his
mat r ix is d ecomposed int o t hree oper at ions: scaling, rot at ion, and shear-
ing (plus an opt ional t r anslat ion if a 3 3 homogeneous mat r ix is used ).
The shear ing, rot at ion, and possible t r anslat ion oper at ions may be gat h-
ered int o one t hree-shear oper at ion. The scale pass prefaces t he rot at ion
190
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 190
if it scales t o a size larger t han 1 : 1; ot her wise it follows t he rot at ion. This
maximizes image qualit y and minimizes d at a t o t he shear (and possibly
rot at e) rout ines. Ot her four pass scale/ shear sequences are d iscussed in
t he lit er at ure (Weiman, 1989).
Compar i sons Compar i sons Compar i sons Compar i sons Compar i sons
As wit h t he Cat mull-Smit h approach, t he algor it hm may be implement ed
as a pipeline for real-t ime vid eo t r ansfor mat ion. Bot h approaches require
t wo rot at or s t o t r anspose t he d at a ent er ing and leaving t he second
scan-line oper at or, since t his st ep requires d at a in column (and not row)
ord er.
Most t wo-pass war ps are d escr ibed in t er ms of separ able funct ions on
x and y (Smit h, 1989). By way of compar ison, t hey may be mod eled by
t he t wo-mat r ix t r ansfor mat ion

x
y

1
]
1
1

1 0
tan sec

1
]
1
1

cos sin
0 1

1
]
1
1

x
y

1
]
1
1
.
These slight ly more gener al mat r ices per for m a simult aneous shear and
scale along one axis, while leaving t he second axis unchanged . This
approach saves one d at a pass, but incur s t he penalt y of more complex
scan-line sampling. Moreover, in t wo-p ass scale-invar iant rot at ion all
pixels und ergo a cos minicat ion and rest or ing sec spat ial magnica-
t ion. Thu s, for int er med iat e valu es rep r esent ed in int eger ar it hmet ic
(fr ame buffer pixels), t here is a small penalt y in round off er ror.
Finally, because sample pixels are bot h sheared and scaled , no pixel-
t o-pixel coherence of fr act ional sampling locat ion exist s. Thus, each pixel
must be sampled at t wo fr act ional locat ions, d oubling t he number of pixel
(aggregat e RGB) mult iplies for each pass. Hand analysis of our microcod e
showed t hat t his is alread y t he d ominant oper at ion in t he pixel loop.
Finally, t he Cat mull-Smit h approach mu st ad d it ionally recomput e t he
fr act ional sample point s for each next pixel or approximat e t heir locat ion
using xed -point ar it hmet ic. In our implement at ion, fr act ional sampling
point s are const ant per scan-line, and are calculat ed exact ly in oat ing
point at t he beginning of each line.
191
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 191
Compared gener ally t o ot her wor k, ou r algor it hm nd s applicat ion
where a gener alized Bit Blt oper at ion is need ed t o per for m rot at ion and
t r anslat ion efcient ly. More complex pixel sampling passes may just ify
t heir ad d ed expense in allowing for gener alized rot at ion oper at ions, such
as Kr ieger s mod ied t wo-pass approach (Kr ieger, 1984) used t o per for m
3D rot at ion wit h per spect ive t r ansfor mat ion, useful in t ext ure mapping.
Concl usi ons Concl usi ons Concl usi ons Concl usi ons Concl usi ons
The t echnique out lined here per for ms ar bit r ar y high-speed r ast er rot at ion
wit h ant i-aliasing and opt ional t r anslat ion. The mat hemat ical d er ivat ion
guar ant ees scaling invar iance when rot at ing. The implement at ion st r at egy
allows for par t icular ly fast oper at ion, while minimizing t he approximat ion
er ror. This algor it hm is a power ful t ool in t he reper t oire of d igit al paint
and r ast er manipu lat ion syst ems. Cou pled wit h st at e-of-t he-ar t r ast er
scaling t echniques, it can t r ansfor m an input r ast er by an ar bit r ar y 2 2
t r ansfor mat ion mat r ix in near real t ime.
Addenda: Hi st or y Addenda: Hi st or y Addenda: Hi st or y Addenda: Hi st or y Addenda: Hi st or y
The shear-mat r ix not at ion and associat ed cod e opt izat ion shed s light on
t he regist er saving t echniqu e which is now an oft -rep eat ed bit of
Comput er Gr aphics lore. It is almost ind isput able t hat t he r st implemen-
t at ion was MIT. An ent r y fr om HAKMEM (Beeler et al., 1972) is
excer pt ed here:
It em 149 (Minsky)
Here is an elegant way t o d r aw almost circles on a point -plot t ing d isplay.
CIRCLE ALGORITHM:
NEW X = OLD X *OLD Y
NEW Y = OLD Y + *NEW(!)X
This makes a ver y round ellipse cent ered at t he or igin wit h it s size d et er-
mined by t he init ial point .
192
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 192
. . . The circle algor it hm was invent ed by mist ake when I t r ied t o save one
regist er in a d isplay hack!
. . . [It is] excit ing t o have cur ves, and I was t r ying t o get a cur ve d isplay
hack wit h minimal inst ruct ions.
The ear liest use of t r iple shear mat r ices is much old er. Gauss paired
one spat ial shear wit h each of t he t hree physical processes t hat occur at
t he bound ar ies and int er ior of each element in an opt ical syst em: refr ac-
t ion, t r ansfer (across t he new med ia), refr act ion (Blaker, 1971). Ironically,
his applicat ion was in r ay t r acing!
Ci r cl e Dr awi ng Ci r cl e Dr awi ng Ci r cl e Dr awi ng Ci r cl e Dr awi ng Ci r cl e Dr awi ng
This algor it hm upd at es t he circle-d r awing rout ine cit ed above. In t his
guise a circle of r ad ius r locat ed at t he or igin is represent ed by t he init ial
point (r, 0) wit h subsequent point s for med it er at ively by applying succes-
sive t r iple shear s. This for mulat ion yield s a r ad ius vect or of d emonst r ably
invar iant lengt h, t hus assur ing t he creat ion of t rue circles. The previous
t wo-pass met hod maint ains a const ant swept area bet ween successive
r ad ii as a consequence of unit syst em d et er minant . The locus of point s
gener at ed t herefore d escr ibes ellipses as a consequence of t wo of Kepler s
laws of mot ion: planet s move along ellipt ical or bit s and sweep out regions
of const ant area per unit t ime. Closure is t hus guar ant eed , albeit wit h a
resid u al eccent r icit y. The t hree-p ass t r igonomet r ic mat r ix for mu lat ion
d escr ibes rot at ion by ar bit r ar y const ant angle. Thus, t he revised algo-
r it hm may be used for t he efcient const ruct ion of ar bit r ar y n-gons in
which no t r igonomet r ic funct ions appear wit hin t he inner most cod e loop.
When represent ing a circle as a polygon of large n t he mat r ix prod uct
(XYX)
n
represent s all shear s const it ut ing one complet e cycle. Therefore,
t his prod uct is t he id ent it y mat r ix and we may per mut e any run of 3n
prod uct s cyclically. Also, mat r ix prod uct s associat e, t hus allowing ar bi-
t r ar y grouping. In par t icular, t he prod uct may be wr it t en as (Y(XX))
n
.
This red uces t he number of shear s t o t wo by summing t he off-d iagonal
values for t he consecut ive X mat r ices, yield ing (YX)
n
as a valid t wo-pass
t echnique for closed cur ve gener at ion. By Taylor expansion we have
X = 2X = 2 t an( / 2) =
3
/ 12 + 0(
5
)
Y = Y= sin( ) =
3
/ 6 + 0(
5
).
For

0
zero we omit all but t he linear t er ms and t hus red er ive Minskys
193
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 193
or iginal circle gener at or :

1
1
2

1
]
1
1

1 0
1

1
]
1
1

1
0 1

1
]
1
1
.
Not e t hat t he coord inat es for circumferent ial point s are no longer gener-
at ed explicit ly, but are now implicit wit hin t he aggregat e X shear. Thus,
t he t wo-pass met hod yield s circles wit h over all X shear : ellipses wit h
major and minor axes not par allel t o t he Car t esian ones. The t wo-pass
t echnique maint ains high accur acy because it provid es an approximat ion
cor rect t o t he quad r at ic t er m while employing only linear oper at ions.
Font Render i ng Font Render i ng Font Render i ng Font Render i ng Font Render i ng
The t hree-pass algor it hm is well-suit ed for t he rend er ing of bit mapped
font s at var ious rot at ions and emphases (nor mal, bold , it alic). Most oft en,
glyph libr ar ies are hand -t uned for specic sizes making a fast rot at or
provid ing ant i-aliasing (but lacking scaling) an id eal choice. Rast er-based
char act er it alicizat ion is most oft en d one using scan-line skewing (x-axis
shear ing) by t he d iscret e values 0.25 or 0.2. These values cor respond t o a
single pixel scan-line d isplacement occur r ing ever y four t h or ft h scan-line
wit hin t he char act er st r ing, respect ively. When combined wit h rot at ion,
t his skew mat r ix S of ar bit r ar y x-shear value prefaces t he t hree-pass
r ot at ion as we wish t o r ot at e an it alic font , not t he conver set he
symmet r y of t he t hree-pass for mula is d est royed and mat r ix mult iplica-
t ion d oes not commut e. For t he new XYXS we may immed iat ely regroup
t he t r ailing x-shear s t o for m XYX The complet e oper at ion ut ilizes shear
mat r ix prod uct s in t heir most gener al capacit y.
Similar ly, glyph embold ening is an oper at ion t hat occur s pr ior t o t he
r st shear pass. Simple embold ening merely br ight ens pixel values. When
mad e implicit t o t he oper at ion of t he scan-line cod e, embold ening in-
creases t he fr act ional pixel cover age value skewf, t hereby t hickening t he
ver t ical ed ges of t he or iginal char act er set . Wit h imaginat ion, relat ed
effect s such as haloing or shad owing can also t ake place concur rent ly.
Gener al i zed Bi t Bl t Gener al i zed Bi t Bl t Gener al i zed Bi t Bl t Gener al i zed Bi t Bl t Gener al i zed Bi t Bl t
The ent ire pseud o-cod e algor it hm can be rewr it t en using increment al
point er ar it hmet ic (C-language el = *pt r + + ) as all memor y read s and
194
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 194
wr it es are t o consecut ive pixels. This is a consequence of t he over all
scale invar iance, which means no r at e d ifferent ials exist on t he input and
out put d at a st reams t hat ot her wise give r ise t o t he bot t lenecking and
fold -over problems of gener alized war ping (Wolberg and Boult , 1989).
The input and out put point er s may share t he same 2D pixel buffer space,
t hus allowing in sit uo rot at ion wit h impunit y: t he out put point er will
never over wr it e an input pixel not yet visit ed . Fr ame buffer algor it hms for
t wo-pass magnicat ion and minicat ion t hat share t his valuable proper t y
have recent ly been d iscussed (Heckber t , in press). A public-d omain C-lan-
guage implement at ion of xshear in bot h xed and oat ing point which
accommod at es out -of-core r ast er s of ar bit r ar y spat ial and pixel d imension
is present in t he IM Rast er Toolkit (Paet h, 1986a, 1986b, 1987).
Advanced Ant i - Al i asi ng Advanced Ant i - Al i asi ng Advanced Ant i - Al i asi ng Advanced Ant i - Al i asi ng Advanced Ant i - Al i asi ng
Text and ot her object s may also be shar p ened by su bst it u t ing more
gener al aliasing lt er s in t he fr act ional cover age t est . The present imple-
ment at ion blend s neighbor ing input pixels t oget her as a linear funct ion of
t he fr act ional pixel d ist ance by which t he out put box over laps bot h input s
t he spat ial skewf value d irect ly d r ives t he blend ing coefcient s. This
simple cover age sampling yield s a t r iangular (Bar t let t ) lt er wind owt he
convolut ion of t he input and out put boxes. Good result s are also afford ed
using t he wind ow creat ed by J. von Hann (t er med Hanning or raised
cosine). Und er t his wind ow t he fr act ional pixel d isplacement along t he
d omain [0..1) maps ont o t he r ange [0..1) in a sinusoid al fashion (Johnson,
1989). As t he inner loop consid er s only left and r ight neighbor s, a
t hree-point ver sion of t he lt er yield s t he funct ion y =

1
2
(1 (cos x),
which biases t he blend ing weight in t he d irect ion of t hat input pixel
closest t o t he fr act ional x locat ion (page 187), shar pening t he d at a.
It is wor t h reit er at ing t hat t he fr act ional offset (t he skew bet ween input
and out put pixel ed ges) remains const ant along an ent ire scan-line be-
cause t he algor it hm is scale-invar iant . Thus, t he weight -ad just ment com-
put at ion t akes place out sid e t he inner most loop. Typically, t he funct ion is
t abulat ed pr ior t o t he ent ire algor it hm and is bot h st ored and ind exed in
int eger precision. This lt er ing upgr ad e t herefore ad d s one t able look-up
per scan-line, which occur s when t he assignment t o skewf is mad e on t he
four t h line of t he pseud o-cod e fr agment . Relat ed lt er s such as t hat by
195
III.6 A FAST ALGORITHM FOR GENERAL RASTER ROTATION
GRAPHIC GEMS I Edit ed by DAVID KIRK 195
Figure 6. Filt er wind ows.
R. W. Hamming int rod u ce negat ive blend ing coefcient s, which can
complicat e oper at ion on pixel eld s, which are t ypically t reat ed as un-
signed int eger s of small precision. Higher-ord er lt er s ad d it ionally re-
quire pixel sampling at more t han merely ad jacent pixels and r apid ly
increase t he complexit y of t he inner loop.
Fur t her Wor k Fur t her Wor k Fur t her Wor k Fur t her Wor k Fur t her Wor k
Dat a st ruct ures mod eling vir t ual fr ame buffer s (Higgins and Boot h,
1986) allow t he implicit represent at ion of scan-line skew, t hereby red uc-
ing st or age. This has great est benet when rend er ing nar row object s such
as char act er st r ings at values of 45. Gener al afne t r ansfor mat ions
are possible when combining rot at ion wit h rect ilinear scaling. As neit her
met hod requires fr act ional (phase) recomput at ions on a per-pixel basis,
highly simplied 1D lt er ing may be used t hroughout . This approach
requires more t han t hree passes, making an algor it hmic t est for image
d egr ad at ion d esir able. The use of mat r ix prod uct s in represent ing spat ial
oper at ions on r ast er s is d esir able and should gener ally be encour aged .
196
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 196
III.7 III.7 III.7 III.7 III.7
U UU UUSEFUL SEFUL SEFUL SEFUL SEFUL 1 - 1 - 1 - 1 - 1 -TO TO TO TO TO- 1 - 1 - 1 - 1 - 1
P PP PPIXEL TRANSFORMS IXEL TRANSFORMS IXEL TRANSFORMS IXEL TRANSFORMS IXEL TRANSFORMS
Dale Schumacher
St Paul, Minnesot a
Many useful kind s of image enhancement s can be achieved wit h 1-t o-1
pixel t r ansfor ms. These are t r ansfor ms where t he new value of a pixel is
st r ict ly a funct ion of t he old value of t hat pixel. The d igit al equivalent s of
most p hot ogr ap hic d ar kroom t echniqu es fall int o t his cat egor y. This
paper d iscusses some of t he more useful 1-t o-1 pixel t r ansfor ms and
illust r at es t heir effect on a sample image.
In t his d iscussion, t he images are composed of monochrome gr ayscale
pixel values wit h a oat ing point r ange of [0.0, 1.0]. The t r ansfor ms will
usually be shown as an input -out put relat ionship gr aph wit h t he or iginal
pixel value on t he hor izont al axis as t he input , and t he new pixel value on
t he ver t ical axis as t he out put .
The simplest t r ansfor m is a null t r ansfor m, where t he new pixel has t he
same value as t he old pixel. If t he t r ansfor m funct ion is called f(x), t hen
t he null t r ansfor m is simply f(x) = x. The cor respond ing input -out put
gr aph is Fig. 1. The next simplest t r ansfor m is phot o-inver sion, where t he
luminance values are rever sed , similar t o a phot ogr aphic negat ive. The
fu nct ion for p hot o-inver sion is f(x) = 1.0 x. Figu r e 2 shows t he
input -out put gr aph.
A quant izat ion t r ansfor m, also called post er izat ion, is accomplished by
d ivid ing t he input values int o d iscret e r anges and assigning t he same
out put value t o all pixels in t he r ange. The out put values are usually
chosen t o creat e a st air-st ep effect . In t he ext reme case, wit h only t wo
197
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 197
Figure 1. Null t r ansfor m.
Figure 2. Phot o-inver sion t r ansfor m.
198
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 198
Figure 3. Quant izat ion t r ansfor m.
r anges (above or below a cer t ain t hreshold value), t his result s in all input
values mapping t o eit her 0.0 or 1.0. Figures 3, 4, and 5 illust r at e t he
effect of a ve-st ep quant izat ion based on t he t r ansfor m funct ion
f x ( )
0. 00 0. 0 x < 0.2
0.25 0.2 x <0.4
0.50 0. 4 x 0.6 .
0. 75 0.6 < x 0.8
1. 00 0.8< x 1. 0

'

A cont r ast enhancement t r ansfor m alt er s t he slope of t he t r ansfor m


funct ion. St eeper slope result s in great er cont r ast . Oft en, when an image
has poor cont r ast , t here is relat ively lit t le pict ure infor mat ion at t he high
and / or low end of luminance r ange. A ver y useful t ool for nd ing t his
clu st er ing of infor mat ion across t he lu minance r ange is a lu minance
hist ogr am. This hist ogr am shows t he relat ive number of pixels in t he
image at each luminance value. Peaks on t his gr aph ind icat e significant
199
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 199
Figure 4. Or iginal image.
Figure 5. Tr ansfor med image.
200
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 200
Figure 6. Or iginal Image
Figure 7. Or iginal hist ogr am
201
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 201
Figure 8. Cont r ast enhancement t r ansfor m.
number s of pixels wit h near ly t he same luminance. Figure 7 shows a
luminance hist ogr am for t he sample image. A signicant improvement in
t he image t hrough increased cont r ast may be gained by set t ing all values
below some lower bound t o 0.0, all values above some upper bound t o
1.0, and all values bet ween t he bound s t o a linear r amp of values from 0.0
t o 1.0. These upper and lower bound s are chosen by examining t he
hist ogr am for relat ively lower pixel count s near t he high and low end s of
t he luminance r ange. Figure 8 shows t he t r ansfor m chosen, wit h a lower
bound of 0.12 and an upper bound of 0.82. The gener al for m of t his
t r ansfor m funct ion is

f x ( )
0. 0
x low ( )
1. 0

'

/ high low
( )
Figure 9 shows t he t r ansfor med image, and Fig. 10 shows t he his-
t ogr am of t he t r ansfor med image. The hist ogr am shows t hat t he bulk of
x low
low < x < high.
x high
202
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 202
Figure 9. Tr ansfor med image.
Figure 10. Tr ansfor med hist ogr am.
203
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 203
t he image infor mat ion is now spread more evenly across t he luminance
r ange.
The over all br ight ness of an image can be ad just ed wit h a gamma
cor rect ion t r ansfor m. This is a nonlinear t r ansfor m, which relat es closely
t o t he effect s of t he br ight ness cont rol on a CRT or var iat ions in
exposure t ime in phot ogr aphy. The gener al for m of t he gamma cor rect ion
funct ion is
f(x) = x
GAMMA
.
A GAMMA value of 1.0 prod uces a null t r ansfor m. Values great er t han 1.0
d ar ken t he image and values bet ween 0.0 and 1.0 light en t he image. In
t he examples below, Figs. 11, 12, and 15 show a gamma cor rect ion of
0.45 and Figs. 13, 14 and 16 show a gamma cor rect ion of 2.2. His-
t ogr ams of t he t r ansfor med images are includ ed t o show t he effect t his
t r ansfor m has in t he d ist r ibut ion of int ensit ies wit hin an image. Some-
t imes an out put d evice like a CRT is d escr ibed as having a gamma of 2.2.
This ind icat es t he amount of d ar kening t hat will occur when t he d evice
d isplays an image. To account for t his, you can apply a gamma cor rect ion
t hat is t he reciprocal of t he gamma of t he out put d evice t o your image
before it is d isplayed . For a d evice wit h a 2.2 gamma, t he cor rect ion is
0.45.
Ar bit r ar y 1-t o-1 t r ansfor ms can also creat e useful effect s t hat have no
phot ogr aphic equivalent s. Some applicat ions, such as med ical imaging,
nd great ut ilit y in a var iet y of band ing, t echniques, which can be used
t o highlight par t icular image feat ures. Two band ing t r ansfor ms are shown
here. The r st t r ansfor m is a sawt oot h funct ion, which d ivid es t he input
r ange int o a ser ies of full-r ange out put sweeps. Each sawt oot h act s as a
d r amat ic cont r ast enhancement t r ansfor m of t he cor resp ond ing inp u t
r ange. This gives consist ent ly high slope in t he t r ansfor m funct ion and
t hus high cont r ast t hroughout t he image, wit h a wr ap-around effect from
whit e t o black at t he bound ar y bet ween r anges. The sawt oot h t r ansfor m
funct ion, in gener al for m, looks like
f(x) = frac(x r anges).
204
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 204
Figure 12. 0.45 gamma hist ogr am.
Figure 11. 0.45 gamma cor rect ion t r ansfor m.
205
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 205
Figure 13. 2.2 gamma cor rect ion t r ansfor m.
Figure 14. 2.2 gamma hist ogr am.
206
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 206
Figure 15. 0.45 gamma t r ansfor med image.
Figure 16. 2.2 gamma t r ansfor med image.
207
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 207
Figure 17. Saw-t oot h t r ansfor m.
Figure 18. Tr ansfor med image.
208
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 208
Figure 19. Ar bit r ar y band t r ansfor m.
Figure 20. Tr ansfor med image.
209
III.7 USEFUL I-TO-I PIXEL TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 209
The second t r ansfor m is an ar bit r ar y ser ies of gr ayband s chosen t o
highlight specic input luminance r anges. This t ype of t r ansfor m t ypically
is d ened as a look-up t able (par t icular ly if t he input is act ually d iscret e
luminance values r at her t han a full oat ing point r ange) or a ser ies of
highlight r anges. The funct ion shown is

f x ( )
1. 0 0.28< x < 0.50
0.5 0.82 < x <0.94
0. 0 else.

'

The use of a hist ogr am helps id ent ify impor t ant relat ionships bet ween t he
image and it s luminance d ist r ibut ion. This infor mat ion can t hen be used
t o guid e t he applicat ion of 1-t o-1 t r ansfor ms for maximum improvement
of t he or iginal image.
See also 1-t o-1 Pixel Tr ansfor ms Opt imized t hrough Color-Map
Manipulat ion (270)
210 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 210
III.8 ALPHA BLENDING
III.8 III.8 III.8 III.8 III.8
A AA AALPHA BLENDING LPHA BLENDING LPHA BLENDING LPHA BLENDING LPHA BLENDING
Kelvin Thompson
Nt h Graphics, Lt d.
Aust in, Texas
Alpha blend ing wor ks best when you premult iply color vect or s wit h
alpha channels (Por t er and Duff, 1984). Specically, it is best t o represent
t he color (r, g, b) and cover age wit h t he color vect or
(r ,g ,b ,).
Some example color s using t his convent ion are as follows:
Color Vect or
half red (0.5, 0, 0, 0.5)
full black (0, 0, 0, 1)
invisible (0, 0, 0, 0).
Suppose we want t o composit e t wo vect or s A = (r
A
, g
A
, b
A
,
A
) and
B = (r
B
, g
B
, b
B
,
B
) int o a new vect or C = (r
C
, g
C
, b
C
,
C
) wit h oper at or
op, t hat is, C = A op B. Then for each component color c {r, g, b, },
premult iplicat ion allows us simply t o wr it e
c
C
= c
A
F
A
+ c
B
F
B
,
where F
A
and F
B
are fr act ions d et er mined by t he oper at or op. Table 1
list s some useful alpha oper at or s and t heir associat ed blend ing fr act ions.
The over oper at or makes a foreground image A occlud e a background
image B. The i n and out oper at or s allow B t o act as a mat t e for A: A is
visible only where allowed or d isallowed by t he alpha channel of B (t he
color channels of B are not used ). Atop is somet imes useful when B
211 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 211
III.8 ALPHA BLENDING
Table 1. Alpha composit ing oper at or s.
should act as bot h background and mat t e for A. The remaining oper at or s
are useful pr imar ily for occasional special effect s.
The component of a color vect or may d escr ibe t wo kind s of cover-
age: t he t r ansparency of t he color, or t he fr act ion of t he pixel t hat t he
color cover s. The d iagr ams in Table 1 show t he meanings of t he oper at or s
when represent s pixel cover age.
4
FRAME BUFFER
TECHNIQUES
215
IV.1 FRAME BUFFERS AND COLOR MAPS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 215
IV.1 IV.1 IV.1 IV.1 IV.1
F FF FFRAME BUFFERS AND RAME BUFFERS AND RAME BUFFERS AND RAME BUFFERS AND RAME BUFFERS AND
C CC CCOLOR MAPS OLOR MAPS OLOR MAPS OLOR MAPS OLOR MAPS
Andrew S. Glassner
Xerox MRC
Palo Alt o, Calif ornia
A frame buffer is a piece of hard ware oft en found in comput er gr aphics
inst allat ions. The id ea behind t he fr ame buffer is t hat a pict ure may be
represent ed by a t wo-d imensional mat r ix of color s. This mat r ix is usually
implement ed wit h fast r and om-access memor y. The RAM it self is t he
fr ame buffer, since it st ores, or buffer s, one fr ame of vid eo infor mat ion.
To d isplay t his fr ame on a CRT, t he color s in t he fr ame buffer are read t o
t he CRTs input circuit s in synchrony wit h t he scanning beam. Thus, as
t he elect ron guns in t he t ube sweep t he face of t he screen t op-t o-bot t om,
left -t o-r ight , a signal is ar r iving t hat species t he color t o be d isplayed at
each point .
In t his scheme t here is a one-t o-one cor respond ence bet ween screen
locat ions and memor y locat ions. Ind ivid ual element s in t he fr ame buffer
ar e called pixels (a cont r act ion of p ict u re element ), and since t he
cor respond ence is so st rong, t he associat ed point on t he screen is usually
called a pixel as well.
Typically, fr ame buffer s organize t heir const it uent pixels in a rect angu-
lar gr id . Each pixel may t herefore be id ent ied by it s ad d ress in a
Car t esian coord inat e syst em as (x, y) (see Fig. 1). Common fr ame buffer
resolu t ions are 512-by-512 and 640-by-480 pixels. Fr ame bu ffer s wit h
higher resolut ions are also available for applicat ions where t he d isplayed
image will be large; for lm applicat ions one somet imes nd s fr ame
buffer s as large as 4096-by-4096. The ad vant age of more resolut ion is
ner cont rol over small d et ails in t he nal image. The d isad vant age is
increased cost , bot h scally t o buy t he ext r a hard war e, and t empor ally in
t he ext r a comput er t ime required t o calculat e t he appropr iat e color for
each pixel.
216
IV.1 FRAME BUFFERS AND COLOR MAPS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 216
Figure 1. The shad ed squar e ind icat es t he same pixel in bot h d evices.
The simplest for m of pixel is a single number. The most common for m
of fr ame buffer s st ore one 8-bit byt e at a pixel (allowing one t o specify a
number from 0 t o 255 inclusive). This number d oes not d irect ly encod e a
color ; r at her, it is an ind ex int o a list of color s called t he color map. In
t his example, t he color map would cont ain 256 ent r ies, one for each
p ossible valu e st ored in t he p ixels. Each ent r y in t he color map is
t ypically composed of t hree number s. These ind icat e t he red , gr een, and
blue int ensit ies of t he color associat ed wit h t hat ent r y.
Thus, when a pict ure is being scanned out t o a CRT, t he value in t he
pixel about t o be d isplayed is r st fet ched from t he fr ame buffer, and t hen
used as an ind ex int o t he color map. Out of t he color map come t he red ,
green, and blue component s of t hat color ; t hese component s are t hen fed
d irect ly t o t he color guns in t he CRT (see Fig. 2).
Figure 2. The highlight ed pixel in t he fr ame buffer ind icat es t hat color number 54 should
be d isplayed at t he cor respond ing screen locat ion. The color-map ent r y for ind ex 54
cont ains t he color (32, 20, 50), which is t hen d isplayed on t he screen.
217
IV.1 FRAME BUFFERS AND COLOR MAPS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 217
This t echnique allows an image t o have up t o 256 d ifferent color s. The
cont ent s of t he color map, not t he pixel it self, d et er mine which color is
associat ed wit h each of t hese 256 number s. A common d emonst r at ion of
color maps is t o t ur n a d ay scene int o a night scene by leaving t he values
in t he pixels const ant , but changing t he color-map ent r ies. For example,
imagine a pict ure of a far mhouse wit h t he sky overhead , and suppose t he
pixels in t he sky region have color number 73. For t he d ay scene, color
number 73 in t he color map might be a light blue, but for t he night scene,
color number 73 may be changed t o d ar k blue or black. The number of
t he color in t he image d id nt change; only t he d escr ipt ion of t he color t o
which it refer red changed .
Typical color maps cont ain 8, 10, or 12 bit s t o represent each of t hese
red , green, and blue component s for each color.
A more expensive but also more power ful fr ame buffer may be built ,
which allows each pixel t o represent any color at all. Rat her t han st or ing
t he ind ex int o a color map, each pixel d irect ly st ores t he red , green, and
blue component s of t he color at t hat locat ion. Thus each pixel cont ains
t hree pieces of d at aas wit h t he color map, t he int ensit y of each of t hese
pr imar ies t ypically is represent ed by 8, 10, or 12 bit s. Thus, such a fr ame
buffer is t ypically 24, 30, or 36 bit s d eep at each pixel.
Oft en t here is a color map associat ed wit h t hese fr ame buffer s as well.
In t his ar r angement , t here are act ually t hree ind ivid ual color maps, one
each for t he red , green, and blue component s of t he image. When a pixel
is t o be d isplayed , it s t hree color component s are ret r ieved from t he
fr ame buffer. The red value is used as an ind ex int o t he red color map t o
d et er mine a new int ensit y for t he red component at t hat point ; similar
look-ups are per for med for green and blue (see Fig. 3). This allows one t o
apply over all int ensit y changes t o t he image wit hout d irect ly affect ing t he
pixel values in t he image. A common use of t hese color maps is t o
implement gamma-cor rect ion, which compensat es for t he nonlinear it y of
t he d isplay monit or.
Each layer of infor mat ion in a fr ame bu ffer is called a plane (or
channel). Thus, t he r st for m of fr ame buffer s d iscussed above has but a
single plane (which is 8 bit s d eep), whereas fr ame buffer s t hat st ore t he
red , gr een, and blue component s at each pixel are t hree planes d eep. A
fr ame buffer may be const ruct ed wit h an ar bit r ar y number of planes at
each pixel. Typically t hese ot her planes hold infor mat ion t hat var ies from
pixel t o pixel across t he image, such as t r ansparency (oft en called alpha),
218
IV.1 FRAME BUFFERS AND COLOR MAPS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 218
Figure 3. Each of t he t hree color planes hold s an 8-bit ind ex int o it s own color map.
or d ept h (oft en called z). Rat her t han use a single physical fr ame buffer
wit h many channels, one may use many smaller fr ame buffer s. Somet imes
one hear s references t o t he alpha buffer or t he d ept h buffer of an
image, refer r ing t o t he plane t hat cont ains t hat infor mat ion, whet her or
not it is physically locat ed in t he same fr ame buffer t hat hold s t he color
infor mat ion for t hat image.
219
IV.2 READING A WRITEONLY MASK
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 219
IV.2 IV.2 IV.2 IV.2 IV.2
R RR RREADING A EADING A EADING A EADING A EADING A
W WW WWRITE-ONLY RITE-ONLY RITE-ONLY RITE-ONLY RITE-ONLY
W WW WWRITE MASK RITE MASK RITE MASK RITE MASK RITE MASK
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Over vi ew Over vi ew Over vi ew Over vi ew Over vi ew
On many fr ame bu ffer s t he cu r rent d isp lay mod es are cont ained in
hard ware regist er s and not in read / wr it e memor y. Alt hough a shad ow
set of ou t boar d memor y might maint ain t hese mod es (inclu d ing t he
cur rent pixel color and wr it e mask) in some cases it is d esir able t o
ascer t ain t he hard ware st at e d irect ly, as it represent s t he t rut h. The
following pseud o-cod e recover s t he cur rent hard ware wr it e mask in a
nond est ruct ive fashion, t hereby provid ing a useful d iagnost ic. The cod e
also ser ves as a st ar t -up congur at ion t est for bit plane d ept h in a manner
analogous t o memor y self-sizing as when boot ing an oper at ing syst em. In
t his case t he wr it e mask is set t o t he known value minus one (all bit
planes enabled ) and t he proced ur e is execut ed d irect ly.
procedure set xy(x, y) set pixel locat ion for fut ure readpix and writ epix
i nteger f uncti on read pix() ret urn pixel (int eger) from t he lat est set xy() locat ion
procedure wr it epix(bit s) writ e pixel (int eger) t o t he lat est set xy() locat ion
220
IV.2 READING A WRITEONLY MASK
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 220
i nteger f uncti on get wr it emask()
int egers must cont ain as many bit s as hardware pixels
begi n
t mp, mask: i nteger; saved pixel, comput ed mask
ones, zeros: i nteger; t wo probe words
set xy(0, 0); t o probe pixel at origin
t mp

read pix(); save original value


wr it epix( 1); writ e all ones t hrough unknown mask
ones

read pix(); recover ON bit planes


wr it epix(0); writ e zeros t hrough unknown mask
zeros

read pix(); recover OFF bit planes


wr it epix(t mp); fully rest ore origin
mask

ones bi t-xor zeros; WRITMASK BITS TOGGLED **IFF** ENABLED


an alt ernat e operat ion is bit -clear i.e., bit -and bit -invert
return[mask]; ret urn comput ed writ e mask
end
221
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 221
IV.3 IV.3 IV.3 IV.3 IV.3
A AA AA DIGITAL DIGITAL DIGITAL DIGITAL DIGITAL
D D D D DISSOLVE EFFECT ISSOLVE EFFECT ISSOLVE EFFECT ISSOLVE EFFECT ISSOLVE EFFECT
Mike Mort on
Honolulu, Hawaii
Comput er screens are looking more and more like movie screenspre-
sent at ion and animat ion applicat ions pan, zoom, and cut . Analog vid eo
hard war e u nd er soft war e cont r ol yield s t he best sp ecial effect s, bu t
all-d igit al solut ions in soft ware can prod uce good result s, t oo.
For inst ance, consid er t he problem of d issolving one image int o an-
ot her. In t he analog wor ld , t his is easy: just mix t wo images, br inging t he
new image' s int ensit y up while d ecreasing t he old images. In t he d igit al
wor ld , color or gr ayscale images can be d issolved by comp u t ing a
weight ed aver age of t he old and new values for each pixel, t hen var ying
t he weight ing from 0 t o 100%, much as wit h analog vid eo.
Dissolving monochrome images is a d ifferent problemin t he wor ld of
binar y pixels, t here are no int er med iat e pixel values t hrough which t o
make a smoot h t r ansit ion. One solut ion is t o copy t he new images pixels
over t he old ones in a pseu d o-r and om ord er. This ar t icle present s a
machine-ind epend ent t echnique for t his, wit h a par t ial case st ud y of an
implement at ion for a single-monit or Macint osh. Carefully cr aft ed assem-
bly cod e, d irect ly manipulat ing vid eo memor y, can d issolve quit e quickly.
For inst ance, a full-screen d issolve on a Macint osh SE t akes only about
t hree second s.
Randoml y Tr aver si ng a Randoml y Tr aver si ng a Randoml y Tr aver si ng a Randoml y Tr aver si ng a Randoml y Tr aver si ng a 2 D 2 D 2 D 2 D 2 D Ar r ay Ar r ay Ar r ay Ar r ay Ar r ay
Tr aver sing a lar ge nu mber of scr een p ixels (an ar r ay, in effect ) in
pseud o-r and om ord er is easier said t han d one. A r st st ep is t o red uce t he
problem by number ing each pixel, st ar t ing at zero. If you can prod uce t he
222
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 222
pixel number s in r and om ord er, you can prod uce coord inat e pair s in
r and om ord er, and br ing t he problem from t wo d imensions t o one.
One simple mapping from a linear r and om sequence t o a 2D sequence
is t o t ake each sequ ence element N and comp u t e N di v widt h and
N mod widt h t o get t he ver t ical and hor izont al coord inat es in t he ar r ay.
This is much like r and omly scr ambling int eger s from 0..51, t hen mapping
t hem t o playing-card values by comput ing (N di v 13) and (N mod 13).
A t ypical met hod for scr ambling a set of int eger s (such as 0..51) is t o
st ore t hem in an ar r ay and swap r and om pair s of number s. This isnt
useful when you want t o shufe a d eck of a million card s. To d issolve a
million-pixel screen, you need t he int eger s 0..1,048,575 scr ambled . Most
applicat ions d ont have t hat much room, nor d o t hey have t he t ime t o
make a million swaps.
Scr ambl i ng Int eger s i n Har dwar e Scr ambl i ng Int eger s i n Har dwar e Scr ambl i ng Int eger s i n Har dwar e Scr ambl i ng Int eger s i n Har dwar e Scr ambl i ng Int eger s i n Har dwar e
The soft ware t r ick t hat avoid s t his is based on a simple circuit . Hard ware
t ypes will recognize Fig. 1 as a linear feed back shift regist er. At each
cycle, select ed bit s are sent t hrough an n-way XOR, t he ent ire regist er is
shift ed left , and t he result of t he XOR feed s in as t he new r ight most bit .
(If youre a hard ware-phobe, d ont panict he soft ware ver sion is present ed
below.)
For each lengt h of regist er, t heres at least one combinat ion of bit s t o
t ake from it t hat will make t he regist er cycle t hrough all nonzero values
(zero is a d emon st at e). If you int er pret t he cont ent s of t he regist er as
a number, t he sequence prod uces each of t he number s from 1 t o 255 (in
t his example) in a fair ly r and om manner.
Figure 1. An 8-bit hard ware sequence gener at or : a shift regist er wit h select ed bit s
XORed t o for m t he next input . In soft ware, t he cor red spond ing mask would be 10111000
(binar y).
223
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 223
How r and om is t his sequence? Act ually, it fails many r and omness t est s,
but t he circuit has a soft ware analog t hat is easy t o cod e and runs so fast
t hat it ,s wor t h it . Knut h point s out t hat t he sequence of bit s shift ed out is
act ually quit e r and om, alt hough t he successive numer ic values arent .
Scr ambl i ng Int eger s i n Sof t war e Scr ambl i ng Int eger s i n Sof t war e Scr ambl i ng Int eger s i n Sof t war e Scr ambl i ng Int eger s i n Sof t war e Scr ambl i ng Int eger s i n Sof t war e
Alt hough you can exact ly mimic t he circuit in soft ware, a much fast er
algor it hm is t o shift t he cur rent element r ight (not left , as in t he circuit );
if a 1 bit falls out because of t he shift , XOR t he mask int o t he new
element . This cod e fr agment shows t he cod e t o ad vance fr om one
element t o t he next :
reg: i nteger; current sequence element
reg 1; st art in any nonzero st at e
...
i f (reg bi t-and 1) 0 is t he bot t om bit set ?
then reg r shift (reg, 1) bi t-xor MASK; yes: t oss out 1 bit ; XOR in mask
el se reg r shift (reg, 1); no: t oss out 0 bit
For cer t ain values of t he const ant MASK, execut ing t he if st at ement in
a loop will make t he regist er t ake on all values from 1..2
n
1, for var ious
values of n. Table 1 gives one const ant (t here may be more t han one) for
each wid t h. (The wid t h of t he values prod uced need not be t he full
wid t h of t he var iable st or ing it , of cour se.)
A Fi r st At t empt A Fi r st At t empt A Fi r st At t empt A Fi r st At t empt A Fi r st At t empt
Wit h a soft ware-based ID sequence gener at or, and a way t o map ele-
ment s of a ID sequence int o a 2D ar r ay of pixels, you can wr it e a d issolve
algor it hm. The id ea is t o nd a r egist er wid t h for t he sequ ence
gener at or such t hat t he sequence will gener at e at least as many element s
as t here are pixels. The highest -numbered element s d ont map t o pixels,
and t hey are d iscard ed . Figure 2 illust r at es t his approach.
The main loop maps each sequence element t o a pair of coord inat es,
using mod ulo and d ivision. Coord inat es past t he last row are ignored ;
t hose in bound s are copied . The loop end s when t he sequence ret ur ns t o
224
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 224
Table 1. For any given bit -wid t h w, t heres usually more t han one mask t hat prod uces
all values from 1 t o 2
w
1. These par t icular masks are chosen because t hey can each
be packed int o a byt enot e t hat bit #w of t he mask for wid t h w) is set , so each mask
can be shift ed d own for compact st or age, t hen shift ed left unt il t he highest l bit is
posit ioned cor rect ly. The masks are shown in 8-, 16-, and 32-bit hex only for read abilit y.
225
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 225
Fi gu r e 2. Ma p p i n g s equ en ce el emen t s i n t o a n a r r a y, u s i n g t h e for mu l a s :
row N di v wid t h
column N mod wid t h.
Values of row which are height are ignored .
t he or iginal element . The funct ion t o copy a single pixel should event ually
get called once for ever y (row, col) such t hat 0 row height 1, and
0 col wid t h 1.
Li st i ng I Li st i ng I Li st i ng I Li st i ng I Li st i ng I
A r st at t empt at t he d issolve algor it hm. This will be improved lat er by
eliminat ing t he d ivision and mod ulo comput at ions in t he main loop.
procedure d issolve1 (height , wid t h: i nteger);
begin
pixels,last num: i nteger; # of pixels; last pixels number
regwid t h: i nteger; widt h of sequence generat or
mask: l ongi nt; mask to XOR with to create sequence
seq: unsi gned l ongi nt; 1 element of sequence
row, column: i nteger; row and column numbers for a pixel
Find t he smallest regist er t hat produces enough pixel numbers
pixels height * wid t h; comput e # of pixels t o dissolve
last num pixels 1; last element (t hey go 0..last num)
regwid t h bit wid t h (last num); how wide must t he regist er be?
mask r and masks [regwid t h]; which mask produces t hat bit widt h?
226
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 226
Now cycle t hrough all sequence element s.
seq 1; 1st element (could be any nonzero)
l oop do
begi n
row seq/ wid t h; how many rows down is t his pixel?
column seq mod wid t h; and how many columns across?
i f row < height does seq element fall in t he array?
then copy (row, column); yes: copy t he (r, c)t h pixel
Comput e t he next sequence element
i f (seq bi t-and 1) 0 is t he low bit set ?
then seq r shift (seq, 1) bi t-xor mask; yes: shift , XOR
el se seq r shift (seq, 1); no: just shift
end;
unti l seq = 1; loop t ill original element
copy (0, 0); kludge: loop doesnt produce (0, 0)
end; of procedure dissolve1
The cor rect wid t h of sequence gener at or is found wit h a funct ion called
bit widt h(). Given a number, t his funct ion comput es how wid e a regist er
is need ed t o hold t he number. Here it s used t o nd what wid t h of
gener at or is need ed t o prod uce at least as many pixel number s as need ed .
Li st i ng 2 Li st i ng 2 Li st i ng 2 Li st i ng 2 Li st i ng 2
The bit wid t h () funct ion
functi on bit wid t h (N: unsi gned i nteger): i nteger;
begi n
wid t h: i nteger 0; init ially, 0 bit s needed for N
whi l e N 0 do loop t ill N is whit t led down t o 0
N r shift (N, 1); NB: N is unsigned
wid t h wid t h + 1; and remember how wide N is
endl oop; end of loop shrinking N
return wid t h; ret urn bit posit ions count ed
end; of funct ion bit widt h
227
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 227
Once t he wid t h has been found , t he randMasks[] ar r ay is used t o nd
t he magic value t o gener at e t he sequence. The sequence lengt h can be
near ly t wice as long as t he number of pixels t o copy, because t he lengt h
must be a power of 2 and larger t han t he number of pixels. The [0] and
[1] element s of t he ar r ay arent d ened t he smallest gener at or is 2 bit s
wid e. Again, see Table 1.
The copy () rout inet he cod e t o copy a pixel from t he old image t o
t he newisnt d ened here. It will d ep end on which hard ware and
gr aphics syst em youre using. Well cover t his in more d et ail in t he case
st ud y for Macint osh, but you will almost cer t ainly want t o make copy() be
in-line cod e, t o save t he cost of a funct ion being called many t housand s of
t imes.
Because t he sequence never prod uces t he value 0, t he d issolve funct ion
must call copy wit h (0, 0) explicit ly. (Shar p-eyed user s wat ching a d is-
solve will not ice t hat t he t op-left pixel is always t he r st or last t o be
copied .)
Fast er Mappi ng Fast er Mappi ng Fast er Mappi ng Fast er Mappi ng Fast er Mappi ng
This met hod wor ks, but it s t oo slow because of t he d ivision and mod ulo
calculat ions. Anot her approach is shown in Fig. 3. Here, t he bit s in t he
sequence element are broken apar t bit wise int o row and column number s.
This bit oper at ion is much fast er t han a d ivision for most CPUs. (If youre
lucky t o have a fast d ivid e on your favor it e CPU, skip t his sect ion.)
Wit h t his met hod , t he number of sequence element s can be almost four
t imes t he number of pixelst wice as bad as t he wor st case for t he
simpler algor it hm. But gener at ing element s is so much fast er t han d ivi-
sion t hat t he new met hod is st ill fast er. In ad d it ion, since many screens
have a wid t h t hat is a power of 2, a full-screen fad e is no slower.
The cod e for t his fast er ver sion is shown in List ing 3. It s a lot like t he
or iginal funct ion, except for t he way t he sequence-t o-coord inat es map-
ping is d one.
Li st i ng 3 Li st i ng 3 Li st i ng 3 Li st i ng 3 Li st i ng 3
An improved d issolve, which breaks up t he sequence element int o coord i-
nat es wit h bit oper at ions, not wit h d ivision
228
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 228
procedure d issolve2 (height , wid t h: i nteger);
begi n
r wid t h, cwid t h: i nteger; bit widt h for rows, for columns
regwid t h: i nteger; widt h of sequence generat or
mask: l ongi nt; mask t o XOR wit h t o creat e sequence
rowshift : i nteger; shift dist t o get row from element
colmask: i nteger; mask t o ext ract column from element
seq; unsi gned l ongi nt; 1 element of sequence
row, column: i nteger; row and column for one pixel
Find t he mask t o produce all rows and columns.
r wid t h bit wid t h (height ); how many bit s needed for height ?
cwid t h bit wid t h (wid t h); how many bit s needed for widt h?
regwid t h r wid t h + cwid t h; how wide must t he regist er be?
mask r and masks [regwid t h]; which mask produces t hat bit widt h?
Find values t o ext ract row and col numbers from each element .
rowshift cwid t h; dist t o shift t o get t op bit s (row)
colmask lshift (1, cwid t h) 1; mask t o ext ract bot t om bit s (col)
Now cycle t hrough all sequence element s.
seq

1; 1st element (could be any nonzero)


l oop do
begi n
row

r shift (seq, rowshift ); nd row number for t his pixel


column

seq bi t-and colmask; and column number


i f (row < height ) does element fall in t he array?
and (column < wid t h) ...must check row AND column
then copy (row, column); in bounds: copy t he (r, c)t h pixel
Comput e t he next sequence element .
i f (seq bi t-and 1) 0 is t he low bit set ?
then seq

r shift (seq, 1) bi t-xor mask; yes: shift , XOR


el se seq

r shift (seq, 1); no: just shift


end:
whi l e seq 1; loop t ill original element
copy (0, 0); kludge: element never comes up zero
end; of procedure dissolue2
229
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 229
Figure 3. A revised calend ar wit h more holid ays? Not quit e... This shows a fast er
mapping of 1D sequence element s int o a 2D ar r ay, using t he for mulas:
row r shift (N, rowShift ) [rowShift = 3]
column N bi t-and colMask [colMask = 00111 base 2].
Coord inat e pair s where row height or column wid t h are ignored .
Case St udy: Det ai l s Case St udy: Det ai l s Case St udy: Det ai l s Case St udy: Det ai l s Case St udy: Det ai l s
When you cod e t his, here are some remind er s t hat probably apply t o most
implement at ions:
Don' t forget t he (0, 0)t h element of t he ar r ay, as hand led
explicit ly in List ings 1 and 3.
The algor it hm breaks d own for t iny images, because t he sequence
gener at or d oesn' t wor k for small wid t h of regist er s. You shou ld
probably hand t his case off t o your syst em' s gr aphical-copy funct ion.
To copy pixels quickly, you probably will not be able t o use your local
gr aphics pr imit ives, but will have t o access vid eo memor y d irect ly, in
assemble language. Be careful t o check exact ly how t his int er feres
wit h your high-level gr aphics. (For inst ance, a Macint osh d issolve
funct ion must hid e t he cur sor in soft ware so as not t o over wr it e it in
hard ware.)
I found it easiest t o conver t byt e ad d resses of a bit map t o bit ad -
d resses, d o all my comput at ion in bit number s, and conver t t o byt es
230
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 230
only when it was t ime t o access vid eo memor y. (This assumes t hat ver y
large ad d resses on your machine wont over ow when scaled t o be bit
ad d resses.)
Case St udy: Opt i mi zat i ons Case St udy: Opt i mi zat i ons Case St udy: Opt i mi zat i ons Case St udy: Opt i mi zat i ons Case St udy: Opt i mi zat i ons
That s all t here is t o it just wr it e a fast copy() funct ion. But if you want
a megapixel screen t o fad e in a few second s, you have only a few
microsecond s p er call of copy. Her e are some hint s t o obt ain sp eed
approaching t hat of analog hard war e.
Many of t hese hint s apply only for cer t ain input s t o t he funct ion. There
are many possible combinat ions of opt imizat ions. Inst ead of t r ying t o
cod e all possible combinat ions, it might be int erest ing t o compile opt imal
d issolve cod e at t he t ime of t he call.
For an act ual funct ion for a single, monochrome Macint osh screen, see
Mor t on (1986). I d ont recommend t his rout ine for t he Mac any more
because it d oesnt wor k in t he br ave, new wor ld of mult iple monit or s
or color. It s t he r st cod e I ever wrot e for t he 68000, so it s hard ly
t ext book read ing.
When t he p ixel ar r ay is less t han 64K p ixels, you can st ore t he
sequence element s in 16-bit word s.
When eit her t he wid t h or height of t he ar r ay in a power of 2, you can
of cour se omit t he respect ive checks for whet her t he coord inat es are
in bound st he sequence will exact ly cover t he need ed coord inat es in
t hat d imension. A d issolve where bot h t he wid t h and height are power s
of 2 will be incred ibly fast .
In pseud o-cod e and C, t here are separ at e checks for whet her t he low
bit is 1 and whet her t he element has ret ur ned t o it s or iginal value. In
assembler, you can somet imes combine t hese, as in t his 68000 cod e:
lsr.l # 1,d 0 shift current element right by one
bhi.s loopTop if no carry out and not zero, do it again
eor.l mask, d 0 ot herwise, ip magic bit s . . .
cmp.l mask, d 0 ...but has t his brought us back t o t he sequence st art ?
bne.s loopTop nope:loop back for more grueling fun
br a d one go copy element (0, 0).
231
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 231
This cod e t akes ad vant age of t he fact t hat t he init ial shift inst ruct ion
(lsr ) affect s t he Zero cond it ion cod e (based on t he value aft er t he shift )
and also set s t he Car r y bit if a 1 bit fell out in t he shift . The r st
br anch inst ruct ion (bhi) br anches if bot h Car r y and Zero are falsejust
r ight t o t est for no XOR need ed , but not a zero result eit her.
The sequence element can be viewed as a pair of coord inat es concat e-
nat ed t oget her. Before ext r act ing eit her one, you can check if t he left
one is in bound s by compar ing t he ent ire sequence element t o t he
maximum value plus 1, shift ed t o align wit h t he left one.
Once t he left end of t he sequence element is found t o be in bound s,
ext r act and check t he coord inat e from t he r ight end , before t aking t he
t ime t o ext r act t he left end .
When t heres no car r y, t he mask isnt XORed int o t he next element ,
and you know t he element has a t op bit equal t o zero (since it wasjust
shift ed ). This means t hat t he value at t he left end of t he sequence
element is in bound s, and t hus t he bhi.s loopTop in t he 68000 cod e
can act ually ent er t he loop lat e, aft er t he point where t he left end is
checked
The cod e cur rent ly has t o t est t he source pixel and br anch t o eit her set
or clear t he d est inat ion pixel. It might be fast er t o XOR t he d est inat ion
int o t he offscreen source, creat ing a bit map t hat has 1 bit s only
where t he bit maps d iffer. Then t he d issolve cod e would ignore 0
source bit s and t oggle t he d est inat ion on 1 source bit s. Finally, it
would copy t he d est inat ion back t o t he source t o und o t he d amage
d one by t he XOR.
On t he 68000, byt es are numbered in increasing ord er from left t o
r ight , while bit s go t he ot her way. Thus if you want t o conver t a bit
number t o a byt e ad d ress and a bit , you need t o map t he bit number
from 0..7 t o 7..0. But if your source and d est inat ion bit maps have bot h
t heir left and r ight ed ges at byt e bound ar ies, you can skip t his map-
ping. A full-screen d issolve, for example, will t ypically be able t o d o
t his.
Fur t her Resear ch Fur t her Resear ch Fur t her Resear ch Fur t her Resear ch Fur t her Resear ch
Ive d one t his t echnique somet hing of a d isser vice by implying it s good
only for copying an image over anot her. In gener al, t his is a way t o
232
IV.3 A DIGITAL DISOLVE EFFECT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 232
t r aver se a 2D ar r ay of near ly any size in pseud o-r and om ord er. (Or
st icking wit h a 1D sequence, it ' s t he wor ld ' s fast est coin-flip r and omizer.)
Some examples of ot her uses and possible var iat ions are as follows:
Given t hat t he or iginal mot ivat ion for t he sequence gener at or comes
from a har d ware circuit , why not d evelop vid eo-hard ware suppor t
for a d issolve?
Because t he sequence gener at or s st at e can be d escr ibed wit h a single
nu mber, it ' s simp le t o st ar t and st op t he sequ ence at any p oint ,
allowing par t ial d issolvessor t of a d it hered blend ing.
Gr aphical pr imit ives besid es copying could be suppor t ed pixel could
be ORed , ANDed , XORed , and so on. Or a repeat ing pat t er n could be
used for t he source inst ead of a source bit map.
Inver t ing t he d est inat ion image (XORing for a black p at t er n) is
especially int erest ing because it ' s an inver t ible process. Combining
t his wit h a par t ial d issolve yield s ver y int erest ing result s: t r aver sing t he
fir st 50% of t he pixels will yield a screen full of gr ay noise. Re-t r aver s-
ing t hose pixels will br ing back t he image.
When images cant be p laced r ap id ly on t he scr een, su ch as in
calculat ing images of t he Mand elbrot set , t he pixels can be and d r awn
pseud o-r and omly, t o present an approximat e image as soon as possible.
Th i s i s a l so u sefu l wh en t r a n smi t t i n g a n i ma ge over a sl ow
communicat ions link.
Suggest ions and comment s for ot her applicat ions are welcome!
See also Gener at ing Rand om Int eger s (438); Circles of Int egr al
Rad ius on Int eger Lat ices (57); Precalculat ing Ad d resses for
Fast Fills, Circles, and Lines (295)
See Append ix 2 for C Implement at ion (715)
233
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 233
IV.4 IV.4 IV.4 IV.4 IV.4
M MM MMAPPING APPING APPING APPING APPING
R RR RRGB TRIPLES ONTO GB TRIPLES ONTO GB TRIPLES ONTO GB TRIPLES ONTO GB TRIPLES ONTO
F FF FFOUR BITS OUR BITS OUR BITS OUR BITS OUR BITS
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
We d escr ibe a met hod for t he efcient map p ing of ar bit r ar y t r ip les
(t yp ically RGB color valu es) ont o a red u ced set of 14 t r ip les. The
int end ed use is for pixel red uct ion for use on four-bit fr ame buffer s,
t hough t he t echnique may be used gener ally on any t hree-element vect or.
Backgr ound Backgr ound Backgr ound Backgr ound Backgr ound
Many fr ame buffer s provid e t he choice of d isplayable color s by way of
color look-up t able (LUT) hard ware. How are represent at ive color s cho-
sen? Fur t her, how can ar bit r ar y input point s (high-precision color pixels)
be efcient ly mapped t o a nearest select point wit hin t his color cube? On
syst ems provid ing a large color palet t e (256 or more) a specic set of
color choices may be allocat ed based on t he nat ure of t he input d at a. A
quant izat ion met hod using t his approach is well-known (Heckber t , 1982).
On syst ems provid ing t he minimal number of color s (eight ) t he choice is
clear : t he ext reme point s of t he int er val [0.0..1.0], must be t aken sepa-
r at ely on each of t he RGB axes.
This Car t esian prod uct yield s t he point set [1/ 2 1/ 2, 1/ 2 1/ 2,
1/ 2 1/ 2] of pr imar y and second ar y color s, which coincid es wit h t he
ver t ices of t he unit color cube (see Fig. 1). Thus, wit h small look-up
t ables t he choice of represent at ive color s may be mad e a prioriwit hout
regard t o t he nat ure of t he input d at a being mapped . The accompanying
soft ware proced ure is st r aight for ward (see Fig. 2). This fr agment locat es
234
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 234
Figure 1. Eight -point color cube.
t hat ver t ex t r iple [R2, G2, B2] closest t o t he input t r iple [R, G, B] and
ad d it ionally r et u r ns a cod e (t he ver t ex nu mber ), which ser ves as a
color-map ind ex.
Unfor t u nat ely, in t he wor ld of compu t er logic most low-end fr ame
buffer s provid e a palet t e of 16 represent able color s based on an und er ly-
ing four-bit d at a pat h. Because any xed color t able necessar ily cont ains
t he eight ver t ices of t he bound ing cube, a four-bit ind ex leaves half of t he
color s unassigned . This suggest s t hat a preselect ed set of suit able values
might be chosen. An essent ial feat ure of a red uced color set is a d ist r ibu-
t ion t hat shows lit t le bias and high unifor mit y, while lend ing it self t o an
efcient nearest neighbor t est . As t he number of pot ent ial color s will
i nteger functi on remap8(real : R, G, B, R, G, B)
begin
i nteger code:
R G B cod e 0;
i f (R 0.5) then begi n R 1.0; cod e cod e bi t-or 1; end
i f (G 0.5) then begi n G 1.0; cod e cod e bi t-or 2; end
i f (B 0.5) then begi n B 1.0; cod e cod e bi t-or 4; end
return[cod e];
end
Figure 2. RGB ont o RGB (t hree-bit ).
235
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 235
d ouble, an algor it hm r unning in not more t han t wice t he t ime of remap8
is sought , wit h a sublinear (or logar it hmic) increase in t ime id eal. Given
t he simplicit y of remap8, a br ut e-force linear search for nearest neighbor
is ruled out .
A Four - Bi t Col or Sol i d A Four - Bi t Col or Sol i d A Four - Bi t Col or Sol i d A Four - Bi t Col or Sol i d A Four - Bi t Col or Sol i d
A highly symmet r ic set of t arget color s is now creat ed , making for a
unifor m select ion set . Consid er a unit cube augment ed by allocat ing new
ver t ices at t he cent er of each of it s six faces. This ext end s t he color set t o
four t een color s in a unifor m manner, owing t o t he high symmet r y of t he
cube. Enumer at ing t he locat ion of ver t ices yield s t he t arget color space.
The new color s includ e t hree d esat ur at ed pr imar ies and t hree second ar ies
of red uced int ensit y. These are t abulat ed in Fig. 3. Not e t hat t he mid point
of t he cube (50% gr ay) d oes not appear.
To for m a unifor m solid wit h t hese color ver t ices, ad d ed ges from each
face cent er t o t he fou r face ver t ices t hat bou nd it . This facet s each
Figure 3. The four t een t arget color s.
236
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 236
Figur e 4. The rhombic d od ecahed ron.
square face int o four equilat er al r ight t r iangles. Next , ext end t he new
ver t ices out ward fr om t he cube cent er so t hat all 14 ver t ices are equid is-
t ant from t his cent er. (This changes t he not ion of color cube int o color
sphere, but t his d iscrepancy will be resolved lat er.) By symmet r y t he t wo
t r iangles joining at each cube ed ge lie in t he same plane and t he ed ge
belonging t o t he or iginal cube may be removed . A rhombus is left (a
par allelogr am wit h all four ed ges congruent ). Repeat ing for each of 12
ed ges in t he und er lying cube t hus yield s t he rhombic d od ecahed ron. This
solid is well-known in lat t ice t heor y: it is t he only Archimed ean d ual
t hat can pack t hree-space, mad e clear by consid er ing it t he union of a
whit e cube wit h six black pyr amid al apexes t hat pack in a checker board -
ing fashion. As wit h ot her Archimed ean d uals all faces are congruent and
all d ihed r al angles equalproper t ies t hat make for fair d ice. (Since six
mat ching solid s pack t o ll space by t he above, t his d ihed r al is / 3
r ad ians or 120 d egrees.) The polyhed ron appear s in Fig. 4.
Dual Sol i ds Dual Sol i ds Dual Sol i ds Dual Sol i ds Dual Sol i ds
Now consid er t he problem of nd ing t he nearest ver t ex (t arget color ) on
t his solid . For a t est point lying along an ed ge of t he solid t here are
merely t wo nearest ver t ices t o be consid ered . The d ecision point for
ver t ex choice occur s at t he mid point of t hat ed ge. This may be ext end ed
t o nd t he region of space cont aining all t est point s nearest one ver t ex.
237
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 237
Figur e 5. The cuboct ahed ron.
By t aking t he mid point s of t he t hree or four ed ges common t o some
ver t ex, a cut t ing plane may be d ened in which all nearest point s lie on
one sid e. By replacing each of t he 14 ver t ices wit h it s relat ed plane t he
solid becomes t r uncat ed . In t his case, t he process of t r uncat ion yield s t he
d ual solid (see Fig. 5).
As d ualit y occur s in symmet r ic pair s, t he or iginal Archimed ean solid a
cuboct ahedronis creat ed . It is semiregular : not among Plat o' s canoni-
cal ve because alt hough t he faces are all regular n-gons in symmet r ical
ar r angement , t he faces are not of id ent ical t ype. This is anot her solid
shar ing t he symmet r y group of t he d ual oct ahed ron-cube family. It can be
for med from eit her of t hese parent solid s by for ming t he convex hull of
all ed ge mid p oint s. (That it may also be for med from t he r hombic
d od ecahed ron in t his fashion at t est s t o how close t he lat t er comes t o
being among t he select ve. It s four-d imensional analog, a cell of 24
oct ahed r a, is regular. In higher d imensions it vanishes t oget her wit h t he
t eapot and all ot her int erest ing r egular solid s, leaving merely hyper t et r a-
hed r a, cubes, and t heir d uals (Coexet er, 1948).) The cuboct ahed ron has
six square and eight t r iangu lar faces, showing it s lineage from bot h
represent at ive parent s.
Pr oxi mi t y Test i ng Pr oxi mi t y Test i ng Pr oxi mi t y Test i ng Pr oxi mi t y Test i ng Pr oxi mi t y Test i ng
The proper t ies of t his solid for m t he crux of our algor it hm. As wit h
t wo-d imensional Voronai d iagr ams and t heir d ualscommon t o problems
238
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 238
concer ning nearest point t o a set of given point st he or iginal ques-
t ion of nearest neighbor ing point t o a t est point has been d ualized . The
problem now is nd ing t hat face cont aining (or nearest t o) a given point .
For t unat ely, t his t est can be mad e quickly by t aking ad vant age of t he
quasi-regular nat ure of t he regular cuboct ahed ron. An at t r ibut e of such
solid s is t hat select , closed , ed ge circuit s not d ening faces st ill lie in a
common plane. This proper t y is r are (t he oct ahed ron shares it , t hough
not t he cu be) and makes for geod esic d ome t ent s, which are easily
assembled as each ed ge circuit becomes one long br ace. In t his case t he
faces are hexagons. They may be d er ived d irect ly by slicing a cube along
a plane per pend icular t o one of it s bod y d iagonals, t hereby exposing a
face along an axis of t hree-fold rot at ional symmet r y. As seen in Fig. 6a,
t his nicely reveals t he color wheel of pr imar ies and second ar ies implicit in
t he color cube.
By t aking all four bod y d iagonals and mat ching hexagons, an empt y
cuboct ahed ron t er med a nolid (Hold en, 1971) appear s as seen in Fig.
6b. The bound ed ind ent at ions (occlusions in t he shape of regular t et r ahe-
d r a and half-oct ahed r al pyr amid s) par t it ion space int o 14 d ist inct vol-
umes. All point s wit hin a par t it ion are associat ed wit h a mat ching nearest
ver t ex on t he rhombic d od ecahed ron. Thus, a t est point s spat ial par t it ion
may be id ent ied by per for ming four inequalit y t est s t o locat e t he half-
sp aces in which t he t est p oint lies. Becau se t he p ar t it ions may be
ext end ed out ward t o d ivid e all of 3-space, t he t est r emains valid for
ar bit r ar y point s, making t he algor it hm well-d ened for any input . This is
Figure 6. Hexagonal const ruct ion.
239
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 239
Figure 7. Select polyhed r al solid s.
impor t ant as cer t ain t r iaxial color spaces, such as t he YIQ basis used in
commercial broad cast ing (Limb et al., 1977), can give r ise t o negat ive
values of I and Q for st rongly sat ur at ed color s. This ext ension account s
for t he d epar t ure from a color cube, allud ed t o when placing ver t ices at
t he cube faces, cent er s. For most d at a set s, t he cuboct ahed ron nolid may
be cont ained wit hin a unit cube case.
A summar y of t he solid s encount ered so far appear s in Fig. 7. Not ice
t hat Euler s equat ion F + V = E 2 is sat ised and t hat for d ualit y F
and V commut e, leaving t he r ight -hand t er m unchanged . Dual solid s
possess mat ching ed ge pair s, which are or ient ed t o each ot her at r ight
angles.
Rel at ed Met hods Rel at ed Met hods Rel at ed Met hods Rel at ed Met hods Rel at ed Met hods
Spat ial search met hod s, which d ivid e volumes by means of planar half-
space t est s, have long been applied t o rend er ing algor it hms (Schumaker
et al., 1969), but univer sally employ cond it ional logic (in t he for m of a
t ree t r aver sal) in t he gener at ion of t est planes. The relat ed geomet r ic
met hod of xed , symmet r ic planes t est ed en masse red er ives t he com-
plet e face char act er izat ion of st ellat ed polyhed r a, wherein t he int er sec-
t ion d iagr am bet ween one ar bit r ar y face plane and t he ent ire remaining
set is d r awn. The canonical enumer at ion of solid s t hus gener at ed is an
open problem (t he oct ahed ron yield s 1, t he icosahed ron 59) but gr aphics
t ools useful in t heir visualizat ion exist (McKeown and Bad ler, 1980).
The hexagonal symmet r y of t he cube is explicit in ot her color spaces,
such as HSVHue Sat ur at ion Value(Smit h, 1978), where it is record ed
as t he hue angle. Und er t his mod el, t he six ad d it ional face color s are
represent ed as d esat ur at ed pr imar ies (S = 1

S = 1/ 2) and as secon-
240
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 240
d ar ies of red uced value (V= 1

V = l/ 2), wit h ot her t er ms left un-


changed . In par t icular, hue angles are st ill member s of t he or iginal eight
ver t ex set . Ot her hexagonally symmet r ic spaces includ e HSL (Foley and
van Dam, 1982). A color space similar in t reat ing t he angular d ist r ibut ion
of point s about a gr ay axis is d escr ibed in (Joblove and Greenberg, 1978).
A gener al t reat ment of color spaces as t opological solid s cont aining axes
of opposing color s (wit h relat ed ind exing met hod s) appear s in Tur kowski
(1986).
Hal f - Space Test i ng Hal f - Space Test i ng Hal f - Space Test i ng Hal f - Space Test i ng Hal f - Space Test i ng
Because t he half-space t est s regard merely t he sign and not t he absolut e
per pend icular d ist ance bet ween point and plane, t he nor malized coef-
cient s in t he plane equat ion may be scaled ar bit r ar ily. The goal is t o
remove ir r at ional values or r at ional fr act ions, t hus yield ing ar it hmet ically
efcient t est s. The plane equat ions t hat d ene t he half-spaces are most
easily d er ived by simple applicat ion of Pyt hagor as t heorem. The gr ay
axis bod y d iagonal shown in Figure 6a has end point s at [0, 0, 0] and
[1, 1, 1]. Thus, for any t est point (x, y, z) a boolean t est may be d er ived ,
which repor t s t he nearer end point using a d ist ance inequalit y. For t est
point s nearer t he whit e point t he following are t r ue:

x 0 ( )
2
+ y 0
( )
2
+ z 0 ( )
2
> x 1 ( )
2
+ y 1
( )
2
+ z 1 ( )
2

x
2
+ y
2
+ z
2
> x 1 ( )
2
+ y 1
( )
2
+ z 1 ( )
2

2 x + y + z
( )
> 3

x + y + z
( )
> 1. 5
This follows int uit ion, since x, y, and z cont r ibut e equally in symmet -
r ic fashion as t he whit e point [1, 1, 1] is approached from t he or igin, and
t he point of ind ecision is locat ed at [.5, .5, .5]. For equat ions t hat t est
along t he remaining t hree bod y d iagonals, int uit ion is less helpful but t he
above approach remains useful in eliminat ing quad r at ic and r ad ical t er ms.

0 > 2x + 1 2y + 1 2z + 1
241
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 241
The remaining d iagonal end point s are t he cyclic coord inat e per mut at ions
of t he line [(1, 0, 0);(0, 1, 1)], which yield t hree boolean t est s of t he for m

x 1 ( )
2
+ y 0
( )
2
+ z 0 ( )
2
> x 0 ( )
2
+ y 1
( )
2
+ z 1 ( )
2

x 1 ( )
2
+ y
2
+ z
2
> x
2
+ y 1
( )
2
+ z 1 ( )
2

x + y + z > 0. 5
Al gor i t hm Desi gn Al gor i t hm Desi gn Al gor i t hm Desi gn Al gor i t hm Desi gn Al gor i t hm Desi gn
A four-bit cod e word may be for med d irect ly from t he signed t est s by
ORing successive cod e bit s int o a cod e word in a manner analogous t o
Sut her land s viewpoint clipping (Newman and Sproull, 1979). Bit signs
and posit ions wit hin t he cod e word may be complement ed and per mut ed
ar bit r ar ily, yield ing 2
4


4! or 384 d ist inct cod ings, cor r esp ond ing t o
rot at ions and inver sions of t he solid . An impor t ant proper t y of all cod ings
is t hat any color s complement may be select ed by complement ing (or
subt r act ing int o 17) in t hat color s cod e word . Put anot her way, given a
color C and it s complement C, t hen Cod e [C] + Cod e[C] = 1111 and
Red [C] + Red [C] = 1.0, and so on. To nd a canonical represent at ion
of choice a boolean t est polar it y is chosen, which assigns black and whit e
t he cod es 0000 and 1111, respect ively. The t wo vacant cod e posit ions
are of t he per mut ed for m 0001 and 1110 and relat e t o t he fact t hat only
seven (and not eight ) d ist inct par t it ions appear in any hemisphere. Per -
mut at ion of t he d issimilar bit yield s vacant cod e word s at t he consecut ive
mid -t able locat ions 0111 and 1000, su ggest ing t heir p ot ent ial u se in
record ing t wo mid -level gr ayscale values. At t his point t he t hree pr imar y
color s occur wit hin t he r st eight ent r ies of t he t able. A nal per mut at ion
allows t hem t o appear in RGB ord er. The pr imar ies are necessar ily not
consecut ive because cod e word s d iffer ing in only one bit posit ion (as wit h
successive even and od d int eger s) represent neighbor ing par t it ions, yet
t he pr imar ies are nonad jacent . Thus, 14 of 16 possible cod e word s are

2x + 1 > 2y + 1 2z + 1

2x + 2y + 2z > 1
242
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 242
V
r
, V
g
, V
b
: array [0..15] of real ;
V
r

[0.,.5 ,.5, 1.,.0, 0., 0.,.5 ,.5, 1., 1., 1., 0.,.5 ,.5 ,1.];
V
g

[0.,.5, 0., 0.,.5, 1., 0.,.5 ,.5, 1., 0.,.5, 1., 1.,.5 ,1.];
V
b

[0., 0.,.5, 0.,.5, 0., 1.,.5 ,.5, 0., 1.,.5, 1.,.5, 1.,1.];
map t he oat ing t riple (R, G, B)

(R, G, B) t hereby quant izing


it ; t he ret urn value is t he vert ex code/ colormap index
i nteger functi on remapl4(real: R, G, B, R, G, B)
begi n
cod e: i nteger

0;
i f R + G + B > 1.5 then cod e

cod e bi t-or 8;
i f R + G + B > 0.5 then cod e

cod e bi t-or 4;
i f R G + B > 0.5 then cod e

cod e bi t-or 2;
i f R + G B > 0.5 then cod e

cod e bi t-or 1;
R

V
r
[cod e];
G

V
g
[cod e];
B

V
b
[cod e];
return[cod e];
end
Figure 8. RGB ont o RGB (four-bit ).
employed . Absence (and pot ent ial t reat ment ) of t he remaining t wo t able
ent r ies is d iscussed und er t he sect ion gr ay int er ior point s. At t his point
a p r act ical, st and ar d ized map p ing of RGB ont o a sp ace of fou r-bit
precision has been d ened : t he or iginal goal. The soft ware proced ure t hat
gener at es a cod e word and companion t arget ver t ex value appear s in
Fig. 8.
The funct ion remap14 is robust in t hat t he vect or

. 49, . 49. 49
[ ]
maps
ont o [0, 0, 0] and t he vect or [.5, .5, .5] maps ont o [1, 1, 1]. In gener al, any
point [t , t , t ] along t he achromat ic axis (as wit h gr ay-level input d at a for
d isplay on a color monit or ) can gener at e only t he black or whit e point s as
represent at ive out put . No mult iplicat ions t ake place in t he rout ine, which
makes for read y ad apt at ion t o scaled -int eger values. For t he common
case of (unsigned ) eight -bit color component s, care must be t aken be-
cause bot h t he scaled value 1.5 and int er med iat e var iable expressions
may exceed t he maximum unsigned byt e value

2
8
1 255.
The fu nct ion remapl4 ret u r ns t he cod e wor d (u sefu l for t he act u al
color map value given t o t he fr ame buffer memor y) and lls t he t rue color
in var iables R, G, B, of use as when halft oning by er ror d iffusion (Floyd
243
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 243
and St einberg, 1975). Because t he d ecod ing t ables cont ain only t hree
d ist inct values it is t empt ing t o infer t able element s implicit ly t hrough
ad d it ional boolean logic. However, d irect look-u p is bot h fast er and
essent ial for use wit h color spaces not possessing six-fold symmet r y
about t he achromat ic axis (as wit h RGB and HSV). As an ad d it ional
benet , t he use of a t able allows t he precomput at ion of 14 color d escr ip-
t or s in a chosen space of represent at ion based on t he RGB values list ed in
Fig. 3. An implement at ion accommod at ing RGB input pixels of ar bit r ar y
precision is par t of t he IM Rast er Toolkit mad e available t hrough t he
Univer sit y of Wat er loo (Paet h, 1986a, 1986b, 1987).
Thr ee ver sus Four Bi t s Thr ee ver sus Four Bi t s Thr ee ver sus Four Bi t s Thr ee ver sus Four Bi t s Thr ee ver sus Four Bi t s
By way of compar ison, t he t hree-bit map p ing case is seen t o be of
id ent ical for m wit h t he above, except t hat a t able look-up st ep is not
need ed t o remap t he cod e word int o a represent at ive set of values. This
happens because in t he t hree-bit case t he half-space t est s d ivid e t he t hree
Car t esian axes allowing each bit in t he cod e word t o gener at e an associ-
at ed axis value d irect ly (Fig. 9).
This illust r at ion makes it clear t hat t he oft en progr ammed t hree-bit
Car t esian case (most oft en concept ualized as a t hree-channel quant izat ion
oper at ion) is of id ent ical for m t o t he four-bit polyhed r al algor it hm d e-
scr ibed it is t he minimal case common t o bot h approaches. The major
d ifference in t he met hod s regard s t he or ient at ion of t he t est planes.
Whereas t he t hree-bit ver sion uses t he cubes t hree planes of mir ror
Figur e 9. Cube as Car t esian and polyhed r al mod el.
244
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 244
symmet r y, t he fou r-bit case u ses t he cu bes fou r axes of t hree-fold
rot at ional symmet r y.
Compl exi t y Anal ysi s Compl exi t y Anal ysi s Compl exi t y Anal ysi s Compl exi t y Anal ysi s Compl exi t y Anal ysi s
The t hree- and four-bit cases are comput at ionally opt imal in t hat t he
number of plane t est s T for ver t ices V mat ches t he infor mat ion t heoret ic
lower bound T = [log
2
V] exact ly. They are opt imal in an implement at ion
sense because t hey are d evoid of any cond it ional logic. That is, result s of
previous plane t est s d o not d r ive subsequent d ecisions. This complet e
d ecoupling implies t hat t he half-space t est ing (progr am i f st at ement s)
may be execut ed in par allel on hard ware allowing concur rent comput a-
t ion. Each t est cont r ibut es 0.25 log
2
14 bit s of infor mat ion, or 0.952,
yield ing an infor mat ion cont ent of 3.81 bit s from t he four boolean t est s.
Gr ay Int er i or Poi nt s Gr ay Int er i or Poi nt s Gr ay Int er i or Poi nt s Gr ay Int er i or Poi nt s Gr ay Int er i or Poi nt s
As not ed , t he gr ay point is not represent ed in t he choice of color s and
cannot appear und er t his mod el. In fact , t he point [.5, .5, .5] is t he
common int er sect ion of t he t est planes for bot h progr am rout ines. Be-
cause t he t arget color ver t ices are creat ed on t he sur face of a color
sphere t hat circumscr ibes ou r polyhed ron, t here is no provision for
allocat ing int er ior point s. This is a major obst acle in gener alizing t his
met hod . Explicit met hod s for int er ior t est ing are expensive. For inst ance,
id ent ifying t he cent r al gr ay subcube requires six inequalit y t est s. Red uc-
t ion t o four by employing a bound ing t et r ahed ron st ill d oubles t he t ot al
number of plane t est s and complicat es t he cod e. Met hod s of ext ension t o
our color polyhed r a approach will appear in a for t hcoming paper (Paet h,
in press).
Car t esi an Quant i zat i on ver sus Pol yhedr a Car t esi an Quant i zat i on ver sus Pol yhedr a Car t esi an Quant i zat i on ver sus Pol yhedr a Car t esi an Quant i zat i on ver sus Pol yhedr a Car t esi an Quant i zat i on ver sus Pol yhedr a
For a 16-ent r y color t able t he lack of gr ay is not a major liabilit y: a
compet it ive 2 3 2 Car t esian t able (weight ed in t he green) cannot
place any int er ior point s, as at least one axis (t he red and blue compo-
nent s) can represent only t he ext rema of t heir int er val. Moreover, only 12
point s are creat ed .
On fr ame buffer s provid ing ad d it ional precision t he Car t esian approach
becomes d esir able. For inst ance, on a hypot het ical ve-bit d evice (32
245
IV.4 MAPPING RGB TRIPLES ONTO FOUR BITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 245
col or s) an excel l en t ch oi ce of t abl e al l ocat i on i s a 3 3 3 Car t esi an
col or Rubiks cube, which fills t he t able near ly t o capacit y (84%) and
fur t her places a t arget point (wit h sur round ing cubical volume) at t he
cent r al point (.5, .5, .5). (See Figure 2 in t he Gem Proper Treat ment of
Pixels as Int eger s on page 254.) This approach requires as a wor st -case
six boolean t est s for t he six panes ver sus t he t heoret ical lower bound of
five. The oper at ion may also proceed in par allel (t hree processes wit h
each p er for ming t wo consecu t ive p lane t est s) and t he logic may be
ar r anged cond it ionally so t hat a t est point locat ed above t he higher (or
beneat h t he lower ) t est p lane need not be consid ered fu r t her. This
red uces t he t ot al number of t est s t o t hree for a select cube cor ner (such
as whit e) and lower s t he aver age-case per for mance t o 1.67 t est s per axis,
or t o five over all. As wit h t he t hree-bit case t he t arget out put color
R, G, B may be for med d irect ly on a per-axis basis wit hout resor t t o a
cod e word . For efficiency reasons a 64-ent r y spar se t able is st ill d esir able
t o comput e t he LUT ind ex d efined as I = 9R + 3G + B, t hereby for ming
a Car t esian prod uct while avoid ing int eger mult iplicat ion.
It is not sur pr ising t he eight ver t ices of t he cube are present in all
higher-ord er Car t esian mod els. What is sur pr ising is t hat t he 14 cuboct a-
hed ron ver t ices can be d er ived for m t he 3 3 3 Car t esian cube. This
is possible because t he lat t er s point lat t ice may be regard ed as t he union
of a cubes ver t ices (8), face mid point s (6), ed ge mid point s (12) and
cent r al point (1), t ot aling 27. Here t he cuboct ahed ron ver t ices are for med
by t he union of only t he fir st t wo spat ial symmet r y groups.
Summar y Summar y Summar y Summar y Summar y
An opt imally efficient t est ing met hod for mapping RGB color s ont o a
red uced palet t e of four bit s is d escr ibed . Implement at ion is st r aight for ward
and t he pred efined set of 14 t arget color s is highly symmet r ic. The exposit ion
provides a good over view int o descr ipt ive solid geomet r y and t he symmet r ies
of cubic lat t ices.
See also A Fast HSL-t o-RGB t r ansfor m (448); A Simple Met hod
for Color Quant izat ion: Oct ree Quant izat ion (287)
See Append ix 2 for C Implement at ion (718)
246
IV.5 WHAT ARE THE COORDINATES OF A PIXEL?
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 246
IV.5 IV.5 IV.5 IV.5 IV.5
W WW WWHAT ARE THE HAT ARE THE HAT ARE THE HAT ARE THE HAT ARE THE
C CC CCOORDINATES OF A OORDINATES OF A OORDINATES OF A OORDINATES OF A OORDINATES OF A
P PP PPIXEL IXEL IXEL IXEL IXEL? ?? ??
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
Did you ever get confused d ecid ing whet her pixels were cent ered at
int eger coord inat es or cent ered halfway bet ween t he int eger s? We present
here a consist ent answer t o t his quest ion.
When mod eling, we use real number s, but pixels have int eger coord i-
nat es, so somewhere d ur ing rend er ing we must quant ize t he coord inat es
t o int eger values. How, exact ly, d o we per for m t his mapping? Do we
round or t runcat e? Consist ency is vit al, but making t he bet t er choice is
also impor t ant . This may seem like a pet t y quest ion, but failure t o ad d ress
it can lead t o misalignment , gaps or over lap bet ween object s, or ed ge
effect s at t he screen bor d er. The quest ion is especially impor t ant if we are
ant i-aliasing.
To clar ify t he problem, we d ist inguish bet ween d iscret e images and
cont inuous images, and also bet ween d iscret e coord inat es and cont inuous
coord inat es. A discret e image is an ar r ay of pixels, t he sor t of image
wit h which were familiar in comput er gr aphics and image processing,
and a cont inuous image is a funct ion d ened over a cont inuous d omain,
as in opt ics or t he real wor ld . In comput er gr aphics we t ake a geomet r ic
d escr ipt ion of a cont inuous image (for example, a list of polygons wit h
oat ing point coord inat es) and sample it at a d iscret e ar r ay of point s t o
creat e a d iscret e image. The d iscret e image we rend er is an approxima-
t ion of t he cont inuous image. We call t he coord inat es in t he d iscret e
image discret e coordinat es and t he coord inat es in t he cont inuous image
cont inuous coordinat es. Discr et e coord inat es t ake on int eger valu es at
t he sample point s, which are t he pixel cent er s. The mapping quest ion is
now red uced t o a choice of phase (d isplacement ) bet ween cont inuous and
d iscret e coord inat es.
247
IV.5 WHAT ARE THE COORDINATES OF A PIXEL?
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 247
Figure 1. Round ing and t r uncat ing schemes for coord inat e conver sion.
If we round when conver t ing oat ing point cont inuous coord inat es t o
d iscret e coord inat es, t his is equivalent t o aligning t he cont inuous and
d iscret e axes. Figure 1 shows t he round ing mapping at t op, for a hypo-
t het ical fr ame buffer four pixels on a sid e, wit h pixel cent er s mar ked by
bullet s. Round ing seems at t r act ive at r st , because cont inuous coord i-
nat es and d iscret e coord inat es are equal. Unfor t unat ely, t he cont inuous
r ange cor respond ing t o our hypot het ical fr ame buffer, using round ing, is
t he awkward r ange of .5 t o 3.5.
The bet t er mapping choice is t runcat ion, or more accur at ely, oor ing,
where cont inuous coord inat es are conver t ed t o d iscret e coord inat es by
t aking t he oor funct ion. In t his scheme, t here is a half-pixel phase shift
bet ween cont inuous coord inat es and d iscret e coord inat es as can be seen
in Fig. 1, bot t om. Cont inuous coord inat es t ake on int eger values halfway
bet ween pixels. The pixel wit h d iscret e coord inat es (x, y) has it s cent er
at cont inuous coord inat es (x + 1/ 2, y + 1/ 2). Assuming as a r st ap-
proximat ion t hat we reconst ruct using a one-pixel-wid e box lt er, t he
cont inuous coord inat e d omain of pixel (x, y) is from x t o x + 1 in x and
from y t o y + 1 in y. For our hypot het ical fr ame buffer, t he cont inuous
coord inat e r ange using t r uncat ion is 0 t o 4simpler number s t han wit h
round ing. The simplicit y of t he coord inat e r ange facilit at es image scaling
and ot her t r ansfor mat ions.
In summar y, bot h cont inuous and d iscret e coord inat es have t heir place.
Cont inuous coord inat es are most appropr iat e when mod eling, t hat is,
when one is concer ned wit h geomet r y, not wit h pixels. Discret e coord i-
nat es are most useful when wor king close t o t he pixel level, as in scan
248
IV.5 WHAT ARE THE COORDINATES OF A PIXEL?
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 248
conver sion or image processing. Not e t hat d iscret e coord inat es are not
always int eger s: it is oft en useful t o use oat ing point var iables for
d iscret e coord inat es. When wr it ing gr aphics soft ware it is vit al t o be
conscious of whet her you are using cont inuous coord inat es or d iscret e
coord inat es.
To conver t from cont inuous t o d iscret e or vice ver sa, where c is t he
cont inuous coord inat e and d is t he d iscret e coord inat e,
d = oor (c)
c = d =

1
2
.
I d eveloped t he above d ualist view of pixel coord inat es while wor king
on an image zoom algor it hm at Xerox PARC in 1988. Thanks also t o Alvy
Ray Smit h at Pixar for reinforcing my reverence for t he pixel.
See also A Digit al Dissolve Effect (221); Circles of Int egr al
Rad ius on Int eger Lat t ices (57); Precalculat ing Ad d resses for
Fast Fills, Circles, and Lines (285)
249
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 249
IV.6 IV.6 IV.6 IV.6 IV.6
P PP PPROPER TREATMENT OF ROPER TREATMENT OF ROPER TREATMENT OF ROPER TREATMENT OF ROPER TREATMENT OF
P PP PPIXELS AS INTEGERS IXELS AS INTEGERS IXELS AS INTEGERS IXELS AS INTEGERS IXELS AS INTEGERS
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Over vi ew Over vi ew Over vi ew Over vi ew Over vi ew
Pixels are all-t oo-oft en viewed as collect ions of hard ware bit s, t o be
shift ed , masked , and mapped . This t hree-par t d iscussion illust r at es shor t -
comings of t his concept ual approach and suggest s efcient alt er nat ives,
which give r ise t o more useful for ms.
Pr oper Int er pr et at i on of Pi xel Int eger s Pr oper Int er pr et at i on of Pi xel Int eger s Pr oper Int er pr et at i on of Pi xel Int eger s Pr oper Int er pr et at i on of Pi xel Int eger s Pr oper Int er pr et at i on of Pi xel Int eger s
The int er pret at ion of t he d at a bit s present wit hin any pixel is ar bit r ar y.
Most oft en, t hey are t reat ed as unsigned int eger s used t o specify int ensit y
acr oss a linear d omain, su ch as black

whit e. A u sefu l convent ion


regard s t his int eger as lying along t he closed , unit int er val [0..1]. Choice
of t his int er val is consist ent wit h t he d omains used in var ious color
spaces, includ ing XYZ and LUV space d ened by t he CIE (Commission
Int er nat ional LEclair age) and t he HSV space well-known in comput er
gr aphics (Smit h, 1978). In t he case of color pixels, t hree ind epend ent
axes are represent ed along t he int er val [0.0..1.0].
Unfor t unat ely, many soft ware t ools in exist ence implicit ly ad opt t he
int er val [0..1). This commonly occur s wit hin soft ware t hat employs bit
shift ing as an efcient , rever sible means t o map bet ween pixels wit h
d iffer ing precisions. For inst ance, four-bit pixels on t he r ange [0..15] may
be mapped int o eight -bit pixels on t he r ange [0..255] by left shift ing four
bit s, such t hat 1111 11110000. Right -shift ing reconst ruct s t he or iginal
250
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 250
Figure 1. Pixel int eger s along t he unit axis.
d at a wit hout round off er ror an impor t ant vir t ue. Moreover, und er t his
scheme a p ixel of select valu e (for examp le, 5/ 8 u nd er a t hr ee-bit
precision) may be represent ed exact ly on any syst em of higher precision
(t he value 5/ 8 exist s und er t hree or more bit s of precision). Here an
n-bi t p i xel r ep r esen t a t i on d i vi d es t h e u n i t a xi s i n t o i n t er va l s
of l en gt h 2
n
; con ver t i n g bet ween p r eci si on s m an d n r equ i r es an
(unsigned ) mult iplicat ion of 2
nm
, which is convenient ly represent ed as a
left shift of n m bit s. A major failing wit h t his syst em is t hat whit e
cannot be represent ed (see Fig. 1a). To t ake t he common, wor st case
occur r ing wit h t wo-level bit maps, mapping one-bit pixels ont o eight bit s
yield s t he values 0.0 and 0.5, as t he binar y value .1 remains .100 when
zeros are inser t ed from t he r ight .
The proper approach subst it ut es t he d ivisor 2
n
1 for t he par t it ioning
of t he unit axis (see Fig. 1 b). The ad opt ion of t his mod el yield s a
symmet r ic represent at ion int er val [0..1] t hat is closed und er t he oper a-
t ions complement at ion (wit h

x = 1 x) and mult iplicat ion. This yield s a
number of benet s, not ably t he proper represent at ion of t he whit e point
at 1.0. As an example, wit h n = 8, black and whit e in t his syst em are
0/ 255 and 255/ 255, r esp ect ively, and not 0/ 256 and 255/ 256. Not e
t hat binar y (one-bit ) d at a u nd er t his scheme r ep resent 0.0 and 1.0
exact ly.
Ad opt ion of t his syst em means replacing bit shift s (mult iplicat ions by a
value 2
m
) by gener al mult iplicat ion and d ivision. This is not a severe
speed penalt y. In pr act ice, a scaling t able can be const ru ct ed and a
look-up oper at ion used t o nd t he appropr iat e mapped value. As wit h t he
previous met hod , mapping t o a syst em of higher bit precision and back t o
t he or iginal syst em int rod uces no round off er ror.
incor rect
(a)
incor rect
(b)
251
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 251
Exact r ep r esent at ion u nd er t he higher syst em is more d ifcu lt t o
achieve. When scaling up t o a syst em of exact ly t wice t he number of bit s,
it is guar ant eed . Here t he higher syst em 2
2n
1 can be fact ored as
(2
n
1)(2
n
+ 1). The left -hand fact or is t he lower syst em, and t he r ight -
hand fact or is simply an int egr al scale value. For inst ance, n = 4 bit d at a
represent s int ensit y values spaced by 1/ 15. When scaling t o n = 8 bit
precision, 2
n
+ 1 = 17, so scaling is by 17. Thus, whit e
4
(15) becomes
whit e
8
(255) because 255 = 15 17. Similar ly, t he mapping bet ween
pixel ind ices of t woand four-bit precision (as appear ing in Fig. lb)
merely requires mult iplying or d ivid ing by ve.
In gener al, exact represent at ion of all pixels und er a lower syst em of m
bit s is possible in a higher n-bit syst em whenever n is a mult iple of m.
That is, 2
m
1 d ivid es 2
n
1 iff m d ivid es n, meaning t hat whit e in t he
lower syst em must d ivid e whit e in t he higher syst em. To illust r at e t his
asser t ion r at her t han prove it r igorously, consid er t his: since 4 is a fact or
of 1 2, we asser t t hat four-bit d at a has an exact represent at ion in a
t welve-bit syst em. Represent ing t he fact or s 2
12
1 and 2
4
1 in binar y,
111111111111 can be d i vi d ed by 1111 gi vi n g 000100010001, or 273.
Thus,

4095 15 273
, and t he represent at ion for whit e is st ill exact .
More gener ally, mult iplying any value in t he four-bit syst em by 273 yield s
exact represent at ion in t he t welve-bit syst em.
Nonl i near Pi xel Mappi ngs Nonl i near Pi xel Mappi ngs Nonl i near Pi xel Mappi ngs Nonl i near Pi xel Mappi ngs Nonl i near Pi xel Mappi ngs
Nonlinear subd ivision along an axis of represent at ion is a wor t hwhile
d epar t ure from t he above r st -ord er mod el. For inst ance, use of logar it h-
mic encod ing of pixels record s int ensit ies as opt ical d ensit y; t his is a
common pr act ice in phot omet r y. Here P = log
b
I where P is t he en-
cod ed pixel value, I is t he int ensit y it represent s, and b = 0.1 is t he
chosen base. It follows d irect ly t hat t he inver se map is I = b
P.
The lat t er
funct ion may be mad e implicit in fr ame buffer look-up t ables, allowing
log-encod ed pixel d at a t o be st ored int er nally and viewed d irect ly. This
ap p r oach h as a n u mber of ben et s. Fi r st , becau se l og(a) + l og(b)
+


+l og(n) = l og

a b n ( )
, su mmi n g i ma ges i n en cod ed
for m yield s t he consecut ive prod uct of t heir const it uent linear int ensit ies,
also in encod ed for m. Composit ion in t his fashion mod els t he physical
252
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 252
super imposit ioning of lm t r ansparencies, in which t ot al t r ansmit t ed light
is comput ed by mult iplying by t he t r ansparency of each successive med ia
(Beer s Law). Second , t he ad jacent int ensit ies represent ed by d iscret ized
pixels P and P + 1 d iffer by a const ant r at io, because b
(P + 1)
/ b
(P)
= b
for any P. This means t hat quant izat ion effect s (visible st eps in br ight -
ness) are not biased t oward any par t of t he d ynamic int ensit y r ange. The
approach also has d r awbacks. Input pixels of I = 0 (no light or full
opacit y) cannot be encod ed und er any base, as log 0 . Also, sum-
ming t wo int ensit ies I
1
and I
2
linear ly mu st be comp u t ed by r st
ret u r ning t o t he linear d omain and t hen re-encod ing: log
b
(I
1
+ I
2
) =
l og
b

b
log
b
I
1
+ b
log
b
I
2
( )
. In gen er a l , comp l ex op er a t i on s on t h e en cod ed
d omain will require t hese t wo recod ing st eps.
Usefu l alt er nat ives exist , which resemble log x and which feat u r e
red uced quant izat ion noise while lacking t he lat t er s d iscont inuit y at zero.
Shift ing and scaling t he inp u t d omain gives t he for m log
b
l + x in
which is an ar bit r ar y scaling par amet er. This funct ion maps zero ont o
zero for any base; a unique choice of base fully const r ains t he funct ion so
F: [0..1] [0..1], a valu able p rop er t y. The cor rect choice is F

(x) =
log(1 + x)/ log(1 + ), found by applying t he id ent it ies log
l +
(1 + )
= 1 and log
l+
(x) = log
n
(x)/ log
n
(1 + ). Here t he log is of ar bit r ar y
base; t he d enominat or becomes a suit ably scaled const ant . The par am-
et er may be t uned t o set t he slope at zero:

F

(0) = / ln(1 + ). In
pr act ice, = 255 is a good choice and closely approximat es t he piece-
wise-linear 255 law encod ing used in d igit al t elephony t hroughout
Nor t h Amer ica. The lat t er maps voice d at a quant ized at 12-bit s precision
ont o eight bit s for long d ist ance t r ansmission wit h increased channel
capacit y, and minimizes quant izat ion effect s ot her wise present at low
volumes.
A similar approach has been used successfully by t he aut hor for t he
accur at e offset -press reprod uct ion of d igit al color imager y t o be includ ed
in ACM TOG jour nals (July, 1987). Here t he or iginal pixels of 1 2-bit linear
precision showed mar ked st ep cont our ing at low levels when linear ly
cod ed at eight -bit precision, because pixel values d iffer ing by one unit in
ad jacent d ar k regions showed a d ist r act ing int ensit y st ep of roughly 6%.
A cust om d isplay t ool suppor t ing bot h 255 law input encod ing plus CRT
gamma cor rect ion red uced t hese effect s t o almost impercept ible levels
and simu lt aneou sly t t ed t he high-p recision linear d at a int o a lm
record er, which suppor t ed only eight -bit pixel channels. The equat ions
used t o encod e pixels und er 255 law appear below; The four t h equa-
253
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 253
The four t h equat ion was inver t ed t o creat e t he cor rect look-up t able
(LUT) ent r ies. This oper at ion prefaces t he cust omar y inver se t r ansfor ma-
t ion used t o linear ize film/ CRT response using t he gamma mod el. The
for ward t r ansfor mat ion of t he lat t er was d er ived empir ically from d ensi-
t omet r y of film samples giving I = V

wit h
syst em
= 2.8, in which I is
illuminat ed film int ensit y and V is CRT d r ive volt age (equivalent t o LUT
ent r ies on syst ems wit h linear DAC, t hat is, Digit al t o Analog Conver t er s).
The film record er provid ed 2
8
= 256 LUT ent r ies wit h values on t he
r ange [0..1023] specifyng linear d r ive volt age in 10-bit precision. Her e is
t he pseud o-cod e used t o ll t he LUT:
lut : array [0..255] of i nteger;
for i 0 to 255 do
: real 2.8; empirically derived
t : real

1
255

_
,
2
8i
255
1

_
,
lut [i] round (1023

t
1

)
endl oop
t ion was used t o read y d at a for t he cust om d isplay t ool d escr ibed .

y =
1
8
log
2
1 + 255x ( )

M: [0. 0..1. 0] [0. 0..1. 0]

y =
sign(x)
8
log
2
1 + 255 x
( )

M: [1. 0..1. 0] [1. 0..1. 0]

y =
255
8
log
2
1 + x ( )

M: [0.. 255] [0.. 255]

y =
255
8
log
2
1 +
17
273
x

_
,

M: [0.. 4095] [0.. 255]
254
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 254
Col or Pi xel s and Car t esi an Pr oduct s Col or Pi xel s and Car t esi an Pr oduct s Col or Pi xel s and Car t esi an Pr oduct s Col or Pi xel s and Car t esi an Pr oduct s Col or Pi xel s and Car t esi an Pr oduct s
When a single pixel is used t o represent a RGB color, most oft en t hree
int eger values are d er ived , which for m t he color pr imar ies d irect ly. The
quick and d ir t y approach t o unifor m d ivision of t he color space (so t hat
R, G, and B can be t reat ed separ at ely) ver y oft en slices up eight -bit
pixels int o t hree bit s for red and green, and t wo bit s for blue (t he pr imar y
for which t he eye is least sensit ive).
This is an unnecessar y over simplicat ion t hat leaves blue wit h only t wo
mid -r ange int ensit ies, which suggest s it self when color is regard ed as
bit s at t he hard war e level, not as N d iscret e int ensit y st eps. This
approach is fur t her suggest ed because t he pixel channels for R, G, and B
can be wr it t en ind ivid ually t hrough t he proper set t ing of t he wr it e mask,
but in pr act ice, t he color-mapped pixel is nor mally wr it t en as a single
byt e, not as t hree consecut ive inking passes. One useful proper t y of
t his power-of-t wo approach t o color axis allocat ion is t hat a number of
gr ay values are always present (t hat is, R = G = B) because t he channel
precisions are all mult iples of each ot her.
A bet t er ap p r oach is t o for m a color t able cont aining a Car t esian
prod uct of int ensit ies by using axis d ivision t hat is not necessar ily a
power of t wo. For example, t aking red = [0..5], green = [0..7], blue =
[0..4] (each suit ably nor malized t o repr esent int ensit ies on t he r ange
[0.0..1.0]) yield s a color t able of 6 8 5 = 240 ent r ies. Compared t o
t he RRRGGGBB bit -allocat ion ap p roach, t he blu e axis shows a 50%
increase in mid -r ange specicat ion.
Figur e 2. Car t esian color cubes.
255
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 255
Gener al ad opt ion of t his met hod allows for more efcient use of t he
color map. For inst ance, consid er t he red uced scenar io of 12 color s (as
on a four-bit syst em in which four color s have been previously d ed icat ed
t o t he oper at ing syst em). Here t he bit plane scheme must allocat e one bit
t o each of t he t hree channels, t ot aling eight color s and leaving four
unused . A bet t er t reat ment would be t o for m t he prod uct 2 3 2 wit h
t he axis of higher precision given t o t he green channel. Similar ly, on a
2
5
= 32 bit color syst em t he allocat ion scheme RRGGB provid es only
on/ off values in blue (see Fig. 2a). A bet t er allocat ion is a 3 3 3
volume, which ad d it ionally allows represent at ion of mid -level gr ay (see
Fig. 2b).
The lat t er approach is t aken in t he IM r ast er t ool (Paet h, 1986a, 1986b,
1987), which provid es X-wind ow d isplay of les cont aining color infor-
mat ion at ar bit r ar y bit p recision. Her e t he creat ion of a syst emat ic,
unifor m color space is essent ial in provid ing a shared color map common
t o all wind ows on t he machine. As t he hard war e plat for m suppor t s eit her
four- or eight -bit color ind ices, conver sion of input pixel precision ont o
t he r ange [0.0..1.0] t akes place as d escr ibed in t he r st par t of t his Gem.
At r u n-t ime, t he low-level allocat ion rou t ine is requ est ed t o bu ild a
prod uct of evenly d ist r ibut ed fact or s whose prod uct is no larger t han
27/ 32 84.4% of t he hard ware color t able size. In pr act ice, four- and
eight -bit d isplay archit ect ures call t he color allocat or wit h values 13 or
216 respect ively, yield ing t he fact or s 12 = (2 3 2) and 216 = (6 6 6) wit h
channel allocat ion d one in GRB ord er. Appear ing in Fig. 3, t he cod e has
split N int o t hree (near) ident ical values R, G, and B
such t hat N R G B and G R B
Max Med Min 0;
whi l e Min Min Min N do Min Min 1 + 1;
Min Min 1;
whi l e Med Med Min N do Med Med 1 + 1;
Med Med 1;
Max n/ (Min Med );
G Max;
R Med ;
B Min;
Figure 3. Fact or ing int o a Car t esian t r iple.
256
IV.6 PROPER TREATMENT OF PIXELS AS INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 256
provision t o alt er t he allocat ion ord er, which is occasionally useful when
rend er ing pred ominant ly sky-blue images.
The allocat ion 216 = (6 6 6) guar ant ees t he exist ence of a gr ay axis
along t he bod y d iagonal of t he und er lying color cube. The largest set
possible on an eight -bit archit ect ure is 6 7 6 = 252. All fact or ings
show signicant ly improved blue precision in compar ison t o t he four
possible blue values implicit und er t he common t hree bit s red and
green, t wo for blue scheme.
Ext ensi ons Ext ensi ons Ext ensi ons Ext ensi ons Ext ensi ons
The gener alized use of Car t esian prod uct s may be combined wit h t he
nonlinear axis sp acing d escr ibed p reviou sly. This yield s a nonlinear
spacing of or t hogonal planes, which d ene a color space in which point s
(color d escr ipt or s) lie in regions of var ying d ensit y. This makes possible
accu r at e color rep resent at ion on fr ame bu ffer s, which lack t he p ixel
p recision necessar y for high-p r ecision color (24-bit RGB), bu t which
minimally provid e color t able ind exing on a per-channel basis. Heckber t
(1982) d escr ibes a means t o creat e such cust om color t ables in which t he
d ensit y of color d escr ipt or s in t he space increases in regions of com-
monly occur r ing color s wit hin t he input d at a.
Gener ally, nonlinear Car t esian prod uct s may be const ruct ed a priori,
which sat isfy gener al const r aint s in t he absence of any input d at a set .
Two approaches are d escr ibed by Paet h (1989a, 1989b); one allows t he
precise represent at ion of a gr ay axis for color spaces of high nonlinear it y
as encount ered on color hard copy d evices; t he ot her is a symmet r ic space
based on t he t heor y of Chebyshev minimax approximat ion.
In t heir most gener al set t ing, pixels as int eger s ser ve as color t able
ind ices int o a space t hat is bot h nonlinear and non-Car t esian: point s may
be d ist r ibut ed ar bit r ar ily. A syst emat ic, nonor t hogonal approach t o color
d escr ipt or s and pixel ind exing is d escr ibed in t he Gem Mapping RGB
Tr iples ont o Four Bit s in t his volume.
See Append ix 2 for C Implement at ion (719)
257
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 257
IV.7 IV.7 IV.7 IV.7 IV.7
N NN NNORMAL CODING ORMAL CODING ORMAL CODING ORMAL CODING ORMAL CODING
Andrew S. Glassner
Xerox MRC
Palo Alt o. Calif ornia
A common t echnique for int er act ive light ing and sur face d esign is t o use
an approach called normal coding. Using t his t echnique, you can wr it e a
t ool t hat gives t he user int er act ive cont rol over t he light ing in a 3D scene,
wit h opt ions t o ad d and d elet e light s, and t o change t he color and
posit ion of each light . The user may also int er act ively change some of t he
su r face p rop er t ies of t he object s in t he scene, su ch as sp ecu lar and
d iffuse reect ivit y.
The secret behind such an int er act ive syst em is t hat it is all d one
t hrough t he color map. Assume an eight -bit d eep fr ame buffer, and an
image rend ered by a point -sampling Z-buffer (t hat is, t here is exact ly one
nearest visible sur face per pixel). When comput ing an image, t ypically we
st ore t he d ept h of t he nearest visible sur face in t he Z-buffer, and t he
shad ed color of t hat sur face in t he image buffer.
Su p p ose t hat we cou ld t u r n any su r face nor mal int o an eight -bit
number. Then inst ead of st or ing t he color of t he nearest object in t he
image buffer, we can st ore t he eye-space sur face nor mal of t hat object .
When t he nal image is comp let ed , each p ixel in t he image bu ffer
cont ains t he encod ed eight -bit su r face nor mal of t he nearest visible
object ; we mi gh t r en a me t h e i ma ge bu ffer t h e normal buffer. Th e
cont ent s of t he nor mal buffer will gener ally not look anyt hing like a
pict ure.
To make an image from t he nor mal buffer, recall t hat t here are only
256 d ifferent sur face nor mals in t he buffer aft er all, each pixel is only
eight bit s d eep. If t he nor mal at some pixel has t he bit pat t er n 00001001,
it will be d isplayed on t he screen wit h t he color st ored in color-map ent r y
258
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 258
9. To creat e an image, we need only comput e t he shad ing appropr iat e t o
each st ored nor mal, and st ore t hat shad e in t he cor rect color-map ent r y.
If a light is moved , we recomp u t e t he shad ing on each of t he 256
nor mals, and wr it e t hat new shad ing int o t he color map. The image is t hus
immed iat ely upd at ed t o includ e t he new light ing.
To make t his scheme wor k, we need a way t o encod e and d ecod e
nor mals, or conver t from nor mals t o eight -bit ent r ies and back again. This
not e suggest s one way t hat wor ks well.
Alt hough t his t echnique makes for a great int er act ive t ool, it has some
rest r ict ions:
You get only 256 unique nor mals in your image.
You cannot d ist inguish bet ween object s.
You cannot use local light sources (t hat is, t hose wit h a posit ion in
space in ad d it ion t o d irect ion), since only t he nor mal of t he visible
sur face is st ored at each point , not t he spat ial locat ion of t he sur face.
The image will show band ing d ue t o t he quant izat ion of nor mals.
The int er act ive image will show aliasing ar t ifact s, since t here is only
one sample per pixel.
Some of t hese problems can be overcome wit h some ad d it ional st or age
and processing; such remed ies are d iscussed aft er we present t he basic
t echnique.
Some Encodi ng Met hods Some Encodi ng Met hods Some Encodi ng Met hods Some Encodi ng Met hods Some Encodi ng Met hods
There ar e a couple of pr inciples t hat we should keep in mind when
build ing a nor mal encod ing scheme. Typically we will not want t o use all
256 ent r ies in t he color map. Oft en t he image in t he fr ame buffer will only
occupy par t of t he screen; menus, cont rols, and a cur sor may all be
visible, and t hey must all have color s allocat ed . I suggest at a minimum
t hat color s 0 and 255 be left unt ouched ; t ypically t hese are used for black
and whit e, t o provid e background s, bord er s, t ext , and so for t h. If a
259
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 259
p rogr am d ecid es t o change t hese color s, t hat s ne, bu t t he nor mal
encod ing process should leave t hem alone.
There ar e many ways t o conver t a nor mal int o an eight -bit quant it y.
Perhaps t he most obvious approaches are d irect , 1-t o-1 inver t ible map-
pings. Assume t hat all nor mals have unit lengt h (t hat is, x
2
+ y
2
+ z
2
= 1). Most a p p r oa ch es sa ve bi t s by en cod i n g ju st t h e x a n d y
component s (we can easily recover t he z component from x and y since
z

1 x
2
y
2
. we choose t he sign of z so t hat t he nor mal point s
back t o t he viewer ).
For examp le, consid er t he x comp onent . We will u se fou r bit s t o
encod e x, mapping 1.0 t o 0, and + 1.0 t o 15. So x
e
, t he encod ed value
for x , i s fou n d by x
e
oor[(x + 1)*7.5]. Si mi l a r l y, y
e
oor
[(y + 1)*7.5]. We can combine t hese t wo four-bit values int o a single
eight -bit composit e byt e by st or ing x
e
in t he high four bit s and y
e
in t he
low four : c (x
e
*16) + y
e
This sum represent s c, t he byt e t hat encod es
t he nor mal.
To d ecod e c back int o component s, r st t ur n t he high and low bit s
back i n t o i n d i vi d u al n u mber s: x
e
oor (c/ 16), y
e
c (16*x
e
).
Now x (x
e
/ 7. 5) 1 an d y (y
e
/ 7. 5) 1. As we saw above,
z

1 x
2
y
2
.
Anot her ap p r oach is t o encod e t he x and y comp onent s of each
nor mal in p olar coord inat es r and , fou r bit s each r

x
2
y
2
,
arct an(y/ x)). As before, we scale each of r and int o number s
from 0 t o 15 and st ore t hem in t he low and high halves of t he byt e, as
before.
Yet anot her encod ing is t o use a single value represent ing 1 of 256
posit ions along an equal-area spir al, which st ar t s at t he or igin and wind s
out ward . Each posit ion along t his spir al represent s t he head of a nor mal,
which begins at t he or igin.
These approaches all require a special t est t o avoid t he foreground and
background color s. Some also have wast ed ent r ies. For example, consid er
t he meaning of bit pat t er n c = 1 1 1 1 1 1 10 using t he r st encod ing. Here,
x
e
= 1111 = 15, so x = +1.0, and y
e
= 1110 = 14, so y = 0.8666667.
Clear ly t here is no unit nor mal wit h t hese x and y component s; in fact
ever y bi t p at t er n 1111xxxx i s mean i n gl ess excep t for 11110000. So
weve wast ed 14 possible encod ings (t here are 16 possible sequences for
xxxx above; 1 1 1 1 is reser ved and 0000 is okay, leaving 14 wast ed ).
260
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 260
Each bit pat t er n t hat we cant use gives us a smaller available r ange of
nor mals t o represent our image, making it look slight ly wor se.
A Bet t er Encodi ng Met hod A Bet t er Encodi ng Met hod A Bet t er Encodi ng Met hod A Bet t er Encodi ng Met hod A Bet t er Encodi ng Met hod
The t echnique I use is t o const ruct a pair of t ables, one associat ing
nor mals wit h ind ices, and t he ot her associat ing ind ices wit h nor mals. To
encod e a nor mal, one looks it up in t he t able, and receives an ind ex
number giving t he color-map ent r y for t hat nor mal. To d ecod e, one looks
up t he ind ex number in t he ot her t able, which provid es a nor mal. This
way t he cor resp ond ence bet ween nor mal bit p at t er ns and geomet r ic
nor mals is not d irect , and we get a great er r ange of possible nor mals. We
can also use more sophist icat ed or expensive mappings, since t hey are
comput ed only once and st ored in t he t ables.
This approach is exact ly analogous t o t he use of color maps in eight -bit
fr ame buffer s. One way (t hough not t he best ) t o encod e a color in eight
bit s is t o use t hree bit s for red , t hree bit s for green, and t wo bit s for blue
(see Pixels as Int eger s in t his volume). This would lock us int o one
p ar t icu lar color sp ace, which d irect ly associat es a color wit h it s bit
pat t er n. A more gener al t echnique is t o use a color map, which allows us
t o associat e any color wit h any bit pat t er n; analagously, t he t echnique
ment ioned here may be t hought of as a normal map.
What might be t he best such nor mal map or t able? A r st t hought is t o
space nor mals equally on t he sur face of a hemisphere. But remember t hat
were viewing t he image from a single, xed locat ion, and t hat t he
nor mals have alread y been t r ansfor med int o eye space. We want most of
t he resolut ion near t he nor mal point ing head -on t oward us, and less t o t he
sid es, where we can see t he changes less clear ly. In ot her word s, we want
t he perceived d ifferences in t he nor mals t o be about equal.
One easy approach is t o lay a gr id over t he unit circle, and use nor mals
cor respond ing t o gr id cent er s wit hin t he circle. This is much like t he r st
encod ing t echnique d escr ibed above (using four bit s each for x and y),
but we d ont wast e bit pat t er ns t hat d ont cor respond t o possible nor mals.
We wish t o build t wo t ables: Bt oNTable[] conver t s a byt e t o a nor mal,
and Nt oBTable[] conver t s a nor mal t o a byt e. To build t hese t ables, we
place a gr id of xed size over t he unit circle and scan t he gr id t op-d own,
left -r ight , looking for cells whose cent er is wit hin t he circle. Suppose we
261
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 261
Figure 1. Using a 17-by-17 gr id t o encod e nor mals. Cell (10, 7) is highlight ed .
have a gr id 17 unit s on a sid e, and t he cell und er consid er at ion is (10, 7)
(see Figs. 1 and 2). The (x, y) coord inat es of t his cells nor mal are found
by subt r act ing t he cells cent er at (10.5, 7.5) from t he circles cent er at
(8.5, 8.5) and d ivid ing by t he r ad ius, giving ((10.5 8.5)/ 8.5, (7.5
8.5)/ 8.5) = (0.2352941, 0.1176471). Since in t his case x
2
+ y
2
< 1,
t his cell is wit hin t he unit circle. We can nd z =

1. 0 x
2
+ x
2
( )
, so
t he nor mal at t his cell is (0.2352941, 0.1176471, 0.9647776).
Given t hat we now have a new nor mal t o encod e, how d o we build t he
t ables? Assume t hat t able ent r ies [0 . . s] have been lled so far, so t his is
Figure 2. Cell (10, 7) is d ened on t he left and r ight by x = 10 and x = 11, and above
and below by y = 7 and y = 8. Thus, t he cent er is at (10.5, 7.5).
262
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 262
t h e s + 1st n or ma l t o be en cod ed . So n ow Bt oNTable[s + 1]
(0.2352941 , 0.1176471, 0.9647776), which associat es ind ex s + 1 wit h
t his nor mal.
The ot her t able, Nt oTable, associat es t he color-map ent r y s + 1 wit h a
qu ant ized ver sion of t his nor mal. For t his t able, I u se a 2D ar r ay
cor respond ing t o t he quant ized (x, y) coord inat es t hat are equal t o t he
cell ind ex, so Nt oBTable[10][7] s + 1.
On a 17-by-17 gr id , 225 ent r ies will be lled t his way. Ent r ies 0 and
255 are reser ved for for eground and background color s (usually black
and whit e, respect ively). The ot her s are available for use by t he int er ac-
t ive t ool. Table 1 provid es t he occupancy d at a for some ot her gr id sizes.
Table 1. Cell Occupancy for Different Gr id Sizes.
Number of
Size Cells Occupied Densit y
12 112 0.7777778
13 137 0.8106509
14 156 0.7959183
15 177 0.7866667
16 208 0.8125
17 225 0.7785467
18 256 0.7901235
19 293 0.8116344
20 316 0.79
21 349 0.7913832
22 384 0.7933884
23 421 0.7958412
24 448 0.7777778
25 489 0.7824
26 540 0.7988166

35 973 0.7942857
36 1020 0.787037
37 1085 0.7925493
38 1124 0.7783933

250 49080 0.78528

1000 785456 0.78545


*
263
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 263
It is int erest ing t o not e t hat an 18-by-18 gr id has exact ly 256 valid
nor mals! We d rop d own one size t o provid e for t he background , fore-
ground , and auxiliar y color s ment ioned ear lier. Not e t hat wit h t he met hod ,
a nor mal such as (1.0, 0.0, 0.0) is never represent ed , since it lies on t he
bound ar y of t he gr id .
Impr ovi ng t he Met hod Impr ovi ng t he Met hod Impr ovi ng t he Met hod Impr ovi ng t he Met hod Impr ovi ng t he Met hod
There are a few t hings one can d o t o make life a lit t le easier ; we will
ad d ress t he ve problems ment ioned in t he r st sect ion.
Theres not much t o be d one about t he limit of 256 nor mals if you have
only an eight -bit image buffer. Of cour se, d eeper buffer s will provid e
more nor mals.
You can wor k wit h ind ivid ual object s in sever al ways. One way is t o
allow t he user t o specify a mat er ial d escr ipt ion, and apply t hat t o all
nor mals in t he scene (so ever yt hing in t he scene is mad e of t hat mat er ial).
Some rend er ing progr ams can prod uce an object buffer, which cont ains
an int eger uniquely id ent ifying t he visible object at each pixel. You can
set all pixels not equal t o some object value t o t he background color, so
only t he select ed object will change in appear ance (t he rest of t he screen
will be t he background color ). Of cour se, youll have t o st ore t he or iginal
nor mal buffer so you can br ing it back when anot her object is select ed .
When calculat ing a shad e for a nor mal you may use a more complet e
shad ing equ at ion, t aking int o accou nt t he object s color, d iffu se and
specular reect ance, and so on.
I d ont have a good solut ion for hand ling local light sources; t hey
require spat ial infor mat ion, which can var y on a pixel-by-pixel basis.
There may be a more complex encod ing scheme using more bit s t hat
hand les t hese efcient ly.
Band ing is an ar t ifact of t his t echnique. Just as wit h color quant izat ion,
one can see band s of equal-nor mal regions of object s on t he screen. This
effect can be red uced by ad d ing some low-level noise t o each nor mal
before quant izat ion (t his is similar t o d it her ing). I have found t hat using
r and om x and y p er t u r bat ion s in t h e r an ge 3/ 17 t o +3/ 17 gives
good result s.
If you provid e int er act ive point ing on t he screen (t hat is, in which t he
user may point t o a spot and t he light will be moved so it is head -on t o
t hat nor mal), youll want t o aver age a local region on t he screen around
264
IV.7 NORMAL CODING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 264
t he point being picked . This will compensat e for bot h t he quant izat ion
and t he d it her ing. Anot her hand y feat ure is t o allow t he user t o point t o
where t he highlight should appear, and t hen aut omat ically calculat e t he
posit ion of t he light t o put t he highlight at t hat spot .
I d ont have any suggest ions about t he aliasing d ue t o a single point per
pixel. I d ont t hink t hat s really much of a problem in an int er act ive t ool,
anywayyoure not making nal images, just set t ing up par amet er s.
Wit h some effor t , t he t echniques in t his not e can be t he hear t of a
useful, int er act ive light ing d esign progr am.
For ot her d iscussions of nor mal encod ing and color-map t echniques,
see Heckber t (1984), and Sloan and Brown (1979).
See also Mapping RGB Tr iples ont o Four Bit s (233)
265
IV.8 RECORDING ANIMATION IN BINARY ORDER FOR PROGRESSIVE TEMPORAL REFINEMENT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 265
IV.8 IV.8 IV.8 IV.8 IV.8
R RR RRECORDING ANIMATION IN ECORDING ANIMATION IN ECORDING ANIMATION IN ECORDING ANIMATION IN ECORDING ANIMATION IN
B BB BBINARY ORDER FOR INARY ORDER FOR INARY ORDER FOR INARY ORDER FOR INARY ORDER FOR
P PP PPROGRESSIVE TEMPORAL REFINEMENT ROGRESSIVE TEMPORAL REFINEMENT ROGRESSIVE TEMPORAL REFINEMENT ROGRESSIVE TEMPORAL REFINEMENT ROGRESSIVE TEMPORAL REFINEMENT
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
When record ing hard ware allows fr ames t o be wr it t en mult iple t imes,
animat ion can be record ed in an ord er t hat provid es progressive t empor al
renement : halfway t hrou gh t he recor d ing p rocess, t he animat ion is
d ouble-fr amed , one quar t er of t he way t hrough record ing, t he animat ion
is quad ruple-fr amed , one eight h of t he way t hrough record ing, t he anima-
t ion is oct uple-fr amed , and so on. By record ing fr ames in binary order,
animat ion er ror s can be d et ect ed ear lier t han t hey would wit h sequent ial
record ing ord er. The t echnique is t ypically t he same speed as sequent ial
record ing, and it is t r ivial t o implement .
Doubl e- Fr amed Or der Doubl e- Fr amed Or der Doubl e- Fr amed Or der Doubl e- Fr amed Or der Doubl e- Fr amed Or der
A t r ick somet imes used in t he animat ion prod uct ion business is t o record
a d ouble-fr amed animat ion in t he process of record ing a single-fr amed
animat ion. This is d one by r st record ing each of t he even numbered
pict ures for t wo fr ames, yield ing a d ouble-fr amed animat ion, and t hen
d ropping in t he od d numbered pict ures for one fr ame each? yield ing t he
n i sh ed , si n gl e-fr a med a n i ma t i on . For exa mp l e, t h i s double-framed
order for an eight -fr ame animat ion is shown here:
i 0 1 2 3 4 5 6 7
st art _frame 0 2 4 6 1 3 5 7
repeat _frame 2 2 2 2 1 1 1 1
266
IV.8 RECORDING ANIMATION IN BINARY ORDER FOR PROGRESSIVE TEMPORAL REFINEMENT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 266
At each st ep i, pict ure number st art _frame is record ed st ar t ing at fr ame
st art _frame and cont inu ing for repeat _count con secu t ive fr ames. In
t his example, wed have a d ouble-fr amed animat ion aft er st ep 3 and a
single-fr amed animat ion aft er st ep 7.
This t r ick requires t hat t he record ing hard ware allow fr ames t o be
record ed mult iple t imes. Most single-fr ame vid eo record er s have t his
capabilit y, but lm record er s and wr it e-once med ia d o not . If fr ames are
being rend ered as t hey are record ed , t hen t he t r ick also requir es t hat t he
rend er ing process have no hist or y, t hat each fr ame can be rend ered
ind epend ent of t he ot her s. The benet of t he t r ick is t hat a rough d r aft of
t he animat ion is available halfway t hrough t he record ing process for
checking pur poses.
Bi nar y Or der Bi nar y Or der Bi nar y Or der Bi nar y Or der Bi nar y Or der
This t r ick can be ext end ed fur t her t o what we call binary order: on t he
way t o gener at ing a single-fr amed animat ion, we gener at e a d ouble-fr amed
animat ion at t he halfway point , a quad ruple-fr amed animat ion at t he
one-quar t er point , an oct uple-fr amed animat ion at t he one-eight h point ,
and so on. Binar y ord er is illust r at ed here for an eight -fr ame animat ion:
As shown in t he t able, st ep 0 record s pict ure 0 over fr ames 07 (t he
ent ire d ur at ion of t he animat ion), st ep 1 record s pict ure 4 over fr ames
47, st ep 2 record s pict ure 2 over fr ames 23, and so on. The sequence
is obvious when i and st art _frame are wr it t en in binar y (last t wo rows
of t he t able). Obser ve t hat st art _frame is t he bit -rever sal of i, and t hat
repeat _count is t h e lar gest p ower of t wo t h at d ivid es st art _frame.
Incid ent ally, bit r ever sal crops up in many Fast Four ier Tr ansfor m algo-
r it hms. The sequence is also relat ed t o bread t h-r st t r aver sal of a binar y
t ree.
i 0 1 2 3 4 5 6 7
st art frame 0 4 2 6 1 5 3 7
repeat _count 8 4 2 2 1 1 1 1
i base 2 000 001 010 011 100 101 110 111
st art _frame base 2 000 100 010 110 001 101 011 111
267
IV.8 RECORDING ANIMATION IN BINARY ORDER FOR PROGRESSIVE TEMPORAL REFINEMENT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 267
Dening binar y ord er in t er ms of bit rever sal wor ks only when t he
number of fr ames is a power of t wo, so we must gener alize bit rever sal t o
ar bit r ar y sequence lengt hs. This can be d one by not ing t hat bit rever sal of
a sequence of lengt h n = 2
k
consist s of k 1 st ages of shufes, where
each shufe br ings all t he even-numbered it ems t o t he front and all t he
od d -numbered it ems t o t he back (see Fig. 1, but not e t hat t hese mult i-
st age shufe d iagr ams are not FFT but t er y d iagr ams). The mult iple-
shufe d enit ion of binar y ord er wor ks for any sequence lengt h. I call t his
gener alizat ion of bit rever sal t ur ning a number insid e-out . An algo-
r it hm is given here:
i nsi de-out: t urn a number inside-out : a generalizat ion of bit -reversal.
For n = power of t wo, t his is equivalent t o bit -reversal.
Turn t he number a inside-out , yielding b. If 0 a < n t hen 0 b < n.
Also ret urn r = min( n b, largest power of 2 dividing b)
procedure insid e_out (n, a: i nt; b, r : ref i nt);
not e: b and r are ret urned via call-by-reference
k, m: i nt;
begin
m

n;
r

n;
b

0;
k

1;
whi l e k < n do
i f 2*a m then begi n
i f b = 0 then r

k;
b

b + k;
a

a (m + 1)/ 2;
m

m/ 2;
end;
el se m

(m + 1)/ 2;
k

k*2;
endl oop;
i f r > n b then r

n b;
endproc;
We can now comput e binar y ord er for any sequence lengt h. For example,
268
IV.8 RECORDING ANIMATION IN BINARY ORDER FOR PROGRESSIVE TEMPORAL REFINEMENT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 268
Figure 1. Bit rever sal by mult i-st age shufe for sequences of lengt h 8 and 11.
if nframes = 11:
To record an n-fr ame animat ion in binar y ord er, st ep a var iable i from
0 t o n 1, and t ur n t his number insid e-out at each st ep:
for i 0 to nfr ames 1 do
insid e_out (nfr ames, i, st ar t _fr ame, repeat _count );
here record pict ure number st ar t _fr ame
int o frames st ar t _fr ame t hrough st ar t _fr ame + repeat _count 1
endl oop;
i 0 1 2 3 4 5 6 7 8 9 10
st art frame 0 8 4 2 10 6 1 9 5 3 7
repeat _count 11 3 4 2 1 2 1 1 1 1 1
269
IV.8 RECORDING ANIMATION IN BINARY ORDER FOR PROGRESSIVE TEMPORAL REFINEMENT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 269
Summar y Summar y Summar y Summar y Summar y
By record ing in binar y ord er, t he t empor al resolut ion of animat ion can be
progressively refined . If record ing is t er minat ed at any t ime, t he anima-
t ion is st ill present able, since t he ent ire d ur at ion has been record ed . The
met hod uses one record oper at ion for each fr ame, so it is as fast as simple,
sequent ial ord er on most record er. If fr ames are being rend ered as t hey are
record ed , binar y record ing ord er allows rough d r aft s of t he animat ion t o
be previewed a fr act ion of t he way t hrough t he rend er ing process, enabling
ear ly d et ect ion of er ror s. The id eas here obviously gener alize t o nont empor al
and mult id imensional sampling process.
The t echnique d escr ibed here was d eveloped by t he aut hor at NYIT in 1981,
and was used for vid eo record ing t here. Ot her s have employed similar
t echniques for progressive t r ansmission of images, but few have ad d ressed
t he t reat ment of d at a sizes t hat are not power s of t wo.
Some relat ed references: Sloan and Tanimot o (1979), Knowlt on (1980), and
Bergman et al. (1986).
See append ix 2 for C Implement at ion (720)
270
IV.9 I-TO-I PIXEL TRANSFORMS OPTIMIZED THROUGH COLOR-MAP MANIPULATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 270
IV.9 IV.9 IV.9 IV.9 IV.9
1 1 1 1 1 TO TO TO TO TO1 1 1 1 1 PIXEL TRANSFORMS PIXEL TRANSFORMS PIXEL TRANSFORMS PIXEL TRANSFORMS PIXEL TRANSFORMS
O OO OOPTIMIZED THROUGH PTIMIZED THROUGH PTIMIZED THROUGH PTIMIZED THROUGH PTIMIZED THROUGH
C CC CCOLOR-MAP MANIPULATION OLOR-MAP MANIPULATION OLOR-MAP MANIPULATION OLOR-MAP MANIPULATION OLOR-MAP MANIPULATION
Dale Schumacher
St . Paul, Minnesot a
Many image manipulat ion su it es inclu d e a nu mber of t ools t hat are
analogou s t o common d ar kr oom t echniqu es. These t ools can all be
d escr ibed as 1-t o-1 pixel t r ansfor ms, since t he ou t p u t p ixel valu e is
st r ict ly a funct ion of t he input pixel value. When wor king wit h d iscret e
pixel values, such as t he int eger r ange [0, 255], t his kind of t r ansfor m can
oft en be implement ed more efcient ly by precomput ing a look-up t able of
out put values for ever y possible input value. As a fur t her opt imizat ion, a
color map can be used t o implement t his t able look-up in hard war e, and
t hus d isplay t he result s of t he t r ansfor m at t he next screen refresh.
Some examples of t ypical 1-t o-1 t r ansfor ms are phot o-inver sion, quan-
t izat ion (also known as post er izat ion), gamma cor rect ion, and cont r ast
ad just ment . Phot o-inver sion simply replaces each pixel value wit h it s
gr ayscale opposit e, like a phot ogr aphic negat ive. Quant izat ion d ivid es t he
input r ange int o a number of subr anges and assigns t he same value t o
each input value in each subr ange. This creat es a st air-st ep effect , which
red uces t he number of d ist inct out put values t hat are used t o represent
t he image. This kind of t r ansfor m is oft en used t o move images from a
syst em t hat suppor t s a large number of gr ay values, like 256, t o a syst em
t hat suppor t s fewer gr ay values, like 16. Gamma cor rect ion is a nonlinear
t r ansfor m cur ve, which ad just s for t he nonlinear response of image input
or out put d evices t o a linear r ange of input s. This t r ansfor m is analogous
t o overexp osu re and u nd erexp osu r e in p hot ogr ap hy. Finally, cont r ast
enhancement is used t o make light values light er and d ar k values d ar ker
at t he same t ime. Upper limit s, above which all out put will be whit e, and
lower limit s, below which all out put will be black, d ene an input r ange
bet ween which out put values are assigned in a linear r amp of gr ay values
271
IV.9 I-TO-I PIXEL TRANSFORMS OPTIMIZED THROUGH COLOR-MAP MANIPULATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 271
Figure 1. Phot o-inver sion t r ansfor m.
Figur e 2. Quant izat ion t r ansfor m.
272
IV.9 I-TO-I PIXEL TRANSFORMS OPTIMIZED THROUGH COLOR-MAP MANIPULATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 272
using t he ent ire out put r ange. This increases t he t ot al cont r ast of an
image.
All of t he t r ansfor ms d escr ibed above are 1-t o-1 pixel t r ansfor ms since
t he out put pixel value d epend s only on t he input pixel value. Techniques
like convolut ion, which examines a neighborhood of input pixels, and
d it her ing, which d epend s on t he posit ion in t he image mat r ix as well as
somet imes d ep end ing on t he valu es of near by p ixels, are not 1-t o-1
t r ansfor ms. 1-t o-1 t r ansfor ms can be d escr ibed as mat hemat ical funct ions
of t he input pixel, and t hus shown as a funct ion gr aph. Figures 1, 2, 3,
and 4 d isplay input -out put gr aphs of t he t r ansfor m funct ions d escr ibed
here. The input pixel values are along t he hor izont al axis and t he out put
pixels values are along t he ver t ical axis.
It is oft en convenient t o wor k wit h image pixels as d iscret e gr ayscale
values in an int eger r ange such as [0, 255] r at her t han id ealized values in
t he cont inuous real number r ange [0.0, 1.0]. When implement ing 1-t o-1
t r ansfor ms on a d iscret e input r ange, t he number of possible input pixel
values is usually much smaller t han t he number of pixels in t he input
image. Therefore, it is more efcient t o precomput e t he out put value
cor resp ond ing t o each p ossible inp u t valu e, st or e t hese valu es in a
look-up t able, and use t he look-up, t able t o comput e t he out put pixel
value for each input pixel. This is par t icular ly t rue as t he t r ansfor mat ion
funct ion get s more complex.
On a gr aphics syst em wit h a color map, t he hard ware can, in essence,
provid e a look-up t able for you. The fr ame buffer hold s ind ex values int o
t he color map and t he color map st ores t he act u al p ixel valu e t hat
cor respond s t o t hat ind ex. To t ake ad vant age of t his feat ure for imple-
ment ing 1-t o-1 pixel t r ansfor ms, set asid e a r ange of color map ent r ies
equal t o t he number of possible input pixel values. The ind ex int o t he
reser ved color map r ange now cor respond s t o an input pixel value. The
cont ent s of each of t hose color map cells d et er mines t he act ual out put
value of t he pixel on t he screen. Simply using t hat color map r ange as
your precomput ed look-up t able causes t he d isplay hard ware t o d o t he
look-up for each pixel for you as par t of it s nor mal oper at ion. Changes t o
t he color map appear on t he screen almost immed iat ely (at t he next
screen refresh). You never need t o examine or change t he pixel d at a
act ually in t he fr ame buffer since it is always t he input pixel value. The
out put pixel value is d et er mined by t he t r ansfor m funct ion look-up t able
t hat is load ed int o t he color map.
273
IV.9 I-TO-I PIXEL TRANSFORMS OPTIMIZED THROUGH COLOR-MAP MANIPULATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 273
Figure 3. Gamma cor rect ion t r ansfor m.
Figure 4. Cont r ast enhancement t r ansfor m.
274
IV.9 I-TO-I PIXEL TRANSFORMS OPTIMIZED THROUGH COLOR-MAP MANIPULATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 274
Using a color map t o implement 1-t o-1 pixel t r ansfor ms allows real-t ime
manipulat ion of t r ansfor m par amet er s, like t he upper and lower bound s
of a cont r ast ad just ment or t he gamma value of a gamma-cor rect ion
t r ansfor m. Visual feed back showing t he effect of t he t r ansfor m is immed i-
at e. This makes t weaking t he t r ansfor m par amet er s much more of an
int er act ive t r y-it -and -see process. The result s of even ver y complex t r ans-
for m funct ions can be shown quickly. When t he d esired result ant image is
obt ained , t he images can be wr it t en t o d isk along wit h t he color map in
effect at t he t ime, t hus saving t he t r ansfor med image.
See also Useful 1-t o-1 Pixel Tr ansfor ms (196)
275
IV.10 A SEED FILL ALGORITHM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 275
IV.10 IV.10 IV.10 IV.10 IV.10
A AA AA SEED FILL ALGORITHM SEED FILL ALGORITHM SEED FILL ALGORITHM SEED FILL ALGORITHM SEED FILL ALGORITHM
Paul S. Heckbert
Universit y of Calif ornia
Berkeley, Calif ornia
Provid ed here is pseud o cod e for seed ll. Given a seed point (x, y), it
set s t his pixel and all of it s 4-connect ed neighbor s wit h t he same pixel
value t o a new pixel value. This is a useful oper at ion in paint progr ams.
Unfor t unat ely, many of t he published algor it hms for seed ll st ress
simplicit y t o t he point of inefciency. A near-opt imal algor it hm for seed
ll is act ually not much more complex t han t he simplest one, as d emon-
st r at ed by t he cod e here. Opt imalit y can be measured by t he number of
t imes a pixel is read . One of t he ear liest published algor it hms for seed ll
read s each pixel t wice (Smit h, 1979). The algor it hm here, which I d evel-
oped in 1982, read s each pixel just a bit more t han once on aver age, as
d oes t he similar algor it hm d escr ibed in Fishkin and Bar sky (1985), which
gives a good analysis of previous ll algor it hms.
Ou r cod e st op s lling wit h any change in p ixel valu e, bu t ot her
st opping r ules, such as st op at a specic pixel value are oft en useful.
The cod e could easily be gener alized in t his way.
l l : set t he pixel at (x, y) and all of it s 4-connect ed neighbors
wit h t he same pixel value t o t he new pixel value nv.
A 4-connect ed neighbor is a pixel above, below, left , or right of a pixel.
Pixel: type i nt;
Wind ow: type record [xmin, ymin, xmax, ymax: i nt]; inclusive window
276
IV.10 A SEED FILL ALGORITHM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 276
procedure ll(
x, y: i nt; seed point
nv: i nt; new pixel value
win: Wind ow; screen window
pixelread : f uncti on(x, y: i nt): Pixel; procedure for reading pixels
pixelwr it e: procedure(x, y: i nt; pv: Pixel); procedure for writ ing pixels
);
st ar t , xl, x2, d y: i nt;
ov: Pixel; old pixel value
Segment : type record [y, xl, xr, d y: i nt];
Filled horizont al segment of scanline y for xl x xr.
Parent segment was on line y dy. dy = 1 or 1
max: const i nt 10000; max dept h of st ack
st ack: array[0..max 1] of Segment ; st ack of lled segment s
sp: i nt 0; st ack point er
procedure push(y, xl, xr, d y: i nt); push new segment on st ack
begi n
i f sp < max and y + d y win.ymin and y + d y win.ymax then begi n
st ack[sp].y y;
st ack[sp].xl xl;
st ack[sp].xr xr ;
st ack[sp].d y d y;
sp sp + l;
end;
endproc push;
procedure pop(y, xl, xr, d y: ref i nt); pop segment off st ack
begi n
sp sp 1;
d y st ack[sp].d y;
y st ack[sp].y + d y;
xl st ack[sp].xl;
xr st ack[sp].xr ;
endproc pop;
277
IV.10 A SEED FILL ALGORITHM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 277
begi n procedure ll
ov

pixelread (x, y); read pixel value at seed point


if ov = nv or x < win.xmin or x > win.xmax or y < win.ymin or y > win.ymax then
return;
push(y, x, x, 1); needed in some cases
push(y + 1, x, x, 1); seed segment (popped 1st )
whi l e sp > 0 do
pop segment off st ack and ll a neighboring scan line
pop(y, x1, x2, d y);
segment of scan line y dy for x1 x x2 was previously lled,
now explore adjacent pixels in scan line y
x x1;
whi l e x win.xmin and pixelread (x, y) = ov do
pixelwr it e(x, y, nv);
x x 1;
endl oop;
i f x xl then goto skip;
st ar t x + 1;
i f st ar t < xl then push(y, st ar t , x1 1, dy); leak on left ?
x xl + 1;
l oop do
whi l e x win.xmax and pixelread (x, y) = ov do
pixelwr it e(x, y, nv);
x x + 1;
endl oop;
push(y, st ar t , x 1, d y);
i f x > x2 + 1 then push(y, x2 + 1, x 1 d y); leak on right ?
skip: x x + 1;
whi l e x x2 and pixelread (x, y) ov do
x x + 1;
endl oop;
st ar t x;
whi l e x x2;
endl oop;
endproc ll;
See also Filling a Region in a Fr ame Buffer (278)
See Append ix 2 for C Implement at ion (721)
278
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 278
IV.11 IV.11 IV.11 IV.11 IV.11
F FF FFILLING A REGION ILLING A REGION ILLING A REGION ILLING A REGION ILLING A REGION
I II IIN A FRAME BUFFER N A FRAME BUFFER N A FRAME BUFFER N A FRAME BUFFER N A FRAME BUFFER
Ken Fishkin
Pixar, Inc.
San Raf ael, Calif ornia
Fill algorit hms are a common gr aphics ut ilit y. They explore t hrough a
fr ame buffer from some seed point , nd ing all t he pixels connect ed t o
t hat seed point t hat share some proper t y (for example, all being t he same
color ). Some fu nct ion (for examp le, changing t he color ) is invoked
exact ly once on each pixel found .
This Gem d oesnt wor r y about exact ly what proper t y youre looking for,
or what funct ion you want t o invoke on each pixel: r at her, it cont ains t he
cont roller, which d ecid es which pixels t o look at and when. It assumes
a boolean-valued funct ion INSIDE(x, y), which ret ur ns t rue for any pixel
t hat has t he proper t y you want , and a void -valued funct ion SET(x, y),
which changes t he pixel as you wish. Fur t her more, INSIDE(x, y) must
r et u r n false if (x, y) has been SET(); commonly, a one-bit -p er -p ixel
mask is used for t his. The INSIDE and SET funct ions encapsulat e t he t ype
of ll (int er ior ll, bound ar y ll, t int ll . . .), while t he cont roller sit t ing
above it , cont ained in t his gem, remains const ant .
As t er minology, t he set of all pixels connect ed t o t he seed point t hat
need t o be lled compr ise t he region. A scanline of pixels, all of which
are in t he region, is t er med a span. Figur e 1a shows a sample before
st at e of a fr ame buffer : pixels wit h a hollow circle insid e t hem have t he
proper t y we are int erest ed in, and t he pixel wit h a solid circle is t he seed
point . Figure 1b shows all SET() pixels wit h a solid circle, and out lines
each span wit h a solid line.
Algor it hms t hat d o t his explor at ion all wor k similar ly: t hey st ar t wit h a
seed span, and t hen have a st ack of unexplored spans, t er med shadows,
which t hey examine in classic gr aph-t r aver sal fashion.
279
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 279
Figur e 1. Filling a region.
When a shad ow is pu shed on t he st ack, it can have one of t hree
possible relat ions t o t he span t hat is pushing it . It can over lap it at bot h
ed ges (a W t ur n), it can over lap it at one ed ge (a U t ur n), or it can
over lap it at neit her ed ge (an S t ur n) (see Fig. 2).
A d et ailed compar ison of t he most popular ll algor it hms would t ake
t oo long: t he int erest ed read er is refer red t o Fishkin and Bar sky (1985).
To make a long st or y shor t , t hey d iffer in how t hey hand le S, U, and W
t ur ns, and in whet her t hey need a bit -per-pixel.
The canonical ll algor it hm, wr it t en by Smit h (1982), wor ks ne on
S t ur ns, but is nonopt imal on U and W t ur ns. An ext ension t o t his,
wr it t en by Levoy (1981), wor ks ne on S and U t ur ns, but is nonopt imal
on W t ur ns. Anot her, wr it t en by Shani (1980), wor ks well on all t hree
t ur ns and d oesnt require a bit -per-pixel, but has poor wor st -case behav-
ior. The gem you see here wor ks ne on all t hree t ur ns, and has t he best
aver age- and wor st -case behavior of t he four.
Figure 2. The t hree kind s of t ur ns.
280
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 280
Figur e 3. About t o process a shad ow.
The algor it hm wor ks by maint aining a st ack of shad ows. A shad ow is
not an area t hat has been lled , but r at her an area t hat might be lled :
any set s of llable pixels t hat int er sect t he shad ow should be lled . In
ad d it ion t o t he shad ow, t he st ack remember s where t he parent of t he
shad ow (t he pusher of t his shad ow) was; t his let s it d et ect U, S, and W
t ur ns.
To p r ocess a p ar t icu lar shad ow t he algor it hm marches acr oss t he
shad ow, nd ing all spans t hat t he shad ow t ouches. It calls SET() on each
pixel in t he span, and t hen pushes t he new shad ows cast by t his new
span. The pushing ord er is ar r anged in such a way t hat shad ows t hat
change d irect ion, t hat is, shad ows t hat are below an upward -moving span
or above a d ownward -moving span are pushed last (and hence processed
r st ). This is a heur ist ic based on t he obser vat ion t hat t ur ns are relat ively
r are and usually lead int o relat ively small areas; by processing t hem r st ,
st ack size is red uced .
To make t his d escr ipt ion more concret e, consid er Figs. 3, 4, and 5.
Figure 3 shows a fr ame buffer as we st ar t t o process a shad ow. The
parent of t he shad ow is shown, and t he X-ed pixels are t hose pixels t hat
are not insid e t he region.
In Figu r e 4, we have fou nd t he sp ans t hat cont act ed t he cu r rent
shad ow. Then, in Figure 5, we push t he new shad ows on t he st ack cast by
our t wo new spans (who become t he parent s of t hese new shad ows).
Child 2 d oes an S t ur n wit h respect t o it s parent , and hence pushes one
shad ow. Child 1 d oes a U t ur n wit h respect t o it s parent , and hence
281
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 281
Figur e 4. Found t he spans in t he shad ow.
pushes t wo shad ows: one t hat cont inues in t he cur rent d irect ion and one
t hat rever ses d irect ion and explores t he ot her lobe of t he U. That shad ow
is pushed last (and processed r st ). Bot h shad ows of child 1 are pushed
before eit her shad ow of child 2, as child 1 was d iscovered before child 2.
A good ll algor it hm is one t hat read s as few pixels as possible. It can
be shown t hat t he algor it hm in t his Gem is opt imal if t he region has no
holes. In ot her word s, if t he region is a solid , connect ed group of pixels,
t hen t he algor it hm will read t he necessar y and sufcient set of pixels. In
t he wor st case, a region full of holes (a gr id ), t he algor it hm will read 50%
Figur e 5. The new shad ows.
282
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 282
more pixels t han it has t o, as opposed t o t he 100% and 200% wor st -case
behavior s of ear lier algor it hms. One ot her feat ure is t hat t he algor it hm
uses only increment s, d ecrement s, negat ions, assignment s, and t est s; t his
is hand y for implement at ion on simple processor s.
St ackElement record [
myLx, myRx: i nteger end point s of t his shadow
d ad Lx d ad Rx: i nteger and of my parent
myY: i nteger
myDirect ion: TWO_VAL records whet her Im above or below my parent :
can only have values of -1 or +1
];
assume a st ack of St ackElement s,
and a Box-valued variable Limit , which indicat es
t he limit s of t he frame buffer window.
macro PUSH(left , r ight , d ad l, d ad r, y, d ir )
pushes a new shadow, going from left t o right (inclusive) on line y,
wit h a parent going from dadl t o dadr (inclusive) on line y dir
macro POP()
pops t he shadow on TOS int o local variables;
lx, rx, y, direct ion, dadLx, and dadRx
macro STACK(d ir, d ad Lx, d ad Rx, lx, r x, y)
pushes one more shadow on t he st ack, given a newly discovered
span and it s parent : t his is where S vs. U vs. W t urns are handled
pushr x r x + 1; pushlx lx 1;
PUSH(lx, r x, pushlx, pushr x, y + d ir, d ir )
i f r x > d ad Rx
then PUSH(dadRx + 1, rx, pushlx, pushrx, y dir, dir) U turn to the right
i f lx < d ad Lx
then PUSH(lx, dadLx 1, pushlx, pushrx, y dir, dir) U turn to the left
a W t urn is just t wo U t urns: bot h ifs would evaluat e t rue.
main: ll a region wit h seed at (seedx, seedy)
Fill(seed x: i nteger, seed y: i nteger)
nd t he span cont aining t he seed point .
283
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 283
Suppose it goes from lx t o rx, inclusive
PUSH(lx, r x, lx, r x, seed y + 1, 1);
PUSH(lx, r x, lx, r x, seed y 1, 1);
whi l e st ack is not empt y do
POP();
i f y < Limit .t op or y > Limit .bot t om
then l oop;
x

lx + 1
i f (wasIn INSIDE(lx, y))
then begi n
SET(lx, y); lx lx 1;
whi l e INSIDE(lx, y) and lx Limit .left do
t he left edge of t he shadow cont act s a span: walk over
t o it s edge.
SET(lx, y); lx lx 1;
endl oop
end
now ent er t he main loop: we are now looking at pixel x, and moving t o t he
right . If wasIn is t rue, t hen I am current ly inside a span of pixels whose left
edge is at lx.
whi l e (x Limit .r ight ) do
i f wasIn
then begi n
i f INSIDE(x, y)
then begi n
case 1: was inside, am st ill inside.
SET(x, y)
end
el se begi n
case 2: was inside, am no longer inside: just
found t he right edge of a span
STACK(d irect ion, d ad Lx, d ad Rx, lx, (x 1), y);
wasIn

fal se
end
el se begi n
i f x > r x
then exi t;
284
IV.11 FILLING A REGION IN A FRAME BUFFER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 284
i f INSIDE(x, y)
then begi n
SET(x, y);
case 3: wasnt inside, am now: just found t he
st art of a new run
wasIn true;
lx x
end
el se begi n
case 4: wasnt inside, st ill isnt
end
x x+ l;
end
endl oop
i f wasIn
then begi n
hit an edge of t he frame buffer while inside a run
STACK(d irect ion, d ad Lx, d ad Rx, lx, (x 1), y);
end
endl oop
return
See also A Seed Fill Algor it hm (275)
285
IV.12 PRECALCULATING ADDRESSES FOR FAST FILLS, CIRCLES, AND LINES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 285
IV.12 IV.12 IV.12 IV.12 IV.12
P PP PPRECALCULATING RECALCULATING RECALCULATING RECALCULATING RECALCULATING
A AA AADDRESSES FOR DDRESSES FOR DDRESSES FOR DDRESSES FOR DDRESSES FOR
F FF FFAST FILLS, CIRCLES, AND LINES AST FILLS, CIRCLES, AND LINES AST FILLS, CIRCLES, AND LINES AST FILLS, CIRCLES, AND LINES AST FILLS, CIRCLES, AND LINES
Bill Wallace
Wat erloo, Ont ario, Canada
When ad jacent pixels are accessed in an algor it hm t hat need s t o be fast ,
macro d enit ions (or inline funct ions) can be used , which precalculat e
one ad d ress and t hen use simple ad d it ion t o nd neighbor ing point s.
Assume screen is a point er t o a screen k byt es wid e, eight bit s d eep (for
ease of explanat ion, but t his met hod can be used for ar bit r ar y d ept h
screens.) Then a fast ll algor it hm can be wr it t en as follows:
macro Calculat e Ad d ress(x, y) (y*k + x + screen ad d ress);
macro Move Left (ad d ress) (ad d ress + 1);
macro Move Right (ad d ress) (ad d ress 1);
macro Move Up(ad d ress) (ad d ress k);
macro Move Down (ad d ress) (ad d ress + k);
macro Plot (ad d ress, color ) Put Byt e (ad d ress, color );
macro Get Color (ad d ress) Get Byt e (ad d ress);
fast ll( ad d ress, color, bound ar y color )
begi n
i f Get Color (ad d ress) bound ar y color
then begi n
Plot (ad d ress, color );
fast ll(Move Left (ad d ress), color, bound ar y color );
fast ll(Move Right (ad d ress), color, bound ar y color );
fast ll(Move Up(ad d ress), color, bound ar y color );
fast ll(Move Down(ad d ress), color, bound ar y;
end;
end;
286
IV.12 PRECALCULATING ADDRESSES FOR FAST FILLS, CIRCLES, AND LINES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 286
This rout ine is called wit h somet hing like
fast ll(Calculat e Ad d ress(x, y) ll color, bound ar y color )
wit h t he resu lt being t hat ever yt hing in an area su r rou nd ed by t he
bound ar y color will be lled wit h t he ll color.
This saves explicit ly passing bot h x and y coord inat es and saves t wo
mult iplies and four ad d s on each it er at ion of fast ll. Using t his met hod
wit h Bresenhams algor it hm or wit h some line d r awing algor it hms saves
one mult iply and t wo ad d s per pixel. If t he screen is 24 bit s d eep wit h
separ at e base ad d ress, savings are increased cor respond ingly. One prob-
lem wit h t his met hod is t hat t here is no easy way t o d et ect if t he ed ge of
t he screen has been encount ered . For t he line d r awing and circle algo-
r it hms, clipping can be per for med before t he line is d r awn and for t he ll
algor it hms it may or may not be a problem.
See also What Are t he Coord inat es of a Pixel? (246); Circles of
Int egr al Rad ius on Int eger Lat t ices (57); A Seed Fill Algor it hm
(275); Filling a Region in a Fr ame Buffer (278)
287
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 287
IV.13 IV.13 IV.13 IV.13 IV.13
A AA AA SIMPLE METHOD SIMPLE METHOD SIMPLE METHOD SIMPLE METHOD SIMPLE METHOD
F FF FFOR COLOR QUANTIZATION OR COLOR QUANTIZATION OR COLOR QUANTIZATION OR COLOR QUANTIZATION OR COLOR QUANTIZATION: :: ::
O OO OOCTREE QUANTIZATION CTREE QUANTIZATION CTREE QUANTIZATION CTREE QUANTIZATION CTREE QUANTIZATION
Michael Gervaut z, Werner Purgat hof er
Technische Universit t Wien
Wien, Aust ria
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
A met hod for lling a color t able is present ed t hat prod uces pict ures of
similar qualit y as exist ing met hod s, but requires less memor y and execu-
t ion t ime. All color s of an image are inser t ed in an oct ree, and t his oct ree
is red uced from t he leaves t o t he root in such a way t hat ever y pixel has
a well-d ened maximum er ror.
The human eye is able t o d ist inguish about 200 int ensit y levels in each
of t he t hree pr imar ies red , green, and blue. All in all, up t o 10 million
d ifferent color s can be d ist inguished . The RGB cube wit h 256 subd ivi-
sions on each of t he red , green, and blue axes, as it is ver y oft en used ,
represent s about 16.77 million color s and sufces for t he eye. It enables
d isplay of color shad ed scenes wit hout visible color ed ges, and is t here-
fore well-su it ed for compu t er gr aphics. Figu re 1, on t he back cover,
shows a comput er-gener at ed image d isplayed wit h 16 million color s.
Color d evices (mainly fr ame buffer s) t hat allow for t he project ion of
t hose 16 million color s at t he same t ime are complicat ed and t herefore
expensive. On t he ot her hand , even good d it her ing t echniques prod uce
relat ively poor-qualit y pict ures on cheap d evices (Jar vis et al., 1976).
Therefore, d evices wit h color t ables are prod uced t hat allow t he use of a
small cont ingent K (for example, K = 256) of color s out of a larger
palet t e (for example, 16 million color s).
When d isp laying images t hat cont ain more t han K color s on su ch
d evices, t he problem ar ises as t o which K color s out of t he possible
color s will be select ed and how t he or iginal color s are mapped ont o t he
represent at ives t o prod uce a sat isfying pict ure. Such a select ion is also
need ed for some ot her algor it hms, such as t he CCC-met hod for image
encod ing (Campbell et al., 1986). The quest ion is, how much expense
288
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 288
can or will be invest ed in t his job? This Gem present s a ver y simple but
effect ive algor it hm we called oct ree quant izat ion t o hand le t his t ask.
There ar e some exist ing solut ions t hat have alread y been int r od uced .
The simplest way t o hand le t he problem is t o d ivid e t he RGB cube int o
equal slices in each d imension and use t he cross prod uct of t hese (few)
color levels of ever y pr imar y for t he color t able. It is called unifor m
quant izat ion, and is d epict ed on t he back cover in Fig. 2, which is t he
same image as Fig. 1, d isplayed wit h 64 color s. The popular it y algo-
r it hm (Heckber t , 1982) chooses t he K most frequent ly occur r ing color s
for t he color t able, shown in Fig.3 on t he back cover. The med ian cut
algor it hm (Heckber t , 1982) t r ies t o select K color s in such a way t hat
each of t hese color s represent s approximat ely t he same number of pixels
(see Fig. 4 on t he back cover ).
Pr i nci pl e of t he Oct r ee Quant i zat i on Met hod Pr i nci pl e of t he Oct r ee Quant i zat i on Met hod Pr i nci pl e of t he Oct r ee Quant i zat i on Met hod Pr i nci pl e of t he Oct r ee Quant i zat i on Met hod Pr i nci pl e of t he Oct r ee Quant i zat i on Met hod
The image is read sequent ially. The r st K d ifferent color s are used as
init ial ent r ies t o t he color t able. Ii anot her color is ad d ed , which means
t hat t he alread y processed par t of t he image has K + 1 d ifferent color s,
some ver y near neighbor s are merged int o one and subst it ut ed by t heir
mean. This st ep is repeat ed for ever y ad d it ional color, so t hat at any
moment no more t han K r ep r esent at ives are left . This p rop er t y, of
cour se, remains t r ue aft er t he image is complet ely processed .
The Oct r ee The Oct r ee The Oct r ee The Oct r ee The Oct r ee
For t his met hod a d at a st r uct u re has t o be u sed t hat enables qu ick
d et ect ion of color s t hat lie close t oget her in t he color space. An oct ree is
well suit ed for t his problem (Jakson and Tanimot o, 1980; Meagher,1932). The
RGB cube can easily be ad minist ered by an oct ree
It suffices t o use an oct ree of d ept h eight (t wo in t he eight h is 256 levels
in red , green, blue; eight in t he eight h gives 16 million color s) t o represent
all possible color s. The red , green, and blue component s (each bet ween 0
and 255) are t he coord inat es wit hin t he oct ree.
Ever y exact color is represent ed by a leaf in d ept h eight . Int er med iat e
nod es represent subcubes of t he RBG space. The great er t he d ept h of
such a nod e, t he smaller is t he color subcube represent ed by it ; t herefore,
t he d ept h of a nod e is a measure for t he maximum d ist ance of it s color s.
289
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 289
The Al gor i t hm The Al gor i t hm The Al gor i t hm The Al gor i t hm The Al gor i t hm
The oct ree quant izat ion is d one in t hree phases:
evaluat ion of t he represent at ives
lling t he color t able
mapping t he or iginal color s ont o t he represent at ives.
These t hree st eps are now d escr ibed in d et ail using t he color oct ree.
Eval uat i on of t he Repr esent at i ves Eval uat i on of t he Repr esent at i ves Eval uat i on of t he Repr esent at i ves Eval uat i on of t he Repr esent at i ves Eval uat i on of t he Repr esent at i ves
The oct ree is const r uct ed only in t hose par t s t hat are necessar y for t he
image we want . At t he beginning, t he oct ree is empt y. Ever y color t hat
occur s in t he image is now inser t ed by gener at ing a leaf in d ept h eight ;
t hereby, t he color is represent ed exact ly.
Inser t Tree (Tree: Oct ree, RGB: Color );
insert s t he color RGB int o t he subt ree Tree
begi n Insert Tree
i f Tree = nil
then NewAnd Init (Tree); produces and init s a new oct ree node.
i f Leaf we have reached t he eight h level.
then
begi n
inc(Tree^ .Color Count ); update the number of represented pixels
Ad d Color s (Tr ee^ .RGB, RGB); sum up t he color values
end
el se Inser t Tr ee (Next [Br anch(RGB)], RGB);
end;
In t his way an incomplet e oct ree is creat ed , in which many br anches are
missing. Act ually, t his oct ree d oes not have t o be lled wit h all t he color s
because ever y t ime t he number of color s reaches K + 1, similar color s
can be merged int o one, so t hat t here are never more t han K color s left .
We will call t his act ion a red uct ion of t he oct ree.
290
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 290
ReduceTree combines t he successors of an int ermediat e node t o one leaf node
Get Reducible (Tree); finds a reducible node
Sum (0,0,0);
Child ren 0;
for i:int eger 0,7 do
i f Next [i] <> nil t here is an it h successor
then
begi n
Child ren Child ren + 1;
Ad d Color s (Sum, Tree^ .Next [i]^ .RGB)
end;
Tree^ .Leaf t r ue; cut t he t ree at t his level
Tree^ .RGB Sum; the node represents the sum of all color values
Size Size Child ren + 1; of children
Ever y t ime t he number of leaves (t hat is, t he number of represent at ives
found up t o t he moment ) exceed s K, t he oct ree is red uced . The red uct ion
begins at t he bot t om of t he oct ree by always subst it ut ing some leaves by
t heir pred ecessor.
Red ucing t he oct ree, t he following cr it er ia are relevant :
From all red ucible nod es, t hose t hat have t he largest d ept hs wit hin t he
oct ree should be chosen r st , for t hey represent color s t hat lie closest
t oget her.
If t here is more t han one nod e in t he largest d ept h, ad d it ional cr it er ia
could be used for an opt imal select ion. For example, red uce t he nod e
t hat represent s t he fewest pixels up t o now. In t his way t he er ror sum
will be kept small. Red uce t he nod e t hat represent s t he most pixels up
t o now. In t his case large areas will be unifor mly lled in a slight ly
wrong color, and d et ailed shad ings (like ant i-aliasing) will remain.
To const ruct t he color oct ree, t he whole image has t o be read once and
all color s of t he image have t o be inser t ed int o t he oct ree.
Fi l l i ng t he Col or Tabl e Fi l l i ng t he Col or Tabl e Fi l l i ng t he Col or Tabl e Fi l l i ng t he Col or Tabl e Fi l l i ng t he Col or Tabl e
At t he end , t he K leaves of t he oct ree cont ain t he color s for t he color
t able (t he mean valu e of all rep r esent ed color s = RGB/ ColorCount ).
They can be wr it t en int o t he color t able by recur sively examining t he
291
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 291
oct ree. Dur ing t his recur sive t ree t r aver sal in ever y leaf nod e of t he oct ree
also it s own color ind ex is st ored .
Mappi ng ont o t he Repr esent at i ves Mappi ng ont o t he Repr esent at i ves Mappi ng ont o t he Repr esent at i ves Mappi ng ont o t he Repr esent at i ves Mappi ng ont o t he Repr esent at i ves
The mapping of t he or iginal color s ont o t heir represent at ives can now be
managed easily wit h t he oct ree, t oo. Tr ying t o nd any or iginal color in
t he red uced oct ree will end at a leaf in some d ept h. This nod e cont ains a
color ver y similar t o t he one in search, and is t herefore it s represent at ive.
Since t he ind ex of t he color t able is st ored t here t oo, no fur t her search
has t o be car r ied out .
If t he or iginal image used less t han K color s, no r ed uct ion will have
t aken place, and t he found color t able ent r y will cont ain exact ly t he
cor rect color. Ot her wise, only t he pat h t o t he leaf in d ept h eight was
shor t ened by t he red uct ion, so t hat t he color will be d isplayed less exact ly
by t he means of all t he color s t hat had t heir pat hs over t his nod e. Since
t he oct ree cont ains only K leaves, all or iginal color s are mapped ont o
valid color t able ent r ies. For t his, t he image has t o be read a second t ime.
Quant (Tree: Oct ree, Or iginal_Color : Color ): ind ex;
for t he original color it s represent at ive
is searched for in t he oct ree, and t he index of
it s color t able ent ry is ret urned
begi n Quant
i f Leaf
then ret ur n Tree^ .Color Ind ex st ored color index in t he t ree
else return Quant(Tree^ .Next[Branch (Original_Color)], Original_Color)
end;
The visual result using t his oct ree quant izat ion is of similar qualit y as
t he result using t he med ian cut met hod (see Fig. 5 on t he back cover ).
Impr ovement s Impr ovement s Impr ovement s Impr ovement s Impr ovement s
A signicant por t ion of t he execut ion t ime is spent wit h t he search for an
opt imal red ucible nod e ever y t ime a red uct ion of t he oct ree has t o t ake
place. These nod es can be collect ed easily in an appropr iat e st r uct ure
d ur ing t he const ruct ion of t he t ree. They have t o be sor t ed by d ept h t o
ensure quick access. An appropr iat e st ruct ure for t his pur pose has proved
292
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 292
to be eight linear list s (one for ever y d ept h level) cont aining all red ucible
nod es. All nod es of one d ept h level are element s of t he same list . The
nod e wit h t he largest d ept h can t hen be found quickly for red uct ion.
An int er med iat e nod e is inser t ed int o it s list of red ucible nod es aft er it s
creat ion d ur ing NewAndlInit .
At any given moment one level of t he oct ree will be t he d ept h in which
t he red uct ions t ake place. This d ept h is t he level of t he d eepest int er med i-
at e nod es. At t he beginning, t his is level seven, and it moves t oward t he
root d ur ing t he oct ree const ruct ion. This red uct ion level st at es what
t he minimal d ist ance bet ween t wo represent at ives will alread y have t o be.
This minimal d ist ance can never again d ecrease, even by ad d ing more
color s t o t he oct ree. Therefore, not hing beneat h t his red uct ion level + 1
will ever again be relevant , so t hat t he inser t ion of color s can also st op at
t hat d ept h. The d ept h of t he oct ree is not const ant , but d ecreases wit h
lifet ime.
Memor y and Comput at i onal Expense Memor y and Comput at i onal Expense Memor y and Comput at i onal Expense Memor y and Comput at i onal Expense Memor y and Comput at i onal Expense
Let N be t he number of pixels of t he or iginal image. If t he image is
run-lengt h encod ed , N can also be t he number of runs of t he image. The
algor it hm has t o be mod ied slight ly by using runs inst ead of pixels in t he
oct ree.
Let K be t he number of represent at ives, t hat is, t he size of t he color
t able. Let D be t he number of d ifferent color s in t he or iginal image.
In gener al t he following equat ions hold :
N > D > K and N >> K.
An upper bound for t he memor y used by t he oct ree is 2.K 1 nod es,
because t here are K leaves and at t he most (in t he case of a bint ree)
K 1 int er med iat e nod es. The algor it hm need s ver y lit t le memor y! It is
also ind epend ent of N and D, t hat is, of t he image. Only t he color t able
size is relevant .
Up p er bou nd s for t he nu mber of st ep s for t he inser t ions, for t he
gener at ion of t he color t able, and for t he quant izat ion are
Inser t ion:N.MaxDept h.
N inser t ions t ake p lace, each of t hem not d eep er t han MaxDept h.
MaxDept h it self is a const ant ( 8).
Color t able gener at ion:2.K.
293
IV.13 A SIMPLE METHOD FOR COLOR QUANTIZATION: OCTREE QUANTIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 293
Table 1. Compar ison of t he Quant izat ion Techniques
To ll t he color t able t he incomplet e oct ree has t o be examined once;
for ever y nod e t here is exact ly one call t o t he proced ure Init Colort able.
Mapping: N.MaxDept h
For ever y pixel t he color ind ex of it s represent at ive is found not d eeper
t han in t he maximum t ree d ept h. Thus, t he oct ree quant izat ion algor it hm
is of O(N), t he larger par t of t he execut ion t ime is spent by I/ O-oper-
at ions.
Compar i son wi t h Ot her Met hods Compar i son wi t h Ot her Met hods Compar i son wi t h Ot her Met hods Compar i son wi t h Ot her Met hods Compar i son wi t h Ot her Met hods
Table 1 gives a shor t compar ison wit h t he ot her ment ioned met hod s.
Concl usi on Concl usi on Concl usi on Concl usi on Concl usi on
A met hod was present ed t o nd a color t able select ion for d isplaying an
image on a screen. The pict ure qualit y of t his oct ree quant izat ion is as
good as t hat for exist ing met hod s. The expense in t er ms of memor y and
execut ion t ime, however, lies signicant ly below t he expense of t hose
algor it hms, especially in t er ms of t he memor y occupied , which is ind e-
pend ent of t he image complexit y. The met hod is t herefore well suit ed for
microcomput er s, t oo.
See also Mapping RGB Tr iples ont o Four Bit s (233)
5
3D GEOMETRY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 297
V.1 USEFUL 3D GEOMETRY
V.1 V.1 V.1 V.1 V.1
U UU UUSEFUL SEFUL SEFUL SEFUL SEFUL
3 33 33D GEOMETRY D GEOMETRY D GEOMETRY D GEOMETRY D GEOMETRY
Andrew S. G!assner
Xerox PARC
Palo Alt o, Calif ornia
Record Sphere: [
cent er : C,
r ad ius: r
] (see Fig. 1a )
A sphere will be r epresent ed in upper case roman (A, B).
Record Plane: [
nor mal: N,
offset : d
] (see Fig. 1b)
A Plane will be represent ed in upper case it alic (J, K).
V3 Di stance f rom Poi nt to Pl ane
d V3 Dist ance from Point (P) t o Plane (J) (see Fig. 2)
Q V3 Nearest Point on Plane (J) t o Point (P)
d V3 Dist ance bet ween Point (P) and Point (Q)
V3 Nearest Poi nt on Pl ane to Poi nt
Q V3 Nearest Point on Plane (J) t o Point (P) (see Fig. 3)
1. Q is on plane J, so J
N
Q + J
d
= 0.
2. The vect or Q P is parallel t o J
N
, so QP = kJ
N
for some k:
Q= PkJ
N
.
3. Plug Q from 2 int o 1: J
N
P J
N
kJ
N
+ J
d
= 0
4. Solve 3 for k:

k
J
d
+ J
N
P
J
N
J
N
5. Plug k int o 2 and nd Q:

Q P (
J
d
+ J
N
P
J
N
J
N
) J
N
.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 298
V.1 USEFUL 3D GEOMETRY
Figur e 3.
Figur e 1.
Figure 2.
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 299
V.1 USEFUL 3D GEOMETRY
V3 Poi nt on Pl ane Nearest the Ori gi n
P V3 Point on Plane (J) Nearest t he Or igin
P Nearest Point on Plane (J) t o Point (0)
V3 Intersecti on of Li ne and Pl ane
P V3 Int er sect ion of Line (l) and Plane (J)
Plug in t he expression for point s on l int o t hose on J.
(l
U
+ l
V
t ) J

+ J
d
= 0
Now solve for t and plug back int o t he line equat ion.

t
J
d
+ l
U
J
N

_
,

i
v
J
N
P l
U
+ l
V
t
V3 Normal i ze
A V3 Nor malize (A)

A
A
V3 Length A ( )
V3 Dot
d V3 Dot (A, B)
d
x
B
x
+ A
y
B
y
+ A
z
B
z
V3 Length
d V3 Lengt h (A)

d A A
V3 Intersecti on of Sphere and Li ne
P1, P2 V3 Int er sect ion of Sphere (S) and Line (l)
G

l
U
S
c
a l
V
l
V
b 2(l
V
G)
c (G G) S

r
2
d

b
2
4ac
i f d < 0
then no int er sect ion
el se

P1 ( b+ d)/ 2a
P2 ( b d)/ 2a
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 300
V.1 USEFUL 3D GEOMETRY
V3 Poi nt on Sphere Nearest Poi nt
Q V3 Point on Sphere (S) Nearest Point (P)
Q S
C
+ S
r
*V3 Nor malize(P S
C
)
V3 Li ne through Two Poi nts
l V3 Line t hrough Two Point s (A, B)
l
U

l
V
V3 Nor malize(B A)
V3 Di stance from Poi nt to Li ne
d V3 Dist ance from Point (P) t o Line (l)
Q V3 Point on Line (l) Closest t o Point (P)
d V3 Dist ance from Point (P) t o Point (Q)
V3 Poi nt on Li ne Cl osest to Poi nt
Q V3 Point on Line (l) Closest t o Point (P)
1. P lies on a plane wit h normal l
V
: P l
V
+ d = 0
2. Find t his plane and int ersect it wit h t he line:
J
N
l
V
J
d
(P l
V
)
Q V3 Int er sect ion of Line (l) and Plane (J)
V3 Poi nt on Li ne Nearest the Ori gi n
Q V3 Point on Line (l) Nearest t he Or igin
Q V3 Point on Line (l) Closest t o Point (0)
V3 Di stance between Poi nt and Poi nt
d V3 Dist ance bet ween Point (P) and Point (Q)
d V3 Lengt h(P Q)
V3 Parameter of Poi nt on Li ne from Poi nt to Poi nt
a V3 Par amet er of Point (P) on Line from Point (Q) t o Point (R)
V3 Dist ance fr om Point (P) t o Point (Q )
V3 Dist ance from Point (P) t o Point (Q + R)
V3 Projecti on of Vector onto
V V3 Project ion of Vect or D ont o Plane J
V D (D J
N
)J
N
See also Useful 2D Geomet r y (3)

a
301 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 301
V.2 AN EFFICIENT BOUNDING SPHERE
V. 2 V. 2 V. 2 V. 2 V. 2
A AA AAN EFFICIENT N EFFICIENT N EFFICIENT N EFFICIENT N EFFICIENT
B BB BBOUNDING OUNDING OUNDING OUNDING OUNDING
S SS SSPHERE PHERE PHERE PHERE PHERE
Jack Rit t er
Versat ec, Inc.
Sant a Clara, Calif ornia
This gem is a met hod for nd ing a near-opt imal bound ing sphere for any
set of N point s in 3D space. It is Ord er (N), and ext remely fast . The
sphere calculat ed is about 5% bigger t han t he id eal minimum-r ad ius
sphere.
The algor it hm is execut ed in t wo passes: t he r st pass nd s t wo point s
t hat are close t o maximally spaced . This pair d escr ibes t he init ial guess
for t he sphere. The second pass compares each point t o t he cur rent
sphere, and enlarges t he sphere if t he point is out sid e. The algor it hm is as
follows:
1. Make one (quick) pass t hrough t he N point s. Find t hese six point s:
Th e p oi n t wi t h mi n i mu m x , t h e p oi n t wi t h ma xi mu m x,
Th e p oi n t wi t h mi n i mu m y , t h e p oi n t wi t h ma xi mu m y,
Th e p oi n t wi t h mi n i mu m z , t h e p oi n t wi t h ma xi mu m z .
This gives t hree pair s of point s. Each pair has t he maximum span for it s
d imension. Pick t he pair wit h t he maximum point -t o-point separ at ion
(which could be great er t han t he maximum d imensional span). Calculat e
t h e i n i t i a l sp h er e, u si n g t h i s p a i r of p oi n t s a s a d i a met er.
2. Make a second pass t hrough t he N point s: for each point out sid e
t he cur rent sphere, upd at e t he cur rent sphere t o t he larger sphere passing
t hrough t he point on one sid e, and t he back sid e of t he old sphere on t he
ot her sid e. Each new sphere will (barely) cont ain t he old sphere, plus t he
new point , and usually some ot her out sid er s as well. The number of
upd at es need ed will be a t iny fr act ion of N. In t est ing each point against
t he cur rent sphere, t he square of it s d ist ance from t he cur rent spheres
302 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 302
V.2 AN EFFICIENT BOUNDING SPHERE
cent er is compared t o t he square of t he cur rent spheres r ad ius, t o avoid
d oing a sqrt () calculat ion.
The following pseud o cod e compares a point (x, y, z) wit h t he cur rent
spher e [cent er = (cenx, ceny, cenz), and r ad iu s = rad]. If (x, y, z) is
out sid e t he cur rent sphere, (cenx, ceny, cenz) and rad ar e upd at ed t o
reect t he new sphere. The cur rent square of t he r ad ius is maint ained in
rad sq:
given x, y, z, cenx, ceny, cenz, rad, and rad_sq
d x x cenx;
d y y ceny;
d z z cenz;
old _t o_p_sq d x*d x + d y*d y + d z*d z;
do economical r**2 t est before calc sqrt
i f (old _t o_p_sq > r ad _sq)
then
Point is out side current sphere. updat e.
old _t o_p

old _ to_ p _ sq ;
r ad (r ad + old _t o_p)/ 2.0;
updat e square of radius for next compare
r ad _sq r ad *r ad ;
old _t o_new old _t o_pr ad ;
cenx (r ad *cenx + old _t o_new*x)/ old _t o_p;
ceny (r ad *ceny + old _t o_new*y) / old _t o_p;
cenz (r ad *cenz + old _t o_new*z) / old _t o_p;
end
The following t wo t est s were run on a Sun 3/ 50 wor kst at ion (68020
wit h MC68881 oat ing point co-processor ).
Case 1 Case 1 Case 1 Case 1 Case 1
A spher ical volume cent ered at t he or igin wit h a r ad ius of 128, was
r and omly populat ed wit h 10,000 point s. Five of t hese were forced t o be
at t he ed ge of t he sphere. This means t hat t he opt imal sphere should have
303 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 303
V.2 AN EFFICIENT BOUNDING SPHERE
a r ad ius of 128. Result s: cent er = (3, 5, 4); r ad ius = 133 (4% > id eal);
processor t ime: 1.8 second s.
CASE 2 CASE 2 CASE 2 CASE 2 CASE 2
A cubic volume with a half-edge length of 128 was randomly populated
wit h 10,000 point s. Inclu d ed were t he eight cor ner p oint s. This means
t h at t h e i d eal r ad i u s

3128 222. Not e: t h i s i s cl ose t o t h e wor st
case for this algorithm, because an orthogonally aligned box means that
no corners will be found in the initial guessing phase. (A box rotated by
any angle ar ou nd any axis wou ld allow cor ner s t o be fou nd init ially.)
Resu lt s: cent er = (5, 21, 2); r ad iu s = 237 (7% > id eal); processor t ime:
1.8 seconds.
A full C ver sion of t his algor it hm can be found in t he append ix.
See Append ix 2 for C Implement at ion (723)
304 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 304
V.3 INTERSECTION OF TWO LINES IN THREE-SPACE
V. 3 V. 3 V. 3 V. 3 V. 3
I II IINTERSECTION OF NTERSECTION OF NTERSECTION OF NTERSECTION OF NTERSECTION OF
T TT TTWO LINES IN WO LINES IN WO LINES IN WO LINES IN WO LINES IN
T TT TTHREE-SPACE HREE-SPACE HREE-SPACE HREE-SPACE HREE-SPACE
Ronald Goldman
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Let each line be d ened by a point P
k
and a unit d irect ion vect or V
k
,
k = 1,2. Then we can express each line par amet r ically by wr it ing
L
1
(t ) = P
1
+ V
1
t and L
2
(s) = P
2
+ V
2
s.
The int er sect ion occur s when L
1
(t ) = L
2
(s) or equivalent ly when
P
1
+ V
1
t = P
2
+ V
2
s
Subt r act ing P
l
from bot h sid es and crossing wit h V
2
yield s
(V
l
V
2
)t = (P
2
P
l
) V
2
.
Now d ot t ing wit h (V
l
V
2
) and d ivid ing by V
1
V
2

2
give us
t = Det {(P
2
P
l
) , V
2
, V
l
V
2
}/ V
l
V
2

2
.
Symmet r ically, solving for s, we obt ain
s = Det {(P
2
P
l
), V
l
, V
l
V
2
}/ V
l
V
2

2
.
Two impor t ant obser vat ions follow:
If t he lines are par allel, t he d enominat or V
l
V
2

2
= 0.
If t he lines are skew, s and t represent t he par amet er s of t he point s of
closest approach.
GRAPHICS GEMS I Edit ed by ANDREW GLASSNER 305
I II IINTERSECTION OF NTERSECTION OF NTERSECTION OF NTERSECTION OF NTERSECTION OF
T TT TTHREE PLANES HREE PLANES HREE PLANES HREE PLANES HREE PLANES
Ronald Goldman
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Let each plane be d ened by a point P
k
and a unit nor mal, vect or V
k
,
k = 1, 2, 3. Then t he unique point of int er sect ion can be wr it t en in closed
for m as
P
Int
= { (P
l
V
l
)(V
2
V
3
) + (P
2
V
2
)(V
3
V
l
)
+ (P
3
V
3
)(V
l
V
2
)}/ Det (V
l
, V
2
, V
3
).
If t wo of t he given planes are par allel t here is no int er sect ion. In t his case
t he d enominat or Det (V
l
, V
2
, V
3
) is zero.
V. 4 V. 4 V. 4 V. 4 V. 4
306 GRAPHICS GEMS I Edit ed by ANDREW GLASSNER 306
V VV VV
M MM MMAPPING SUMMARY APPING SUMMARY APPING SUMMARY APPING SUMMARY APPING SUMMARY
Pr oject i n g a sp h er e on t o t h e p l a n e i s n ot a n ea sy p r obl em.
Ca r t ogr a p h er s, i n t er est ed i n ma ki n g ma p s for n a vi ga t i on , h a ve
long known t hat one mu st make a var iet y of t r ad eoffs in su ch a
p r oject i on . You ma y wa n t t o p r eser ve d i st a n ces, a n gl es, h ea d -
ings, or areas, but you cant preser ve t hem all at once.
Ma n y st yl es of ma p p r oject i on h a ve been d evel op ed for
d i ffer en t goa l s: p ol i t i ca l bou n d a r i es, wa t er n a vi ga t i on , l a n d n a v-
i ga t i on , a r ea ca l cu l a t i on , a n d so on . Th e fol l owi n g t wo Gems
d i scu ss a va r i et y of p r oject i on s a p p r op r i a t e for comp u t er gr a p h -
i cs. Th ey a r e u sefu l for t op i cs r a n gi n g fr om t ext u r e ma p p i n g t o
spher ical funct ion rend er ing.
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 307
V VV VV. .. ..5 55 55
D DD DDIGITAL CARTOGRAPHY FOR IGITAL CARTOGRAPHY FOR IGITAL CARTOGRAPHY FOR IGITAL CARTOGRAPHY FOR IGITAL CARTOGRAPHY FOR
C CC CCOMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Over vi ew Over vi ew Over vi ew Over vi ew Over vi ew
The mapping of a sphere ont o a plane ar ises oft en in comput er gr aphics.
Direct applicat ions includ e t he aut omat ion of t r ad it ional map-making.
Here t he sphere represent s a wor ld globe and t he plane represent s t he
char t . Ind irect applicat ions includ e t he creat ion of environment maps
(Greene, 1986) or specialized applicat ions such as t he rewor ks simula-
t ion t hat conclud es t his Gem. Most references on t his t opic lie at t he
subject s ext reme point s: on t he specics of pr act ical car t ogr aphy or on
t he mat hemat ical t heor y of confor mal mapping. This ent r y seeks t he
mid d le gr ou nd wit h emp hasis on ease of comp u t at ion. Car t ogr ap hic
t er ms (appear ing in it alics) and mat hemat ical not at ionpar t icular ly re-
gard ing spher ical t r igonomet r yare employed preferent ially.
Pr oj ect i on Pr oper t i es Pr oj ect i on Pr oper t i es Pr oj ect i on Pr oper t i es Pr oj ect i on Pr oper t i es Pr oj ect i on Pr oper t i es
As t he sphere cannot be developed ont o a at sheet (unlike a cylind er,
cone, or plane of project ion) d ist or t ions must necessar ily be int rod uced in
for ming a char t , t aking t he for m of local scale changes on bot h axes. A
mat hemat ical cat egor izat ion of local scale changes reveals t hree pr inciple
a n d d i st i n ct p r oject i on cl a sses: conformal, equal-area, a n d equidis-
t ant . Classes are exclusive wit h t he r st t wo of pr imar y impor t ance and
of opposing nat ure. Confor malit y implies t hat small areas on t he char t
have t rue local scale. As a consequence all confor mal project ions pre-
ser ve angles, but when used at global scales suffer severe d ist or t ion of
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 308
shap e. Common ap p licat ions at bot h large and small scales inclu d e
naut ical char t s and municipal maps, respect ively. Equal-area maps allow
t he d epict ion of large land masses wit h red uced d ist or t ion of shape; exact
area preser vat ion makes t hem at t r act ive for t hemat ic census or st at ist ical
maps. Conver sely, t hey suffer angular d ist or t ions of local scale. Equid is-
t ant project ions maint ain t rue scale along select cur ves; t hese and spe-
cialized project ions seek a mid d le ground bet ween t he shape and angle
d ist or t ions present in t he r st t wo classes. They are commonly employed
for d epict ing areas of global ext ent in pleasing fashion. All proper maps
can rend er a small, cent r al por t ion of t he globe wit h vanishingly small
d ist or t ion as t he ear t hs cur vat ure t hen becomes negligible.
Fi r st Pr i nci pl es Fi r st Pr i nci pl es Fi r st Pr i nci pl es Fi r st Pr i nci pl es Fi r st Pr i nci pl es
Coord inat es for a point P on a sphere of r ad ius R cent ered about t he
or igin O are most oft en given in t er ms of spher ical coord inat es (R, , ).
Here t he z axis pierces t he sphere at it s nor t h and sout h poles. Planes
cont aining t he or igin O cut t he sphere for ming great circles of r ad ius R
ot her planes of int er sect ion for m small circles of lesser r ad ius. The
xy plane per pend icular t o t he polar axis cont ains t he equat or, a great
circle t hat bisect s t he sphere int o a nor t h and sout h hemisphere. Car t og-
r aphy univer sally employs t o d ene longit ude as t he count erclockwise
angular measure bet ween t he x axis and vect or OP project ed ont o t he xy
plane. This is a bearing measure, which ser ves an id ent ical funct ion t o
t he angle used in comput er gr aphics t o measure 2D rot at ion about t he
or igin. Similar ly, d enes lat it ude or angular d ist ance bet ween OP and
t he z axis such t hat point s on t he equat or have zero lat it ud e and point s at
t he nor t h and sout h poles lie at + or r ad ians, respect ively. This is
an az imut h measu r emen t sp eci c t o 3D sp h er i cal coor d i n at es. Th e
comp lement ar y colat it ude () measu r es t he angle fr om t he + z axis:
= / 2 . Colat it ud e t hus expresses great circle d ist ance from t his
pole.
Spher ical coord inat e t r iples d ene a unique point in t hree-space; pr in-
ciple argument s are rest r ict ed t o t he r ange < and / 2
/ 2. For rend er ing using convent ional gr aphics soft ware t hese may
be mapped int o Car t esian 3D wor ld coord inat es by changing from spher i-
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 309
cal t o rect angular for m:
x = R cos cos
y = R sin cos
z = R sin .
Viewed at a nit e d ist ance t he globe appear s as und er t he perspect ive
project ion. The limit ing case wit h view at innit y simplies t he foreshor t -
ening process, yield ing t he ort hographic project ion (Fig. 5a) in which
exact ly one hemisphere is d epict ed . Alt hough not immed iat ely concept u-
alized as map project ions, bot h are member s of t he azimut hal family
d escr ibed lat er. The inver se mapping is
R =

x
2
+ y
2
+ z
2
= t an
1

y
x
= t an
1

z
x
2
+ y
2
.
In t his for m t he at an2( ) funct ion common in many subrout ine libr ar ies
may be used t o ensure proper four-quad r ant oper at ion.
Di r ect Char t i ng of Dat abases Di r ect Char t i ng of Dat abases Di r ect Char t i ng of Dat abases Di r ect Char t i ng of Dat abases Di r ect Char t i ng of Dat abases
Geogr aphic d at abases provid e merely (, ) lat it ud e and longit ud e coor-
d inat e pair s, wit h R t he d er ived d ist ance from t he ear t hs cent er t o mean
sea level by using a st and ard ized reference geoid. For many pur poses R
may be set t o t he mean equat or ial ear t h r ad ius, a semimajor axis of lengt h
6378.160 km. (True geod esy employs higher-ord er st and ard mod els t o
account for t he oblat e gure of t he Ear t h; a polar at t ening of 1/ 298.25
yield s an approximat ion for R wit h maximum elevat ion er ror less t han
100 met er s). For pur poses of exposit ion only unit spheres are consid ered
in what follows.
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 310
Longit u d e 180W t o 0E (gr id 10) Lat it u d e: 0N t o 90N (gr id 10)
Cyl i ndri cal Equi di stant
Figur e 1. Equirect angular cylind r ical project ion.
Too oft en, a vect or plot used t o proof a car t ogr aphic d at a set uses t he
simple project ion
x s
y s ,
which is implicit when execut ing command s of t he for m plot xy (, ).
Here s is a const ant scale fact or for bot h axes used t o t t he d at a t o t he
char t d imensions. The r esu lt ing cylindrical equirect angular map (see
Fig. 1) represent s t he sphere at small scales and t hen only near t he
equat or many car t ogr apher s d o not consid er it a proper map. For t u-
nat ely, simple remed ies exist .
Cyl i ndr i cal Maps Cyl i ndr i cal Maps Cyl i ndr i cal Maps Cyl i ndr i cal Maps Cyl i ndr i cal Maps
Maps of t he cylind r ical family allow a d irect mapping of longit ud e lines,
or meridians, ont o equ ally sp aced , ver t ical char t lines. Geomet r ically,
t his suggest s project ing t he globe ont o an encasing ver t ical cylind er and
t hen unwr apping t o for m a plane. Maps of t his for m are well-suit ed t o
comput er gr aphics because t he mapping funct ion M
2
: (, ) (y, x)
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 311
becomes one-d imensional M
1
: () (y) wit h scaled d irect ly ont o x,
t hereby simplifying t he d er ivat ion of bot h for ward and inver se projec-
t ions. Cylind r ical maps suppor t clipping of in wor ld coord inat es when
plot t ing on a sur face of unifor m wid t h (a rect angular page or d isplay).
Mapping of screen (char t ) coord inat es back ont o (, ) is also st r aight for-
ward , suggest ing simplied d irect select ion and ed it ing of t he und er lying
car t ogr aphic d at a set .
On t he globe t he relat ive longit ud e scale shift s as a funct ion of lat it ud e.
This effect account s for t he red uced d ist ance bet ween meridians (lines
of const ant longit u d e) measu red along parallels of const ant lat it u d e.
Because scales agree at t he equat or (where one d egree is d ened as sixt y
naut ical miles) t he scale r at io is 1: 1 and d ecreases t o zero at eit her pole:
cos () or sin () d enes t his r at io. Thus, a rect angular char t for a local
region may use t he ad just ed scale
y s
y s sec
cen
,
t hereby provid ing an aspect r at io cor rect for a chosen par allel
cen
and
ar bit r ar y char t scale s. This for ms t he modied cylindrical equidist ant
project ion, which und er lies many st reet and municipal maps. Not e t hat a
scaling of cos along t he x axis would also for m a proper local aspect
r at io (see Sansons Sinusoid, below) but would alt er t he spacing of t he
par allels, which must be unifor m for any cylind r ical project ion. By em-
ploying a secant mult iplier, scale increases at high lat it ud e: an arct ic
explorer looping t he Nor t h Pole quickly t r aver ses t he char t s x ext ent ;
t he y scale must accord ingly be large. As t his r at io is const ant for t he
ent ire char t , t r igonomet r ic calculat ions need not appear in t he inner most
cod e loop per for ming t he coord inat e project ion. Alt hough comput at ion-
ally fast and more pleasing in appear ance t han d irect coord inat e plot t ing,
t he map is unsuit ed for large regions.
Mer cat or Pr oj ect i on Mer cat or Pr oj ect i on Mer cat or Pr oj ect i on Mer cat or Pr oj ect i on Mer cat or Pr oj ect i on
In t he p reviou s examp le, confor malit y was achieved along a chosen
par allel by increasing t he scale by sec t o offset t he cos change in
asp ect . By t reat ing ver t ical scale change cont inu ally a map may be
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 312
const ru ct ed as a mont age of nar row st r ip s having small (ver t ical)
ext ent but wit h ext ent encircling t he sphere. Away from t he equat or at
y = 0, successive st r ips are joined t oget her by offset t ing t hem a d ist ance
sec d t o accommod at e t he cont inuous change in scale. This gr aphi-
cally d epict s t he int egr al y = sec d , which for ms t he basis of t he
Mercat or project ion. Solut ion of t he int egr al and Taylor ser ies expansion
yield s t he inver se Gud er mannian funct ion and it s arcfunct ion:

y gd
1

_
,
log
e
tan

2
+

_
,



+
1
6

3
+
1
24

5
+
61
5040

7
+ O
9
( )

gd y
( )
2 tan
1
e
y
( )

_
,



y +
1
6
y
3

1
24
y
5
+
61
5040

7
+ O y
9
( )
Because of confor malit y, t rue head ings (t he angle for med bet ween a
ground t ack and a globe mer id ian) for m t rue angles on confor mal maps.
Becau se t he Mercat or is ad d it ionally cylind r ical, char t mer id ians are
or ient ed in rect ilinear fashion. Thus, lines of const ant head ing (called
loxodromes or rumb lines) may be p l ot t ed d i r ect l y, exp l ai n i n g t h e
project ions almost univer sal applicat ion in naut ical char t s. Despit e t hese
benet s, t he map has t he d isad vant age of excessive d ist or t ion at high
lat it ud es (see Fig. 2). Mercat or was revolut ionar y in d er iving a useful
p r oject ion t hrou gh analyt ic means. In p ar t icu lar, t her e is no simp le
geomet r ical mod el t hat d epict s t he project ions const r uct ion.
SansonFl amst eed Si nusoi dal SansonFl amst eed Si nusoi dal SansonFl amst eed Si nusoi dal SansonFl amst eed Si nusoi dal SansonFl amst eed Si nusoi dal
This simple project ion may be obt ained by choosing t he cent r al mer id ian
as t he axis of const ant spacing, or y = s . Ad just ment of t he x axis
yield s a map t hat p reser ves ar eaa valu able p rop er t y. Becau se t he
mapping fu nct ion may once again be mod eled as a one-d imensional
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 313
Longit u d e:140E t o 40W (gr id 10) Lat it u d e: 0N t o 90N (gr id 10)
Mercator Conf ormal
Figure 2. Mercat or cylind r ical project ion.
funct ion M
1
: () (x) t he useful proper t ies of wor ld -coord inat e clipping
(now on a ver t ical ext ent of const ant height ) and inver se mapping may
again be claimed .
Consid er a rect angular map of equally spaced par allels in which t he
cent r al par allel represent s t he equat or. At high lat it ud es t he char t par al-
lels represent small circles of red uced r ad ius cos . The area of a st r ip
bound ed by t wo near par allels appear s const ant on t he rect angular char t :
t he t r u e globe ar ea has d ecr eased by an amou nt cos becau se of
red uct ion in t he bound ing par allels circumferent ial lengt h. Red ucing t he
x scale of t he char t by t his fact or maint ains const ant area bet ween char t
and globe. The result ing for m resembles t he mod ied cylind r ical projec-
t ion except t hat t he slid ing scale fact or is now applied t o t he x axis:
x s cos
y s .
This project ion owes it s name t o t he sinusoid s t hat for m t he map ed ge
and reference mer id ians (see Fig. 3) and can be d er ived as t he limit ing
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 314
Longit u d e:60W t o 120E (gr id 12.5) Lat it u d e: 25S t o 90N (gr id 12.5)
Sansons Si nusoi dal
Figure 3. SansonFlamst eed equal-area project ion.
case of Bonnes gener al p roject ion of equ isp aced char t p ar allels. The
sinusoid al project ion is useful in replacing t he polar d iscont inuit y of
cylind r ical project ions wit h a zero: t he char t is bound ed and d epict s one
hemisphere. Though at t r ibut ed t o Sanson, t his char t was well-known t o
Mercat or.
Azi mut hal Pr oj ect i ons Azi mut hal Pr oj ect i ons Azi mut hal Pr oj ect i ons Azi mut hal Pr oj ect i ons Azi mut hal Pr oj ect i ons
In t his family a plane is placed t angent t o t he sphere and r ays ext end ed
from a point of project ion d ene a cor respond ence bet ween point s of
int er sect ion on t he sphere and t he char t . The project ions are d er ived in
polar for m: spher ical t r igonomet r y provid es a means of changing t he
und er lying coord inat e syst em, t hus d ening t he point of t angency. Wit h
t he Nor t h Pole ser ving as t he point of t angency cor respond ing t o t he
char t cent er, all great circles t hrough t his point (globe mer id ians) project
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 315
as r ad ials t hrough t he char t cent er. Polar bear ing of char t r ad ials is t hus
t aken d irect ly from t he globe: = . Dist ance away from t he char t cent er
is d ened using t he angular measure for colat it ud e: = = / 2 .
By applying t he well-known polar t o rect angular conver sion for mulae
x = r cos , y = r sin , t he ent ire family may be char act er ized by suit -
able choice of r as a funct ion of , which maps angular d ist ance away
from t he globes pole ont o r ad ial d ist ance away from t he char t cent er.
This yield s t he progr am cod e:
polar azimut h project ion of (, ) ont o (x, y)

2
colat it ude: N Pole is 0 degrees; Equat or 90
(more complex for oblique project ions)
r f() lengt h of chart radial
x r cos convert (r, ) polar chart
y r sin coordinat es int o rect angular (x, y)
Rever se mapping from char t t o spher ical coord inat es is accomplished
by conver t ing from rect angular t o polar for m and t hen applying t he
inver se funct ion = f
1
(r). These funct ion pair s are t abulat ed in Fig. 4,
wit h shor t d escr ip t ions following. Not e t hat for all fu nct ions f(x)
f
1
(x) x when x 0; t his may be ver ied t hrough Taylor expansion.
Sample azimut hal plot s appear in Figs. 5a and 5b.
Th e gnomonic or cent ral p r oject i on i s n ei t h er con for mal n or ar ea
preser ving. By placing t he r ay or igin at t he spheres cent er, all st r aight
lines on t he char t are geodesicsgreat circle pat hs of shor t est d ist ance
because any t wo project ed r ays d ene bot h t he char t line and a plane
t hat cut s t he sphere t hrough it s cent er. Dist or t ion grows t o innit y along
Name r = f() = f(r)
Gnomonic t an t an
1
r
St ereogr aphic 2 t an(/ 2) 2 t an
1
r/ 2
Equid ist ant r
Equal-Area 2 sin(/ 2) 2 sin
1
r/ 2
Or t hogr aphic sin sin
1
r
Figur e 4. Azimut hal project ion par amet er s.
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 316
Longit u d e: 90E (gr id 15) Lat it u d e: 0N (gr id 15) Longit u d e: 80.6W (gr id 10) Latitude: 43.5N (grid 10)
Orthographi c (transverse) Stereographi c (obl i que)
Figure 5. Azimut hal project ions.
a hemisphere, requir ing t he omission or clipping of coord inat es when
/ 2. R. Buckminst er Fuller creat ed a dymaxion gnomonic pr ojec-
t ion by cent r ally project ing t he sphere ont o an inscr ibing icosahed ron,
allowing great -circle rout es t o remain lines (albeit broken when crossing
t he ed ge bet ween t wo non-cont er minous faces) when d eveloped as a
sheet of 20 broken t r iangles.
The st ereographic map places t he point of project ion at t he t angent
point s opposit e pole or ant ipode. It is remar kable in being bot h confor-
mal and circle preser ving: circles of any r ad ius (lines when t he r ad ius of
cur vat ure ) map freely bet ween t he char t and sphere. This feat ure
may obviously be exploit ed in mixed proximit y t est ing and clipping using
bot h wor ld and d evice coord inat es. Because it rend er s an ent ire sphere
ont o a p lane, it nd s gener al scient ic u se in mod eling sp her ically
d ist r ibut ed point set s, as in cr yst allogr aphy. An example appear s in Fig.
5b; not e in par t icular t hat globe mer id ians (small circles) are nonconcen-
t r ic circles on t he char t .
The azimut hal equidist ant p r oject ion equ at es r wit h colat it u d e (a
r are case in which an angle measu re ap p ear s d irect ly and not as a
t r igonomet r ic argument ), t hereby yield ing a map t hat gives simult aneous
bear ing and r ad ial d ist ance for great -circle t r avel away from t he char t
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 317
cent er. It nd s applicat ion in t he comput at ion of head ing and d ist ance, as
when aligning ant ennas for t r ansoceanic broad cast elect romagnet ic r a-
d iat ion follows geod esics.
The az imut hal equal-area p r oject ion emp loys a simp le scale change
t o account for t he area cont r ibut ions of successive annuli away from t he
ch a r t cen t er a s r ch a n ges i n cr emen t a l l y. It r esembl es t h e globular
project ion d er ived for simplicit y of const ruct ion t hat has been impr int ed
billions of t imes on jour nals of t he Nat ional Geogr aphic Societ y.
Spher i cal Coor di nat e Tr ansf or mat i on Spher i cal Coor di nat e Tr ansf or mat i on Spher i cal Coor di nat e Tr ansf or mat i on Spher i cal Coor di nat e Tr ansf or mat i on Spher i cal Coor di nat e Tr ansf or mat i on
Azimut h project ions have lit t le value when project ed merely at t he Nor t h
Pole. Spher ical t r igonomet r y may be employed t o reor ient t he und er lying
polar coord inat e syst em about a new t angent point (
0
,
0
). By applying
coord inat e t r ansfor mat ions ar bit r ar ily, t he aspect of any project ion may
be changed from normal t o oblique. As a special case, or ient at ion at 90
d egr ees yield s t ransverse p r oject ions (Figu res 5a, 5b). The following
equat ions reexpress t he point s (, ) about t he reference point (
0
,
0
) in
t er ms of angular d ist ance and r ad ial bear ing (, ) as required by t he r st
t wo lines of t he gener ic azimut hal pseud o-cod e. These for mulae are also
encou nt ered when map p ing fr om ast ronomical (, ) p air s ont o t he
celest ial sphere in t er ms of hor izon (compass) angle and elevat ion for an
obser ver at a given locat ion, or when comput ing great -circle d ist ances
and init ial head ing.
= cos
1
[sin sin
0
+ cos cos
0
cos (
0
)]
= sin
1

sin sin
0
cos
cos
0
sin

1
]
1,
in which s supplement ( ) or (180 ) is used when (
0
) < 0.
This form maps latitude and longitude into counterclockwise bearing (longitude)
a n d d i st a n ce (col a t i t u d e). Wh en emp l oyed t o gi ve con ven -
t ional lat it ud e or clockwise bear ing, angular argument s must be ad -
just ed by use of angular complement s: / 2 ; in many cases t his is
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 318
mad e implicit by rever sing t he roles of cos and sin. Subrout ine implemen-
t at ion fur t her requir es cos
1
and sin
1
rout ines wit h full four-quad r ant
oper at ion, t hat is, rout ines t hat ret ur n a proper ly signed angle.
Gener al Pr oj ect i ons Gener al Pr oj ect i ons Gener al Pr oj ect i ons Gener al Pr oj ect i ons Gener al Pr oj ect i ons
Azimut h maps may be gener alized by moving t he point of project ion off
t he globe. This gener alit y ult imat ely lead s back t o t he perspect ive and
ort hographic project ions. These and t he cylind r ical project ions belong t o
t he conics, t he largest family of maps. Geomet r ically, a cone encircling a
sphere possesses a cent r al, symmet r ic point . Moving t his apex ont o t he
sphere at t ens t he cone int o a t angent ial circle. Moving t he apex t o
innit y creat es a cylind er. The conic family includ es pair s of bot h confor-
mal and equ al-ar ea p roject ions. The mor e accu r at e p roject ionst he
Lambert Conformal Conic a n d A lbersemp l oy a secant con e, wh i ch
cu t s t h e gl obe at t wo st andard parallels, t h er eby bet t er t t i n g t h e
sphere. The Lamber t nd s almost univer sal applicat ion in aeronaut ical
sect ionals, since it is confor mal and represent s great cir cle rout es in
near ly st r aight lines. H. C. Alber s project ion is id eally suit ed t o t he
d epict ion of cont inent al land masses of large longit ud inal ext ent and
frequent ly appear s in at lases of Nor t h Amer ica. (See also Alber s Equal
Area Conic Map Project ion on page 321.)
Fu r t her gener alizat ion yield s t he p olyconics, whose rend er ing was
vir t u ally int r act able before t he invent ion of t he compu t er. The lat t er
represent t he broad est project ion class in which x = f(, ) and y =
g(, ). While excellent for problems of applied car t ogr aphy, such for ms
are oft en bot h comput at ionally int ensive and lacking any closed -for m
inver ses, leaving t hem u nsu it able for d irect ap p licat ion t o p r oblems
ar ising in comput er gr aphics. Inst ead , we ret ur n t o a simple project ion
and relat ed applicat ion.
Cyl i ndr i cal Equal Ar ea Cyl i ndr i cal Equal Ar ea Cyl i ndr i cal Equal Ar ea Cyl i ndr i cal Equal Ar ea Cyl i ndr i cal Equal Ar ea
This project ion is obt ained by wr apping a unit cylind er around a unit
sphere and project ing par allels of lat it ud e d irect ly out ward along planes
nor mal t o t he polar axis. This geomet r ical d er ivat ion yield s t he pseud o-
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 319
cod e
x s
y s sin .
As t he cylind er is t angent along t he equat or, t his t hin region preser ves
area and angle. In regions of increasing lat it u d e, t he sp her ical shell
locat ed bet ween par allels is a project ed area, which is foreshor t ened by
an amount cos on t he cylind er, t hereby red ucing t he char t area along
t he y axis. However, t his loss is per fect ly offset by t he increase in t he
char t s represent at ion of each p ar allels circu mferent ial lengt h, which
grows by cos as d escr ibed for Sansons sinusoid al project ion.
As a consequence of t his innit esimal equat ion, a unit sphere and unit
cylind er have id ent ical sur face area (known t o t he Greeks before Newt ons
calculus). As a quick check, t he spheres area is 4r
2
(or 4 st er ad ians
for a unit sphere). Similar ly, t he cylind er has unwr apped area w h wit h
w = 2r and h = 2r giving 4r
2
, in agreement . Being bot h cylind r ical
and comput at ionally efcient , t his project ionr st at t r ibut ed t o Lamber t
is anot her cand id at e t o supplant t he all-t oo-common d irect plot xy(, )
approach.
A Pr act i cal Appl i cat i on A Pr act i cal Appl i cat i on A Pr act i cal Appl i cat i on A Pr act i cal Appl i cat i on A Pr act i cal Appl i cat i on
Consid er t he rend er ing of an explod ing rewor ks shell, whose fr agment s
show no und er lying d irect ional biast he ame t r ails suggest a d and elion.
How are t he fr agment vect or s gener at ed ? Consid er a st at ionar y shell
explod ing at t he or igin (t he t ype loat hed by leat her-clad pyrot echnicians)!
Fr agment s have an equal likelihood in piercing an encompassing sphere
t heir ux d ensit y is equal per unit area. An equal-area project ion (map)
of t he penet r at ion will be a t wo-d imensional scat t er plot wit h const ant
d ensit y. Tur ning t he problem around , plot t ing r and om point s of linear
d ist r ibut ion along t wo ind epend ent axes of a 2D equal-area project ion
and t hen using t he inver se project ion for ms a unifor m spher ical d ist r ibu-
t ion.
The project ion of choice is cylind r ical (one axis of t r ansfor mat ion is
linear ), suggest ing t he above cylind r ical equal-area project ion. It s inver se
t r ansfor mat ion is = u and = sin
1
v giving a char t running from
/ 2 t o +/ 2 in u and from 1 t o +1 in v. Here (, ) are on t he
sphere and (u, v) are char t coord inat es gener at ed along t heir int er vals
V.5 DIGITAL CARTOGRAPHY FOR COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 320
using a convent ional random( ) rout ine wit h suit able offset and scaling.
The inver se map for ms cor respond ing (, ) values. Finally, t he (x, y, z)
locat ion of a par t icle is d er ived by conver sion from spher ical t o rect angu-
lar coord inat es:
x = R cos cos = R cos u cos sin
1
v
y = R sin cos = R sin u cos sin
1
v
z = R sin = R sin sin
1
v.
Wit h sin
1
x = cos
1

1 x
2
, t he r st t wo equ at ions may be fu r t her
simplied . R = 1 places t he point s in spher ical d ist r ibut ion on a unit
sphere about t he or igin. This yield s t he progr am cod e:
Dist ribut e random point s (x, y, z) uniformly on t he unit sphere
wit h result s also in (, ) polar coordinat es.
random( ) ret urns a random, uniform dist ribut ion on t he range [0..1].
(2 r and om () 1) [ ..]
z 2 r and om () 1 [ 1..1]
s

1 z
2
x s cos
y s sin
at an2(z, s) convent ional four-quadrant t an
1
The cod e d ist r ibut es point s linear ly along t he z axis because par allel
cross-sect ions t hrough a spher ical shell gener at e annuli of equal ed ge
area, as explained above. Axial bear ing t o x and y also for m a linear
d ist r ibut ion. The square root t er m scales t he spat ial d ist r ibut ion from
cylind r ical int o spher ical coord inat es. Wit h t his t ool in hand , one can
immed iat ely ext end t he rewor ks mod el wit hout fur t her regard ing t he
specics of t he und er lying project ion. Ext ensions might includ e fr ag-
ment s of r and omized mass d ist r ibut ion wit h r ad ial velocit ies in inver se
propor t ion (t hus conser ving t he cent er of mass), or par abolic fr agment
t r aject or ies for par t icles of appreciable mass.
321
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 321
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
V. 6 V. 6 V. 6 V. 6 V. 6
A AA AALBERS EQUAL-AREA LBERS EQUAL-AREA LBERS EQUAL-AREA LBERS EQUAL-AREA LBERS EQUAL-AREA
C CC CCONIC MAP PROJECTION ONIC MAP PROJECTION ONIC MAP PROJECTION ONIC MAP PROJECTION ONIC MAP PROJECTION
Paul D. Bame
Hewlet t -Packard
Colorado Springs, Colorado
Map project ions are used t o show a por t ion of a sphere or ellipsoid ,
usually ear t h, in t wo d imensions in some reasonable wayt hey usually
t r y t o red uce a specic t ype of d ist or t ion. Different project ion algor it hms
are suit ed t o d ifferent t ypes of maps. The Alber s project ion d escr ibed
here is a popular and suit able project ion for Nor t h Amer icapar t icular ly
t he cont inent al Unit ed St at es. Check wit h t he U.S. Geological Sur vey
Bullet in (Snyd er, 1984) for d et ails of ot her project ion algor it hms and
t heir use.
The Alber s project ion is a par t icular way t o project a sphere or ellipsoid
ont o a cone. The cone is t hen unrolled , yield ing t he project ed coord i-
nat es. Because t he ear t h is an ellipsoid r at her t han a sphere, t he calcula-
t ions are pret t y painful.
Imagine int er sect ing a cone wit h t he ear t h so t hat t he int er sect ion is
t wo circles of const ant lat it ud e (see Fig. 1). The lat it ud es of t hese circles
of i n t er sect i on a r e ca l l ed nort hlat a n d sout hlat i n t h e albers_set up
rout ine and called st and ard par allels, by USGS. Each point on t he ear t h
is project ed ont o t he cone as if a nor mal from t he sur face of t he cone at
each point were int er sect ed wit h t he ear t h. At t he st and ard par allels t he
nor mal is of zero lengt h and t he map project ion has least d ist or t ion.
Dist or t ion increases wit h d ist ance from t hese lat it ud es.
The st and ard par allels are usually chosen t o be bet ween t he nor t her n
and sout her n limit s of t he map t o be plot t ed so t hat t he d ist or t ion is about
equal ever ywhere. The st and ar d par allels may span t he equat or but may
not be equid ist ant from it . In ad d it ion t o t he st and ard par allels, a cent er
322
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 322
Figure 1. Cone-ellipsoid int er sect ion.
p oi n t (d en ot ed by originlon an d originlat ) mu st be ch osen for t h e
project ion in Fig. 2.
Project ing maps of t he sout her n hemisphere is d iscussed in USGS. One
simple met hod of project ing maps ent irely in t he sout her n hemisphere is
t o inver t t he sign of lat it ud es and project ed Y values.
323
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 323
Figure 2. Cont inent al Unit ed St at es project ion. Dashed lines show st and ar d par allels
and cross mar ks t he cent er of t he pr oject ion.
USGS Standard Paral l el s
Region Nor t h Par allel Sout h Par allel
Cont inent al USA 45.5 29.5
Nor t h Amer ica 60.0 20.0
Alaska 65.0 55.0
Hawaii & Phillipines 18.0 8.0
Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on
Implement ing map project ions is similar t o implement ing hash t ables.
One usually consult s a reference, nd s t he appropr iat e algor it hm, and
t r anslat es t he reference pseud o-cod e int o t he language of choice. De-
t ailed und er st and ing is oft en not required . Det ailed d escr ipt ion of t he
for mulae present ed here is not given. Those int erest ed might d er ive t he
simpler spher ical Alber s project ion and cer t ainly should obt ain USGS,
from which most of t he not at ion and mat h was t aken.
324
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 324
Two funct ions are used repeat ed ly in t he calculat ions. The r st , d e-
not ed by q F(lat ), is given a lat it ud e and prod uces q:

q(1e
2
)
sin(lat)
1e
2
sin
2
(lat)

1
2e
ln
1e sin(lat)
1+e sin(lat)

_
,

1
]
1
1
1
,
where e is t he eccent r icit y of t he ear t h, which for t he Clar ke ellipsoid is
0.0822719. The following funct ion d enot ed by m G(lat ), prod uces m
given a lat it ud e:

m cos
lat
1 e
2
sin
2
lat ( )

_
,
.
Bot h t hese funct ions have common and repeat ed t er ms and run-t ime ma
be saved by precomput ing and reusing t hese t er ms. Also not e t hat in t he
for mu lae t o follow, m valu es are always squ ar ed befor e u se, which
su ggest s mod ifying G(lat ) t o p rod u ce m
2
r at her t han m. For clar it y,
none of t hese enhancement s are mad e here.
Sever al values are usually precomput ed from t he project ion par amet er s
t o save comput e t ime. These values are lon
0
, t he longit ud e at t he cent er
of t he map; n, t he cone const ant ; C and
0
see USGS for specic
d enit ions. The albers_set up rout ine calculat es t hese values.
alber s_set up(nor t hlat ,sout hlat ,or iginlat ,or iginlon).
begi n
end;

lon
0
originlon
q
1
F southlat ( )
m
1
G southlat ( )
q
2
F northlat ( )
m
2
G northlat ( )
q
0
F originlat
( )
n
m
1
2
m
2
2
q
2
q
1
C m
1
2
+ nq
1

0

C nq
0
n
325
V.6 ALBERS EQUAL-AREA CONIC MAP PROJECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 325
Aft er t his set up has been per for med , t he following rout ine is used t o
project each [lon, lat ] pair t o a Car t esian [x, y] pair :
alber s_project (lon,lat ,x,y)
begi n

q F lat

_
,

q n lon lon
0

_
,

r
Cnq
n
x and y are in t erms of eart h radii. Mult iply each by eart hs
radius in miles (kilomet ers) t o convert t o miles (kilomet ers)

x sin ( )
y
0
cos ( )
end;
See Append ix 2 for C Implement at ion (726)
326
I.2 USEFUL TRIGONOMETRY
GRAPHIC GEMS I Edit ed by DAVID KIRK 326
B BB BBOXES AND OXES AND OXES AND OXES AND OXES AND
SPHERES SPHERES SPHERES SPHERES SPHERES
SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
Ma n y comp u t er gr a p h i cs a p p l i ca t i on s i n vol ve r ect a n gl es a n d
circles or boxes and spheres. The r ect a n gl e i s common beca u se
i t i s t h e sh a p e common l y a ssu med for a p i xel ; i n 3D, t h e box
cor r esp on d s t o a p op u l a r voxel sh a p e. Th e ci r cl e a r i ses fr om
t h e u se of r a d i a l l y symmet r i c sa mp l i n g a n d r econ st r u ct i on l -
t er s, a n d t h e d esi r e t o r en d er ci r cl es. Sp h er es a r e a p op u l a r
shape for use as geomet r ic pr imit ives and bound ing volumes.
Often one simply needs to know if a given rectangle and
circle (or box and sphere) overlap. If the exact geometry of
overlap is needed it may then be computed. A 2D algorithm for
rectangles and circles was presented in the chapter on 2D
Geometry. The following two Gems discuss the problem in 3D.
Th e r st Gem p r ovi d es a n a l gor i t h m for sca n -con ver t i n g a
sp h er e i n t o a n a r r a y of 3D voxel s. Th i s a l gor i t h m begi n s wi t h
t h e object , an d t h e r esu l t i s a l i st of boxes t h at ar e i n t er sect ed .
Th i s t ech n i qu e ma y be a d a p t ed t o ot h er sh a p es, su ch a s cyl i n -
d er s a n d con es. On e wou l d p r oba bl y u se i t i n a n en vi r on men t
wh er e a r egu l a r gr i d of voxel s a l r ea d y exi st ed , a n d member sh i p
was need ed for each of a var iet y of pr imit ives.
The second Gem is a more direct solution for a sphere and a
given box. It is appropriate when the boxes are of different
sizes, such as an adaptively subdivided space. It may also be
applied to 3D ellipses, and generalized easily to higher dimen-
sions.
See also Fa st Ci r cl e-Rect a n gl e In t er sect i on Ch ecki n g (51); Fa st
Ra y-Box In t er sect i on (395); A Si mp l e Met h od for Box-Sp h er e
Int er sect ion Test ing (335); Ray Tr acing Gems
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 327
S SS SSPHERES-TO-VOXELS PHERES-TO-VOXELS PHERES-TO-VOXELS PHERES-TO-VOXELS PHERES-TO-VOXELS
C CC CCONVERSION ONVERSION ONVERSION ONVERSION ONVERSION
Claudio Mont ani and Robert o Scopigno
Consiglio Nazionale delle Ricerche
Pisa, It aly
The use or prot ot yping of volumet r ic represent at ion schemes based on
t hr ee-d imensional cellst yp ical examp les are t he cu bic fr ame bu ffer
(Ka u fma n a n d Ba ka l a sh , 1988) or t h e oct r ee (Mea gh er, 1982)
schemesoft en requires t he availabilit y of efcient rout ines for conver t -
ing element ar y solid object s int o cells.
In t his not e we present a conver sion algor it hm from spheres t o voxels.
It accept s as input t he d enit ion of a sphere (in t er ms of it s r ad ius and
cent er ) and ret ur ns t he set of voxels complet ely or par t ially cont ained
int o t he space bound ed by t he sur face of t he sphere, t hat is, a 3D scan
conver sion of t he sphere.
Based on t he well-known Bresenham met hod for t he d iscret izat ion of a
cir cle (Bresenham, 1977), ou r conver sion algor it hm can be easily ex-
tended to cylinders or cones, in which the main axis is parallel to either of
t he Car t esian axes.
The proced ure Circle d escr ibes Bresenhams algor it hm for t he r ast er i-
zat ion of t he r st (posit ive) quad r ant of an or igin-cent ered circle of r ad ius
R (R is an int eger ). Because of t he symmet r y of t he circle, consid er ing
t he r st quad r ant only is not rest r ict ive, neit her is refer r ing t o an or igin-
cent ered circle. The d et ailed analysis and explanat ion of t his algor it hm
can be found in Roger s (1985). The pixels locat ed by t he algor it hm for a
circle of r ad ius R = 8 are shown in Fig. 1.
Circle (R):
begi n
x 0; y R;
2(1 R);
V. 7 V. 7 V. 7 V. 7 V. 7
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 328
Figure 1.
limit

0;
Pixel (x,y); Pixel(x, y) light s t he pixel having (x, y) coordinat e
whi l e y

limit do
begi n
i f < 0
then begi n


2 + 2y 1;
i f

> 0
then begi n


x x + 1; y y 1;


+ 2x 2y + 2;
end;
el se begi n
xx+1;
+2x + 1
end;
end;
el se i f > 0
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 329
Pixel(x,y);
end;
end l oop;
end Circle;
The algor it hm for t he conver sion fr om sp her es t o voxels ap p lies
Bresenham' s met hod t wice: r st d ivid e t he sphere int o hor izont al slices,
t hen d iscret ize t he slices. Though we will consid er an or igin-cent ered
sphere, one can easily t r anslat e voxel coord inat es wit h r espect t o t he
cent er.
The proced u re Sphere(R) d iscret izes t he r st qu ad r ant of t he circle,
wit h r ad ius R, obt ained by int er sect ing t he or igin-cent ered sphere wit h
t he plane Z = 0 (see Fig. 2). The abscissa of t he r ight most pixel of each
line Y = y will represent t he r ad ius of t he circle being d iscret ized , which
lies on t he XZ plane of height y. Then, t he proced ure Slice is called before
t h e y coor d i n at e ch an ges, t h at i s, on t h e r i gh t most p i xel of each
line.
Sphere (R):
begi n
x 0; y R;
2(1 R); limit 0;
whi l e y

limit do
then begi n
2 2x 1;
i f > 0
then begi n
y y 1;
2y + 1;
end;
el se begi n
x x + 1; y y 1;
+ 2x 2y + 2;
end;
end;
el se begi n
x x + 1; y y1;
+ 2x 2y + 2;
end;
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 330
Figure 2.
begi n
i f < 0
then begi n
2 + 2y1;
if >0
then begi n
Slice(x, y);
x x + l; y y l;
+ 2x 2y + 2;
end;
el se begi n
x x + l;
+ 2x + 1;
end;
end;
el se i f > 0
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 331
whi l e y

2 limit do
then begi n
2 2x 1;
i f > 0
then begi n
Slice(x, y);
y y l;
2y + 1;
end;
el se begi n
Slice(x, y);
x x + l; y y l;
+ 2x 2y + 2;
end;
end;
el se begi n
Slice(x, y);
x x + l; y y l;
+ 2x 2y + 2;
end;
end;
endl oop;
end Sphere;
The proced ure Slice(r, y) d iscret izes t he r st quad r ant of t he circle of
r ad ius r belonging t o t he plane Y= y. For each pixel d et er mined by t he
proced ure, t he rout ine Track(x ,y, z) is called . In t u r n, t he lat t er calls
Voxel(x, y, z) on t he ind ices of t he voxels belonging t o t he int er val from
(x, y, z) t o (x, y, z). As is t he case of t he Pixel rou t ine u sed by t he
Circle p r oced u r e, t h e Voxel r ou t in e is imp lemen t ion -d ep en d en t : t h e
voxel coord inat es could be st ored , used t o set an element in t he cubic
fr ame buffer, or used t o build an oct ree.
Slice(r, y):
begi n
x 0; z r ;
2(1 r ); limit 0;
Tr ack(x, y, z);
whi l e z > limit do
begi n
i f < 0
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 332
then begi n
2 + 2z 1;
i f > 0
then begi n
x x + l; z z l;
+ 2x 2z + 2;
Tr ack(x,y,z);
Tr ack( x,y,z);
end;
el se begi n
x x + l; + 2x + 1;
Tr ack(x, y, z);
Tr ack(x, y, z);
end;
end;
el se i f > 0
then begi n
2 2x 1;
i f > 0
then begi n
z z 1;
2z + 1;
end;
el se begi n
x x + l; z z l;
+ 2x 2z + 2;
Tr ack(x, y, z);
Tr ack(x, y, z);
end;
end;
el se begi n
x x + l; z z l;
+ 2x 2z + 2;
Tr ack(x, y, z);
Tr ack( x, y, z);
end;
endl oop;
end Slice;
end;
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 333
Figur e 3.
Due t o t he symmet r y of t he sphere, if x 0 t he proced ure Slice calls
Track also on t he point (x, y, z), while if y 0 t he proced ure Track
invokes Voxel also on t he list of voxels in t he int er val from (x, y, z)
t o (x, y, z).
It is wor t h not ing t hat Slice d oes not call Track in case of ver t ical
d isplacement ; in fact , in t his sit uat ion, t he list of cor respond ing voxels
has just been d et er mined . In Fig. 3 t he set s of voxels prod uced by t he
algor it hm for t he gener ic (x, y, z) point are d epict ed .
Tr ack(x, y, z):
begi n
for k: i nteger z, k k + 1, k < z do
Voxel (x, y, k);
endl oop
i f y 0
then
for k: i nteger z, k k + 1, k < z do
Voxel(x, y, k);
endl oop;
end Tr ack;
V.7 SPHERES-TO-VOXELS CONVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 334
While an O(R
3
) number of voxels is ret ur ned , t he comput at ional cost of
t he algor it hm is propor t ional t o R
2
(R being t he r ad ius of t he sphere), wit h
a small propor t ionalit y const ant . This is t rue for t wo reasons: one is because
t he Sphere and Slice proced ures only wor k on t he posit ive quad r ant s of t he
circles t o be d iscret ized ; t he ot her is t hat t he act ivat ion of Sphere wit h r ad ius
R causes t he act ivat ion of Slice R t ime wit h par amet er r (t he r ad ius of a
slice) such t hat 1 < r

R.
V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 335
A AA AA SIMPLE METHOD FOR SIMPLE METHOD FOR SIMPLE METHOD FOR SIMPLE METHOD FOR SIMPLE METHOD FOR
B BB BBOX SPHERE OX SPHERE OX SPHERE OX SPHERE OX SPHERE
I II IINTERSECTION TESTING NTERSECTION TESTING NTERSECTION TESTING NTERSECTION TESTING NTERSECTION TESTING
James Arvo
Apollo Syst ems Division of Hewlet t -Packard
Chelmsf ord, Massachuset t s
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
There are a number of comput er gr aphics applicat ions in which it is
necessar y t o d et er mine whet her a sphere int er sect s an axis-aligned par al-
lelep ip ed , or box. In t wo d imensions t his ar ises when r ast er izing
circles, which are t wo-d imensional spheres, int o rect angular viewpor t s,
which are t wo-d imensional boxes. The int er sect ion t est is used t o d et er-
mine whet her any por t ion of t he circle is ind eed visible before r ast er izing.
In t hree d imensions t his oper at ion may ar ise in spat ial subd ivision t ech-
niques t hat id ent ify voxels, or 3D boxes, pierced by t he sur faces of
var ious object s. If spheres are among t he object s, or are used as bound ing
volumes for ot her object s, we need t o t est for 3D box-sphere int er sec-
t ion.
This not e d escr ibes a simp le met hod for d et ect ing int er sect ions of
n-d imensional boxes wit h n-d imensional spheres t aken as eit her sur faces
or solid s. Applicat ions for n > 3 are not ad d ressed here, t hough t he
algor it hm wor ks in any d imension. For great est gener alit y all algor it hms
shown below t ake t he d imension of t he space, n, as an input par amet er,
t hough t ypical implement at ions would be opt imized for eit her t wo or
t hree d imensions.
Sol i d Obj ect s Sol i d Obj ect s Sol i d Obj ect s Sol i d Obj ect s Sol i d Obj ect s
Suppose we wish t o d et er mine whet her a solid n-d imensional box, B,
int er sect s a solid n-d imensional sphere wit h cent er C and r ad ius r. Here
V. 8 V. 8 V. 8 V. 8 V. 8
V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 336
solid means t hat we includ e t he int er ior as well as t he bound ar y.
Denot e C by (C
1
, . . . , C
n
) and B by t he closed int er vals

[B
1
min
, B
1
max
],
. . . ,

B
n
min
, B
n
max
[ ]
. We can per for m t his t est by nd ing t he point P on or in
t he box t hat is closest t o t he cent er of t he sphere. If t he d ist ance bet ween
t his point and C is no great er t han r, t hen t he box int er sect s t he sphere.
Thus, if t he point P R
n
minimizes t he d ist ance funct ion

dist(P) (C
1
P)
2
+ ...+(C
n
P
n
)
2
,
subject t o t he const r aint s
t hen t he solid s int er sect if and only if d ist (P) r. As a simplicat ion we
will eliminat e t he square root by comput ing squared d ist ances and com-
par ing wit h

r
2
. It is a simple mat t er t o nd t he P t hat minimizes t he
square of Eq. 1 because each t er m of t he sum is nonnegat ive and can be
minimized ind ep end ent ly. If t he it h coord inat e of t he sp here cent er
sa t i ses t h e it h con st r a i n t , t h a t i s, i f

B
i
min
C
i
B
i
max
, t h en set t i n g
P
i
= C
i
red uces t his t er m t o zero. Ot her wise, we set

P
i
equal t o eit her

B
i
min
or B
i
max
d epend ing on which is closer. Summing t he squares of t he
d ist ances prod uces t he minimum t ot al squared d ist ance. The algor it hm in
Fig. 1 uses t his pr inciple t o d et er mine whet her a solid box int er sect s a
solid sphere. The input par amet er s are t he d imension n, box B, sphere
boolean funct ion Solid Box_Solid Sphere(n, B, C, r )
begi n

d
min
0;
for i 1 . . . n d o
i f
el se
i f
endl oop;
i f

d
min
r
2
then return [True]
el se return [False];
end;
Figure 1. An algor it hm for int er sect ing a solid n-d imensional box wit h a solid nd imen-
sional sphere.
(1)
(2)

C
i
<B
i
min
then d
min
d
min
+(C
i
B
i
min
)
2
;

C
i
>B
i
max
then d
min
d
min
+(C
i
B
i
max
)
2
;

B
i
min
P
i
B
i
max
for i 1,..., n,
V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 337
cent er C, and sphere r ad ius r. The funct ion value is ret ur ned as True if
t he object s int er sect and False ot her wise.
Hol l ow Obj ect s Hol l ow Obj ect s Hol l ow Obj ect s Hol l ow Obj ect s Hol l ow Obj ect s
If we wish t o make eit her or bot h of t he object s hollow and t est only t heir
sur faces, we can d o so by regard ing t ot al inclusion of one object insid e
t he ot her as nonint er sect ion. For inst ance, if we wish t o t est whet her t he
sur face of t he sphere int er sect s t he solid box, we can ad d a t est for
whet her t he box is ent irely cont ained wit hin t he sphere and regard t his as
nonint er sect ion. This is shown in t he algor it hm of Fig. 2, in which weve
ad d ed t he comput at ion of t he square d ist ance from C t o t he far t hest
point of B. If t his value, d enot ed

d
max
,
is less t han

r
2
, t he ent ire box is
insid e t he sphere and t here is no int er sect ion.
The approach is d ifferent if we wish t o d isregard t he int er ior of t he box.
Here t he const r aint s of Eq. 2 st ill hold but in ord er for t he point P t o be
on t he bound ar y of t he box we require t he ad d it ional const r aint t hat

P
i
B
i
min
or P
i
B
i
max
for some i. In t he algor it hm of Fig. 1 we see t hat
t h i s h ol d s u n l ess

C
i
(B
i
min
, B
i
max
)
for al l i. In t h i s case we n eed t o
d et er mine whet her moving P t o t he nearest face of t he box places it
boolean funct ion Solid Box_HollowSphere(n, B, C, r )
begi n
d
max
0;
d
min
0;
f or i 1 . . . n do
a (C
i


B
i
min
)
2
b (C
i


B
i
max
)
2
;
d
max
d
max
+ max(a, b);
i f C
i


B
i
min
, B
i
max
[ ]
then d
min
d
min
+ min(a,b);
endl oop;
i f r
2
[d
min
, d
max
] then return [True]
el se return [False];
end;
Fi gu r e 2. An al gor i t h m for i n t er sect i n g a sol i d n-d i men si on al box wi t h a h ol l ow
n-d i men si on al sp h er e.
V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 338
(3)
boolean funct ion HollowBox_Solid Sphere(n, B, C, r )
begi n
d
min
0
d
face
infinit y
f or i 1 . . . n do
t min((C
i


B
i
min
)
2
,(C
i


B
i
max
)
2
);
i f C
i
[

B
i
min
, B
i
max
] t hen d
face
min(d
face
,t );
el se begi n
d
face
= 0;
d
min
d
min
+ t ;
end
endl oop;
i f d
min
+ d
face
r
2
then return [True]
el se return [False];
end;
Figure 3. An algor it hm for int er sect ing a hollow n-d imensional box wit h a solid n-
d imensional sphere.
out sid e t he sphere. In Fig. 3 we have mod ied t he algor it hm t o comput e
t he smallest such ad just ment , d enot ed d
face
.We ad d t his t o t he minimum
d ist ance bet ween t he box and C, and if t he result remains less t han r
2
,
t he sur face of t he box int er sect s t he solid sphere. The approaches in Fig.
2 and Fig. 3 can be combined t o t est for int er sect ion bet ween a hollow
box and a hollow sphere.
Gener al i zi ng t o El l i psoi ds Gener al i zi ng t o El l i psoi ds Gener al i zi ng t o El l i psoi ds Gener al i zi ng t o El l i psoi ds Gener al i zi ng t o El l i psoi ds
We can easily gener alize t his id ea t o wor k wit h axis-aligned ellipsoid s,
t hat is, ellipsoid s t hat result from scaling a sphere d ifferent ly along t he
coord inat e axes. We can specify such a ellipsoid in n-space by it s cent er,
C[R
n
, a n d a r a d i u s for ea ch a xi s, a
1
, . . . , a
n
. Th e p oi n t P R
n
is on or in such an ellipsoid if and only if

C
1
P
1

1
]
1
2
+ +
C
n
P
n

1
]
1
2
1.
is on or in such an ellipsoid if and only if
V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 339
boolean funct ion Solid Box_Solid Ellipsoid (n, B, C, )
begi n
d
min
0;
f or i 1 ... n do
i f C
i
<

B
i
min
then

d
min
d
min
+
C
i
B
i
min

1
]
1
2
;
el se
i f C
i
>

B
i
max
then

d
min
d
min
+
C
i
B
i
max

1
]
1
1
2
;
endl oop;
i f d
min
1 then return [True]
el se return [False];
end;
Figure 4. An algor it hm for int er sect ing a solid n-d imensional box wit h a solid n-d imen-
sional axis-aligned ellipsoid .
Mod ifying t he algor it hm in Fig. 1 t o hand le t his t ype of ellipsoid result s
in t he algor it hm shown in Fig. 4. Here t he scalar input par amet er r has
been changed t o t he ar r ay . Mod icat ions for eit her hollow boxes or
hollow ellipsoid s are analogous t o t hose previously d escr ibed .
See Append ix 2 for C Implement at ion (730)
6
3D RENDERING
343
VI.1 3D GRID HASHING FUNCTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 343
VI.1 VI.1 VI.1 VI.1 VI.1
3 33 33 D GRID HASHING D GRID HASHING D GRID HASHING D GRID HASHING D GRID HASHING
F FF FFUNCTION UNCTION UNCTION UNCTION UNCTION
Brian Wyvill
Universit y of Calgary
Calgary, Albert a, Canada
For var ious applicat ions in comput er gr aphics it is oft en necessar y t o sor t
object s int o a 3D gr id of cubes or voxels accord ing t o t heir posit ion in
space. One way of keeping t r ack of which object s are st ored in which
voxels is simply t o keep a 3D ar r ay of voxels in memor y. Voxel gr id s
d ont have t o be ver y large before t his becomes impr act ical, since t he size
of t he ar r ay goes up as t he cube of t he sid es. Somet imes it is convenient
t o st ore t he gr id at a var iet y of resolut ions; t his is d one using an oct ree.
The t ree d ivid es int o eight br anches at ever y nod e. This cor respond s t o
cut t ing t he cube (nod e) along t hree or t hogonal planes int o eight sub-
cubes. A nod e is subd ivid ed if it is required t o examine it s cont ent s in
great er d et ail. If great d ep t h is requ ired , oct rees can also u se lar ge
amount s of memor y. If many of t he voxels are empt y or t he level of d et ail
is known at t he st ar t , a t able of point er s can be used t o represent t he gr id .
Each locat ion in t he t able represent s a group of neighbor ing voxels; a
linked list allows access t o t he voxels in t he group. Each voxel wit hin t he
group point s t o a list of object s t o be found in t hat par t icular voxel. The
problem is how t o make a good hash funct ion t hat can be comput ed from
t he x, y, z locat ion of t he object t o be st ored . The funct ion really act s as
a sor t ing mechanism t o d ist ingu ish gr ou p s of neighbor ing object s.
344
VI.1 3D GRID HASHING FUNCTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 344
Comput i ng t he Hash Funct i on Comput i ng t he Hash Funct i on Comput i ng t he Hash Funct i on Comput i ng t he Hash Funct i on Comput i ng t he Hash Funct i on
If t he object posit ion is char act er ized by t he origin, an int eger coord inat e
t r iple, (x, y, z), t hen t he ad d ress in t he hash t able can be comput ed by
combining t he most signicant few bit s of each of x, y, and z. The t able
cont ains a relat ively small number of ent r ies, each one represent ing a
group of voxels. Object s wit h t he same hash ad d ress are in fact near
neighbor s, which are in t he same group but not necessar ily t he same
voxel.
To comput e t he ad d ress from t he (x, y, z) t r iple it is necessar y t o know
t he lengt h of t he t able and t he number of voxels. For example, if t he
wor ld is t o be d ivid ed int o a cube 256 voxels on a sid e t hen 256*256*256
(16,777,216) locat ions would be required t o st ore t he ent ir e t able. If t he
t able was t o be only 4096 locat ions in lengt h, a t able ad d ress could be
Figur e 1. Find ing a 12-bit ad d ress from x, y, z. In t his example x, y, and z are
shown as 1-byt e values.
345
VI.1 3D GRID HASHING FUNCTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 345
st ored in four bit s for each d imension. (ad d ressed 0 t o 4095). To ensure
t hat an out -of-bound s ind ex is not comput ed , x, y, and z must lie wit hin
a pred ened r ange of posit ive values. For example, if r ange is set t o 256
(8 bit s) and t he hash ad d ress is 12 bit s t hen t he t op 4 bit s of each of x, y,
and z are used t o comput e t his ad d ress. A bit mask is used t o select t he
most signicant four bit s from t he bot t om eight bit s of an x, y, z int eger ;
in t his example t he mask is set t o (11110000) (oct al 360). (see Fig. 1). In t he
C example RANGE is set t o 256, NBITS t o 4.
Not e t hat t he following cond it ions must hold :
log2(RANGE) NBITS,
MIXINT RANGE
0 x RANGE 1
0 y RANGE 1
0 z RANGE 1
2 t o t he power NBITS*3 gives t he lengt h of t he t able.
See also Ray Tr acing (383)
See Append ix 2 for C Implement at ion (733)
346
VI.2 BACKFACE CULLING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 346
VI.2 VI.2 VI.2 VI.2 VI.2
B BB BBACKFACE CULLING ACKFACE CULLING ACKFACE CULLING ACKFACE CULLING ACKFACE CULLING
Jef f Hult quist
NASA Ames Research Cent er
Mof f et t Field, Calif ornia
Convex polygonal object s can be d r awn r apid ly by t est ing each face for
visibilit y. We can const ru ct t he d at abase for each shap e so t hat t he
ver t ices around each face are list ed in a count erclockwise ord er (such an
object is called orient ed).
Once all of t he ver t ices of such an object are mapped int o t he t wo-
d imensional screen coord inat es, t he visible faces will be wr apped in
one d irect ion, and t he invisible faces will be wr apped in t he ot her. We can
t est t he wr apping d irect ion of a face by t aking ad jacent vect or s BA and
BC, which lie along t wo ed ges of t hat face. The cross-prod uct of t hese
t wo ed ges is a vect or t hat will point eit her int o or out of t he screen,
d epend ing on t he wr apping d irect ion.
To d et er mine which ed ges of a convex or ient ed polygonal object are
visible, we can t est t he visibilit y of each face. An ed ge shared by t wo
visible faces is it self visible. An ed ge t hat aligns wit h only one visible face
is on t he silhouet t e of t he object . All ot her ed ges are hid d en from view.
This t est can be used t o d et er mine which ed ges are visible on a convex,
closed , and or ient ed object . Fir st associat e wit h each ed ge a visibilit y
count init ialized t o zero, t hen examine each face of t he object . Use t he
wr apping t est d escr ibed above t o d et er mine if t he polygon is facing int o
or out of t he screen. If it is facing out of t he screen, t hen increment t he
visibilit y count for each of it s ed ges. Once all of t he faces have been
processed , t he visibilit y count for each ed ge will 0, 1, or 2. Ed ges wit h a
347
VI.2 BACKFACE CULLING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 347
count of 2 are fully visible, whereas a 1 ind icat es an ed ge t hat d enes t he
silhouet t e of t he object . Ed ges wit h a count of zero are fully obscured .
See also Scan Conver sion Summar y (75)
Figur e 1.
348
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 348
VI.3 VI.3 VI.3 VI.3 VI.3
F FF FFAST DOT AST DOT AST DOT AST DOT AST DOT
P PP PPRODUCTS FOR RODUCTS FOR RODUCTS FOR RODUCTS FOR RODUCTS FOR
S SS SSHADING HADING HADING HADING HADING
Mark Lee
Amoco Product ion Company
Tulsa, Oklahoma
As images become more sophist icat ed , t he illuminat ion mod els used in
gener at ing t he images are also gener ally more sophist icat ed . These illumi-
nat ion mod els become more and more expensive t o comput e. This ar t icle
d escr ibes a met hod of reusing some of t he comput at ions t o save on t he
t ot al number of ar it hmet ic oper at ions necessar y for evaluat ing t he illumi-
nat ion mod el.
Sever al d irect ion vect or s are need ed when using cur rent illuminat ion
mod els. The following is a glossar y of t he vect or s t o be used in t his ar t icle
(see Fig. 1):
Lt he vect or t hat point s in t he d irect ion of t he light source,
Vt he vect or t hat point s in t he viewing d irect ion,
Nt he nor mal t o t he reect ing or refr act ing sur face,
Ht he nor mal t o an imaginar y sur face t hat would reect light per-
fect ly from L t o V,
Ht he nor mal t o an imaginar y sur face t hat would refr act light per-
fect ly from L t o V,
H
u
an unnor malized for m of H,
H
u
an unnor malized for m of H.
The vect or s N, L, V, H, and H are assumed t o have unit lengt h. Also,
all of t hese vect or s are assumed t o be or ient ed point ing away from t he
349
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 349
Figur e 1. Geomet r y for t he reect ion and refr act ion of light .
sur face. H is used whenever L and V lie on t he same sid e of t he sur face
[(N V)(N L) > 0] and H is used whenever L and V lie on opposit e
sid es of t he sur face [(N V)(N L) < 0].
Gener ally, t he vect or s V, L, and N are known at t he t ime t he illumina-
t ion mod el is invoked . The vect or s H and H are usually gener at ed wit hin
t he illuminat ion mod el t o calculat e t he values N H, N H, V H, and
V H for use in comput ing t he facet d ist r ibut ion funct ion, D, t he facet
self-shad owing fact or, G, and t he Fr esnel reect ance, F (Blinn, 1977;
Cook and Tor r ance, 1982). Gener ally, however, by t he t ime t he t er ms D,
350
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 350
F, and G are t o be evaluat ed , t he t er ms N L and N V have been
gener at ed for u se by ot her p ar t s of t he illu minat ion mod el (d iffu se
reect ance, vect or or ient at ion and so for t h). By ut ilizing t he t er ms N L
and N V, t he vect or s H and H need not be explicit ly creat ed . The
remaind er of t his ar t icle will d iscuss calculat ing N H, N H, V H, and
V H given L, V, N, N L, and N V.
Let s st ar t by examining t he case of t he reect ion of light (see Fig. la).
Remember, H is t he nor mal t o an imaginar y sur face t hat would reect
light per fect ly from L t o V. H is d ened as
H =
L + V
L + V
,
(Cook, 1982; Glassner et al., 1989; Hall, 1989). The t er ms N H and
V H could be calculat ed by r st for ming t he vect or H, and t hen calculat -
ing N H and V H d irect ly. However, by using previously calculat ed
infor mat ion, namely N L and N V, t he number of ar it hmet ic oper at ions
may be d ecreased .
Dene H and H
u
(t he unnor malized for m of H) as follows:
H
u
= L + V and H =
H
u
H
u
.
Remember, H
u
H
u
H
u
( ) , which means t hat
H
u
H
u
L + V
( )
L + V
( )
L V
( )
+ 2 L V
( )
+ V V
( )
2 L V
( )
+ 2

H
u
2 L V

_
,
+ 2.
351
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 351
Now t he fact or s N H and V H can be rewr it t en as follows:
N H N
H
u
H
u

_
,

V H V
H
u
H
u

_
,

Now, compare t he number of ar it hmet ic oper at ions need ed by t he t wo


met hod s t o calculat e N H and V H. Fir st , d ene V = (v
x
, v
y
, v
z
),
L = (l
x
, l
y
, l
z
), N = (n
x
, n
y
, n
z
) and H = (h
x
, h
y
, h
z
).
Direct algorit hm, given, L, N, V, N L and N V
h
x
l
x
+ v
x
h
y
l
y
+ v
y
h
z
l
x
+ v
z
b h
x
h
x

_
,
+ h
y
h
y

_
,

+ h
z
h
z
( )
nd ot h [( n
x


h
x
) + (n
y
h
y
) + (n
z
h
z
)]/ b
vd ot h [( v
x
h
x
) + (v
y
h
y
) + (v
z
h
z
)]/ b
New algorit hm, given, L, N, V, N L and N V
a (l
x
v
x
) + (l
y
v
y
) + (l
z
v
z
) + 1
b
nd ot h [nd ot l + nd ot v]/ b
vd ot h a/ b
This implement at ion of t he d irect algor it hm requires 9 ad d it ions/ sub-
t r act ions, 11 mult iplicat ions/ d ivisions, and 1 square root , whereas t he
implement at ion of t he new algor it hm requires only 5 ad d it ions/ subt r ac-

N H
u
( )
H
u

N L + V
( )
[ ]
H
u

N L
( )
+ N V
( )
[ ]
H
u

N L
( )
+ N V
( )
[ ]
2 L V
( )
+ 2

V H
u
( )
H
u

V L + V
( )
[ ]
H
u

L V
( )
+ V V
( )
[ ]
H
u

L V
( )
+ 1
[ ]
2 L V
( )
+ 2
.
a + a
352
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 352
t ions, 5 mult iplicat ions/ d ivisions, and 1 square root (for scalar-based
machines). The d ifference comes from making more use of previously
calculat ed infor mat ion.
Now, let s examine t he case of t he refr act ion of light (see Fig. 1b). Let
n
i
be t he ind ex of refr act ion for t he med ium of incid ence and n
t
be t he
ind ex of refr act ion for t he med ium of t r ansmit t ance. Fr om Hall (1989)
and Glassner et al. (1989), H can be for mulat ed as
and
Not e, however, t hat

H =
L +
V + L
n
t
n
i
1

_
,

1
]
1
1
1
1
L +
V + L
n
t
n
i
1

_
,

1
]
1
1
1
1
=

n
t
n
i
L + V
n
t
n
i
1

_
,

n
t
n
i
L + V
n
t
n
i
1
if n
i
< n
t
,
if n
i
> n
t
.

n
i
n
t

_
,


n
t
n
i
L V

1
]
1

n
i
n
t

_
,

n
t
n
i
1

1
]
1

n
i
n
t
V + L
n
i
n
t
1
,

H =
V +
L + V
n
i
n
t
1

_
,

1
]
1
1
1
1
V +
L + V
n
i
n
t
1

_
,

=

n
i
n
t
V + L
n
i
n
t
1

_
,

n
i
n
t
V + L
n
i
n
t
1
353
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 353
which proves t hat all of t hese for mulat ions of H are really one and t he
same; t herefore, we will choose

H
n
i
n
t
V + L
n
i
n
t
1

_
,

n
i
n
t
V + L
n
i
n
t
1
Unlike H, H d oes not exist for all congur at ions of L and V. The r st
except ion occur s when n
i
= n
t .
In t his case, H exist s only when L and V
are par allel t o each ot her and point in opposit e d irect ions, in which case,
H = V; t herefore, N H = N V and V H = 1 when H exist s.
The second exception occurs when the law of refraction does not hold.
Refraction is governed by Snells law, which states that n
i
sin
i
= n
t
sin
t

where
i
is t he angle of incidence and
t
is t he angle of t ransmit t ance.
Since 1 sin 1 and n
i
, n
t
> 1, t her e exist n
i
, n
t

i
,
t
such t hat
Snells law cannot be satised. Another constraint is that
i
and
t
must
lie in the range [0,

/ 2].
Su p p ose t h a t n
i
< n
t
. Rea r r a n gi n g Sn el l s l a w gi ves si n
i
=
(n
t
/n
i
)sin
t
. Obviou sly, t he r ight -hand sid e of t he equ at ion can get
larger t han 1, wher eas t he left -hand sid e of t he equat ion cannot . The
point where 1 = (n
t
/n
i
)sin
t
is called t he cr it ical angle,
c
. Beyond t his
angle, t ot al int er nal reect ion occur s; t herefore, t he region of exist ence
for H is 0
t

c
for n
i
< n
t
.
Now, from Fig. 2, t he angle bet ween V and L is (

/ 2
i
) +

/ 2 +
t
.
Using t he fact s t hat
cos

2
+
t

_
,

cos

2
+
t
+

2

i

_
,

_
,

L V
cos

2
+
t
+

2

i

_
,

_
,

,
sin
t
cos

2
+
t

_
,

354
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 354
we can d er ive t he following:

n
i
n
t
= sin
t

cos

2
+
t ( )
cos

2
+
t
+

2

i

_
,

_
,

= L V

n
i
n
t

= L V
Also, (n
i
/n
t
) < (n
t
/ n
i
).
Suppose now t hat n
t
< n
i
. Rear r anging Snells law gives (n
i
/n
t
)sin
i
= sin
t
. The cr it ical angle occur s at (n
i
/n
t
)sin
i
= 1.
Figur e 2.
355
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 355
Figure 3.
From Fig. 3, t he angle bet ween V and L is (

/ 2
t
) +

/ 2 +
i
.
Using t he fact s t hat
sin
i
cos

2
+
i

_
,

we can d er ive t he following:



n
t
n
i
sin
i

cos

2
+
i

_
,


cos

2
+
i
+

2

t

_
,

_
,


L V

n
t
n
i
L V
Also, (n
t
/n
i
) < ( n
i
/n
t
)

cos

2
+
i

_
,

cos

2
+
i
+

2

t

_
,

_
,



L V cos

2
+
i
+

2

t

_
,

_
,

,
356
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 356
Combining t he result s for t he cases n
i
< n
t
and n
t
< n
i
, H exist s if
min ((n
i
/ n
t
),(n
t
/ n
i
)) L V.
The t er ms N H and V H could be calculat ed by r st for ming t he
vect or H, and t hen calculat ing N H and V H d irect ly. However, by
reusing N L and N V, t he number of ar it hmet ic oper at ions may be
d ecreased .
Dene H and H
u
(t he unnor malized for m of H) as
Take a look at t he exp r ession (n
i
/n
t
)
2
+ 2(n
i
/n
t
)(L V) + 1. Since
(n
i
/ n
t
)
2
+ 1 > 0, only L V could be less t han zer o. The expression
2(n
i
/ n
t
)(L V) is smallest when L H = 1. Now, assuming n
i
n
t


1
n
i
n
t
1

_
,

2
n
i
n
t

_
,

2
V V ( ) + 2
n
i
n
t
L V ( ) + L L ( )

1
]
1
1


1
n
i
n
t
1

_
,

2
n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1

1
]
1
1


1
n
i
n
t
1
n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1
H
u
H
u

H
H
u
H
u
.

H
u
H
u

n
i
n
t
V + L
n
i
n
t
1

_
,


n
i
n
t
V + L
n
i
n
t
1

_
,

and


n
i
n
t
V + L
n
i
n
t
1
357
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 357
and L V = 1,
Th er efor e, (n
i
/ n
t
)
2
+ 2(n
i
/ n
t
)(L V) + 1 > n
i
n
t
a n d n o ch eck
need be mad e for t he square root of a negat ive number.
Now, t he d ot prod uct s N H and V H can be refor mulat ed as


1
H
u
N
n
i
n
t
V + L
n
i
n
t
1

_
,

1
]
1
1
1


1
H
u

n
i
n
t
1

_
,


n
i
n
t
N V ( ) + N L ( )

1
]
1

N H N
H
u
H
u

_
,



N H
u
( )
H
u


n
i
n
t

_
,
2
2
n
i
n
t
+ 1

n
i
n
t

_
,
2
+ 2
n
i
n
t
L V ( ) + 1


n
i
n
t
1

_
,
2
> 0.
358
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 358

1

n
i
n
t
1

_
,


1
n
i
n
t
1

n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1


n
i
n
t
N V ( ) + N L ( )

1
]
1

1

n
i
n
t
1

_
,


n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1


n
i
n
t
N V ( ) + N L ( )

1
]
1
sign

1

n
i
n
t
1

_
,


1
n
i
n
t
1

n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1


n
i
n
t
V V ( ) + L V ( )

1
]
1
and


1
H
u

n
i
n
t
1

_
,


n
i
n
t
V V ( ) + V L ( )

1
]
1


1
H
u

V
n
i
n
t
V + L
n
i
n
t
1

_
,

1
]
1
1
1
1


V H
u
( )
H
u

V H V
H
u
H
u

_
,

359
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 359
Once again, compare t he number of ar it hmet ic oper at ions need ed by
t he t wo met hod s t o calculat e N H and V H. Dene H = (h
x
, h
y
, h
z
)
Direct algorit hm, given L, N, V, N L, and N V
ld ot v [(l
x
v
x
) + (l
y
v
y
) + (l
z
v
z
)]
i f (n
i
= n
t
)
then i f (ld ot v = 1)
then begi n
H V
nd ot h nd ot h
vd ot h 1.0
end
el se FLAG nul l
el se begi n
a n
i
/ n
t
i f ( ld ot v min(a, n
t
/ n
i
))
then begi n
d a 1
h
x
[(av
x
) + l
x
]/ d
h
y
[(av
y
) + l
y
]/ d
h
z
[(av
z
) + l
z
]/ d
b

h
x
h
x
( ) h
y
h
y
( )
h
z
h
z
( )
nd ot h [(n
x
h
x
) + (n
y
h
y
) + (n
z
h
z
)]/ b
vd ot h [(v
x
h
x
) + (v
y
h
y
) + (v
z
h
z
)]/ b
end
el se FLAG nul l
end
New algorit hm, given L, N, V, N L, and N V
Id ot v [(l
x
v
x
) + (l
y
v
y
) + (l
z
v
z
)]
i f (n
i
= n
t
)

1

n
i
n
t
1

_
,


n
i
n
t

_
,

2
+ 2
n
i
n
t
L V ( ) + 1


n
i
n
t
+ L V ( )

1
]
1
.
sign
360
VI.3 FAST DOT PRODUCTS FOR SHADING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 360
then i f (ld ot v = 1)
then begi n
nd ot h nd ot h
vd ot h 1.0
end
el se FLAG nul l
el se begi n
a n
i
/ n
t
i f ( ld ot v min(a, n
t
/ n
i
))
then begi n
b [(a + ld ot v + ld ot v) a] + 1
i f(a > 1)
then b

b
el se b

b
nd ot h [(a nd ot v) + nd ot l]/ b
vd ot h (a + ld ot v)/ b
end
el se FLAG nul l
end
If FLAG = nul l , t hen H, N H, and V H d o not exist .
This implement at ion of t he d irect algor it hm requires in t he wor st but
most common case, 12 ad d it ions/ subt r act ions, 22 mult iplicat ions/ d ivi-
sions, and 1 square root , whereas t he implement at ion of t he new algo-
r it hm requires only 7 ad d it ions/ subt r act ions, 9 mult iplicat ions/ d ivisions,
and 1 square root . Once again, t he d ifference comes from making more
use of previously comput ed infor mat ion.
See also Scan Conver sion Summar y (75)
361 GRAPHIC GEMS I Edit ed by ANDREW S. GLASSNER 361
VI.4 SCANLINE DEPTH GRADIENT OF A Z-BUFFERED TRIANGLE
VI.4 VI.4 VI.4 VI.4 VI.4
S SS SSCANLINE DEPTH GRADIENT CANLINE DEPTH GRADIENT CANLINE DEPTH GRADIENT CANLINE DEPTH GRADIENT CANLINE DEPTH GRADIENT
OF A OF A OF A OF A OF A Z- Z- Z- Z- Z-BUFFERED TRIANGLE BUFFERED TRIANGLE BUFFERED TRIANGLE BUFFERED TRIANGLE BUFFERED TRIANGLE
Kelvin Thompson
Nt h Graphics, Lt d.
Aust in, Texas
Given a t r iangle wit h Z d ept h values at it s ver t ices and basic geomet r y as
shown in Fig. 1: if we rend er each scanline of t he t r iangle in t he d irect ion
shown, t hen for each increment in t he X d irect ion, t he Z d ept h will
change by
dz
dx

z
2
z
1
( )
y
3
y
1

_
,
z
3
z
1

_
,
y
2
y
1
( )
x
2
x
1
( )
y
3
y
1

_
,
x
3
x
1

_
,
y
2
y
1
( )
That is, t his is t he Z slope of t he t r iangle wit h respect t o X. This slope
can be used wit h a Bresenhams-like, DDA, or par allel-rend er ing algo-
r it hm (Swanson and Thayer, 1986; Niimi et al., 1984).
The r at io above can also be used for Gour aud rend er ing. On syst ems
wit h color look-up t ables, you simply replace t he Z values wit h color
ind ex values. On full-color syst ems you effect ively need t o have t hree
par allel calculat ionsone for each color in place of each Z calculat ion.
Pr oof Pr oof Pr oof Pr oof Pr oof
We calculat e t he X/ Z slope by ext end ing ed ge

p
1
p
2
t o point

p
4
, which
is hor izont al wit h point

p
3
(see Fig. 2).
362 GRAPHIC GEMS I Edit ed by ANDREW S. GLASSNER 362
VI.4 SCANLINE DEPTH GRADIENT OF A Z-BUFFERED TRIANGLE
By Const ruct ion y
4
= y
3
and
so

x
4

x
2
x
1
y
2
y
1
y
3
y
1
( )
+ x
1
, and z
4

z
2
z
1
y
2
y
1
y
3
y
1
( )
+ z
1
.

x
4
x
1
y
4
y
1

x
2
x
1
y
2
y
1
, and
z
4
z
1
y
4
y
1

z
2
z
1
y
2
y
1
,

Figure 2.

Figure 1.
363 GRAPHIC GEMS I Edit ed by ANDREW S. GLASSNER 363
VI.4 SCANLINE DEPTH GRADIENT OF A Z-BUFFERED TRIANGLE
Hence t he Z slope wit h respect t o X is

z
4
z
3
x
4
x
3

z
2
z
1
y
2
y
1
y
3
y
1
( )
+ z
1
z
3
x
2
x
1
y
2
y
1
y
3
y
1
( )
+ x
1
x
3


z
2
z
1
( ) y
3
y
1
( )
z
3
z
1
( ) y
2
y
1
( )
x
2
x
1
( ) y
3
y
1
( )
x
3
x
1
( ) y
2
y
1
( )
.
See also Scan Conver sion Summar y (75)
364
VI.5 SIMULATING FOG AND HAZE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 364

dl d
0
d

VI.5 VI.5 VI.5 VI.5 VI.5


S SS SSIMULATING IMULATING IMULATING IMULATING IMULATING
F FF FFOG AND HAZE OG AND HAZE OG AND HAZE OG AND HAZE OG AND HAZE
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
One popular mod el for fog and haze is as a cont r ast -red ucing med ium.
The fog is assigned an over all color, usually a light gr ay. The far t her an
object is from t he viewer, t he lower it s cont r ast wit h respect t o t his fog
color. We can approximat e t his cont r ast red uct ion on a point -by-point
basis.
Suppose t hat t he fog has RGB color F, and point P on some object has
color K (t his is t he color of P aft er all sur face-d epend ent shad ing has
been per for med ; t hat is, t his is t he color of t he light r ad iat ed from P
t oward t he viewer, measured at P). We can push t he object color t oward
t he fog color by int er polat ing t he t wo: K lerp(, K, F). When = 0,
t he object is not at all colored by t he fog; when = 1, t he color has
shift ed t o t hat of t he fog.
The choice of is gover ned by t he proper t ies of t he fog. Assume t hat
t he fog is of const ant d ensit y t hroughout t he environment . Let d be t he
d ist ance from t he viewer at V t o t he point P being shad ed , and be
t he d ensit y of fog p er u nit d ist ance. Then t he t ot al fog bet ween V
and P is , so t he propor t ion of fog we encount er along a
sight line is linear wit h t he d ist ance t o t he point . A useful (t hough not ver y
realist ic) t echnique is t o say t hat unt il d ist ance d
near
t here should be no
fog effect , and t hat aft er d ist ance d
far
not hing but fog should be visible.
Then, since t he fog d ensit y is const ant , we can wr it e as a simple linear
bl en d fr om d
near
t o d
far
, so = clamp(0, 1, (d d
near
)/ (d
far
d
near
)).
For example, suppose our color s are d escr ibed in RGB, wit h object
col or K = (. 1, . 8, . 3) an d fog col or F = (. 6, . 6, . 6). If d
near
= 50, d
far
365
VI.5 SIMULATING FOG AND HAZE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 365
= 550, and d = 100, t hen = 0.1. Thus, t he color wed use for t he
p oi n t i s K K + [0. 1 (F K)] = (. 1, . 8, . 3) + [. 05, . 02, . 03] =
(.15, .78, .33). The approach her e is equally applicable t o more sophist i-
cat ed color mod els t han RGB.
In an environment where reect ions and t r ansmissions are visible, one
may wish t o apply cont r ast red uct ion t o t he t r ansmit t ed and reect ed
light as well, simulat ing t he d issipat ion of t his light as it t r avels from one
object t o anot her. This is appropr iat e in bot h r ay t r acing and r ad iosit y
progr ams.
A p op u lar t echniqu e in vect or gr ap hics is dept h cuing, where t he
int ensit y of a st roke is d iminished as a linear funct ion of it s d ist ance from
t he viewer. This is a special case of t he approach d escr ibed here, using
black as t he fog color. Oft en in t hese syst ems d
near
and d
far
are set t o
t he minimum and maximum d ept h component of all st rokes in t he scene.
Using a d ar k blue or black fog color is also appropr iat e for night t ime
haze.
This not e has present ed a ver y simple mod el for fog and haze, appro-
pr iat e for a quick hack or a special effect . More realist ic and sophist icat ed
mod els may be found in Blinn (1982), and Kajia and Von Her zen (1984).
See also Shad ow At t enu at ion for Ray Tr acing Tr ansp ar ent Ob-
ject s (397)
366
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 366
VI.6 VI.6 VI.6 VI.6 VI.6
I II IINTERPRETATION OF TEXTURE MAP NTERPRETATION OF TEXTURE MAP NTERPRETATION OF TEXTURE MAP NTERPRETATION OF TEXTURE MAP NTERPRETATION OF TEXTURE MAP
INDICES INDICES INDICES INDICES INDICES
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
A t ypical rend er ing problem is t o nd t he aver age value wit hin some
region of a t ext ure map. Since t his t ask can occur for ever y pixel (and in
some Z-buffer syst ems, pot ent ially many t imes per pixel), we would like
t o nd t his aver age as efcient ly as possible. Sever al t echniques have
been proposed , such as t he mip-map (Willams, 1983) for square regions,
sum t ables (Crow, 1984) for rect angular regions, rened sum t ables for
quad r ilat er al regions (Glassner, 1984), and t he ellipt ically weight ed aver -
age lt er (Green and Heckber t , 1986) for ellipt ic r egions.
In t his not e we rest r ict ou r at t ent ion t o r ect angu lar r egions. Ou r
problem is t o nd t he aver age value wit hin some axis-aligned rect angle of
t ext ure. Such a rect angle may be d escr ibed by t he point s at opposit e end s
of one of it s d iagonals; in t his not e we choose t hese t o be t he point s
nearest and far t hest from t he or igin of t he t ext ure coord inat e syst em.
We d ene t he t ext ure t o be available on t he Car t esian prod uct of t he
half-open int er vals [0, 1) in U and [0, 1) in V. We d iagr am t his in Fig. 1,
using a solid line and a shad ed pat t er n for available t ext ure, and a d ashed
line for t he bound ar y of t he open int er vals.
Figur e 1. The fund ament al t ext ure cell, from [0,0] t o (1,1).
367
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 367
Figure 4. Two possible int er pret at ions of polygon 3 in Figur e 2. The t ext ure is only
specied by t he rect angle cor ner s (0, 0) and (.75, .9).
There ar e many ambiguit ies t hat can ar ise when one d enes a t ext ure
rect angle wit h only t wo t ext ur e coord inat es. For example, suppose we
have an open, four-sid ed cylind er wit h v = 0 at t he bot t om and v = .9 at
t he t op, and U r unning around t he upper ver t ices from 0 t o 0.75 in equal
st eps, as in Fig. 2.
Consid er t he t hree rect angles labeled 0, 1, and 2. Figure 3 shows t he
images of t hese rect angles in t ext ur e space.
Now consid er rect angle 3 near t he bot t om of t he image. It has t ext ure
coor d in at es wit h ext r emes (0, 1) in V an d (0, .75) in U. Th e (u, v )
ext remes of t his rect angle in t ext ure space are t hen (0, 0) and (.75, .9).
Figure 3. The texture-space images of polygons 0, 1, and 2 in Figure 2.
Figure 2. A t ext ured cylind er.
a b
368
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 368
Figure 4 shows t wo int er pret at ions of mappings t hese point s d irect ly int o
t ext ure space.
The r st int er p r et at ion simp ly shows t he r egion for med by t hose
cor ner s, but it s hard ly a nar row slab like t he ot her r ect angles. We want
t he t ext ure t o wr ap in lit t le pieces ar ound t he cylind er, but t he int er pret a-
t ion in Fig. 4a will cause t he t ext ure t o y all t he way around , backward s,
on t he last piece. This looks ver y bad .
One common solut ion t o t his problem is t o wr ap t ext ure space as a
t wo-t or us, and nd t he smallest rect angle on t hat sur face. Thus, t he r ight
sid e of t he t ext ure is t aped t o t he left sid e, and t he t op is t aped t o t he
bot t om, as in many vid eo games. Alt er nat ively, we may wr ap in only one
d irect ion, for ming a cylind er. Then Fig. 4b is ind eed t he smallest rect an-
gle on t he cylind er, given our t wo point s. Of cour se, we never act ually
const r uct t he cylind er or t orus; r at her, t he progr am checks t he area of t he
possibilit ies. Of t hese rect angles t he one wit h t he smallest area is chosen.
A similar problem comes up when t he t ext ure coord inat es are out sid e
t he unit square. Some applicat ions might prod uce object s wit h t ext ure
coord inat es of unknown ext ent ; t his can happen when a t ext ure funct ion
is scaled up t o replicat e t he t ext ure on an object s sur face. We would st ill
like t o rend er such object s sensibly. Before we can come up wit h an
algor it hm, t hough, we need t o d ecid e what it should mean t o specify a
t ext ure coord inat e such as (1.3, 4.5).
There are many solut ions t o t his problem. The t ext ure could be t reat ed
as t he fund ament al cell for any of t he symmet r y groups wit h exclusively
t wo- or four-fold symmet r y. We could also d evise complicat ed proced ur al
answer s. A really good analysis of t his problem and t he pros and cons of
t he d ifferent solut ions makes for some int erest ing t hinking. For simplicit y
in t his ar t icle, we will just replicat e t he t ext ure over t he plane wit hout
t r ansfor mat ion, as in Fig. 5.
You may prefer t o reect or rot at e t he t ext ure as you repeat it .
The gener al id ea is t o d et er mine if t he d esired t ext ure spans one or
more unit squares (we will call t he t ext ure-space unit square a cell). If
t he t ext ure cover s more t han one cell we will nd t he cont r ibut ion from
each cell and ad d t he cont r ibut ions t oget her. Each cell will be easy, since
it s coord inat es will be wit hin t he unit square.
We begin wit h fou r t ext u r e coor d inat es as inp u t : (u
1
, v
2
), (u
2
,v
2
),
(u
3,
v
3
), and (u
4
, v
4
). From t hese we nd t he smallest axis-aligned box
t hat encloses t hem all. Call t he point on t his enclosing box nearest t he
369
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 369
Figure 5. Replicat ing a single t ext ur e cell t o creat e a larger t ext ure.
or igin S (for small), and t he point far t hest L (for large). We d ene t wo
points, S and L, and a translation vector D, which we will use to build S
and L:
1. S (min(u
i
), min(v
i
))
2. L (max(u
i
), max(v
i
))
3. D (frac(S
u
), frac(S
v
))
4. S S D
5. L L D
St eps 3 and 4 build S by t r anslat ing S int o t he unit square; L is t hen
const r uct ed in st ep 5 by t r anslat ing L by t he same amount . So now
0 S
u
, S
v
<

1.
Next we will d et er mine t he sor t s of cells t hat are over lapped by S and
L. Figure 6 gives a d ecision t ree for each of t he nine d ifferent sor t s of
sit uat ions t hat can ar ise. A cell d r awn wit h a solid circle in it means t hat
t here are one or more inst ances of t hat cell.
The t r ee in Fig. 6 cont ains r at her mor e nod es t han it mu st . For
example, all four leaves in t he lower left are of t he same for m: four
cor ner s plus one or more ot her pieces. If were willing t o red ene a solid
circle t o mean zero or more of t hese pieces, we can wr it e a much smaller
t ree, as in Fig. 7.
370
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 370
Figure 6. A decision tree for determining how different types of cells should be used t o
create a replicated texture. Each solid dot indicates one or more instances of that t ype of
cell.
Figure 7. A simplied for m of Figure 6, where each d ot represent s zero or more
inst ances of t hat t ype of cell.
371
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 371
This t ree shows 16 d ifferent t ypes of cells at t he leaves. Our only
remaining t asks are t o nd how many of t he opt ional ones exist at each
leaf wit h opt ional cells, and t he geomet r y of each of t hese 16 d ifferent
t ypes of cells. Let us at t ack t hese problems in ord er.
Consid er t he r ight most leaf in Fig. 7t here are no opt ional cells, and
t he whole rect angle is in t he unit t ext ure square, so were d one.
Move left t o t he leaf wit h t hree cells st acked ver t ically. Consid er t hat
t he bot t om of t he bot t ommost cell is v = 0, and t he t op of t hat cell is
v = 1. If t he V coord inat e of point L is less t han 2, t hen t here are no
int er med iat e cells, but ot her wise t here is one such cell for each ad d it ional
int eger increment of t he t op coord inat e. Remember t hat we named t he
coord inat e far t hest from t he or igin Lin our left -hand ed syst em, t his is
t he upper-r ight point . So t he number of opt ional cells in t he mid d le is
oor(L
v
) 1.
Move left in t he t ree t o t he cell wit h t he t hree hor izont al cells. The same
reasoning we just went t hrough applies, but swap U and V. Thus, t he
number of cent er cells is oor(L
u
) 1.
Move now t o t he far-left leaf. There are four cor ner cells always t here.
Then we must consid er t he cells in t he cent er of t he t op and bot t om rows.
This sit uat ion is analogous t o t he leaf immed iat ely t o t he r ight , except
now we must remember t hat t here are oor(L
u
) 1 pairs of cells. The
same argu ment hold s for t he cent er of t he left and r ight colu mns;
t here ar e oor(L
v
) 1 p air s of t hese. The nu mber of cent er cells is
simply t he prod uct of t he ext ensions of t he t wo sid es (oor(L

u
) 1)
(oor(L
v
) 1).
Now t hat we know how many of each kind of cell we need t o nd t he
sum, we need t o be able t o nd t he rect angle d escr ibed by each cell. This
is not t oo hard if we reason about one cell t ype at a t ime. If t he cell
cont ains t he lower -left cor ner of t he box, t hat p oint is p oint S. If it
cont ains t he upper-r ight cor ner, t hen t he coord inat es of t hat point in t he
u n i t squ a r e a r e F = (frac(L
u
), frac(L
v
))r emember t h a t t h e op t i ma l
cells just d iscussed hand le t he int eger par t s of L. Alt hough we have been
careful about saying t hat t he lines u = 1 and v = 1 are act ually not par t
of our t ext ure, most progr ams are happy t o hand le such values. Im going
t o t ake ad vant age of t his freed om now. In t he following t ables I oft en use
t he coord inat es u = 1 or v = 1, r at her t han present t he cell geomet r ies
as open int er vals, which would t hen st ar t t o get messy (bot h logically and
ar it hmet ically). Figu r e 8 p r ovid es a su mmar y of t he 16 t yp es of cells;
t heir associat ed coord inat es are given in Table 1.
372
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 372
Figure 8. The d ifferent t ypes of cells and t heir names.
Table 1. Text ure Sampling
Cell Type Occupied Region
C: (S
u
, S
v
)(L
u
, L
v
)
F (0, 0)(1, 1)
TE: (S
u
, S
v
)(L
u
, 1)
BE: (S
u,
0)(L
u
, L
v
)
LE: (0, S
v
)(L
u
, L
v
)
RE (S
u
, S
v
)(1, L
v
)
UL: (0, S
v
)(L
u
, 1)
LR (S
u,
0)(1, L
v
)
LL: (0, 0)(L
u
, L
v
)
UR: (S
u
, S
v
)(1, 1)
LW: (0, 0)(L
u
, 1)
BW: (0, 0)(1,L
v
)
RW: (S
u
, 0)(1, 1)
TW: (0, S
v
)(1, 1)
V: (S
u
, 0)(L
u
, 1)
H: (0, S
v
)(1, L
v
)
373
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 373
These are all t he t ools necessar y t o wr it e a pr ogr am t hat can int egr at e
any axis-aligned rect angle.
We can express t he algor it hm for med by t he d ecision t ree above in
pseud o-cod e:
map (S, L) {S and L are t wo input point s in t he 2d UV syst em.
S
u
L
u
, S
v
L
v
.
This returns the sum of all samples in that rectangle; divide by the area of t he
rect angle for t he average value.
We use the function GetSum (type, S, L) which takes a cell type and the
two original input points and returns the sum of all samples in that types of cell
in that rectangle.
vc floor (L
v
) 1 The number of vert ical cells in t all st rips.
hc floor (L
v
) 1 The number of horizont al cells in wide st rips.
i f L
u


1 then
i f L
v


1 then
sum Get Sum(UR, S, L) + Get Sum(UL, S, L) +
Get Sum(LR, S, L) + Get Sum(LL, S, L)
i f vc > 0 then sum sum +
[vc(Get Sum(LW, S, L) + Get Sum(RW, S, L))]
i f hc > 0 then sum sum +
[hc(Get Sum(TW, S, L) + Get Sum(BW, S, L))]
i f hcvc > 0 then sum sum +[(hcvc)Get Sum(F, S, L)]
el se
sum Get Sum(LE, S, L) + Get Sum(RE, S, L)
i f hc > 0 then sum sum + [hcGet Sum(H, S, L)]
el se
i f L
v


1 then
sum Get Sum(TE, S, L) + Get Sum(BE, S, L)
i f vc > 0 then sum sum + [vc*Get Sum(V, S, L)]
el se
sum Get Sum(C, S, L)
return[sum]
}
374
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 374
Get Sum(t ype, S,L) {
We assume a rout ine Get SumTableRect angle which will t ake t wo point s
A and B, A
u
B
v
, A
v
B
v
, and ret urns t he average value in t he rect angle
formed by A and B.
Text urePoint A, B;
sel ect t ype from
C: begi n A (S
u
, S
v
); B (L
u
, L
v
); end;
F: begi n A (0, 0); B (1, 1); end;
TE: begi n A (S
u
, S
v
); B (L
u
, 1); end;
BE: begi n A (S
u
, 0); B (L
u
, L
v
); end;
LE: begi n A (0, S
v
); B (L
u
, L
v
); end;
RE: begi n A (S
u
, S
v
); B (1, L
v
); end;
UL: begi n A (0, S
v
); B (L
u
, 1); end;
LR: begi n A (S
u
, 0); B (1, L
v
); end;
LL: begi n A (0, 0); B (L
u
, L
v
); end;
UR: begi n A (S
u
, S
v
); B (1, 1); end;
LW: begi n A (0, 0); B (L
u
, 1); end;
BW: begi n A (0, 0); B (1, L
v
); end;
RW: begi n A (S
u
, 0); B (1, 1); end;
TW: begi n A (0, S
v
); B (1, 1); end;
V: begi n A (S
u
, 0); B (L
u
, 1); end;
H: begi n A (0, S
v
); B (1, L
v
); end;
endsel ect;
return [Get SumRect (A,B)];
};
As I ment ioned at t he st ar t , you may want t o reflect or rot at e t he
t ext ure as you t ile t he plane. You' ll need t o keep t r ack of t he r ight
t r ansfor mat ion at each cell. It ' s t hen a simple mat t er t o eit her reflect or
rot at e t he ind ices t o t he cor rect for m.
The bookkeeping necessar y t o make sure t his all come out st r aight
over repeat ed cells is not t r ivial, bu t it ' s not par t icu lar ly hard ju st
messy. Figure 9 shows t he eight t r ansfor mat ions wit h t heir geomet r ic and
algebr aic meanings.
375
VI.6 INTERPRETATION OF TEXTURE MAP INDICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 375
Figure 9. There are eight ways t o r igid ly t r ansfor m a squar e cell. Each has a simple
int er pret at ions in t er ms of t r ansfor med t ext ur e coord inat es.
See also Mult id imensional Sum Tables (376)
376
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 376
VI.7 VI.7 VI.7 VI.7 VI.7
M MM MMULTIDIMENSIONAL ULTIDIMENSIONAL ULTIDIMENSIONAL ULTIDIMENSIONAL ULTIDIMENSIONAL
S SS SSUM TABLES UM TABLES UM TABLES UM TABLES UM TABLES
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
Sum t ables were int rod uced t o comput er gr aphics by Crow (1984) as a
t echnique for r apid ly int egr at ing rect angular areas of t ext ure. They are
popular for t wo-d imensional t ext ure mapping, where t hey are used t o nd
t he aver age t ext ure value in some rect angular region. You can nd t hem
d iscussed in t he st at ist ics lit er at ure as joint cumulat ive probabilit y d ist r i-
but ion funct ions on n var iables (Ross, 1976).
One may gener alize t he sum t able met hod t o an ar bit r ar y number of
d imensions, in ord er t o nd t he aver age value wit hin a rect angular box of
any number of sid es. I need ed t he aver age value in 3D boxes when
build ing an int er act ive slicing progr am. The user moved a slicing plane
t hrough a 3D volume of int ensit y values, and I const ant ly upd at ed a
d isplay wit h t he gr ayscale slice of t he volu me represent ed by t he
cur rent posit ion of t he plane. There wasnt enough t ime t o rend er t his
image at full resolut ion and st ill st ay int er act ive, so I rend ered t he image
at low resolut ion (or iginally 32 by 32 samples, where each sample was a
big box on t he screen). I want ed each of t hese samples t o represent t he
aver age d ensit y in t he 3D box spanned by four point s on t he 3D sample
gr id ; a 3D sum t able was just t he r ight t echnique t o nd t hese aver ages
quickly.
Let s br iey review t he t wo-d imensional sum t able. Given a sampled
input I(x, y), a sum t able S(x, y) is built from t he samples of I so t hat
S(x, y) = I(j, k). Thus, each point on t he sum t able is t he sum
of all int ensit ies in t he box for med by t hat point and t he or igin.
To nd t he aver age value in some rect angle wit h cor ner s UR (upper -
r ight ), UL (upper-left ), LR (lower-r ight ), and LL (lower-left ), we r st nd
t he sum of all values in t hat box, and t hen d ivid e by t he area of t he box.

k 0
y

j 0
x

377
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 377
The beaut y of sum t ables is t hat you can quickly nd t he t ot al value in
t hat region by comput ing
Sum of samples in box = S(UR) S(UL) S(LR) + S(LL). (1)
So t hen t he aver age value may be found by
Aver age = (Sum of samples in box)/ (Area of box). (2)
Why d oes Eq. 1 wor k? Figure 1 suggest s a pict or ial explanat ion. The
box we want can be found by nd ing t he sum at UR, and by t hen
removing t he ext r aneous area t o t he left and below of LL by removing t he
sums at UL and LR. But we have implicit ly removed t he area t hat is bot h
t o t he left and below of LL t wo t imes, so we explicit ly ad d it back in once,
so it is only subt r act ed from t he t ot al once.
Equat ion 1 can also help us build t he sum t able in t he r st place. The
br ut e-force way t o build a sum t able is t o loop t hrough all t he pixels
below and t o t he left of each sample you want t o comput e. A smar t er way
t o go is t o pret end you want ed t o nd t he sum in t he box for med by t he
sample at (x, y) and it s neighbor s immed iat ely left , below, and below-left :
S(x, y) S(x 1, y) S(x, y 1) + S(x 1, y 1) = I(x, y). (3)
Figure 1. The sum of all values in a rectangle may be found by combining the sum table
value at the four corners.
378
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 378
We d ont have t o d ivid e t he sum because t he area of t his box is exact ly
1. We can just rewr it e t his for S(x, y):
S(x, y) = I(x, y) + S(x 1, y) + S(x, y 1) S(x 1, y 1). (4)
This shows t hat we can use t he previously lled -in ent r ies in t he sum
t able and t he image it self t o help build new ent r ies.
A common t echnique for gener alizing somet hing is t o see what happens
when you go one d imension higher, and t hen see if you can nd t he
ar bit r ar y case by ind uct ion. Let s t r y t his and move from 2D t o a 3D sum
t able.
Figure 2 shows a t hree-d imensional sum t able and a box we would like
t o sample (for clar it y in t he gure we have suppressed t he space beyond
point P). Suppose t hat t he point P is at (x, y, z), and t he opposit e
d iagonal Q is at (x x, y y, z z). In Figure 3a we have shown
t he eight oct ant s of t he or iginal 3D image as d ened by t he planes of t he
box were invest igat ing. Figure 3b labels each of t he eight oct ant s creat ed
Figure 3. Figure 3a shows t he eight oct ant s in 3D creat ed by t he box in Fig.
2. Figure 3b provid es labels for t hese oct ant s. Figure 3c labels t he eight
ver t ices of t he sampled box.
Figure 2. In 3D, the near corner Q and far corner P define the diagonal of a box of 3D
t ext ure.
379
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 379
Figure 3. Figure 3a shows the eight octants in 3D created by the box in Fig. 2. Figure 3b
provides labels for these octants. Figure 3c labels the eight verti ces of the sampled box.
by t he planes of t his box; not e t hat t he box we wish t o sample is labeled
F. Figure 3c labels t he ver t ices of t he box.
We would like t o nd t he sum of all samples in box F. Suppose t hat we
have built a sum t able in t hree d imensions. To clar ify how t he t able is
built , we will say t hat n is t he sum of all samples in box n. Then we can
wr it e each Si (t he value of t he sum t able at ver t ex i) as a sum of t hese
380
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 380
oct ant sums:
S1 = C + A
S2 = C + A + + D
S3 = C
S4 = C + D
S5 = C + A + + G
S6 = C + B + + D + + F + G
S7 = C + G
S8 = C + D + G+ H
Now t hat we have a 3D sumt able, we need t o know how t o combine t he
values at t he ver t ices of t he box t o nd , t he F sum of t he values in
oct ant F. The cor rect combinat ion is given by
F = Sl S2 S3 + S4 S5 + S6 + S7 S8. (5)
If we d ivid e F by it s volume we get t he aver age value in t hat box.
We can now st at e our gener alizat ion of sum t able met hod s, which says
t hat you can nd t he t ot al value insid e any box by nd ing t he sum t able
values at each ver t ex and t hen combining t hose sum t able values. But t he
cor rect coefcient s are impor t ant ; we would like t o nd a simple way t o
d et er mine which sum t able values need t o be ad d ed and which need t o be
subt r act ed Eq. 5 was simply prod uced out of t hin air.
Look back at Fig. 1, where we want ed t he value at t he upper-r ight
cor ner of t he box. We st ar t ed by ad d ing t he sum t able value at t hat point ,
and t hen subt r act ed off t he values at each of t he t wo ver t ices of t he box
t hat were one st ep away from our sample point , and t hen ad d ed back t he
ver t ex of t he box t hat was t wo st eps away.
Now look at Figs. 2 and 3 and Eq. 5, and obser ve t hat we d id exact ly
t he same t hing for t he 3D box. S6 it self was ad d ed , ver t ices t hat were one
st ep away from S6 were subt r act ed , ver t ices t wo st eps away were ad d ed ,
and ver t ex S3 (t he only ver t ex t hree st eps away) was subt r act ed .
What s going on? Int uit ively, each t ime we t ake a st ep were cleaving
away a half-space of sums. When we t ake t wo st eps were rest or ing some
of t he space t hat was removed more t han once. When we st ep again, we
compensat e for t oo many subt r act ions by ad d ing some spaces back in,
and so on.
In d d imensions, you can easily enumer at e all t he point s in a d-d imen-
sional box by gener at ing all t he binar y number s from 0 t o 2
d-1
. Each bit
381
VI.7 MULTIDIMENSIONAL SUM TABLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 381
posit ion cor respond s t o an axis. You can int er pret a zero bit as t elling you
t o st ay put , and a one bit as inst ruct ing you t o move along t he cor re-
spond ing axis. You t hen look up t he sum t able value S at t he point youve
const r uct ed . If you moved along an even number of axes t hen you ad d S
int o your r unning sum; if you moved along an od d number of axes t hen
you subt r act it .
In t he following pseud o-cod e, we nd t he aver age value in some box in
a d-d imensional space. P is t he locat ion of t he box cor ner far t hest from
t he or igin, represent ed by an ar r ay of d number s. The box we want t o
sample has sid e lengt hs given by t he ent r ies in an ar r ay sideLengt h[]see
Fig. 4 for an illust r at ion of t his set up. We assume t hat t her es a funct ion
SumTable[], which will t ake as input a point and ret ur n t he value of t he
sum t able at t hat n-d imensional point .
Find -sum-at (P, sid eLengt h[], d ) {
sum 0
for i 0, 2
d 1
do
Q P
for j 0, d do check each axis for movement
mask 2
j
i f i bi t-and mask then { move along t his axis?
count count + 1;
Q
j
Q
j
sid eLengt h[j];
};
endl oop;
i f count mod 2 = 0
then sum sum + SumTable[Q];
el se sum sum SumTable[Q];
endl oop;
return(sum);
};
There are all kinds of nice symmetry patterns to be found in the points
gener at ed , t heir coefcient s, and t he volumes t hey sample.
See also Int er pret at ion of Text ure Map Ind ices (366)
7
RAY TRACING
385
VII.1 A SIMPLE RAY REJECTION TEST
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 385
VII.1 VII.1 VII.1 VII.1 VII.1
A AA AA SIMPLE SIMPLE SIMPLE SIMPLE SIMPLE
R RR RRAY REJECTION TEST AY REJECTION TEST AY REJECTION TEST AY REJECTION TEST AY REJECTION TEST
Jack Rit t er
Versat ec, Inc.
Sant a Clara, Calif ornia
Here is a simple met hod for fast r ay t r acing t hat can t r ivially reject eye
r ays t hat d ont int er sect wit h object s in t he scene.
For each object : Comput e it s axis-aligned 3D bound ing box. Project t he
boxs eight cor ner s ont o t he screen and nd t he 2D bound ing box t hat
sur round s t hem. This box should be in screen space. (Tight er bound ing
polyhed r a could be used as well.)
To t est a r ay against an object , check if t he pixel t hrough which t he r ay
passes is in t he object s 2D box. If not , reject it .
This is much fast er t han d oing a 3D d ist ance check of t he r ay against
t he object s bound ing volume.
The benet would be most pronounced in scenes t hat d ont involve a
lot of reect ion and refr act ion, since t hese phenomena gener at e r ays t hat
d ont or iginat e from t he eye. However, t he ear ly d evelopment st age of any
r ay t r acer will benet from t his t echnique, since frequent init ial bugs
necessit at e many rend er ings, and any speed -up will save t ime, par t icu-
lar ly if only opaque object s are consid ered at r st .
This 2D box scheme can be used t o calculat e aut omat ically t he eld -
of-view (fov) angle of t he synt het ic camer a const ruct used in most 3D
syst ems. The user must d ene t he camer as locat ion, t he look-at point ,
and t he up point , but t he lenss angle can be aut omat ically comput ed
so as t o encompass t he whole scene. Here, fov is act ually t he half-angle
from t he cent er of t he scene t o t he screens ed ge, in r ad ians.
Assume t he cent er of t he screen is at (0, 0). The met hod is as follows:
pick an init ial fov known t o encompass t he scene. A fov of 60 d egrees
386
VII.1 A SIMPLE RAY REJECTION TEST
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 386
(meaning t he ed ge-t o-ed ge angle is 120 d egrees) should sufce for all but
t he most severely d ist or t ed scenes. Calculat e t he camer a based on t his.
Make a d r y run t hrough t he scene, wit hout rend er ing.
For each object in t he scene, just calculat e t he 2D bound ing box, as
d escr ibed above. Find t he ed ge of t he box far t hest away from t he screens
cent er, and call t his d ist ance max_edge.
Pi ck t h e ma xi mu m max _edge, for t h e wh ol e scen e. Ca l l i t max _
max_edge. Calculat e t he d ist ance from t he eye t o t he screens cent er.
Call it eye_t o_screen. The new fov will be
fov = arct an(max_max_widt h/eye_t o_screen).
Now you can run t hrough t he d at a again and rend er, using fov; all object s
will be in t he pict ure, wit h a lit t le bord er t o spare. Not e, eye_t o_screen
mu st be i n t h e sa me u n i t s a s max _max _widt h. Th e con ver si on i s
d ep end ent on t he p ar t icu lar camer a mod el being imp lement ed . The
screens aspect r at io is not a fact or.
A more rened var iat ion of t he id eas present ed here was published in
Bronsvoor t et al. (1984).
See also Efcient Gener at ion of Sampling Jit t er Using Look-up
Tables (64); Fast LineEd ge Int er sect ions on a Unifor m Gr id
(29); Tr ansfor ming Axis-Aligned Bound ing Boxes (548)
387
RAY SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 387
R RR RRAYOBJECT AYOBJECT AYOBJECT AYOBJECT AYOBJECT
I II IINTERSECTION SUMMARY NTERSECTION SUMMARY NTERSECTION SUMMARY NTERSECTION SUMMARY NTERSECTION SUMMARY
Th e i n t er sect i on of a r a y a n d a n object i s p er h a p s t h e most
cr i t i ca l st ep wh en r a y t r a ci n g. Beca u se i t i s a t t h e h ea r t of t h e
i n n er l oop of a n y r a y t r a ci n g a l gor i t h m, a r a y/ object i n t er sec-
t i on a l gor i t h m sh ou l d be a s efci en t a s p ossi bl e. Th er e a r e
ma n y st r a t egi c a n d t a ct i ca l met h od s a va i l a bl e for op t i mi z i n g
va r i ou s r a y/ object i n t er sect i on s; t h e fol l owi n g Gems p r esen t a
few of t hose t echniques.
See also Efci en t Gen er a t i on of Sa mp l i n g Ji t t er Usi n g Look-u p
Ta bl es (64); Fa st Li n e-Ed ge In t er sect i on s on a Un i for m Gr i d
(29); Tr ansfor ming Axis-Aligned Bound ing Boxes (548)
388
VII.2 INTERSECTION OF A RAY WITH A SPHERE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 388
VII.2 VII.2 VII.2 VII.2 VII.2
I II IINTERSECTION OF A NTERSECTION OF A NTERSECTION OF A NTERSECTION OF A NTERSECTION OF A
RAY WITH A SPHERE RAY WITH A SPHERE RAY WITH A SPHERE RAY WITH A SPHERE RAY WITH A SPHERE
Jef f Hult quist
NASA Ames Research Cent er
Mof f et t Field, Calif ornia
Int er sect ing a r ay wit h a sphere is one of t he simplest of problems. When
viewed in t he plane d ened by t he r ay and t he cent er of t he sphere, t he
geomet r y of t he problem looks like t hat present ed in Fig. 1.
We need t o nd t he point P at which t he r ay r st int er sect s t he sphere.
We obser ve t hat
v
2
+ b
2
= c
2
d
2
+ b
2
= r
2
d =

r
2
c
2
v
2
( )
If we let V be the unit vector in the direction of the ray, then the point of
int er sect ion can be found like so . . .


v = EO V;
d isc = r
2
((EO EO) v
2
)
i f(d isc < 0)
then no int er sect ion
el se begi n
d =

disc;
P = E + (vd )V;
end;

389
VII.2 INTERSECTION OF A RAY WITH A SPHERE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 389
Figure 1.
See also Efcient Gener at ion of Sampling Jit t er Using Look-up
Tables (64); Fast Line-Ed ge Int er sect ions on a Unifor m Gr id
(29); Tr ansfor ming Axis-Aligned Bound ing Boxes (548)
390
VII.3 AN EFFICIENT RAY-POLYGON INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 390
VII.3 VII.3 VII.3 VII.3 VII.3
A AA AAN EFFICIENT N EFFICIENT N EFFICIENT N EFFICIENT N EFFICIENT
R RR RRAY-POLYGON AY-POLYGON AY-POLYGON AY-POLYGON AY-POLYGON
I II IINTERSECTION NTERSECTION NTERSECTION NTERSECTION NTERSECTION
Didier Badouel
IRISA, Rennes, France
Using a r ay t r acing met hod wit h polygonal d at abases, we must d ene a
fast algor it hm t o comput e r ay-polygon int er sect ion. The following algo-
r it hm is quit e similar t o but fast er t han t he bar ycent r ic approach d e-
scr ibed in Snyd er and Bar r (1987).
The goal of t he algor it hm is r st t o d et er mine if a r ay goes t hrough t he
polygon, and t hen t o d et er mine t he coord inat es of t he int er sect ion point
and p ar amet er s, t o localize t his p oint wit h resp ect t o t he p olygons
ver t ices. These par amet er s are used t o comput e t he int er polat ed nor mal
at t his point , and can be used also t o comput e t he ent r y of a t ext ure map.
Fi r st St ep: Int er sect i ng t he Embeddi ng Pl ane Fi r st St ep: Int er sect i ng t he Embeddi ng Pl ane Fi r st St ep: Int er sect i ng t he Embeddi ng Pl ane Fi r st St ep: Int er sect i ng t he Embeddi ng Pl ane Fi r st St ep: Int er sect i ng t he Embeddi ng Pl ane
This st ep is common wit h t he ot her int er sect ion algor it hms but can be
present ed again. A polygon is d escr ibed by it s ver t ices V
i
(i [ (0, . . . ,
n1}, n 3). Let x
i
, y
i
, and z
i
t he coor d inat es of t he ver t ex V
i
. The
nor mal of t he plane cont aining t he polygon, N, is comput ed wit h t he
cross prod uct :
N = V
0
V
1
V
0
V
2
.
For each point P of t he plane, t he quant it y P N is const ant . This
const ant value is comput ed by t he d ot prod uct d = V
0
N. The implicit

391
VII.3 AN EFFICIENT RAY-POLYGON INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 391
represent at ion of t he plane,
N P + d = 0, (1)
is comput ed once, and t hen st ored in t he polygon d escr ipt ion.
Let t he par amet r ic represent at ion of t he r ay be
r(t ) = + Dt . (2)
The evaluat ion of t he par amet er t cor respond ing t o t he int er sect ion point
can be obt ained using t he equat ions (1) and (2):


t
d +N
N D
. (3)
This calculat ion requires 12 oat ing oper at ions and t hree t est s:
If polygon and r ay are par allel (N D = 0), t he int er sect ion is reject ed .
If t he int er sect ion is behind t he or igin of t he r ay (t 0), t he int er sec-
t ion is reject ed .
If a closer int er sect ion has been alread y found for t he r ay (t > t
ray
),
t he int er sect ion is reject ed .
Second St ep: Int er sect i ng t he Pol ygon Second St ep: Int er sect i ng t he Pol ygon Second St ep: Int er sect i ng t he Pol ygon Second St ep: Int er sect i ng t he Pol ygon Second St ep: Int er sect i ng t he Pol ygon
A par amet r ic resolut ion is now present ed . This solut ion is based on
t r iangles. If a polygon has n ver t ices (n > 3), it will be viewed as a set of
n 2 t r iangles. For t his reason, t he resolut ion is rest r ict ed t o convex
polygons. The point P (see Fig. 1) is given by
V
0
P = V
0
V
1
+ V
0
V
2
.
The point P will be insid e t he t r iangle ( V
0
V
l
V
2
) if
0, 0, and + 1.
Equat ion (4) has t hree component s:
x
P
x
0
= (x
l
x
0
) + (x
2
x
0
)
y
P
y
0
= (y
l
y
0
) + (y
2
y
0
) (5)
z
P
z
0
= (z
l
z
0
) + (z
2
z
0
).

'


392
VII.3 AN EFFICIENT RAY-POLYGON INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 392
Figure 1. Par amet r ic represent at ion of t he point P.
A solut ion exist s and is unique. To red uce t his syst em, we wish t o
project t he polygon ont o one of t he pr imar y planes, eit her xy, xz, or yz.
If t he polygon is per pend icular t o one of t hese planes, it s project ion ont o
t hat plane will be a single line. To avoid t his problem, and t o make sure
that the projection is as large as possible, we nd the dominant axis of t he
nor mal vect or and use t he plane per pend icular t o t hat axis. As in Snyd er
and Bar r (1987), we comput e t he value i
0
,
0 if |N
x
| = max(|N
x
|, | N
y
|, |N
z
|)
1 if |N
y
| = max(|N
x
|, | N
y
|, |N
z
|)
2 if |N
z
| = max(|N
x
|, | N
y
|, |N
z
|).
Consid er i
l
and i
2
(i
l
and i
2
{0,1,2}), t he ind ices d ifferent from i
o
.
They represent t he pr imar y plane used t o project t he polygon Let (u, v)
be t he t wo-d imensional coord inat es of a vect or in t his plane; t he coord i-
nat es of V
0
P, V
0
V
1
, and V
0
V
2
, project ed ont o t hat plane, are


u
0
P
i
1
V
0
i
1
u
1
= V
1
i
1
V
0
i
1

u
2
V
2
i
1
V
0
i
1


v
0
= Pi
2
V
0
i
2
v
1
V
1
i
2
V
0
i
2

v
2
V
2
i
2
V
0
i
2
Equat ions 5 t hen red uces t o
u
0
= .u
l
+ .u
2
v
0
= .v
l
+ .v
2
i
0
=

'

'

.
393
VII.3 AN EFFICIENT RAY-POLYGON INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 393
The solut ions are



det
u
0
u
2
v
0
v
2

_
,

det
u
1
u
2
v
1
v
2

_
,




det
u
1
u
0
v
1
v
0

_
,

det
u
1
u
2
v
1
v
2

_
,

The int er polat ed nor mal from t he point P may be comput ed by


N
p
= (1 ( + ))N
0
+ N
1
+ N
2
.
Pseudo- Code f or a Ray Pseudo- Code f or a Ray Pseudo- Code f or a Ray Pseudo- Code f or a Ray Pseudo- Code f or a RayTr i angl e Int er sect i on Tr i angl e Int er sect i on Tr i angl e Int er sect i on Tr i angl e Int er sect i on Tr i angl e Int er sect i on
0: poi nt; Origin of t he ray
D: vector; Direct ion of t he ray
P: poi nt; Int ersect ion point
V: array[0..2] of poi nt; Polygon vert ices
P 0 + Dt ;
i
1
and i
2
are in t he polygon descript ion.
u
0
P[i
1
] V[0][i
1
];
v
0
P[i
2
] V[0][i
2
];
u
1
V[1][i
1
] V[0][i
1
];
u
2
V[2][i
1
] V[0][i
1
];
v
1
V[1][i
2
] V[0][i
2
];
v
2
V[2][i
2
] V[0][i
2
];
i f u
1
= 0
then u
0
/ u
2
;
i f 0 1
then (v
0
v
2
)/ v
1
;
el se (v
0
u
1
u
0
v
1
)/ (v
2
u
1
u
2
v
1
);
i f 0 1
then (u
0
/u
2
)/u
l
;
The values and are t he int erpolat ion paramet ers.
return ( 0 and 0 and ( + ) 1)
See also Efci en t Gen er at i on of Samp l i n g Ji t t er Usi n g Look-u p
Ta bl es (64); Fa st Li n eEd ge In t er sect i on s on a Un i for m Gr i d
(29); Tr ansfor ming AxisAligned Bound ing Boxes (548)
See Append ix 2 for C Implement at ion (735)
and
394
VII.4 FAST RAY-POLYGON INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 394
VII.4 VII.4 VII.4 VII.4 VII.4
F FF FFAST RAY-POLYGON AST RAY-POLYGON AST RAY-POLYGON AST RAY-POLYGON AST RAY-POLYGON
I II IINTERSECTION NTERSECTION NTERSECTION NTERSECTION NTERSECTION
Andrew Woo
SAS Inst it ut e Inc.
Don Mills, Ont ario, Canada
In many rend er ing progr ams, t he polygon t end s t o be t he main pr imit ive
available t o mod el complex sur faces. In ad d it ion, many sur faces t hat arc
exp r essed in some comp lex for m (su ch as qu ad r ic, p ar amet r ic, and
implicit sur faces) are oft en broken d own t o polygons d ur ing t he rend er ing
st age for t he simple reason t hat most syst ems can easily and efcient ly
rend er polygons. Thus, special at t ent ion should be paid t o r ay-polygon
int er sect ions.
In r ay t r acing, r ay-polygon int er sect ion involves t wo processes: int er-
sect ion against t he plane on which t he polygon lies, and a check if t he
int er sect ion point on t he plane is insid e t he polygon. If each polygon has
associat ed wit h it a bound ing box (par allel t o t he axes), we can use t his
bound ing box t o speed up r ay-polygon int er sect ion: aft er t he int er sect ion
wit h t he plane, if t he point int er sect ed lies out sid e t he bound ing box, t hen
t he insid e-out sid e check can again be avoid ed . This check basically act s
as an approximat e insid e-out sid e t est and requires only an ad d it ional six
compar isons at wor st .
This quick check can be used as a second ar y culler for your favor it e
int er sect ion culler in r ay t r acing, especially when t he cand id at e set of
object s t o be int er sect ed is large. I have t est ed t his met hod against a
u nifor m voxel t r aver sal algor it hm wit h t he r ay bou nd ing box check
(which requires bound ing boxes for t he object s, anyway), and it pays off
hand somely.
See also Efcient Gener at ion of Sampling Jit t er Using Look-up
Tables (64); Fast LineEd ge Int er sect ions on a Unifor m Gr id
(29); Tr ansfor ming Axis-Aligned Bound ing Boxes (548)
395
VII.5 FAST RAY-BOX INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 395
VII.5 VII.5 VII.5 VII.5 VII.5
F FF FFAST RAY-BOX AST RAY-BOX AST RAY-BOX AST RAY-BOX AST RAY-BOX
INTERSECTION INTERSECTION INTERSECTION INTERSECTION INTERSECTION
Andrew Woo
SAS Inst it ut e, Inc.
Don Mills, Ont ario, Canada
A fast int er sect ion scheme wit h t he bound ing box is proposed . It assumes
t hat par allel planes for m t he box, where each set is par allel t o one axis.
This approach is ver y similar t o t he bound ing box evaluat ion d iscussed by
Haines (1989), except t hat it cut s d own on t he number of oat ing point
oper at ions and ret ur ns t he int er sect ion point wit h t he box, which is
required und er some circumst ancesfor inst ance, in voxel t r aver salt o
id ent ify t he r st voxel t hat t he r ay pierces.
Assume we are wor king in 3D. We r st search for t hree cand id at e
planes t hat t he r ay can possibly int er sect , t hat is, we cull away back
facing planes for ming t he box. From t hese t hree cand id at e planes, t he
maximum of t he hit d ist ances wit h t he cand id at e planes must be t he
closest plane t he r ay hit s (wit h t he cond it ion t hat t he hit is insid e t he box;
ot her wise t here is no hit ).
As an example, in 2D, let x1, x2, y1, y2 be t he bound ar ies of t he box
(see Fig. 1). In t his case, wit h t he r ay or igin in t he lower-r ight cor ner, t he
can d i d at e p l an es ar e x2 an d y1. t x an d t y h i t d i st an ces ar e t h en
calculat ed from t he r ay or igin t o x2 and y1, respect ively. Since t x > t y,
x2 is t he closest plane hit by t he r ay.
396
VII.5 FAST RAY-BOX INTERSECTION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 396
Figur e 1.
See also Efcient Gener at ion of Sampling Jit t er Using Look-up
Tables (64); Fast LineEd ge Int er sect ions on a Unifor m Gr id
(29); Tr ansfor ming Axis-Aligned Bound ing Boxes (548)
See Append ix 2 for C Implement at ion (736)
t
x
t
y
397
VII.6 SHADOW ATTENUATION FOR RAY TRACING TRANSPARENT OBJECTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 397
VII.6 VII.6 VII.6 VII.6 VII.6
S SS SSHADOW ATTENUATION FOR HADOW ATTENUATION FOR HADOW ATTENUATION FOR HADOW ATTENUATION FOR HADOW ATTENUATION FOR
R RR RRAY TRACING AY TRACING AY TRACING AY TRACING AY TRACING
T TT TTRANSPARENT OBJECTS RANSPARENT OBJECTS RANSPARENT OBJECTS RANSPARENT OBJECTS RANSPARENT OBJECTS
Andrew Pearce
Alias
Toront o, Ont ario, Canada
This t r ick at t empt s t o avoid const ant int ensit y shad ows from t r ansparent
object s when r ay t r acing. It s loosely based on Sweeneys (1984) t r ick for
ambient light .
In r ealit y, light t r ansmit t ed t hr ou gh a t r ansp arent object p r od u ces
caust ics in t he object s shad ow. Unfor t unat ely, t r ad it ional r ay t r acing
d oes not t ake t his effect int o account . Recent ly researcher s have at t acked
t his problem in sever al ways; by backward s r ay t r acing (Ar vo, 1986),
which involves t r acing r ays from a point light source, t hrough t he t r ans-
parent object , and record ing t he result ing illuminat ion as it falls on t he
occlud ed sur face; by pencil t r acing (Shinya et al., 1987), where bund les
of r a ys a r e t r a ced fr om a p oi n t l i gh t sou r ce, t h r ou gh a n op t i ca l
syst emt he t r ansparent object t o creat e illuminance polygons on t he
occlud ed sur faces; and by gr id pencil t r acing (Shinya et al, 1989), which
is similar t o pencil t r acing except t he illuminance polygons are scan
conver t ed int o an illuminance map t o avoid aliasing problems, and t he
light source may be an area source.
All of t hese t echniqu es p r od u ce ver y imp ressive images, bu t t hey
require a lot of effor t t o implement . An alt er nat ive, which ignores caus-
t ics, is t o at t enuat e t he light based on t he d ist ance a shad ow r ay t r avels
t hr ou gh a t r ansp ar ent object and on t he object s op t ical d ep t h (t he
object s t r ansparency). Unfor t unat ely, t his approach may require a power
and / or an exponent ial funct ion; it d oes not allow for t he light t o appear
focu sed in one p ar t icu lar region; it requ ires r emember ing all of t he
shad ow int er sect ions, since t hey may be found in no specic ord er, and a
sense of opt ical med ium must be d et er mined for t he shad ow r ay.
398
VII.6 SHADOW ATTENUATION FOR RAY TRACING TRANSPARENT OBJECTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 398
A t r ick I prefer is t o at t enuat e t he int ensit y of t he light source based on
t he cosine of t he incid ent angle of t he shad ow r ay wit h t he t r ansparent
object at each int er sect ion along t he shad ow r ay. The amount of at t enua-
t ion is cont rolled by a scale value, which has a r ange from 0.0 t o 1.0. The
r at ionale for t his t r ick is t hat shad ows from glasses of wat er or cr yst al
balls t end t o focus light more t oward t he cent er of t he shad ow where t he
angle bet ween t he light r ays and t he sur face nor mal is small. This is not
always t rue; t he focus can be t ot ally out sid e of t he shad ow alt oget her,
usually seen from object s wit h a high ind ex of refr act ion. This t r ick is
good enough t o fool t he eye on object s wit h small ind ices of refr act ion,
such as glass asht r ays, plast ic mar bles, or bot t les of beer.
The new t r ansparency t er m mult iplier (t
m
) is calculat ed
t
m
= MAX[0, 1 + (

r
S


r
N
)t
s
] ,
where

r
S is t he nor malized shad ow r ay d irect ion vect or

r
N is t he sur face nor mal vect or (facing t he eye)
t
s
is t he scale fact or for t he effect (0.0 t o 1.0).
The act ual t r ansparency value (k
t
) for t he object is assumed t o lie in
t he r ange 0.0 t o 1.0. The nal t r ansparency value for t he object (

k
t
) is
comput ed

k
t
= k
t
t
m
If t
s
= 0.0, t he shad ow will be of const ant int ensit y (equivalent t o t he
t r a n sp a r en cy of t h e object ) r ega r d l ess of t h e a n gl e of i n ci d en ce.
If t
s
= 1.0, t he shad owing will be complet e when t he angle bet ween

r
N and

r
S is 90 d egrees; t he shad owing will be equivalent t o t he t r ansparency of
t he object when t he angle bet ween

r
N and

r
S is 180 d egrees.
The t er m t
m
is comput ed for ever y t r ansparent sur face int er sect ed by
t he shad ow r ay and fact ored int o

k
t
, which is init ialized t o 1.0 for t he
shad ow r ay. Of cour se all of t his wor k is wast ed if t he shad ow r ay st r ikes
399
VII.6 SHADOW ATTENUATION FOR RAY TRACING TRANSPARENT OBJECTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 399
an opaque sur face, which is why Haines id ea of t est ing opaque object s
for shad owing before checking t r ansparent object s should be used in
conjunct ion wit h t his t echnique.
Set t ing t
s
= 0.6 wor ks well in most cases. This t r ick can be used in
conju nct ion wit h int egr at ing t r ansp arency over t he d ist ance t r aveled
t hrough t he object , result ing in a d et ailed shad ow.
See also Simulat ing Fog and Haze (364); Efcient Gener at ion
of Sampling Jit t er Using Look-up Tables (64); Fast Line-Ed ge
Int er sect ions on a Unifor m Gr id (29); Tr ansfor ming Axis-Aligned
Bound ing Boxes (548)
8
NUMERICAL AND
PROGRAMMING
TECHNIQUES
403
ROOT FINDING SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 403
R RR RROOT FINDING OOT FINDING OOT FINDING OOT FINDING OOT FINDING
S SS SSUMMARY UMMARY UMMARY UMMARY UMMARY
Fi n d i n g t h e r oot s of a n equ a t i on i s a common t a sk i n comp u t er
gr a p h i cs. In 3D, t h e op er a t i on i s ver y common i n r a y t r a ci n g,
wh en object / r a y i n t er sect i on s a r e comp u t ed u si n g t h e i mp l i ci t
for m for a n object ; t h e i n t er sect i on s of t h e object a n d r a y a r e
t h en r ep r esen t ed by t h e z er os of t h e equ a t i on for med by p l u g-
gi n g t h e exp l i ci t for m of t h e r a y i n t o t h e exp l i ci t for m of t h e
object . In 2D, some a p p l i ca t i on s of r oot -n d i n g i n cl u d e t h e
d et er minat ion of bound ing boxes and accur at e t r acing of cur ves.
Th e fol l owi n g Gems d i scu ss n u mer i ca l r oot n d i n g. Li n ea r
a n d qu a d r a t i c equ a t i on s a r e t r i vi a l , a n d t h e sol u t i on s wel l -
kn own . Cu bi c a n d qu a r t i c equ a t i on s ma y a l so be sol ved a n a l yt i -
cally, bu t it t akes some car e t o make su r e t h e solu t ion is st able;
t he r st Gem ad d resses t hat quest ion.
Th e secon d a n d t h i r d Gems a r e mor e gen er a l n u mer i ca l
sol u t i on s, wh i ch a r e d esi gn ed t o n d t h e z er os of a fu n ct i on
efcient ly and robust ly.
See also Ray Tr acing (383); Dist ance Measures Summar y (423)
404
VIII.1 CUBIC AND QUARTIC ROOTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 404
VIII.1 VIII.1 VIII.1 VIII.1 VIII.1
C CC CCUBIC AND UBIC AND UBIC AND UBIC AND UBIC AND
Q QQ QQUARTIC ROOTS UARTIC ROOTS UARTIC ROOTS UARTIC ROOTS UARTIC ROOTS
Jochen Schwarze
ISA GmbH
St ut t gart , Federal Republic of Germany
The r ay-object int er sect ion used in r ay t r acing requires t he solut ion of
cubic and quar t ic equat ions as soon as more complex object s like splines
and t or i are t o be suppor t ed . It er at ive algor it hms are oft en slow and
numer ically unst able. St ar t values and t he number of noncomplex root s
are hard t o d et er mine.
An approach t o nd ing cubic and quar t ic root s analyt ically is present ed
in t he following. Sample cod e in C shows a possible implement at ion wit h
int er med iat e var iables and case d ecisions for t ime efciency. It u ses
d ouble precision ar it hmet ic; no complex nu mber s and oper at ions are
required .
Sol ut i on of t he Cubi c Sol ut i on of t he Cubi c Sol ut i on of t he Cubi c Sol ut i on of t he Cubi c Sol ut i on of t he Cubi c
A cubic equat ion of t he for m
c
3
x
3
+ c
2
x
2
+ c
1
x + c
0
= 0
is r st d ivid ed by c
3
, giving t he nor mal for m
x
3
+ Ax
2
+ Bx + C = 0
Subst it ut ion of

x y
A
3
405
VIII.1 CUBIC AND QUARTIC ROOTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 405
eliminat es t he quad r at ic t er m, yield ing
y
3
+ 3py + 2q = 0.
Using Card anos For mula (G. Card ano, 1501-1576), t he d et er minant is
D = q
2
+ p
3

u, v q t D
3
,
and t he root s are
y
1
= u + v

y
2, 3

u + v
2
t
3
2
u v ( )i.
Three cases can be d ist inguished :
D > 0: one real (y
l
), t wo conjugat ed complex values (y
3
, y
3
)
D = 0: t wo real values, y
2
= y
3
D < 0: t hree d ifferent real values.
In t he case of D < 0 (t he so-called casus irreducibilis) t r igonomet r ic
subst it ut ion helps t o nd all t hree solut ions wit hout t he need for complex
ar it hmet ics:

cos
q
p
3
y
1
2 p cos

3
y
2,3
2 p cos
t
3
.
Resubst it ut ion yield s t he cor rect values for x.
406
VIII.1 CUBIC AND QUARTIC ROOTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 406
Sol ut i on of t he Quar t i c Sol ut i on of t he Quar t i c Sol ut i on of t he Quar t i c Sol ut i on of t he Quar t i c Sol ut i on of t he Quar t i c
A quar t ic equat ion,
c
4
x
4
+ c
3
x
3
+ c
2
x
2
+ c
1
x + c
0
= 0,
is d ivid ed by c
4
:
x
4
+ Ax
3
+ Bx
2
+ Cx + D = 0,
and subst it ut ion of

x y
A
4
eliminat es t he cubic t er m
y
4
+ py
2
+ qy + r = 0.
The resolvent cubic is t hen

z
3

p
2
z
2
rz +
rp
2

q
2
8
0.
Wit h z being one root of t he above equat ion, t he root s of t he quar t ic can
be obt ained by solving t he t wo quad r at ic equat ions

y
2
t y 2z p + z m z
2
r 0.
Resubst it ut ion yield s t he cor rect values for x.
Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on Impl ement at i on
Th e t h r ee fu n ct i on s SolveQuadric(), SolveCubic(), an d SolveQuart ic()
t ake bot h an ar r ay of coefcient s and an ar r ay of solut ions as par amet er s.
They ret ur n t he number of noncomplex solut ions and put t he r oot s int o
t he second ar r ay. Double and t r iple solut ions are d et ect ed in t he cod e but
are ret ur ned only once.
407
VIII.1 CUBIC AND QUARTIC ROOTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 407
Incor rect result s prod uced by oat ing point precision er ror s have t he
most ser i ou s effect s wi t h smal l n u mber s ar ou n d zer o. A fu n ct i on
IsZero() is required for such values t o be recognized . The ext ent of t he
appropr iat e epsilon sur round ing EQN_EPS should be set accord ingly.
Int er med iat e var iables are used t o avoid red und ant oat ing point oper -
at ions. More opt imizat ion could be d one by checking for d ivision by one,
and ot her special cases. The IsZero() funct ion could be inline cod ed if
suppor t ed by t he compiler.
The present ed solut ion for cubic and quar t ic root s allows easy imple-
ment at ion and ad apt ion. It is sufcient ly fast and accur at e t o meet t he
requirement s of gr aphics applicat ions.
See Append ix 2 for C Implement at ion (738)
408
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 408
VIII.2 VIII.2 VIII.2 VIII.2 VIII.2
A A A A A BZIER BZIER BZIER BZIER BZIER
C CC CCURVE-BASED URVE-BASED URVE-BASED URVE-BASED URVE-BASED
R RR RROOT-FINDER OOT-FINDER OOT-FINDER OOT-FINDER OOT-FINDER
Philip J. Schneider
Universit y of Geneva
Geneva, Swit zerland
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Mat hemat ics, especially geomet r y and linear algebr a, is fund ament al t o
comp u t er gr ap hics. One imp or t ant class of p roblems t hat ar ises fr e-
quent ly is t hat of nd ing t he root or root s of funct ionsfor example
nd ing t he int er sect ion of a r ay wit h a t orus or t he int er sect ion of t wo
cur ves. More oft en t han not , t he polynomials one must solve are of high
enough d egree t hat no closed -for m solut ion exist s. This Gem d escr ibes an
algor it hm for nd ing all t he root s of such higher-ord er equat ions. This
algor it hm was d eveloped in t he cont ext of an int er act ive t wo-d imensional
cur ve-d r awing syst em (Schneid er, 1983), and was or iginally used t o solve
t he problem d escr ibed in Solving t he Nearest -Point -On-Cur ve Problem
in t his volume. Var iant s on t his root -nd ing algor it hm have apparent ly
been d iscovered ind epend ent ly by a number of r esearcher s, but it appear s
t o be relat ively unknown.
The root -nd ing algor it hm present ed here is a var iant of t he bisect ion
met hod . By exploit ing cer t ain char act er ist ics of Bzier cur ves, we are
able t o nd all t he root s of equat ions wit h no closed for m. A common
approach t o problems such as t hese is t o use Newt on-Raphson it er at ion,
a met hod t hat begins wit h an init ial guess at a solut ion, and it er at ively
converges on t he root . Unfor t unat ely, t ext s d escr ibing Newt on it er at ion
usually begin t heir explanat ion wit h a phr ase such as, Assume you have
an init ial guess u. . . . The met hod d escr ibed here, on t he ot her hand ,
need s no such init ial guess, and wor ks recur sively t o nd t he root or
409
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 409
root s. The basic met hod can be st at ed quit e br iey:
1. Conver t t he equat ion t o Ber nst ein-Bzier for m.
2. Find t he root s of t he equat ion by nd ing t he int er sect ion(s) of t he
result ing Bzier cur ve wit h t he hor izont al 0-axis.
Conver si on t o Ber nst ei nBzi er f or m Conver si on t o Ber nst ei nBzi er f or m Conver si on t o Ber nst ei nBzi er f or m Conver si on t o Ber nst ei nBzi er f or m Conver si on t o Ber nst ei nBzi er f or m
Fu nct ional cu r ves have t he gener al for m y = f(x), where f is some
polynomial. This can be recast as a par amet r ic polynomial:
Graph(t ) = (x(t), y(t ))
= (X,Y).
The quest ion is now: what is t he Bzier cont rol polygon for Graph? That
is, we seek cont rol point s V
i
= (x
i
, y
i
) in t he plane t hat sat isfy

X , Y ( ) V
i
i 0
n

B
i
n
t ( ).
The x and y component s are ind epend ent , so t he above vect or-valued
equat ion is really t wo scalar equat ions:

X x
i
i 0
n

B
i
n
t ( )
Y y
i
i 0
n

B
i
n
t ( ).
By t he linear precision proper t y of Bzier cur ves (Boehm et al, 1984; Far in,
1988), we know t hat we can rewr it e t he funct ion as

Graph t ( )
i
n
, y
i

_
,
i 0
n

B
i
n
t ( ).
410
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 410
That is, t he cont rol point s of t he Ber nst ein-Bzier for m of Graph are
spaced evenly in t he int er val [0, 1] (t he x d ir ect ion, if you will).
So, it remains t o d et er mine t he coefcient s of Y, which will cor r espond
t o t he y component of t he cont rol point s of t he Ber nst ein-Bzier for m of
Graph. This is relat ively st r aight for ward . What follows is a d escr ipt ion of
a met hod d ue t o Lane (1989) for conver t ing from t he power basis t o
Bzier basis. Given
we want

Y
i
{ }
0
n
such t hat
A simple algor it hm for t he conver sion is:
for j: i nteger 1, j + 1, whi l e j n do
begi n
d 1;
e c;
for i: i nteger n, i 1, whi l e i j do
begi n
Y
i
d Y
i
+ eY
i 1
;
d d c;
e e + c;
end;
endl oop;
end;
endl oop;
Then,

{Y
i
}
0
n
are t he d esired coefcient s, and t he nal for m of t he equat ion

P(t ) =
i=o
n
i A
t
i
, t [0, 1],

P(t ) =
i=o
n

n
i
( )
t
i
(1 t )
n i
.

c
1
n + 1 j
( )
;
411
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 411
is:
The at t ent ive read er may object t hat t he r ange of t he polynomial above is
xed at [0, 1]. For polynomials t hat d o not nat ur ally fall int o t his for m,
one may apply t he Cauchy cr it er ion (Collins and Loos, 1976) t o nd t he
root bound ing int er val, and t hen map t his ont o t he [0, 1] int er val.
Fi ndi ng t he Root s Fi ndi ng t he Root s Fi ndi ng t he Root s Fi ndi ng t he Root s Fi ndi ng t he Root s
An example of a ft h-d egree cur ve for which we must nd t he root s is
shown in Fig. 1. The root s of t he polynomial are exact ly t hose values of
t where t he Ber nst ein-Bzier cur ve crosses t he hor izont al axis (t he t -axis).
In gener al, t he values of coefcient s of ar bit r ar y polynomials given in
power basis for m provid e no int uit ion regard ing t he shape of t he func-
t ional cur ve, and are of lit t le d irect help in nd ing root s. However, t he
values of coefcient s (t hat is, cont rol point s) of Bzier cur ves ar e ver y
int uit ively relat ed t o t he shape of t he cur ve. We can exploit sever al
char act er ist ics of Bzier cur ves in ord er t o help us nd t he root s. These
Figur e 1. A ft h-d egree polynomial in Bzier for m.

Graph (t ) =
i = 0
n
i
n
, Y
i

_
,
B
i
n
(t ).
412
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 412
char act er ist ics are as follows:
1. The convex hull proper t yt he cur ve it self will always lie wit hin t he
convex hull of t he cont rol point s.
2. The var iat ion-d iminishing proper t yt he cur ve is an approximat ion
t o t he cont rol polygon t hat is smoot her t han t he cont rol polygon.
More specically, any st r aight line will int er sect t he cur ve at most as
many t imes as t he st r aight line int er sect s t he cont rol polygon.
3. Repeat ed subd ivision of t he cur ve yield s cont rol point s t hat converge
on t he cur ve it self in t he limit .
These obser vat ions lead t o a simple recur sive algor it hm, which appear s
in Fig. 2. The algor it hm st ar t s by count ing t he number of sign changes in
t he list of Bzier cont rol point s (t he coefcient s)t his cor respond s t o t he
number of t imes t he cont rol polygon cr osses The t-axis. By Descar t es
Law of Signs, and t he var iat ion-d iminishing proper t y of Bzier cur ves, we
know t hat t he number of root s in an int er val will be less t han or equal t o
t he number of such crossings. If t here are no sign changes, t he Bzier
FindRoots (V)
V: array[0..n] of point; Control points of Bezier representation
begin
if (control polygon does not cross t-axis)
then return NoRoots;
else if ((control polygon crosses t-axis once) and
(control polygon is at enough or recursion limit reached))
then return (t-intercept of chord from V[0] to V[n] as the root)
else begin
subdivide the Bezier curve V at midpoint, using deCasteljaus algorithm;
FindRoots(left half);
FindRoots(right half);
return (all solutions thereby generated);
end;
end;
end.
Figure 2. The root -nd ing algor it hm.
413
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 413
cur ve cannot t he t -axis, and t here are no root s, so we just ret ur n. If t here
is one sign change, we check if t he Bzier cont rol polygon is flat enough
or if we have r eached t he recu r sion d ep t h limit . By at enou gh,
we mean t hat t he cont rol polygon is close enough t o a st r aight line so t hat
we can approximat e t he root in t hat region by t aking t he int er sect ion of a
chord from t he r st t o last cont rol point s wit h t he t -axis as t he root t his
exploit s t he convex hull and var iat ion-d iminishing proper t ies.
When t here is mor e t han one sign, t here may be more t han one root in
t he int er val. In t his case, we subd ivid e t he cur rent cont rol polygon in t he
cent er of t he cur rent int er val using d eCast eljaus algor it hmt his gives us
t wo new Bzier cur ves, each of which represent exact ly half t he cur rent
cur ve. We t hen call FindRoot s recur sively on each new subcur ve. The
root s found at t he bot t om of each recur sive call are gat hered t oget her,
and make up t he set of root s for t he or iginal equat ion.
Whet her we subd ivid e at any par t icular recur sive d ept h d ue t o t he
cont rol polygon not being at enough or because t here is more t han one
t -axis crossing, subd ivision creat es smaller and smaller subcur ves. Be-
cause each subcur ve result ing from an applicat ion of d eCast eljaus algo-
r it hm is shor t er t han t he cur ve from which it came from, and because t he
new cont rol point s converge t oward t he cur ve, t he algor it hm can be
viewed as homing in on t he point at which t he cur ve it self crosses t he
t -axis.
As st at ed ear lier, we can st op t he recur sion when t he cont rol polygon
of t he cur rent polynomial is at enough; t hat is, when t he cont rol polygon
is close enough t o a line segment (and crosses t he t -axis just once), t he
root can be approximat ed by t he point of int er sect ion of t he t -axis and a
line segment d r awn from t he r st t o t he last cont rol point s. Our problem
now is t o d et er mine how at is at enough for a specied accept able
er r or. For a cont r ol p olygon cr ossing ju st once, (see Fig. 3 for an
example), we can d ene a bound ing box whose t op and bot t om are
par allel t o t he line connect ing t he r st and last cont rol point s, and whose
end s are per pend icular t o t he t op and bot t om. Then, t he er ror of t he
solut ion is t hen bound ed by half t he d ist ance bet ween t he point s at which
t he bound ing box int ercept s t he t -axis (or one or bot h end point s of t he
int er val [0, 1], if t he int er sect ion point (s) lie out sid e t his int er val). When
t he er r or of t he solut ion is wit hin t he d esired precision of t he root
approximat ion, we comput e t he int er sect ion of t he line from t he r st t o
last wit h t he t-axis, and ret ur n. Because we are d oing binar y subd ivision
414
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 414
Figur e 3. Bound ing box for a fift h-d egree polynomial
of t he fift h-d egree polynomial, and because t he t -values are bound ed
bet ween 0 and 1, we can easily comput e t he relat ionship bet ween t he
d ept h of t he recur sive subd ivision and t he d esired er ror. At t he t op level
(before any subd ivision), t he int er val is [0,1] and t he expect ed er ror is
0.5. Each binar y subd ivision t herefore cut s t he expect ed er ror in half, so
we merely st op t he recur sion when we have reached a d ept h cor repond -
ing t o t he base 2 logar it hm of t he d esired er ror bound . For example, if
our d esired precision is

1
128
, or

1
2
7 , we set our d ept h limit t o 7 (assuming
t he t op level is consid ered t o have d ept h 1).
A var iat ion on t his algor it hm can be used t o provid e an init ial bound
and guess for using Newt on-Raphson it er at ion. Not e t hat our algor it hm
here find s an int er val in which a root exist s (when t here is one t -axis
crossing). An init ial guess might be t he use t he mid point of such an
int er val or t he int er sect ion met hod d escr ibed ear lier. This may be an
415
VIII.2 A BEZIER CURVE-BASED ROOT-FINDER
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 415
essent ial mod ificat ion if one wishes t o find root s of ver y high-d egree
funct ions, because excessive repeat ed subd ivision of high-d egree Bzier
cu r ves t end s t o accu mu lat e float ing-p oint er r or s, which can lead t o
imprecise r oot s.
A C implement at ion of t his algor it hm appear s in t he append ix, in t he
le point _on_curve.c. The r ou t ine of int erest is FindRoot s; t he ar gu -
ment s t o t his rout ine are t he cont rol point s of t he Bzier cur ve, t he
d egree of t he cur ve, an ar r ay of real number s in which t o st ore t he root (s)
it nd s, and t he cur rent recur sive d ept h. The rout ine ret ur ns t he number
of root s found . In t hat sample implement at ion, t he d egree of t he root -
nd er is xed at 5 for simplicit y.
See Append ix 2 for C Implement at ion (787)
416
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 416
VIII.3 VIII.3 VIII.3 VIII.3 VIII.3
U UU UUSING STURM SEQUENCES TO SING STURM SEQUENCES TO SING STURM SEQUENCES TO SING STURM SEQUENCES TO SING STURM SEQUENCES TO
B BB BBRACKET REAL ROOTS RACKET REAL ROOTS RACKET REAL ROOTS RACKET REAL ROOTS RACKET REAL ROOTS
O OO OOF POLYNOMIAL EQUATIONS F POLYNOMIAL EQUATIONS F POLYNOMIAL EQUATIONS F POLYNOMIAL EQUATIONS F POLYNOMIAL EQUATIONS
D. G. Hook, P. R. McAree
The Universit y of Melbourne
Parkville, Melbourne, Aust ralia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
The polynomial
y = a
n
x
n
+ a
n1
x
n1
+ + a
1
x + a
0
f(x)
wit h real coefcient s a
0
, a
1
, . . . , a
n
is a cont inuous gr aph on t he (x,y)
plane whose real r oot s cor respond t o t he values of x where f(x) t ouches
or crosses t he axis y = 0. Recall t hat a polynomial of d egree n has n
root s, and t hat t hese may be real or complex and t hey need not be
d i st i n ct . In h i s t r ea t i se Mm. prsent es par div ers sav ant s, Ch a r l es
St ur m, (1835) d et ailed a met hod for nd ing t he exact number of real
root s of f(x) = 0 in t he int er val a < x < b. The approach provid es a
convenient and comput at ionally efcient t echnique for br acket ing root s
of f(x) = 0. Once a root is br acket ed , root polishing t echniques, such as
bisect ion, false posit ion, Brent s met hod , and Newt on-Raphson, can be
used t o obt ain accur at e est imat es.
The Theor em of St ur m The Theor em of St ur m The Theor em of St ur m The Theor em of St ur m The Theor em of St ur m
St u r ms Th eor em: Th er e exi s t s a s et of r ea l p ol yn omi a l s f
1
(x ),
f
2
(x), . . ., f
m
(x) whose d egrees are in d escend ing ord er, such t hat if b > a,
t he number of d ist inct real root s of f(x) = 0 in t he int er val x = a t o x = b
is given by t he d ifference N = s(a) s(b), where s(a) and s(b) are t he
number of sign changes in t he sequence f, f
1
, f
2
,. . ., f
m
at x = a and
x = b respect ively.
417
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 417
Thus, t o d et er mine t he number of d ist inct real root s of f(x) = 0 in t he
int er val (a, b), it sufces t o est ablish t he d ifference in t he number of sign
changes in t he St ur m sequence of f(x) bet ween limit s x = a and x = b.
The r eal r oot s of f(x) = 0 obviou sly fall in t he int er val (,+ ).
Applying t he St ur m t heorem t o t his int er val gives t he t ot al number of
such root s, while applying t he St ur m t heorem t o t he int er vals (, 0) and
(0, +) yield s, respect ively, t he number of negat ive and t he number of
posit ive real root s of f(x) = 0.
Of t he var iet y of met hod s for const r uct ing t he St ur m sequence of t he
p olynomial f(x), we give t h e most wid ely u sed (see, for examp le,
Tu r nbu ll, 1957 or Wilkinson, 1988). Set f
1
(x) = f(x) and f
2
(x) = f(x).
Then set f
3
(x). equal t o t he remaind er, wit h it s sign rever sed , of t he
d ivision f
1
(x)/ f
2
(x). Subsequent t er ms in t he sequence are found using
t he same process of polynomial d ivision. In gener al if t he member s f
k1
and f
k
have been found , f
k+l
will be t he remaind er aft er d ivid ing f
k1
by f
k
and r ever sing it s sign. This p rocess of p olynomial d ivision is
cont inued unt il t he remaind er t er m becomes a const ant .
Ever y polynomial f(x) wit h real coefcient s has a St ur m sequence t hat
will yield t he t ot al nu mber of d ist inct r eal root s of f(x). If f(x) has
repeat ed root s each member of t he sequence f, f
1
, f
2
,. . ., f
m
will share a
great est common mult iplier G = (x )
p
(x )
q
. . . , wher e power s
p, q, . . . are int eger s. The member s of t he St ur m sequence can be wr it t en
f
1
= G
1
, f
2
= G
2
,. . ., f
m
= G
m
, where
= (x )(x ) . . . (x ).
The repeat ed root s , , . . . can be found by ot her met hod s (for example,
synt het ic d ivision) aft er all of t he d ist inct root s have been found .
Exampl e Exampl e Exampl e Exampl e Exampl e
The polynomial
f(x) = x
3
+ 3x
2
1
418
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 418
has t he following St ur m sequence:
f
1
= x
3
+ 3x
2
1, f
2
= 3x
2
+ 6x, f
3
= 2x + 1, f
4
= 9/ 4.
The t able below shows t he signs for each member of t he St u r m
sequence at d ifferent values of x. From it we see t hat t he polynomial
f(x) has t hree real root s , , , which sat isfy t he following inequalit ies:
3 < < 2, 1 < < 0, 0 < < 1.
f
1
(x) f
2
(x) f
3
(x) f
4
(x) sign changes
x = + + 3
x = 3 + + 3
x = 2 + 0 + 2
x = 1 + + 2
x = 0 0 + + 1
x = +l + + + + 0
x = + + + + + 0
Const r uct i ng t he St ur m Sequence Const r uct i ng t he St ur m Sequence Const r uct i ng t he St ur m Sequence Const r uct i ng t he St ur m Sequence Const r uct i ng t he St ur m Sequence
Al gor i t hm A ( Dr i vi ng Al gor i t hm) Al gor i t hm A ( Dr i vi ng Al gor i t hm) Al gor i t hm A ( Dr i vi ng Al gor i t hm) Al gor i t hm A ( Dr i vi ng Al gor i t hm) Al gor i t hm A ( Dr i vi ng Al gor i t hm)
Given a polynomial
f(x) = a[n]x
n
+ a[n l]x
n l
+ + a[l]x + a[0],
creat e t he St ur m sequence f
1
, f
2
,. . . f
m
.
A1 Set f
1
, equal t o f(x), and set f
2
equal t o f(x).
A2 For k = 0, l , . . . , ord er (f
2
), set f
2
[k] = f
2
[k]/ abs(f
2
[ord er (f
2
)])
A3 Set k = 3. While ord er (f
k
) 0, d o algor it hm B (which t akes
paramet ers f
k 2
, f
k 1
, and set s f
k
), d o st ep A4, set k = k + 1. Set
f
k
[0] = f
k
[0]. St op.
419
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 419
A4 (rev erse t he signs of t he coefcient s of t he poly nomial and
normaliz e against t he leading coefcient . ) For j = 0, 1, . . . ,
ord er (f
k
), set f
k
[j] = f
k
[j]/ abs(f
k
[ord er (f
k
)]).
Al gor i t hm B ( Pseudo- di vi si on of pol ynomi al s t o Al gor i t hm B ( Pseudo- di vi si on of pol ynomi al s t o Al gor i t hm B ( Pseudo- di vi si on of pol ynomi al s t o Al gor i t hm B ( Pseudo- di vi si on of pol ynomi al s t o Al gor i t hm B ( Pseudo- di vi si on of pol ynomi al s t o
gi ve r emai nder ) gi ve r emai nder ) gi ve r emai nder ) gi ve r emai nder ) gi ve r emai nder )
Given polynomials
u(x) = u[m]x
m
+ u[m 1]x
m1
+ +u[1]x + u[0],
v(x) = v[n]x
n
+ v[n 1]x
n1
+ +v[1]x + v[0],
where v[n] = t 1.0 and m > n > 0, t his algor it hm nd s polynomial
r(x), which is t he remaind er of t he polynomial d ivision u(x)/ v(x).
B1 Copy u(x) int o r(x).
B2 If v[Ord er (v)] < 0.0, d o st eps B3, B4; ot her wise d o st eps B5, B6.
St op.
B3 k = Or d er (u) Or d er (v ) 1. Wh i l e k 0 d o r[k] = r[k],
k = k 2. Do st ep B4 for k = Or d er (u) Or d er (v ), Or d er (u)
Ord er (v) 1, . . . ,0
B4 j = Ord er (v) + k 1, Or d er (v) + k 2, . . . , k, r[j] = r [j]
r[Ord er (v) + k]v[ j k].
B5 Do st ep B6, for k = Ord er (u) Ord er (v), Ord er (u) Ord er (v)
1, . . . , 0
B6 j = Ord er (v) + k 1, Or d er (v) + k 2, . . . , k, r[j] = r [j]
r[Ord er (v) + k]v[ j k].
Count i ng t he Si gn Changes Count i ng t he Si gn Changes Count i ng t he Si gn Changes Count i ng t he Si gn Changes Count i ng t he Si gn Changes
Al gor i t hm C Al gor i t hm C Al gor i t hm C Al gor i t hm C Al gor i t hm C
Given t he St u r m sequ ence f
1
, f
2
,. . ., f
m
, cou nt t he nu mber of sign
changes for t he sequence at a given x. S is t he number of sign changes.
420
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 420
C1 Set S = 0. Do st eps C2, C3 for k = 2, 3, . . . , m. St op.
C2 If x = +
a = f
k - 1
[ord er (f
k - 1
)], b = f
k
[ord er (f
k
)].
If x =
if ord er (f
k - 1
) is od d , a = f
k - 1
[ord er (f
k - 1
)],
ot her wise a = f
k - 1
[ord er (f
k - 1
)],
if ord er (f
k
) is od d , b = f
k
[order(f
k
)],
ot her wise b = f
k
[ord er (f
k
)].
if x = 0.0
a = f
k - 1
[0], b = f
k
[0]
ot her wise
a = f
k - 1
(x), b = f
k
(x).
C3 If a b < 0.0 or a = 0.0, S = S + 1.
Some oper at ions in t his algor it hm can be avoid ed if we make use of t he
following char act er ist ics of St ur m sequences (Tur nbull, 1957; Wilkinson,
1988):
1. Contiguous members of the sequence cannot evaluate to zero at the
same value of x.
2. If f
k
= 0, t he signs of f
k 1
and f
k + 1
are equal in magnit ud e but
opposit e in sign.
Consequent ly, t here ar e circumst ances when t he polynomial f
k + 1
d oes
not need t o be evaluat ed once t he value of f
k 1
is known.
Met hod of Bi sect i on Met hod of Bi sect i on Met hod of Bi sect i on Met hod of Bi sect i on Met hod of Bi sect i on
The St ur m met hod can be used t o isolat e any real root of f(x) = 0say
t he kt h in ord er of d ecreasing valuesusing the met hod of bisect ion.
421
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 421
Suppose we have an int er val (a,b) such t hat
b > a; s(a) s() k; s(b) s() < k.
By t he St ur m t heorem, t he int er val (a, b) must cont ain t he kt h root ,
k
Of
f(x) = 0. Repeat ed bisect ion of t he int er val (a, b) will isolat e
k
t o an
int er val (a
p
, b
p
), of wid t h (b a)/s
p
aft er p st eps, and provid ed s(a
p
)
s(b
p
) = 1,
k
will be t he only d ist inct real root of f(x) in t his int er val.
Algor it hm D below, is based on t he analysis by Wilkinson (1988).
Al gor i t hm D ( Bi sect i on Met hod) Al gor i t hm D ( Bi sect i on Met hod) Al gor i t hm D ( Bi sect i on Met hod) Al gor i t hm D ( Bi sect i on Met hod) Al gor i t hm D ( Bi sect i on Met hod)
Given a St ur m sequence for t he polynomial f(x) and an int er val (a, b)
known t o cont ain t he kt h root of f(x) = 0, t his algor it hm isolat es an
int er val t hat cont ains only t he kt h root .
Dl Det er mi n e s(a), s(b). Do st ep s D2, D3 u n t i l s(a) s(b) = 1.
St op.
D2 set c = (a + b))/ 2 and d et er mine s(c).
D3 if s(c) s( ) k set a = c, ot her wise set b = c.
The t ime t aken by algor it hm D is ind epend ent of t he separ at ion of t he
root s of f(x) = 0 and is except ionally st able even when t he root s are
pat hologically close. Algor it hm D could be used t o nd

k
t o any d esired
accur acy; once an int er val cont aining t he root has been isolat ed , however,
fast er convergence is possible using t he known infor mat ion about f(x) at
each st ep. Br acket ing met hod s, such as classical bisect ion, false posit ion,
an d Br en t s met h od , ar e mor e st abl e t h an op en met h od s, su ch as
Newt on-Raphson and t he secant met hod . Not e t hat classical bisect ion
r elies on f(x) changing sign acr oss any int er val cont aining an od d
number of real root s. It should not be confused wit h algor it hm D.
Concl usi ons Concl usi ons Concl usi ons Concl usi ons Concl usi ons
We have used algor it hms A, B, C, and D in a r ay t r acer t hat r end er s
algebr aic sur faces of ar bit r ar y ord er. The point s where each r ay int er sect s
422
VIII.3 USING STURM SEQUENCES TO BRACKET REAL ROOTS OF POLYNOMIAL EQUATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 422
t he sur face cor respond t o t he real root s of a polynomial whose ord er is
t he same as t he sur face. These polynomials are frequent ly ill-cond it ioned .
In t his applicat ion we used t he mod ied false posit ion algor it hm t o polish
t he root s. It converges in almost quad r at ic t ime, is ext remely st able, and
is gener ally fast er t han ad apt ive met hod s (such as Brent s met hod ) when
t he polynomial is unpred ict able. In similar sit uat ions we recommend it .
St u r ms met hod can also be u sed t o nd t he comp lex r oot s of a
polynomial. The approach is d et ailed in Pinker t (1976).
See Append ix 2 for C Implement at ion (743)
423
DISTANCE MEASURES SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 423
D DD DDISTANCE MEASURES ISTANCE MEASURES ISTANCE MEASURES ISTANCE MEASURES ISTANCE MEASURES
S SS SSUMMARY UMMARY UMMARY UMMARY UMMARY
Ca l cu l a t i n g d i st a n ce i s a n i mp or t a n t a n d common op er a t i on i n
comp u t er gr a p h i cs. Oft en a n exa ct a n swer i s n ot i mp or t a n t . For
exa mp l e, su p p ose you a r e wr i t i n g a 2D d r a wi n g p r ogr a m, a n d
you ar e con t emp l at i n g wr i t i n g t h e cod e t o d o sel ect i on : t h e u ser
cl i cks t h e mou se, a n d t h e n ea r est object i s sel ect ed . Si n ce t h e
op er a t i on i s i n t er a ct i ve, you d o n ot n eed a n exa ct sol u t i on for
t h e d i st a n ce t o ea ch n ea r by object ; t a ki n g t h e mi n i mu m of
est i ma t ed d i st a n ces i s p r oba bl y good en ou gh . If t h e wr on g
object i s occa si on a l l y sel ect ed , t h e u ser ma y move cl oser t o t h e
one d esired and click again.
A 3D a p p l i ca t i on for a n a p p r oxi ma t e d i st a n ce met r i c cou l d be
t r aci n g t h e p at h of a movi n g, ch ar ged p ar t i cl e t h r ou gh a vol u me
l l ed wi t h st a t i c ch a r ged p a r t i cl es. On l y t h ose st a t i c ch a r ges
cl oser t h a n some t h r esh ol d (d et er mi n ed by t h ei r ch a r ge) wi l l
i n u en ce t h e movi n g p a r t i cl e; a n a p p r oxi ma t e d i st a n ce i s
en ou gh t o d et er mi n e i f a ch a r ge i s cl ose en ou gh t o i n u en ce
t h e movi n g p a r t i cl e. Mor e common a p p l i ca t i on s i n 3D i n cl u d e
a n ext en si on of t h e 2D exa mp l e t o a 3D d r a wi n g p r ogr a m;
d et er mi n i n g n ea r i mp l i ci t fu n ct i on s for t est i n g i n a r a y t r a c-
i n g syst em; a n d n d i n g ca n d i d a t es for col l i si on d et ect i on i n a
d ynamics syst em.
Th e fol l owi n g Gems p r esen t d i st a n ce a p p r oxi ma t i on s i n 2D
a n d 3D. Not e t h a t you ca n gen er a l i z e a 2D d i st a n ce met r i c
d = f(d1, d2) t o 3D by wr i t i n g d3 = f(d1, f(d2, d3)), a n d t h en
exp a n d i n g t h e r esu l t for d3. Th i s p r ocess ma y be i t er a t ed t o
higher d imensions if need ed .
See also Root Find ing Summar y (403)
424
VIII.4 A HIGH SPEED LOW PRECISION SQUARE ROOT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 424
VIII.4 VIII.4 VIII.4 VIII.4 VIII.4
A AA AA HIGH SPEED HIGH SPEED HIGH SPEED HIGH SPEED HIGH SPEED, ,, ,,
L LL LLOW PRECISION OW PRECISION OW PRECISION OW PRECISION OW PRECISION
S SS SSQUARE ROOT QUARE ROOT QUARE ROOT QUARE ROOT QUARE ROOT
Paul Lalonde and Robert Dawson
Dalhousie Universit y,
Halif ax, Nova Scot ia, Canada
Tr ad it ional met hod s for evaluat ing square root s using it er at ive met hod s
are oft en t oo slow for use eit her in int er act ive syst ems or when large
number s of square root s must be evaluat ed . Oft en, par t icular ly in com-
put er gr aphics, t he precision required is much less t han t hat comput ed .
For inst ance, t he sqrt () fu nct ion in most C libr ar y imp lement at ions
ret ur ns a d ouble precision result , even when passed a single precision
oper and .
When only a few d igit s of accur acy are required a fast er approach can
be used . The t echnique is t o use t he most signicant bit s of t he mant issa
as an ind ex int o a t able of square root s. By using t his looked -up value and
halving t he exponent , a low-precision square root funct ion can be built
t hat runs much fast er t han it er at ive met hod s.
A review of oating point formats is in order. A oating point number in
bi n ar y i s of t h e for m 2
ee...e
mm...m, wh er e m an d e r ep r esen t
bit s. All oat ing point number s can be expressed in nor malized for m, in
which a nu mber t akes t he for m eeeeeeee 1.mmmmmmm. Since any
oat ing point number can be nor malized , most syst ems assume nor mal-
izat ion and st ore only t he fr act ional par t . For illust r at ive pur poses we will
consid er a oat ing point t ype wit h an eight -bit exponent , one-bit sign,
a n d seven -bi t ma n t i ssa , st or ed i n t h e for m [2^ ]e
7
e
6
e
5
e
4
e
3
e
2
e
1
e
0
[*]
[1.]m
6
m
5
m
4
m
3
m
2
m
1
m
0
.

2
m
n 2
m
2
n
Equ at ion 1 shows t hat t he op er at ion of t aking a squ ar e root of a
oat ing point number red uces t o halving t he exponent and nd ing t he
(1)
425
VIII.4 A HIGH SPEED LOW PRECISION SQUARE ROOT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 425
e exponent(V)
i manti ssa(V)
i f (e bi t-and 1) t he exponent is odd
set t he high bit of i
e e/2 divide e by t worecall equat ion 1
(This division must preserve sign)
j T[i]
U 2
e
1.j
Figur e 1.
square root of t he mant issa. As an od d exponent cannot be d ivid ed by 2,
we br ea k t h e n u mber i n t o a n even exp on en t e
7
e
6
e
5
e
4
e
3
e
2
e
1
0, a n d a
quat ernary mant issa, e
0
m
6
m
5
m
4
m
3
m
2
m
1
m
0
, i n t h e r a n ge [1. . 4).
The sign bit of t he mant issa is ignored , but may be used to ag an error if
negat ive. This st ores all t he values of [1..4) wit h no loss of infor mat ion
from t he or iginal oat ing point represent at ion.
A look-up t able is creat ed d ur ing t he applicat ions init ializat ion, which
st ores t he square root s of t he values 2^ e
0
*1.m
6
m
5
m
4
m
3
m
2
m
1
m
0
.
build _t able(precision: i nteger, t able: array [0..2
precision+l
] of i nteger)
i, j: i nteger;
f, sf: real ;
begi n
for i: i nteger 0, 2
precision
1
f 1.i
t able[i] mant issa(

f
);
f 2*1.i
t able[i + 2
precision
] mant issa(

f
);
endl oop;
end
Figur e 2.
426
VIII.4 A HIGH SPEED LOW PRECISION SQUARE ROOT
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 426
Once t he t able, T, is built , t he algor it hm t o nd a square root , U, of a
given oat ing p oint nu mber V is as follows. Let p be t he r equ ired
precision in bit s, and i be an int eger p + 1 bit s wid e. Pseud o-cod e t o
build t he t able is given in Fig. 2. Pseud o-cod e for ext r act ing t he square
root is shown in Fig. 1. Sample cod e illust r at ing t his process is shown in
t he append ix. This cod e is in no way opt imal. An assembler implement a-
t ion of a similar algor it hm execut ed almost ve t imes fast er t han t he C
implement at ion. Not e t hat we are halving t he exp onent as a signed
var iable; a simple shift r ight will not preser ve t he sign bit .
This whole process, once t he t able is gener at ed , can be per for med wit h
one bit wise and, t wo bit wise ors, one bit wise t est and ve shift oper a-
t ions. Clear ly t his is fast er t han any it er at ive square root process. The
main d isad vant ages are t he t ime required t o build t he t able of square
root s, which ad d s t o t he applicat ions st ar t -up t ime, and t he memor y
r equ ired t o st or e t he t able. For n bit s of p r ecision, 2
n + 1
wor d s of
memor y are required . For example, a seven-bit t able requires 256 byt es.
Similar t echniques can be used for ot her per iod ic and logar it hmically
per iod ic funct ions.
See Append ix 2 for C Implement at ion (756)
427
VIII.5 A FAST APPROXIMATION TO THE HYPOTENUSE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 427
VIII.5 VIII.5 VIII.5 VIII.5 VIII.5
A AA AA FAST FAST FAST FAST FAST
A AA AAPPROXIMATION TO THE PPROXIMATION TO THE PPROXIMATION TO THE PPROXIMATION TO THE PPROXIMATION TO THE
H HH HHYPOTENUSE YPOTENUSE YPOTENUSE YPOTENUSE YPOTENUSE
Alan W. Paet h
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Over vi ew Over vi ew Over vi ew Over vi ew Over vi ew
A fast ap p r oximat ion t o t he hyp ot enu se (Pyt hagor ean d ist ance) h=
(x
2
+ y
2
)
1/ 2
and it s relat ed approximat ion er ror is d er ived . The met hod
is useful in provid ing accept / r eject d ist ance t est s in 2D gr aphics. These
are commonly used in provid ing gr avit y eld s or ot her proximit y t est s
for circle or ellipse select ion (see conclud ing cod e).
Der i vat i on Der i vat i on Der i vat i on Der i vat i on Der i vat i on
When for ming a boolean proximit y t est for a known d ist ance d, t he
inequalit y may be rewr it t en in t he for m d
2
x
2
+ y
2
t hereby removing
t he square root . A useful approximat ion for h should d o likewise and
id eally d ispense wit h mult iplicat ion as well. To d er ive su ch a for m,
consid er (wit hout loss of gener alit y) t he case x > y > 0. Next , nor malize
t he t est :

h = x
x
2
x
2
+
y
2
x
2
= x 1 +
y
x

1
]
1
2
x +
1
2

x
y

1
]
1
1
2

1
8

x
y

1
]
1
1
4
+ O
x
y

1
]
1
1
6

_
,

.
The last for m is t he d irect Taylor expansion. Ret aining t er ms t hrough
t he qu ad r at ic gives h = x (1 +

1
2
(y/ x)
2
) wit h cu bic accu r acy. This
for m is commonly employed in libr ar ies offer ing a high-pr ecision hypot
428
VIII.5 A FAST APPROXIMATION TO THE HYPOTENUSE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 428
as t he convent ional for m is prone t o severe loss of accur acy: for y < x
it hold s t hat y
2
x
2
and t he sum wit hin t he r ad icand d iscard s much of
ys or iginal precision.
Fact or ing t he expansion as h = x + 1/ 2 y (y/x) and t hen fur t her
approximat ing by consid er ing (y/x) 1 (when it . in fact lies on t he r ange
[0..1 ]) yields the valuable approximation h = x +

1
2
y. The error is not as
great as may be expect ed because t he over size const ant approximat ion t o
(y/x) is in par t offset by t he Taylor ser ies t r uncat ion, since t he r st
omit t ed t er m is negat ive. By d iscard ing t he or iginal assumpt ion t hat
x > y > 0, t his may be rewr it t en as
h h = max(x, y) + 1/ 2 min(x, y).
For many languages, minimum and maximum oper at ions involve cost ly
cond it ional logic. Op er at ion cou nt may be r ed u ced by not ing t hat
max(a, b) + min(a, b) = a + b for all a and b yield ing t he well-known
for m
h h = x + y 1/ 2 min(x, y).
Not e t hat t he cod e is symmet r ic about t he axis x = y = 1 wit hin t he
r st quad r ant . Absolut e value oper at ions on t he input argument s allow for
four-quad r ant oper at ion, yield ing isomet r ic d ist ance lines of eight -fold
symmet r y (see Fig. 1) in close relat ion wit h t he circular Euclid ean nor m.
Figure 1. Equat ions of (approximat e) unit d ist ance.

429
VIII.5 A FAST APPROXIMATION TO THE HYPOTENUSE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 429
i nteger functi on id ist (i nteger: x1, y1, x2, y2)
begi n
min: i nteger;
i f (x2 x2 x1) < 0 then x2 x2;
i f (y2 y2 y1) < 0 then y2 y2;
i f x2 > y2 then min y2 el se min x2;
return [x2 + y2 rshi ft(min, 1)];
end
Figure 2. Approximat e vect or lengt h.
For great est speed t he 1/ 2 y scaling oper at ion may be replaced by a
single bit shift r ight . As t his oper at ion is on nonnegat ive quant it ies,
concer ns regard ing round ing d irect ion and ar it hmet ic ver sus logical shift -
ing vanish. The cod e is of gener al use when present ed as t he d ist ance
bet ween t wo point s, as in Fig. 2 above. Calls of t he for m idist (x1, y1, 0, 0)
provid e vect or lengt h by measur ing point -or igin d ist ance wit h lit t le ad d ed
expense.
Er r or Anal ysi s Er r or Anal ysi s Er r or Anal ysi s Er r or Anal ysi s Er r or Anal ysi s
Consid er t he locus of point s at unit d ist ance from t he or igin (cos , sin )
wit hin t he r st oct ant (cos > sin ). The d eviat ion bet ween t his id eal arc
and t he approximat ed d ist ance is t hen d ev() = cos +

1
2
sin 1. Dif-
ferent iat ion locat es t he point s of minimum and maximum d eviat ion at
2 sin = cos . The minimum lies at = 0 where t he funct ions h(0, 1) =
h(0, 1) = 1 are in per fect agreement . The maximum (found by d ivid ing
bot h t er ms by cos) lies at 26.5 at t he p oint (cos t an
-1
(1/ 2),
sin t an
-l
(1/ 2)). Subst it ut ion int o t he approximat e hypot enuse equat ion
for h yi el d s t h e vect or l en gt h (5/ 4) cos t a n
-1
(1/ 2) = (5/ 2) si n t a n
-1
(1/ 2) =

1
2

5
1.118. Thus, vect or est imat es are t oo large by a wor st -
case value of no more t han 12%, but are never small. Not e t hat t his is a
relat ive er ror as calculat ions were in refer ence t o an exact unit vect or.
Scaling h by an over all value of 94% split s t he d ifference in er ror but
d iscard s t he valuable proper t y h(x, y) h(x, y) for all x and y. When
cod ed in int eger precision, t he quant ized relat ive er ror may be smaller
430
VIII.5 A FAST APPROXIMATION TO THE HYPOTENUSE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 430
i nteger functi on Pnt OnCirc(i nteger: x
p
, y
p
, x
c
, y
c
, r )
begi n
ret urns t rue IF a t est point (x
p
, y
p
) is t o wit hin a
pixel of t he circle of cent er (x
c
, y
c
) and radius r ;
see also C source comment s
d : i nteger id ist (x
p
, y
b
, x
c
, y
c
);
i f r > d then return[0]; far-in
i f 9

r < 8 x (d 1) then return[0]; far-out


full t est : r < hypot (xp xc, yp yc) < r + 1
x
p
x
p
x
c
;
y
p
y
p
Y
c
;
d x
p
+ y
p
2
;
i f d < r
2
then return[0]; nearin
r r + 1
i f d > r
2
then return[0]; nearout
return[1]; WITHIN
end
Figur e 3. Point -on-circle t est .
t han it s t heoret ic bou nd becau se of t he loss of one bit of precision
inherent in t he halving oper at ion. While maint aining t he cont ainment
p r op er t y, t his effect mu st nonet heless be accou nt ed for in car efu lly
cr aft ed progr am cod e (See C-language source comment s).
If slight ly great er accur acy is d esired at t he cost of one mult iplicat ion,
t he 1/ 2 min t er m may be scaled d ownward s t o provid e a t ight er t
(Fig. 1, left ). The opt imal scale fact or occur s when t his value is 0.414 +


2 1 = t an(/ 8). Not e t hat t his br ings bot h met hod s int o agree-
ment at h(1, 1) = h(1, 1) =

2 so t hat t he approximat ion is now exact at
eight pr inciple compass point s. This change moves t he angle for wor st -
case d eviat ion from 26.5 + t o t he symmet r ic por t ion of t he oct ant at
/ 8 r ad ians (22.5). The wor st -case er ror is t hen red uced t o cos(/ 8) +
t an (/ 8) sin(/ 8) = sec(/ 8) 1.0824. Th is ch an ge main t ain s t h e
valuable proper t y h(x, y) h(x, y) wit h equalit y hold ing iff x = y
or x = 0 or y = 0. For many applicat ions t he mult iplicat ion is not wor t h
t he marginal gain in accur acy.
2
431
VIII.5 A FAST APPROXIMATION TO THE HYPOTENUSE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 431
Given t he bound ed nat ure of t he est imat e, a br acket ing int er val t est
may be mad e in which point s eit her t oo near and t oo far from a reference
lengt h are d iscard ed . A r ange check of t his nat ure is used as a circle pick
mechanism wit hin t he Lemming por t able gr aphics ed it or (Paet h, 1988).
Here t he approximat ion is used t o d iscard unlikely select ion cand id at es
quickly wit hout resor t ing t o mult iplicat ion unless necessar y, while yield -
ing no concessions t o accur acy (see Fig. 3).
Not e t hat all argument s are int eger s and t hat no int er med iat e oat ing
point calculat ions appear. As t he nal squar ing oper at ions d ouble t he
number of bit s in int eger s x
p
, y
p
, and r, 16-bit archit ect ures will require
int eger var iables of 32-bit precision t o accommod at e argu ment s wit h
values out sid e of t he byt e r ange ( 127).
See also A Fast 2D Point -on-Line Test (49); Tr igonomet r ic
Funct ions at Select Point s (18)
See Append ix 2 for C Implement at ion (758)
432
VIII.6 A FAST APPROXIMATION TO 3D EUCLIDIAN DISTANCE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 432
VIII.6 VIII.6 VIII.6 VIII.6 VIII.6
A AA AA FAST FAST FAST FAST FAST
A AA AAPPROXIMATION TO PPROXIMATION TO PPROXIMATION TO PPROXIMATION TO PPROXIMATION TO
3 D 3 D 3 D 3 D 3 D EUCLIDIAN DISTANCE EUCLIDIAN DISTANCE EUCLIDIAN DISTANCE EUCLIDIAN DISTANCE EUCLIDIAN DISTANCE
Jack Rit t er
Versat ec, Inc
Sant a Clara, Calif onia
Th i s Gem i s a fa st a p p r oxi ma t i on t o 3D Eu cl i d i a n d i st a n ce

dx dx + dy dy + dz dz

_
,

. It cou l d be u sed i n r a y t r a ci n g for


quickly calculat ing r ay ver sus bound ing volume d ist ances, or object t o
object d ist ances in gener al.
It t akes ad vant age of t he fact t hat t he sum of t he lengt hs of t he t hree
component s of a 3D vect or is a rough approximat ion of t he vect or s
lengt h. This way of measur ing d ist ance is also called Manhat t an d is-
t ance.
If t he vect or has only one nonzero component , t he Manhat t an d ist ance
is exact ly cor rect . This is t he best case. The wor st case is where all t hree
component s are equal in lengt h (d): t hen t he Manhat t an d ist ance will be
d + d + d = 3 d, where t he t r ue Euclid ian d ist ance is

3 d. The
wor st case is off by a fact or of 3/

3
1.7. The algor it hm below
compromises bet ween t hese ext reme cond it ions, giving a maximum er ror
much less t han 1.7. The algor it hm is as follows:
1. Find t hese t hree values: ABS(d x), ABS(d y), ABS(d z).
2. Sor t t hem (3 compares, 03 swaps).
3. Approximat e d ist ance = max + (1/ 4)med + (1/ 4)min.
This approximat ion is accur at e wit hin 13%.
Two successively more rened st ep 3 for mulae are
max + (5/ 16)med + (1/ 4)min (has 9%er ror )
max + (11/ 32)med + (1/ 4)min (has 8% er ror ).
433
VIII.6 A FAST APPROXIMATION TO 3D EUCLIDIAN DISTANCE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 433
In all cases, t he coefficient s are fr act ions whose d emoninat or s ar e
p ower s of 2. This avoid s d ivision. The numer at or s can be calculat ed by
shift ing and ad d ing , avoid ing mult iplicat ion. This allows ver y simple fixed
point ar it hmet ic, which is fast er t han t he 3D Euclid ian calculat ion, even wit h
a float ing point coprocessor.
If t he vect or component s are float ing point values, t hey may need t o be
scaled before being conver t ed t o int eger s. If t he maximum lengt h vect or
we wou ld exp ect is 100 or more, for examp le, t hen no scaling need be
d one; a span of 100 is enough resolut ion, given t hat we are only accur at e
t o t8% t o begin wit h. If t he span is, say, 1.0, t hen we need t o scale up.
Mor e gener ally, t his algor it hm is an alt er nat ive t o find ing t he squ are
r oot of t he su m of t hr ee squ ares, which need not mean d ist ance, or
anyt hing spat ial at all.
Her e is p seu d o-cod e for t he 13% ver sion, where scaling is need ed . A
scale of 1024 is used , which can be accomplished by shift ing by 10. This
means t hat t he cod e cont ains no mult iplies, d ivid es, or square root s, and
is all in fixed point .
approx_lengt h_13(d x, d y, d z) :real
d x, d y, d z: real ;
begi n
maxc, med c, minc: i nteger:
Convert t he reals t o scaled int egers.
maxc abs ( 1shift (d x, 10) );
med c abs ( 1shift (d y, 10) );
minc abs ( 1shift (d z, 10) );
Sort . Need only find max.
i f maxc < med c
then swap(maxc, med c);
i f maxc < minc
then swap(maxc, minc);
Comput e 1/ 4 of med & min in 1 st ep.
med c med c + minc;
maxc maxc + r shif(med c, 2);
return ( (real ) r shift (maxc,10) );
end
434
VIII.7 FULL PRECISION CONSTANTS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 434
VIII.7 VIII.7 VIII.7 VIII.7 VIII.7
F FF FFULL-PRECISION ULL-PRECISION ULL-PRECISION ULL-PRECISION ULL-PRECISION
C CC CCONSTANTS ONSTANTS ONSTANTS ONSTANTS ONSTANTS
Kelvin Thompson
Nt h Graphics, Lt d
Aust in, Texas
Most ext end ed -precision oat ing point for mat s st ore t he equivalent of
14-16 d ecimal d igit s of p recision (see Conver t ing bet ween Bit s and
Digit s in t his volume); many of us have t rouble remember ing t his many
d igit s in some imp or t ant const ant s. Her e are some const ant s whose
precision should sat isfy any oat ing point for mat .
3.1415926535897932384626433832795028841971693993751

2
1.4142135623730950488016887242096980785696718753769

3
1.7320508075688772935274463415058723669428052538104
log
e
2 0.693147180559945309417232121458176568075500134360255
Anot her way t o get full precision in a const ant is simply t o use a
mat h libr ar y rout ine t o init ialize a global var iable, t hen use t hat var iable
as a const ant . A pseud o-cod e example follows:
pi: real 3*acos(0.5).
(Unfor t unat ely, some mat h libr ar ies arent accur at e t o full precision.)
435
VIII.8 CONVERTING BETWEEN BITS AND DIGITS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 435
VIII.8 VIII.8 VIII.8 VIII.8 VIII.8
C CC CCONVERTING BETWEEN ONVERTING BETWEEN ONVERTING BETWEEN ONVERTING BETWEEN ONVERTING BETWEEN
B BB BBITS AND DIGITS ITS AND DIGITS ITS AND DIGITS ITS AND DIGITS ITS AND DIGITS
Kelvin Thompson
The Universit y of Texas at Aust in
Pr obl em st at ement Pr obl em st at ement Pr obl em st at ement Pr obl em st at ement Pr obl em st at ement
Find out how many binar y bit s cor respond t o a given number of d ecimal
d igit s . . . or vice ver sa.
Sol ut i on Sol ut i on Sol ut i on Sol ut i on Sol ut i on
If b is t he number of bit s and d t he number of d igit s, sat isfy t he relat ion
2
b
= 10
d
.
Solving for each var iable in t er ms of t he ot her, we get
b = d 1og
2
10 d 3.3219280948873623478703
d = b log
10
2 b 0.301029995663981195213739.
Exampl e Exampl e Exampl e Exampl e Exampl e
The IEEE single-precision oat ing point for mat has 23 bit s of precision;
t his is equivalent t o about 6.9 d ecimal d igit s of precision.
436
VIII.9 STORAGEFREE SWAPPING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 436
VIII.9 VIII.9 VIII.9 VIII.9 VIII.9
S SS SSTORAGE-FREE SWAPPING TORAGE-FREE SWAPPING TORAGE-FREE SWAPPING TORAGE-FREE SWAPPING TORAGE-FREE SWAPPING
Brian Wyvill
Universit y of Calgary
Calgary, Albert a, Canada
I r st not iced t his lit t le Gem in some C cod e wr it t en by my brot her, Geoff
Wyvill. It is oft en t he case t hat t he values of t wo var iables have t o be
swapped . The usual way t hat t his is d one is t o d eclare a t empor ar y
var iable t o st ore t he value of one of t he t wo it ems t o be swapped , t hen
over wr it e t he var iable wit h t he value of t he ot her. The id ea of t his Gem
is t o use t hree exclusive or oper at ions t o avoid d eclar ing a t empor ar y
var iable. The following pseud o-cod e d oes t his:
a a xor b;
b b xor a;
a a xor b;
This is best illust r at ed by example:
Value of a Value of b
5 6
a a xor b (101 xor 110 = 011) 3 6
b b xor a (110 xor 011 = 101) 3 5
a a xor b (011 xor 101 = 110) 6 5
This met hod will wor k regard less of t he d at a t ype of t he var iables t o be
swapped since it is good for any bit pat t er n. In C a macro can be d ened
437
VIII.9 STORAGEFREE SWAPPING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 437
whose argument s can be of any t ype:
# dene SWAP(a, b) {a = b; b = a; a = b;}
Using a macro not only saves st or age, but also avoid s t he overhead s of a
funct ion call.
See also Med ian Find ing on a 3 3 Gr id (171)
438
VIII.10 GENERATING RANDOM INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 438
VIII.10 VIII.10 VIII.10 VIII.10 VIII.10
G GG GGENERATING ENERATING ENERATING ENERATING ENERATING
R RR RRANDOM INTEGERS ANDOM INTEGERS ANDOM INTEGERS ANDOM INTEGERS ANDOM INTEGERS
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
Suppose you want t o gener at e a r and om int eger n from t he r ange [l, h].
You would like t o have equal probabilit y of get t ing any int eger in t he set
{l, l + 1, l + 2, . . . , h 1, h}. The typical way to do this is to start with a
r and om-number gener at or t hat ret ur ns a oat ing point number in t he
r ange [0, 1]; call t his real number u.
To conver t u t o t he r ange [l, h] you might be t empt ed t o simply scale
u int o t he new r ange by using t he expression n round[l + (h l) u].
This would be a bad id ea. To see why, consid er t he fr agment of t he real
number line shown in Fig. 1, where l = 3 and h = 5. Then [l + (h l)u]
will be in t he closed int er val [3, 5]. When you t ake t he int eger par t of t his,
youre likely t o get only 3 or 4; you will get 5 only when u = 1.0, which
should be a rare event. What you really want in this example is to scale u
int o t he open int er val (2.5, 5.5), as shown in Fig. 2. Then when you round
t o t he nearest int eger youll be equally likely t o get 3, 4, or 5. To simulat e
t his open int er val, offset t he end s of t he or iginal r ange by an amount
slight ly smaller t han 0.5; perhaps 0.4999.
So t o comput e a r and om int eger n in t he r ange [l, h] from a r and om
real u in t he r ange [0, l], choose a number slight ly smaller t han 0.5,
Figure 1. The closed interval [3, 5]. Rounding points in this interval will vir t u ally never
ret ur n 5.
439
VIII.10 GENERATING RANDOM INTEGERS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 439
Figure 2. The open interval (2.5, 5.5). Rounding points in this interval you are equ ally
likely t o get 3, 4, or 5.
creat e t wo new t empor ar y var iables l l and h h + , and use
t he for mula n round[l + (h l)u].
See also A Digit al Dissolve Effect (221)
440
VIII.11 FAST 2D-3D ROTATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 440
VIII.11 VIII.11 VIII.11 VIII.11 VIII.11
F FF FFAST 2D AST 2D AST 2D AST 2D AST 2D-3D -3D -3D -3D -3D ROTATION ROTATION ROTATION ROTATION ROTATION
Jack Rit t er
Versat ec Inc.
Sant a Clara, Calif ornia
This Gem shows a way t o rot at e a point around t he or igin quickly and
accur at ely. The rot at ion can be in 2D or 3D. In gener al, rot at ed coord i-
nat es (X, Y) are d er ived from (X, Y) by
X X* cos() Y* sin()
Y X* sin() + Y* cos(). (1)
Th e p r oblem is t hat comp u t in g sin() an d cos() is ver y cost ly. Th e
met hod d escr ibed here makes use of a precomput ed t able of sine values,
scaled such t hat 16-bit xed point mult iplies can be used . No oat ing
point calculat ions or t r ig funct ions are used .
The t able is d ened for t he r st quad r ant , t o save st or age. Rot at ions in
t he ot her t hree quad r ant s can be t r ansfor med int o r st quad r ant rot at ions
by reect ion and / or t r ansposit ion.
You gener at e t he t able wit h any d esired resolut ion. Let s say you want
t o be able t o rot at e a point in increment s of d egrees. This means t hat t he
t able will have 91 ent r ies (t he number of gr ad at ions + 1). The t able is
pregener at ed via t he following t echnique:
for i = 0, + 1, i 90
t able[i] 16384*sin(i*Dt oR)
endl oop
The last ent r y in t he t able, t able[90], will hold t he maximum value: 16384.
This means t hat t he t able can be held in 16-bit word s.
441
VIII.11 FAST 2D-3D ROTATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 441
From Eq. 1, we see t hat t o comput e X and Y, we need t o comput e
four t er ms: X*cos, Y*sin, X*sin, and Y*cos. Each of t hese four is of t he
for m coord inat e*sin() or coord inat e*cos(). Thus, we need only t o
know how t o calculat e t hese t wo for ms.
The algor it hm is as follows:
To calculat e t he form coordinat e sin():
ind ex ]
value Rshift (coord inat e t able [ind ex], 14)
To calculat e t he form coordinat e cos():
ind ex ]
value Rshift (coord inat e t able [90 ind ex], 14)
In each case, t he mult iply by t he t able value occur s before t he r ight shift
of 14, which cor rect s for t he scaling. The mult iply creat es an int er med iat e
32-bit value. The nal value is 16 bit s. The t able, as well as t he input
coord inat e values, must be 16-bit word s, so t hat int eger mult iplies are
gener at ed .
The cos() for m d iffer s from t he sin() for m only in t hat t he ind exing is
from t he end of t he t able. This is why t here must be 91 ent r ies.
If you successively accumulat e , as in a t umbling object , you must
wr ap around from 0 t o 360, or fr om 360 t o 0, d epend ing on whet her is
being increment ed or d ecrement ed . Wr ap-around is clumsy in t he 90
gr ad at ion case. If t he t able hold s 128 gr ad at ions, for example, inst ead of
90, wr ap around is t r ivial:
+ increment , possibly causing wrap.
bit -and 511 correct for wrap.
can be posit ive or negat ive.
The t able has only 16 bit s of accu r acy. Su ccessive r ot at ions may
event ually cause an object t o d efor m. For higher accur acy, use 32-bit
values. On t he 68020, for example, you would d o a quad word mult iply
(any 32-bit p r ocessor has some way t o hold an int er med iat e 64-bit
prod uct ).
See also Rot at ion Tools (465); Rot at ion Mat r ix Met hod s Sum-
mar y (455); Bit Pat t er ns for Encod ing Angles (442)
442
VIII.12 BIT PATTERNS FOR ENCODING ANGLES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 442
VIII.12 VIII.12 VIII.12 VIII.12 VIII.12
B BB BBIT PATTERNS FOR IT PATTERNS FOR IT PATTERNS FOR IT PATTERNS FOR IT PATTERNS FOR
E EE EENCODING ANGLES NCODING ANGLES NCODING ANGLES NCODING ANGLES NCODING ANGLES
Ken Shoemake
Xerox MRC
Palo Alt o, Calif ornia
The quest ion oft en ar ises of what unit s t o use for angles, and most folks
only consid er t wo choices: r ad ians and d egrees. There is anot her choice,
however, which is oft en used for comput er music.
You can use a xed -point fr act ion of 2, or 360 d egreesit s t he same
eit her way. The implicit binar y point is just above t he high-ord er bit . This
not at ion meld s per fect ly wit h t wos complement int eger ar it hmet ic. The
high-ord er bit can be viewed as eit her a sign bit or a fr act ion bit ; plus and
minu s 180 d egr ees are ind ist ingu ishable, bot h being r ep r esent ed by
(0).100000. As you incr ement an angle, it nat ur ally wr aps around at 360
d egrees. All your bit s are meaningful; each angle has a unique bit pat t er n.
The t wo high bit s t ell you which quad r ant youre in, and make it cheap
t o use a quar t er wave look-up t able for sines and cosines.
Inst ead of d ecid ing bet ween d egrees and r ad ians, you use a nat ur al unit
t hat var ies bet ween 0 and 1, and one t hat makes sense.
Here are some examples t o give you t he id ea.
See also Rot at ion Tools (465); Rot at ion Mat r ix Met hod s Sum-
mar y (455); Fast 2D-3D Rot at ion (440)
443
VIII.13 BIT INTERLEAVING FOR QUAD-OR OCTREES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 443
VIII.13 VIII.13 VIII.13 VIII.13 VIII.13
B BB BBIT INTERLEAVING FOR IT INTERLEAVING FOR IT INTERLEAVING FOR IT INTERLEAVING FOR IT INTERLEAVING FOR
Q QQ QQUAD- OR OCTREES UAD- OR OCTREES UAD- OR OCTREES UAD- OR OCTREES UAD- OR OCTREES
Clif f ord A. Shaf f er
Virginia Tech
Blacksburg, Virginia
The linear quad or oct ree is an alt er nat ive t o t he t r ad it ional t ree st ruct ure
based on explicit point er s t o nod es. The linear represent at ion can red uce
t he t ot al st or age requir ement s for a quad t ree. It st ores only t he leaves
from t he or iginal t ree st ruct ure, in t he ord er t hat t hose nod es would be
visit ed d ur ing a t r aver sal of t he t ree. The int er nal nod es and point er s of
t he explicit t ree are replaced by posit ion d escr ipt or s d escr ibing t he pat h
fr om t he root of t he t ree t o t he leaf nod e. Alt er nat ively, t he linear
represent at ion can be viewed as st or ing square blocks from t he image in
a sor t ed list . The sor t key is obt ained by bit int er leaving t he coord inat es
of t he upper-left cor ner of each such block.
Below is pseud o-cod e for bit int er leaving 2D coord inat es (a similar
process can be used for t hree d imensions). The result is a 32-bit ad d ress
cod e, whose lower four bit s t ell t he size of t he block represent ed by t hat
ad d ress. You could also view t he ad d ress as a ser ies of t wo-bit cod es, wit h
each cod e t elling which way t o br anch in a quad t ree t o r each t he nod e. In
t hat case, t he lower four bit s t ell t he number of br anches t o t ake. Thus, a
32-bit cod e can st ore ad d resses for quad t rees wit h 15 levels (count ing t he
root ), equivalent t o a 2D image wit h 2
14
x 2
14
pixels. In 3D, a 32-bit cod e
word can represent an ad d ress in a 2
9
pixel cube; higher resolut ion would
require a longer cod e word .
Since t he bit int er leaving funct ion is heavily used in a linear quad or
oct ree syst em, it s efciency is crucial. The cod e present ed here makes
use of look-up t ables. The gener al approach t o cr eat ing t he int er leaved
value is t o break t he 32-bit wor k int o 4 byt es, and use look-up t ables t o
calculat e d irect ly t he value of each byt e from t he cor respond ing four bit s
444
VIII.13 BIT INTERLEAVING FOR QUAD-OR OCTREES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 444
of t he t wo input coord inat es. Following t he int er leaving cod e are func-
t ions t o ext r act t he x, y, and d ept h values from an int er leaved ad d ress.
Not e t hat MAX_DEPTH d enes how big t he int er leaved value can be,
t hat is, how may levels t he t ree may have. Any par t icular t r ee has an
act u al valu e (max_dept h) t hat cor r esp ond s t o t he r esolu t ion of t hat
t rees image. The alt er nat ive is t o int er pret all x and y values in t er ms of
t he maximum possible resolut ion (2
MAX_DEPTH
). MAX_DEPTH also has
t he effect of limit ing t he size of t he x and y coord inat e valuesin our
case, only t he lower 14 bit s are used .
macro MAX_DEPTH 14; Maximum possible dept h
byt eval is t he look-up t able for coordinat e int erleaving. Given a four-bit
port ion of t he (x, y) coordinat es, ret urn t he bit int erleaving. Not ice t hat t his
t able looks like t he order in which t he pixels of a 16 x 16 Pixel image would
be visit ed.
[ 0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85],
[ 2, 3, 6, 7, 18, 19, 22, 23, 66, 67, 70, 71, 82, 83, 86, 87],
[ 8, 9, 12, 13, 24, 25, 28, 29, 72, 73, 76, 77, 88, 89, 92, 93],
[ 10, 11, 14, 15, 26, 27, 30, 31, 74, 75, 78, 79, 90, 91, 94, 95],
[ 32, 33, 36, 37, 48, 49, 52, 53, 96, 97,100,101,112,113,116,117],
[ 34, 35, 38, 39, 50, 51, 54, 55, 98, 99,102,103,114,115,118,119],
[ 40, 41, 44, 45, 56, 57, 60, 61,104,105,108,109,120,121,124,125],
[ 42, 43, 46, 47, 58, 59, 62, 63,106,107,110,111,122,123,126,127],
[128,129,132,133,144,145,148,149,192,193,196,197,208,209,212,213],
[130,131,134,135,146,147,150,151,194,195,198,199,210,211,214,215],
[136,137,140,141,152,153,156,157,200,201,204,205,216,217,220,221],
[138,139,142,143,154,155,158,159,202,203,206,207,218,219,222,223],
[160,161,164,165,176,177,180,181,224,225,228,229,240,241,244,245],
[162,163,166,167,178,179,182,183,226,227,230,231,242,243,246,247],
[168,169,172,173,184,185,188,189,232,233,236,237,248,249,252,253],
[170,171,174,175,186,187,190,191,234,235,238,239,250,251,254,255]];
byt emask is t he mask for byt e int erleavingmask out t he nonsignicant bit
posit ions. This is det ermined by t he dept h of t he node. For example, a node
of dept h 0 is at t he root . Thus, t here are no branches and no bit s are
signicant . The bot t om four bit s (t he dept h) are always ret ained. Values are
described in hexidecimal not at ion.
byt eval: array [0 . . . 15][0 . . . 15] of i nteger [
445
VIII.13 BIT INTERLEAVING FOR QUAD-OR OCTREES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 445
byt emask: array [0..MAX_DEPTH] of i nteger [0xf,
0xc000000f, 0xf000000f, 0xfc00000f, 0xff00000f,
0xffc0000f, 0xfff0000f, 0xfffc000f, 0xffff000f,
0xffffc00f, 0xfffff00f, 0xfffffc0f, 0xffffff0f,
0xffffffcf, 0xffffffff];
i nteger int er leave(x, y, d ept h, max_d ept h)
Ret urn t he int erleaved code for a quadt ree node at dept h dept h whose
upper left hand corner has coordinat es (x, y) in a t ree wit h maximum dept h
max_d ept h.
begi n
ad d r : i nteger; Assumes 32-bit int egers.
Scale x, y values to be consistent with maximum coord size and depth of
t ree.
x lshift (x, MAX_DEPTH-max_d ept h);
y lshift (y, MAX_DEPTH-max_d ept h);
Calculat e t he bit int erleaving of t he x, y values t hat have now been
appropriat ely shift ed, and place t his int erleave in t he address port ion of ad d r.
Not e t hat t he binary represent at ions of x and y are being processed from
right t o left .
ad d r d ept h;
ad d r ad d r bi t-or lshift (byt eval[y bi t-and 03][x bi t-and 03], 4);
ad d r bad d r bi t-or
lshift(byteval[rshift(y, 2) bit-and 0xf][rshift(x, 2) bit-and 0xf], 8);
ad d r ad d r bi t-or
lshift(byteval[rshift(y, 6) bit-and 0xf][rshift(x, 6) bit-and 0xf], 16);
ad d r ad d r bi t-or
lshift(byteval[rshift(y, 10) bit-and 0xf][rshift(x, 10) bit-and 0xf], 24);
ad d r ad d r bi t-and byt emask[d ept h];
return (ad d r );
end;
446
VIII.13 BIT INTERLEAVING FOR QUAD-OR OCTREES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 446
The next t wo arrays are used in calculat ing t he (x, y) coordinat e of t he
upper left -hand corner of a node from it s bit int erleaued address. Given an
eight -bit number, t he arrays ret urn t he effect of removing every ot her bit (t he
y bit s precede t he x bit s).
xval:array[0..255] of i nteger [
[ 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3],
[ 4, 5, 4, 5, 6, 7, 6, 7, 4, 5, 4, 5, 6, 7, 6, 7],
[ 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3],
[ 4, 5, 4, 5, 6, 7, 6, 7, 4, 5, 4, 5, 6, 7, 6, 7],
[ 8, 9, 8, 9, 10, 11, l0, 11, 8, 9, 8, 9, 10, 11, 10, 11],
[ 12, 13, 12, 13, 14, 15, 14, 15, 12, 13, 12, 13, 14, 15, 14, 15],
[ 8, 9, 8, 9, 10, 11, l0, 11, 8, 9, 8, 9, 10, 11, 10, 11],
[ 12, 13, 12, 13, 14, 15, 14, 15, 12, 13, 12, 13, 14, 15, 14, 15],
[ 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3],
[ 4, 5, 4, 5, 6, 7, 6, 7, 4, 5, 4, 5, 6, 7, 6, 7],
[ 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3],
[ 4, 5, 4, 5, 6, 7, 6, 7, 4, 5, 4, 5, 6, 7, 6, 7],
[ 8, 9, 8, 9, 10, 11, l0, 11, 8, 9, 8, 9, 10, 11, 10, 11],
[ 12, 13, 12, 13, 14, 15, 14, 15, 12, 13, 12, 13, 14, 15, 14, 15],
[ 8, 9, 8, 9, 10, 11, l0, 11, 8, 9, 8, 9, 10, 11, 10, 11],
[ 12, 13, 12, 13, 14, 15, 14, 15, 12, 13, 12, 13, 14, 15, 14, 15]]
[ 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3],
[ 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3],
[ 4, 4, 5, 5, 4, 4, 5, 5, 6, 6, 7, 7, 6, 6, 7, 7],
[ 4, 4, 5, 5, 4, 4, 5, 5, 6, 6, 7, 7, 6, 6, 7, 7],
[ 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3],
[ 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3],
[ 4, 4, 5, 5, 4, 4, 5, 5, 6, 6, 7, 7, 6, 6, 7, 7],
[ 4, 4, 5, 5, 4, 4, 5, 5, 6, 6, 7, 7, 6, 6, 7, 7],
[ 8, 8, 9, 9, 8, 8, 9, 9, 10, 10, 11, 11, 10, 10, 11, 11],
[ 8, 8, 9, 9, 8, 8, 9, 9, 10, 10, 11, 11, 10, 10, 11, 11],
[ 12, 12, 13, 13, 12, 12, 13, 13, 14, 14, 15, 15, 14, 14, 15, 15],
[ 12, 12, 13, 13, 12, 12, 13, 13, 14, 14, 15, 15, 14, 14, 15, 15],
[ 8, 8, 9, 9, 8, 8, 9, 9, 10, 10, 11, 11, 10, 10, 11, 11],
[ 8, 8, 9, 9, 8, 8, 9, 9, 10, 10, 11, 11, 10, 10, 11, 11],
[ 12, 12, 13, 13, 12, 12, 13, 13, 14, 14, 15, 15, 14, 14, 15, 15],{[
[ 12, 12, 13, 13, 12, 12, 13, 13, 14, 14, 15, 15, 14, 14, 15, 15]]
yval:array[0..255] of i nteger [
447
VIII.13 BIT INTERLEAVING FOR QUAD-OR OCTREES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 447
i nteger get x(ad d r, max_d ept h)
Ret urn t he x coordinat e of t he upper left -hand corner of ad d r for a t ree
wit h maximum dept h max_d ept h.
begi n
x: i nteger;
x xval[r shift (ad d r, 4) bi t-and 0xf]; do bot t om t wo bit s.
x x bit-or lshift(xval[rshift(addr, 8) bit-and 0xff]], 2); next four bits.
x x bit-or lshift(xval[rshift(addr, 16) bit-and 0xff]], 6); next four bit s.
x x bit-or lshift(xval[rshift(addr, 24) bit-and 0xff]], 10); top four bit s.
x r shift (x, MAX_DEPTHmax_d ept h); scale t o t ree dept h.
return (x);
end;
i nteger get x(ad d r, max_d ept h)
Return the y coordinate of the upper left hand corner of addr for a tree wit h
maximum dept h max_d ept h.
begi n
y: i nteger;
y yval[r shift (ad d r, 4) bi t-and 0xf]; do bot t om t wo bit s.
y y bit-or lshift(yval[rshift(addr, 8) bit-and 0xff]], 2); next four bit s.
y y bit-or lshift(yval[rshift(addr, 16) bit-and 0xff]], 6); next four bit s.
y y bit-or lshift(yval[rshift(addr, 24) bit-and 0XffI], 10); top four bit s.
y r shift (y, MAX_DEPTHmax_d ept h); scale t o t ree dept h.
return (y);
end;
i nteger get d ept h(ad d r )
Ret urn t he dept h of t he node. Simply ret urn t he bot t om four bit s.
begi n
return (ad d r bi t-and 0xf);
end;
See Append ix 2 for C Implement at ion (759)
448
VIII.14 A FAST HSL-TO-RGB TRANSFORM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 448
S
v

V MIN
V
,
VIII.14 VIII.14 VIII.14 VIII.14 VIII.14
A AA AA FAST FAST FAST FAST FAST
H HH HHSL-TO-RGB SL-TO-RGB SL-TO-RGB SL-TO-RGB SL-TO-RGB
T TT TTRANSFORM RANSFORM RANSFORM RANSFORM RANSFORM
Ken Fishkin
Pixar, Inc.
San Raf ael, Calif ornia
Three common color spaces used in comput er gr aphics are RGB, HSV,
and HSL color space. RGB color space is by far t he most common, and is
pr act ically t he lingua fr anca of comput er gr aphics color spaces: it s axes
(R, G, and B) refer t o t he amount of red , green, and blue light t hat a pixel
emit s. The ot her t wo are percept ual color spaces, whose axes represent
more p ercep t u al qu alit ies: t he hu e, t he sat u r at ion, and t he valu e or
light ness.
The use of HSV and HSL color spaces for comput er gr aphics arose at
almost t he same t ime. HSV was r st proposed by Smit h (1978), and HSL
by t he Core commit t ee (Gr aphics St and ard s Planning Commit t ee, 1979).
The HSV-t o-RGB and RGB-t o-HSV t r ansfor mat ions are simple and fast .
While t he RGB-t o-HSL t r ansfor mat ion is also simple and fast , t he HSL-t o-
RGB t r ansfor mat ion is about t wice as slow, and now near ly as simple, as
t he HSV-t o-RGB t r ansfor mat ion.
This Gem lls in t he missing link, supplying an improved HSL-t o-RGB
t r ansfor mat ion t hat is near ly t he same as t he HSV-t o-RGB t r ansfor mat ion.
It d oes t his by massaging t he HSV-t o-RGB t r ansfor mat ion int o an
HSL-t o-RGB t r ansfor mat ion.
Hu e (H) is d ened t he same in t he t wo syst ems. Valu e (V) and
light ness (L) are d ened as
V MAX(R, G, B), and L (MIN + MAX)/ 2.
Sat ur at ion is d ened d ifferent ly: HSV Sat ur at ion S
V
is d ened as
449
VIII.14 A FAST HSL-TO-RGB TRANSFORM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 449
while HSL Sat ur at ion SL is d ened as
S
L

MAX MIN
MAX + MIN
if L 1 2,

MAX MIN
2 MAX MIN
if L > 1 2.
This Gem ju st comp u t es V and S
v
from L and S
L
, and t hen d oes
st and ar d HSV t o RGB, wit h a lit t le st rengt h red u ct ion and common
subexpression eliminat ion t hrown in.
A pseud o-cod e d escr ipt ion of t he algor it hm follows:
given H, S
L
, L on [0 1], comput e R, G, B on [0 1]
i f L
1
2
then v L (1.0 + S
L
);
el se v L + S
L
L S
L
;
i f v = 0
then R G B 0.0;
el se begi n
min 2L v;
S
v
(v min) / v;
H 6H; map ont o [0 6)
sext ant : i nt oor (H);
fr act : real H sext ant ;
vsf: real v S
v
fr act ;
mid l; real min + vsf;
mid 2: real v vsf;
[R, G, B] = sel ect sext ant from
0: [v, mid 1, min];
1: [mid 2, v, min];
2: [min, v, min1];
3: [min, mid 2, v];
4: [mid 1, min, v];
5: [v, min, mid 2];
endcase;
end
See also Mapping RGB Tr iples ont o Four Bit s (233)
See Append ix 2 for C Implement at ion (763)
9
MATRIX
TECHNIQUES
453
IX.1 MATRIX IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 453
IX.1 IX.1 IX.1 IX.1 IX.1
M MM MMATRIX IDENTITIES ATRIX IDENTITIES ATRIX IDENTITIES ATRIX IDENTITIES ATRIX IDENTITIES
Kelvin Thompson
The Universit y of Texas at Aust in
Below are some mat r ix id ent it ies t hat are useful for a number of pur-
poses: const ruct ing and d ecomposing t r ansfor ms; opt imizing mat r ix mul-
t iplicat ion rout ines (see Fast Mat r ix Mult iplicat ion in t his volume); and
und er st and ing t he subt let ies of mat r ix concat enat ion.

a b c 0
d e f 0
g h i 0
0 0 0 1

1
]
1
1
1
1


1 0 0 0
0 1 0 0
0 0 1 0
x y z 1

1
]
1
1
1
1
=

a b c 0
d e f 0
g h i 0
x y z 1

1
]
1
1
1
1


1 0 0 0
0 1 0 0
0 0 1 0
x
1
y
1
z
1
1

1
]
1
1
1
1


1 0 0 0
0 1 0 0
0 0 1 0
x
2
y
2
z
2
1

1
]
1
1
1
1
=

1 0 0 0
0 1 0 0
0 0 1 0
x
1
y
1
z
1
1

1
]
1
1
1
1
+

1 0 0 0
0 1 0 0
0 0 1 0
x
2
y
2
z
2
1

1
]
1
1
1
1
454
IX.1 MATRIX IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 454

a b c 0
d e f 0
g h i 0
0 0 0 1

1
]
1
1
1
1


x 0 0 0
0 y 0 0
0 0 z 0
0 0 0 1

1
]
1
1
1
1
=

ax by cz 0
dx ey fz 0
gx hy iz 0
0 0 0 1

1
]
1
1
1
1

x 0 0 0
0 y 0 0
0 0 z 0
0 0 0 1

1
]
1
1
1
1


a b c 0
d e f 0
g h i 0
0 0 0 1

1
]
1
1
1
1
=

ax bx cx 0
dy ey fy 0
gz hz iz 0
0 0 0 1

1
]
1
1
1
1
The id ent it ies below simply say t hat (1) t he p rod u ct of t wo linear
mat r ices is a linear mat r ix, and (2) t he prod uct of t wo afne mat r ices is
an afne mat r ix.

? ? ? 0
? ? ? 0
? ? ? 0
0 0 0 1

1
]
1
1
1
1


? ? ? 0
? ? ? 0
? ? ? 0
0 0 0 1

1
]
1
1
1
1
=

? ? ? 0
? ? ? 0
? ? ? 0
0 0 0 1

1
]
1
1
1
1
(1)


? ? ? 0
? ? ? 0
? ? ? 0
? ? ? 1

1
]
1
1
1
1


? ? ? 0
? ? ? 0
? ? ? 0
? ? ? 1

1
]
1
1
1
1
=

? ? ? 0
? ? ? 0
? ? ? 0
? ? ? 1

1
]
1
1
1
1
(2)
See also Tr ansfor mat ion Id ent it ies (485)
455
ROTATION MATRIX METHODS SUMMARY
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 455
R RR RROTATION MATRIX OTATION MATRIX OTATION MATRIX OTATION MATRIX OTATION MATRIX
M MM MMETHODS SUMMARY ETHODS SUMMARY ETHODS SUMMARY ETHODS SUMMARY ETHODS SUMMARY
Ma t r i x t ech n i qu es for m t h e h ea r t of geomet r i c t r a n sfor ma t i on s
u sed i n comp u t er gr a p h i cs. A good gr ou n d i n g i n l i n ea r a l gebr a
ca n h el p a p r ogr a mmer n a vi ga t e t h r ou gh t h e va r i ou s t yp es of
p r oject i on s a n d t r a n sfor ma t i on s i n common u se. Typ i ca l l y a
u ser sh ou l d be sh i el d ed fr om t h e ma t h ema t i cs of ma t r i x ma n i p -
u l a t i on s, wh i ch a r e ju st a con ven i en t mech a n i sm for a ccom-
p l i sh i n g cer t a i n goa l s. A good comma n d of ma t r i x t ech n i qu es
ca n h el p a p r ogr a mmer wr i t e syst ems t h a t i n su l a t e t h e u ser
from t he und er lying represent at ion.
Th e fol l owi n g Gems p r esen t a va r i et y of ma t r i x t ech n i qu es
t h a t h el p i n va r i ou s r ot a t i on t a sks. Ma t r i x met h od s for 3D
r ot a t i on a r e con si d er ed d i fcu l t or a bst r u se by ma n y p r ogr a m-
mer s, bu t t h i s d oes n ot h ave t o be. Th e fol l owi n g Gems p r ovi d e
some basi c t ool s an d fu n d amen t al s t h at can h el p you mast er t h e
applicat ion of mat r ices in comput er gr aphics.
See also Rot a t i on Tool s (465); Bi t Pa t t er n s for En cod i n g An -
gles (442); Fast 2D-3D Rot at ion (440)
456
IX.2 TRANSFORMING AXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 456
IX.2 IX.2 IX.2 IX.2 IX.2
T TT TTRANSFORMING AXES RANSFORMING AXES RANSFORMING AXES RANSFORMING AXES RANSFORMING AXES
Kelvin Thompson
The Universit y of Texas at Aust in
Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement
Somet imes an int er act ive 3D applicat ion need s t o know roughly which
way a set of t r ansfor med coord inat e axes are point ing on t he screen. For
example, t he applicat ion may want t o know if t he x-axis of a given
mod eling sp ace is p oint ing int o or ou t of t he screen aft er t he axis
und ergoes a sequence of mod eling and viewing t r ansfor ms; t his could
help t he ap p licat ion u nd er st and t he or ient at ion of an object on t he
screen. Even if t he applicat ion knows only t he net t r ansfor m for t he
object , it can st ill calculat e t he approximat e or ient at ion of t he object s
axes quit e efcient ly.
Sol ut i on Sol ut i on Sol ut i on Sol ut i on Sol ut i on
Given an ar bit r ar y 4 4 mat r ix A = [a
ij
] t hat t r ansfor ms 3D point s in
an input space t o 3D point s in an out put screen space in t he usual
manner,

x
in
y
in
z
in
1
[ ]

a
11
a
12
a
13
a
14
a
21
a
22
a
23
a
24
a
31
a
32
a
33
a
34
a
41
a
42
a
43
a
44

1
]
1
1
1
1
=
[ ]
(1)

x
out

, y
out

, z
out

.
(2)
457
IX.2 TRANSFORMING AXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 457
Table 1. Gener al Direct ion Rat ios.
Figure 1 shows six gener al d irect ions in screen space (t oward and
away are d irect ly in and out of t he screen). To d et er mine t he gener al
d irect ion of an input axis aft er it is t r ansfor med t o t he screen space,
we check t he sign of t he appropr iat e r at io in Tables 1 or 2. Gener ally we
use Table 1; however, if a
i4
is zero for some ent r y in Table 1 (i 4,
since a
44
should never be zero), t hen we use t he cor respond ing ent r y in
Table 2.
If t he out put space is t he usual left -hand ed eye space, wit h t he eye
looking t oward posit ive z, sign convent ions are as follows: if a given
quot ient is negat ive, t he t r ansfor med axis is point ing in t he upper d irec-
t ion ind icat ed at t he t op of t he column; if t he quot ient is posit ive, t he axis
point s t oward t he lower d irect ion. (For example, if a quot ient in t he
mid d le column is negat ive, t hen t he cor respond ing axis is point ing d own.)
Figur e 1. Gener al d irect ions.
458
IX.2 TRANSFORMING AXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 458
If we subt r act t he t r ansfor med or igin from t he t r ansfor med x vect or, we
get

xa
13
+ a
43
xa
14
+ a
44
.

xa
13
+ a
43
xa
14
+ a
44
.

a
43
a
44
.
(3)
Table 2. Zero-Case Direct ion Rat ios.
On t he ot her hand , if t he out put space is a r ight -hand ed eye space,
wit h t he eye looking t oward negat ive z, t he posit ions of t he t oward and
away labels need t o be swapped (or t he sign t est s in t he r st column
rever sed ).
We d ont have t o d o any oat ing-point d ivision t o d et er mine t he signs
of t he r at ios. The sign of each quot ient can be d et er mined by t he XOR of
t he sign bit s of t he d ivid end and d ivisor.
Pr oof Pr oof Pr oof Pr oof Pr oof
Let us t ake t he case of whet her t he x-axis point s t oward or away from t he
viewer. All vect or s along t he x-axis t ake t he for m [x 0 0 1], and we will
want t o look at t he z coord inat e of t he t r ansfor med vect or. Doing t he
ar it hmet ic from Eqs. (1) and (2), we see t hat t he t r ansfor med z coord i-
nat e will be
If we now let x approach posit ive innit y, and apply LHospit als r ule
Similar ly, t he t r ansfor med or igin [0 0 0 1], has a z coord inat e of

a
43
/ a
44
.
459
IX.2 TRANSFORMING AXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 459

a
13
a
14

a
43
a
44

a
13
a
44
a
43
a
14
a
14
a
44
,
where appropr iat e, t his becomes
which is exact ly t he upper-left ent r y in Table 1. If t his r at io is negat ive,
t he t r ansfor med x-axis point s in t he negat ive z d irect ion; if it is posit ive,
it point s t oward posit ive z.
Next , if a
14
is zero, we simply use x = 1 in Eq. 3 and get a
l3
/ a
44
,
which agrees wit h Table 2. Der ivat ions of ot her ent r ies in t he t able are
similar.
460
IX.3 FAST MATRIX MULTIPLICATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 460
m_out [0][0] m1[0][0] m2[0][0]+
m1[0][1] m2[1][0]+
m1[0][2] m2[2][0]+
m1[0][3] m2[3][0].
IX.3 IX.3 IX.3 IX.3 IX.3
F FF FFAST MATRIX AST MATRIX AST MATRIX AST MATRIX AST MATRIX
M MM MMULTIPLICATION ULTIPLICATION ULTIPLICATION ULTIPLICATION ULTIPLICATION
Kelvin Thompson
The Universit y of Texas at Aust in
Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement
Speed up rout ines t hat mult iply mat r ices, perhaps at t he expense of cod e
size and read abilit y.
Sol ut i ons Sol ut i ons Sol ut i ons Sol ut i ons Sol ut i ons
Fir st , remove unnecessar y oat ing point oper at ions. If you are mult iply-
ing mat r ices t hat you know cont ain 1s or 0s in cer t ain posit ions, remove
mult iplicat ion and ad d it ion by t he appropr iat e t er ms. It may be useful t o
wr it e special rout ines for mu lt iplying afne and linear mat r icessee
Mat r ix Id ent it ies in t his volume.
Second , unwind loops. Rout ines like V3Mat Mu1 in t he Vect or C Libr ar y
are compact , but t hey wast e unnecessar y t ime upd at ing loop var iables
and per for ming ar r ay look-ups. Such progr ams run fast er when wr it t en as
a st r ict sequence of oper at ions wit h const ant -valued ind ices int o ar r ays.
For example, pseud o-cod e t o mult iply t wo gener al 4 4 mat r ices would
cont ain a sequence of sixt een assignment s similar t o
461
IX.3 FAST MATRIX MULTIPLICATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 461
(It may not be necessar y t o unwind loops if you know your compiler is
smar t enough t o unwind or vect or ize loops for you.)
Finally, when progr amming in C, use regist er point er s (see How t o
Use C Regist er Var iables t o Point t o 2D Ar r ays in t his volume); many C
compiler s wont use regist er s unless you explicit ly t ell t hem t o d o so.
462
IX.4 A VIRTUAL TRACKBALL
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 462
IX.4 IX.4 IX.4 IX.4 IX.4
A AA AA VIRTUAL TRACKBALL VIRTUAL TRACKBALL VIRTUAL TRACKBALL VIRTUAL TRACKBALL VIRTUAL TRACKBALL
Jef f Hult quist
NASA Ames Research Cent er
Mof f et t Field, Calif ornia
This simple cod e is t he hear t of a vir t ual manipulat or t hat mimics a
t r ackball. Using t he mouse, a user can select any point on t he t r ackball
and rot at e t he ent ire scene about t he cent er of rot at ion. The var iables
oldMouse and newMouse hold t he successive posit ions of t he cur sor in
object coord inat es. The t r ackball is cent ered at point C and has r ad ius r
(see Fig. 1).
Figur e 1.
463
IX.4 A VIRTUAL TRACKBALL
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 463
r ay0 [[old Mouse.x, old Mouse.y, 0], [0,0,l]];
r ay1 [[newMouse.x, newMouse.y, 0], [0,0,l]];

P
0
int er sect Sphere(r ay0, C, r );

P
1
int er sect Sphere(r ayl, C, r );
V
0
(

P
0
C)/ |(P
0
C)|;
V
1
(P
l
C)/ |(P
1
C)|;
A V
0
V
1
;
arcsin

( A )
if (V
0

V
1
< 0)
t hen + / 2;
rot at eAbout Axis(A, );
464
IX.5 MATRIX ORTHOGONALIZATION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 464
IX.5 IX.5 IX.5 IX.5 IX.5
M MM MMATRIX ATRIX ATRIX ATRIX ATRIX
O OO OORTHOGONALIZATION RTHOGONALIZATION RTHOGONALIZATION RTHOGONALIZATION RTHOGONALIZATION
Eric Raible
Campbell , Calif ornia
It is oft en t he case when su ccessively comp osing rot at ions t hat t he
resu lt ing mat r ix becomes nonor t hogonal. This lead s t o skewing and
scaling effect s. Alt hough t here are sever al common algor it hms t o re-
or t hogonalize mat r ices (for example, Gr am Schmid t ), I prefer t he follow-
ing.
We reor t hogonalize t he mat r ix R by comput ing an approximat ion t o a
cor rect ion mat r ix C = (R
T
R)
1/ 2
, which, if premult iplied by R, would
give an or t hogonal mat r ix. (To see t hat RC is ind eed or t hogonal, it
sufces t o show t hat (RC

)
T
= (RC

)
1
, since t he inver se of an or t hogonal
mat r ix is also it s t r anspose. This is easily d one.)
It is d ifcult t o nd C d irect ly, so we inst ead nd an approximat ion t o
C by comp u t i n g t h e mat r i x an al og t o Tayl or Ser i es exp an si on of
(1 + x

)
1 2
about x = 0.
Since R is close t o or t hogonal, R
T
R will be close t o t he id ent it y mat r ix,
and X = R
T
R I will be close t o t h e zer o mat r ix. Th u s, we can
comp u t e C by d oi n g t h e Ta yl or exp a n si on of C = (R
T
R)
1/ 2
= [I + (R
T
R I)]
1/ 2
= [I + X]
1/ 2
about X = 0I, t o give us
C = I (1/ 2)X + (3/ 8)X
2
(5/ 16)X
3
+ 0(X
4
).
There are t wo ways of increasing t he accur acy of t his approximat ion.
The r st is t o increase t he number of t er ms (t he next ones are 35/ 128,
63/ 256, 231/ 1024, a n d 429/ 2048). Th e secon d i s t o r ep ea t t h i s
proced ure (and using only t he r st t wo or t hree t er ms). Exact ly which
approach t o t ake d epend s on how t he loop t hat evaluat es C is cod ed , and
how far t he or iginal mat r ix is from or t hogonal. A lit t le exper iment at ion
should d et er mine t he best approach for your applicat ion.
See Append ix 2 for C Implement at ion (765)
465
IX.6 ROTATION TOOLS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 465
IX.6 IX.6 IX.6 IX.6 IX.6
R RR RROTATION TOOLS OTATION TOOLS OTATION TOOLS OTATION TOOLS OTATION TOOLS
Michael E. Pique
Research Inst it ut e of Scripps Clinic
La Jolla, Calif ornia
Here are some fact s and t echniques youll want t o have at hand when
wor king wit h rot at ions. This sect ion uses t he following convent ions: row
vect or s go on t he left and are post -mult iplied by mat r ices on t he r ight ;
point s are row vect or s.
True for any 3 3 rot at ion mat r ix R:
R is nor malized : t he squares of t he element s in any row or column
sum t o unit y, or +1.
R is or t hogonal: t he d ot prod uct (sum of t he prod uct s of cor respond -
ing element s) of any pair of rows is zero, likewise t he d ot prod uct of
any pair of columns.
The rows of R represent t he coord inat es in t he or iginal space of unit
vect or s along t he coord inat e axes of t he rot at ed space.
The columns of R represent t he coord inat es in t he rot at ed space of
unit vect or s along t he coord inat e axes of t he or iginal space.
The d et er minant of R is +1. (Reect ion mat r ices have d et er minant s of
1.)
466
IX.6 ROTATION TOOLS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 466
Conver t i ng bet ween Mat r i x and Conver t i ng bet ween Mat r i x and Conver t i ng bet ween Mat r i x and Conver t i ng bet ween Mat r i x and Conver t i ng bet ween Mat r i x and
Axi s- Amount Repr esent at i ons Axi s- Amount Repr esent at i ons Axi s- Amount Repr esent at i ons Axi s- Amount Repr esent at i ons Axi s- Amount Repr esent at i ons
The only rot at ion mat r ix gener at ing for mula youll ever need : t he mat r ix
t hat rot at es about an ar bit r ar y axis t hrough t he or igin is
R = rot at ion_about _axis_by_angle(axi s, )

tx
2
+ c txy + sz txz sy
txy sz ty
2
+ c tyz + sx
txz + sy tyz sx tz
2
+ c

1
]
1
1
1
1
,
where x, y, and z are t he component s of a unit vect or (use V3Normal-
ize funct ion) along t he axis, is t he angular amount of rot at ion, and
s = sin, c = cos, t = 1 cos. See Roger s and Ad ams (1976), or any
classical mechanics t ext for t he d er ivat ion. See Bar r y et al. (1971) for
hint s on d oing t his using only int eger ar it hmet ic.
When t he rot at ion angle is small, approximat e sin by (in r ad ians,
d ont forget ) and cos by 1:


R
1 z y
z 1 x
y x 1

1
]
1
1
1
.
One way of int er pret ing t his approximat ion is as t he prod uct of succes-
sive rot at ions about t he t hree coord inat e axes, wit h angles small enough
t hat t he ord er in which t he rot at ions are applied makes no signicant
d ifference. The vect or sum of t hese t hree increment s implicit ly d enes an
axis and magnit ud e of rot at ion.
To nd t he axis and angle of a rot at ion, given t he mat r ix, nd from
cos = (R[0][0] + R[1][1] + R[2][2] 1)/ 2;
467
IX.6 ROTATION TOOLS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 467
t hen provid ing sin 0,

axis. x
R 1
[ ]
2
[ ]
R 2
[ ]
1
[ ]
2 sin

axis. y
R 2
[ ]
0
[ ]
R 0
[ ]
2
[ ]
2 sin

axis. z
R 0
[ ]
1
[ ]
R 1
[ ]
0
[ ]
2 sin
.
See Par s (1965). Ther e is a u niqu e solu t ion excep t t hat a negat ive
rot at ion amount about a rever sed axis yield s t he same mat r ix, as of
cour se d o mult iples of 360 ad d ed t o t he amount . The axis is und ened
when t he angular amount is zero or a mult iple of 360.
Nest i ng Nest i ng Nest i ng Nest i ng Nest i ng
The effect of mult iple rot at ions is cumulat ive, and d epend ent on t heir
ord er of applicat ion. Aft er a rot at ion, A, is applied t o an object t he object
is consid ered rot at ed by A from it s or iginal (unspecied ) or ient at ion. If
t he object is subject ed t o a second rot at ion B whose d irect ion (axis)
t ur ns wit h rot at ion A, t hen rot at ion B is nest ed wit hin rot at ion A. The
object s nal posit ion and or ient at ion t hen d epend s on bot h rot at ions.
The inst ant aneous relat ion bet ween any t wo spaces can always be com-
plet ely d escr ibed by a single t r anslat ion combined wit h a single rot at ion.
The t r anslat ion par t is t he amount by which t he or igin of B is moved
from t he or igin of A: t he coord inat es of Bs or igin in space A, repre-
sent ed here by t he vect or
b
T
a
, in which t he subscr ipt let t er s ind icat e t he
names of t he spaces. The rot at ion par t of t he relat ion is
b
R
a
, t he rot at ion
of space B wit h respect t o space A.
Nest ing space G wit hin space F by rot at ion
g
R
f
means calculat ing t he
relat ionship
g
R
a
bet ween space G and a reference space A, somet imes
called composit ion or concat enat ion. We always d ene t he relat ion in
t he parent spaces coord inat e syst em.
468
IX.6 ROTATION TOOLS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 468
To nest space G insid e space F, d escr ibe t he or ient at ion of F wit h
respect t o some reference space A by t he 3 3 mat r ix
f
R
a
and t he
or ient at ion of G wit h respect t o F by
g
R
f
. Descr ibe t he or igin point of F
in space A by t he 1 3 row t r anslat ion vect or
f
T
a
and t he or igin point
(pivot point ) of G in F by
g
T
f
. Then t he or ient at ion of G wit h respect t o
A is
g
R
a
= (
g
R
f
)(
f
R
a
), a mat r ix t imes a mat r ix, and t he or igin of G wit h
respect t o A is
g
T
a
= (
f
T
a
) + (
g
T
f
)(
f
R
a
), a vect or plus a vect or.
Let t ing Rl, R2, and R3 be any t r ansfor mat ions and t he symbol
represent t he nest ing oper at ion, nest ing is associat ive: (R1R2)R3 =
R1(R2R2). When calculat ing mult ilevel nest ing, t his per mit s evaluat -
ing int er med iat e resu lt s in t he ord er t hat allows t he most reu se of
relat ions common t o sever al nest ing chains. Not e t hat t he inner most
space is on t he left of t he list and t he out er most on t he r ight , when
following t he post -mult iplicat ion convent ion. Mnemonic: t he inner most
space is closest t o t he point being t r ansfor med , t he vect or t hat will go at
t he left of t he concat enat ed mat r ices.
Tr ansf or mat i on Inver ses Tr ansf or mat i on Inver ses Tr ansf or mat i on Inver ses Tr ansf or mat i on Inver ses Tr ansf or mat i on Inver ses
Given t he rot at ion and t r anslat ion relat ionship of space C wit h respect t o
space B,
c
R
b
and
c
T
b
, we can wr it e t he inver se relat ionship, t hat of B
wi t h r esp ect t o C, a s:
b
R
c
= (
c
R
b
)
1
= (
c
R
b
)
T
a n d
b
T
c
= (
c
T
b
)-1 =
(
b
T
c
)(
b
R
c
). It is convenient t hat t he inver se of t he rot at ion component
is just it s t r anspose.
To h a n d l e i n ver ses mi xed wi t h n est i n g, (R1R2)
1
= R2
1
R1
1
;
and in gener al (R1R2 Rn)
1
= Rn
1
R2
1
R1
1
.
It er at i on: Appl yi ng and Accumul at i ng Changes It er at i on: Appl yi ng and Accumul at i ng Changes It er at i on: Appl yi ng and Accumul at i ng Changes It er at i on: Appl yi ng and Accumul at i ng Changes It er at i on: Appl yi ng and Accumul at i ng Changes
t o a Mat r i x t o a Mat r i x t o a Mat r i x t o a Mat r i x t o a Mat r i x
A change mat r ix species t he d irect ion and amount of a change t o a
t r ansfor mat ion mat r ix: MMC. When ap p lied r ep eat ed ly, nu mer ical
er ror accumulat es and t he mat r ix must be reconclit ioned ever y d ozen or
so upd at es. Tount as and Kat z (1971) renor malize t he mat r ix at t he end of
469
IX.6 ROTATION TOOLS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 469
each cycle or by renor malizing t he rows and columns in alt er nat e cycles.
Since t he rows and columns are almost unit vect or s, t hey approximat e
t he nor malizat ion fact or

1/ x
2
+ y
2
+ z
2
by t he fast er

1/ (0. 5 +
0.5(x
2
+ y
2
+ z
2
)).
When t he user s hand moves a cont rol d evice, t he image of t he object
cont rolled by t he d evice should move in t he same d irect ion. To achieve
such kinest het ic cor respond ence, one must compensat e for t he effect s of
nest ing. For example, a simple y mouse-mot ion or z-axis d ial rot at ion
would become t r anslat ions and rot at ions about ar bit r ar y 3D d irect ions
aft er t he viewing d irect ion compensat ion. See Br it t on et al. (1978) and
Pique (1986) for d et ails and wor ked examples.
In br ief, st ore t he d evice increment (mouse movement , d ial change, . . .)
int o a 3D vect or mot ion and rot at e it backward s by t he cur rent viewing
mat r ix before applying it t o t he object s old mod elling mat r ix t o yield t he
new mod elling mat r ix:
Not e mot ion is t he V3Lengt h funct ion. This is t he scalar
amount of t he mot ion: t he t ranslat ion dist ance or t he rot at ion angle.
mod elling_t r anslat ion (mot ion)(viewing_rot at ion
T
) + mod elling_t r anslat ion
mod elling_rot at ion renor malize_mat r ix((mod elling_rot at ion) rot at ion_about _axis_
by_angle((mot ion)(viewing_rot at ion
T
), mot ion))
470
IX.7 MATRIX INVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 470
IX.7 IX.7 IX.7 IX.7 IX.7
M MM MMATRIX INVERSION ATRIX INVERSION ATRIX INVERSION ATRIX INVERSION ATRIX INVERSION
Richard Carling
Independent Graphics Consult ant
Bedf ord, Massachuset t s
This Gem d emonst r at es how t o calculat e t he inver se and ad joint of a
4-by-4 mat r ix.
Comput at ion of t he inver se of a mat r ix M(M
1
) is an impor t ant st ep in
many image synt hesis algor it hms. Typically t he project ion of object s from
wor ld space t o image space is accomplished wit h a t r ansfor mat ion ma-
t r ix. The inver se of t his project ion may be represent ed by t he inver se of
t he t r ansfor mat ion mat r ix.
The inver se mat r ix is used freely in comput er gr aphics paper s and
published algor it hms, but rout ines for it s calculat ion are usually left t o
t he read er. There are sever al met hod s for calculat ing t he inver se of a
mat r ix; t he one present ed here is one of t he easiest t o follow, t hough it is
not t he fast est . If t he inver se of a mat r ix larger t han 4-by-4 is need ed or
if speed is cr it ical, ot her met hod s of calculat ing t he inver se may be more
suit able.
A common use of t he inver se mat r ix is in t ext ure mapping applicat ions,
where screen coord inat es need t o be mapped back ont o t he sur face of an
object in wor ld space. The inver se is also useful in r ay t r acing, when one
wishes t o int er sect a par amet r ically d ened object . Rat her t han act ually
t r ansfor m t he object from it s par amet r ic space int o wor ld space, t he r ay
is inver se-t r ansfor med int o t he object s canonical space, where t he int er -
sect ion is t ypically easier and fast er. One example of t his t echnique is
d escr ibed in Blinn (1989).
The inver se is also useful for t r ansfor ming from one d evice space t o
anot her. For example, t he shad ow map t echniqu e of Williams (1978)
requires t r ansfor ming a point in screen space t o a 3D coord inat e syst em
471
IX.7 MATRIX INVERSION
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 471
d ened by t he light source. In t his algor it hm, a d ept h buffer is con-
st r uct ed by rend er ing t he scene from t he point of view of t he light source.
To d et er mine if a par t icular point is in shad ow, one nd s it s line of sight
in t he coord inat e syst em of t he light source, and looks up (from t he d ept h
buffer ) t he nearest sur face visible from t he light in t hat d irect ion. If t he
point in quest ion is far t her from t he light t han t he st ored object , it is
blocked by t hat object and is in shad ow. This calculat ion requires t aking
a screen space coord inat e and t r ansfor ming it r st int o wor ld space, and
t hen int o t he space of t he light source for look-up in t he d ept h buffer.
The ad joint mat r ix (M*) is similar t o t he inver se, except for a scaling
fa ct or. In p a r t i cu l a r, M* = (1/ d et (M))M
1
(ea ch el emen t a d
ij
=
inv
ij
/ d et ), where d et is t he d et er minant of t he or iginal mat r ix. If t he
mat r ix is singular, t hen d et = 0, so t here is no inver se mat r ix. However,
comput ing t he ad joint d oes not require t he d et er minant . The t wo main
ad vant ages of t he ad joint are t hat it always exist s, and t hat it is easier and
fast er t o calculat e t han t he inver se.
The d ist inct ion bet ween t he ad joint and t he inver se may be d emon-
st r at ed by obser ving t he t r ansfor mat ion of a vect or. Suppose one calcu-
lat es a vect or V by t r ansfor ming an input V by a mat r ix M: V = VM. We
can post -mult iply V by M
1
t o recover V: V = VM
1
= V. Suppose
i n st ea d we u se t h e a d joi n t , t h en V = V M* = V MM* =
VM(1/ d et (M))M
1
= (1/ d et (M))V. Thu s, u sing t he ad joint r at her t han
t he inver se get s us back t o t he or iginal input vect or, but it has been
scaled by 1/ d et (M). A common use for t he ad joint is for t r ansfor ming
sur face nor mals, which t ypically must be rescaled t o unit lengt h aft er
t r ansfor mat ion, anyway, so t here is no penalt y for t his scaling creat ed by
use of t he ad joint .
See Append ix 2 for C Implement at ion (766)
472
IX.8 MATRICES AND TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 472
IX.8 IX.8 IX.8 IX.8 IX.8
M MM MMATRICES AND ATRICES AND ATRICES AND ATRICES AND ATRICES AND
T TT TTRANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS
Ronald Goldman
Universit y of Wat erloo,
Wat erloo, Ont ario, Canada
People oft en st ruggle t o nd t he 4 4 mat r ices for afne or project ive
t r ansfor mat ions t hat are not relat ive t o t he or igin or t he coord inat e axes,
but r at her relat ive t o some ar bit r ar y point s or lines. Oft en t hey proceed
by t r ansfor ming t he problem t o t he or igin and coord inat e axes, nd ing
t he t r ansfor mat ion mat r ix relat ive t o t his canonical posit ion, and t hen
t r ansfor ming back t o an ar bit r ar y locat ion. All t his ext r a wor k is unneces-
sar y. Here we d escr ibe t he 4 4 mat r ices for t he following t r ansfor ma-
t ions:
Tr anslat ion
Rot at ion
Mir ror Image
Scaling
Unifor m
Nonunifor m
Project ions
Or t hogonal
Par allel
Per spect ive
Each of t hese t r ansfor mat ions in any ar bit r ar y posit ion can be d ened
in t er ms of t hree basic mat r ices: t he t ensor prod uct , t he cross prod uct ,
and t he id ent it y mat r ix. We includ e t he d enit ions of t hese basic build ing
blocks along wit h t he afne and project ive t r ansfor mat ion mat r ices.
473
IX.8 MATRICES AND TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 473
Not at i on Not at i on Not at i on Not at i on Not at i on
a. Id ent it y
b. Tensor Prod uct


v w
v
1
w
1
v
1
w
2
v
1
w
3
v
2
w
1
v
2
w
2
v
2
w
3
v
3
w
1
v
3
w
2
v
3
w
3

v
1
v
2
v
3

_
,

w
1
w
2
w
3
( )
c. Cross Prod uct
Obser vat i ons Obser vat i ons Obser vat i ons Obser vat i ons Obser vat i ons
a. u*I = u
b. u*(v w) = (u v)w
c. u*(wx_) = wxu
Tr ansl at i on Tr ansl at i on Tr ansl at i on Tr ansl at i on Tr ansl at i on
w = t r anslat ion vect or

I
1 0 1
0 1 0
0 0 1

wx_
0 w
3
w
2
w
3
0 w
1
w
2
w
1
0

T w ( )
I 0
w 1
474
IX.8 MATRICES AND TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 474
Rot at i on Rot at i on Rot at i on Rot at i on Rot at i on
L = Axis line
w = Unit vect or par allel t o L
Q = Point on L
= Angle of rot at ion
R (w, ) = (cos ) I + (1 cos )w w + (sin )wx_


R w, , Q ( )
R w, ( ) 0
Q Q *R w, ( ) 1
Mi r r or Image Mi r r or Image Mi r r or Image Mi r r or Image Mi r r or Image
S = Mir ror plane
n = Unit vect or per pend icular t o S
Q = Point on S

M n, Q ( )
I 2 n n ( ) 0
2 Q n ( )n 1
Scal i ng Scal i ng Scal i ng Scal i ng Scal i ng
Q = Scaling or igin
c = Scaling fact or
w = Unit vect or par allel t o scaling d irect ion
a. Unifor m scaling

S(Q, c) =
cI 0
(1 c)Q 1
475
IX.8 MATRICES AND TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 475
b. Nonunifor m scaling
Pr oj ect i on Pr oj ect i on Pr oj ect i on Pr oj ect i on Pr oj ect i on
S = Image plane
n = Unit vect or per pend icular t o S
Q = Point on S
w = Unit vect or par allel t o project ion d irect ion
R = Per spect ive point
a. Or t hogonal project ion
b. Par allel project ion
c. Per spect ive project ion

Persp n, Q, R ( )
R Q ( ) n
[ ]
I n R
t
n
Q n ( )R R n

S(Q, c, w) =
I (1- c)(w w) 0
(1 c)(Q w)w 1

Pproj n, Q, w ( )
I n w ( ) / w n ( ) 0
Q n ( ) / w n ( )
[ ]
w 1

Oproj n, Q ( )
I n n 0
Q n ( )n 1
476
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 476
IX.9 IX.9 IX.9 IX.9 IX.9
E EE EEFFICIENT FFICIENT FFICIENT FFICIENT FFICIENT
P PP PPOST-CONCATENTION OF OST-CONCATENTION OF OST-CONCATENTION OF OST-CONCATENTION OF OST-CONCATENTION OF
T TT TTRANSFORMATION MATRICES RANSFORMATION MATRICES RANSFORMATION MATRICES RANSFORMATION MATRICES RANSFORMATION MATRICES
Joseph M. Cychosz
Purdue Universit y
W. Laf ayet t e, Indiana
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Present ed in t his paper are t he comput at ions required for efcient , d irect ,
post -concat enat ion of st and ard 4 4 t r ansfor mat ion mat r ices. The use of
post -concat enat ed t r ansfor mat ion mat r ices has a wid e var iet y of applica-
t ions, not ably in comput er gr aphics. This paper compares t he cost s of
comput ing t he result ing t r ansfor mat ion t hrough d irect post -concat ena-
t ion, ver sus using a st and ard 4 4 mat r ix mult iply.
Post -concat enat ion of t r ansfor mat ion mat r ices is used ext ensively in
comput er gr aphics, as well as ot her applicat ions t hat require t he hier ar-
chical t r ansfor mat ion of point s, lines, and su r faces in t hree-space. A
d et ailed explanat ion of t he use of concat enat ed t r ansfor mat ion mat r ices
can be found in sever al sources, includ ing Foley and Van Dam (1982) and
Gasson (1983). Two examples of t heir use are t he viewing of hier archi-
cally const ruct ed mod els, and t he comput at ion of end effect or posit ion
and or ient at ion of robot ic ar ms. Shown in Fig. 1 is an example of a 3D
t r ansfor mat ion resu lt ing from t he concat enat ion of sever al ind ivid u al
t r ansfor mat ion mat r ices t hat t r anslat e t he ver t ical bar t o t he or igin, rot at e
it 90 count erclockwise, and t hen t r anslat e it back.
Typ ical imp lement at ions of t he p ost -concat enat ion comp u t at ion r st
const r uct t he t r ansfor mat ion mat r ix, t hen for m t he prod uct of t he t r ans-
for mat ion mat r ix wit h t he cur rent t r ansfor mat ion mat r ix, and nally copy
t he r esu lt ing mat r ix ont o t he cu r rent t r ansfor mat ion mat r ix, t hereby
yield ing a new cur rent t r ansfor mat ion mat r ix. Figure 2 d epict s t he cod ed
477
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 477
Figure 1. Example use of concat enat ed t r ansfor mat ion mat r ices.
implement at ion of a rot at ion about t he x-axis using t his approach. This
t ype of implement at ion not only requires a full mat r ix mult iply but also
ext r a comput at ion in for ming t he t r ansfor mat ion and in copying t he
result ing mat r ix. A more efcient met hod would per for m t he post -con-
cat enat ion d irect ly on t he cur rent t r ansfor mat ion mat r ix.
functi on rot x (M,a)
M: matri x4; Current t ransformat ion mat rix
a: real ; Angle of rot at ion about t he x axis
begi n
T: matri x4 I; ident (T)
T[1][1] = T[2][2] = cos (a);
T[1][2] = sin (a);
T[2][1] = T[1][2];
R: matri x4 M T; mmult (R, M, T)
M R mcopy (M, R)
end
Figur e 2. Typical implement at ion of a t r ansfor mat ion oper at ion.
478
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 478
Di r ect Post - Concat enat i on Di r ect Post - Concat enat i on Di r ect Post - Concat enat i on Di r ect Post - Concat enat i on Di r ect Post - Concat enat i on
Present ed in t his sect ion are t he comput at ions required for d irect post -
concat enat ion of st and ard t r ansfor mat ions ont o t he cur rent t r ansfor ma-
t ion mat r ix. By relying on t he spar seness of t he t r ansfor mat ion mat r ix
(t hat is, t he number of zero and one element s in t he mat r ix), d irect
post -concat enat ion can be per for med efcient ly by comput ing only t he
element s of t he cur rent t r ansfor mat ion mat r ix t hat act ually change. In t he
following examples t he t r ansfor mat ion mat r ix is shown on t he r ight , while
t he appropr iat e pseud o-cod e fr agment for post -concat enat ing t he mat r ix
t o t he cur rent t r ansfor mat ion is shown on t he left . The cur rent t r ansfor-
mat ion mat r ix, represent ed by M in t he cod e fr agment s, is not shown. It
is assumed t o be a gener al 4 4 mat r ix (t hat is, matri x4).
x-axis rot at ion:
c: real cos (a); s: real sin (a):
f or i: i nteger 0, i i + 1 whi l e i < 4 do
t : real M[i][1];
M[i][1] t c M[i][2] s;
M[i][2] t s + M[i][2] c,
endl oop;
y-axis rot at ion
c: real cos (a); s: real sin (a);
f or i: i nteger 0. i i + 1 whi l e i < 4 do
t : real M[i][0];
M[i][0] t c + M[i][2] s;
M[i][2] M[i][2] ct s;
endl oop;
z-axis rot at ion
c: real cos (a); s: real sin (a);
f or i: i nteger 0, i i + 1 whi l e i < 4 do
t : real M[i][0];
M[i][0] t c + M[i][1] s;
M[i][1] t sM[i][1] c;
endl oop;

R
x
(a)
1 0 0 0
0 cos(a) sin(a) 0
0 sin(a) cos(a) 0
0 0 0 1

R
y
(a)
cos(a) 0 sin(a) 0
0 1 0 0
sin(a) 0 cos(a) 0
0 0 0 1

R
z
(a)
cos(a) sin(a) 0 0
sin(a) cos(a) 0 0
0 0 1 0
0 0 0 1
479
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 479
scaling:
for i: i nteger 0, i i + 1 whi l e i < 4 do
M[i][0] M[i][0] s
x
;
M[i][1] M[i][1] s
y
;
M[i][2] M[i][2] s
z
;
endl oop;
t ranslat ion:
for i: i nteger 0, i i + 1 whi l e i < 4 do
M[i][0] M[i][0] + M[i][3] t
x
;
M[i][1] M[i][1] + M[i][3] t
y
;
M[i][2] M[i][2] + M[i][3] t
z
;
endl oop;
perspect ive:
(along z-axis, image plane at origin, eye at z = d)
f: real 1/ d ;
for i: i nteger 0, i i + 1 whi l e i < 4 do
M[i][3] M[i][3] + M[i][2] f;
M[i][2] 0;
endl oop
Table 1. Oper at ional Cost Compar ison.

S
xyz
(s
x
, s
y
, s
z
)
s
x
0 0 0
0 s
y
0 0
0 0 s
z
0
0 0 0 1

T
xyz
(t
x
,t
y
,t
z
)
1 0 0 0
0 1 0 0
0 0 1 0
t
x
t
y
t
z
1

P
z
(d)
1 0 0 0
0 1 0 0
0 0 0 1/ d
0 0 0 1
480
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 480
Table 2. Comput at ional Compar ison for Var ious CPUs.
481
IX.9 EFFICIENT POST-CONCATENATION OF TRANSFORMATION MATRICES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 481
Comput at i onal Cost Compar i son Comput at i onal Cost Compar i son Comput at i onal Cost Compar i son Comput at i onal Cost Compar i son Comput at i onal Cost Compar i son
The d irect met hod s require consid er ably less comput at ion t han a st an-
d ard 4 4 mat r ix mult iply. Table 1 compares t he oper at ional cost s of
d irect post -concat enat ion wit h t hat of st and ard mat r ix mu lt iplicat ion.
Table 2 compares t he comput at ional cost s for each of t he t r ansfor mat ions
for a var iet y of comput er s. Each t ype of t r ansfor mat ion has t hree columns
for each of t he CPUs. The r st column shows t he CPU t ime in second s
required using a st and ard 4 4 mat r ix mult iply t o per for m t he concat e-
nat ion; t he second column shows t he CPU t ime for d irect concat enat ion;
and t he t hird shows t he speed up of t he d irect approach over t he st and ard
approach. The CPU t ime repor t ed represent s t he t ime required t o per-
for m 10,000 concat enat ion oper at ions.
See Append ix 2 for C Implement at ion (770)
10
MODELING AND
TRANSFORMATIONS
485
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 485
X. 1 X. 1 X. 1 X. 1 X. 1
T TT TTRANSFORMATION RANSFORMATION RANSFORMATION RANSFORMATION RANSFORMATION
I II IIDENTITIES DENTITIES DENTITIES DENTITIES DENTITIES
Ned Greene
Apple Comput er
Cupert ino, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
3D gr aphics syst ems usually per for m coord inat e t r ansfor mat ions wit h
4 4 mat r ices creat ed by concat enat ing pr imit ive geomet r ic t r ansfor ma-
t ions such as rot at ion and t r anslat ion. Algebr aic manipulat ion of t r ansfor-
mat ion exp ressions is cr u cial t o a var iet y of p roblems t hat ar ise in
mod eling and mot ion specicat ion. Oft en it is necessar y, for example, t o
reor d er t r ansfor mat ions or t o nd equivalent par amet er set s for d ifferent
represent at ions of t he same t r ansfor mat ion. Some problems of t his t ype
are simple and int uit ive, but oft en t his is not t he case, since solut ion may
involve algebr aic manipulat ion of 3 3 or 4 4 mat r ices having coef-
cient s t hat are t r igonomet r ic expressions. This collect ion of id ent it ies is
int end ed t o make wor king wit h t r ansfor mat ion expressions easier and
more int uit ive.
Not at i on Not at i on Not at i on Not at i on Not at i on
Pr imit ive geomet r ic t r ansfor mat ions are specied as follows:
T
u,v,w
(A,B,C) t r anslat e A in u, B in v, C in w
(subscr ipt s u, v, w encompass t he six possible
combinat ions of x, y, and z)
R
u
(D) rot at e by angle D about axis u
486
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 486
S
u,v,w
(E,F,G) scale u axis by E, v axis by F, w axis by G
(subscr ipt s u, v, w encompass t .he six possible
combinat ions of x, y, and z)
K
u, v
(J) skew u axis relat ive t o v axis by angle J,
/ 2 < J < / 2
P(,N,F) per spect ive t r ansfor mat ion where

is t he ver t ical view


angle (0 <

<

), N is t he z coord inat e of t he near


clipping plane, and F is t he z coord inat e of t he far
clipping plane.
In keeping wit h t he usual convent ions in comput er gr aphics, t he xyz
coord inat e fr ame is r ight -hand ed , d irect ion of rot at ions is est ablished by
t he r ight -hand r ule, and point s are represent ed by row mat r ices. Thus,
mapping a point from a local t o a global coord inat e syst em is d one as
follows:
[x
G
y
G
z
G
w
G
] = [x
G
y
G
z
L
1]M,
where M is a concat enat ion of t r ansfor mat ion mat r ices. 4 4 mat r ix
expressions for t he above pr imit ive t r ansfor mat ions are given in t he
append ix.
Tr ansf or mat i on Ident i t i es Tr ansf or mat i on Ident i t i es Tr ansf or mat i on Ident i t i es Tr ansf or mat i on Ident i t i es Tr ansf or mat i on Ident i t i es
Throughout t his paper u, v, w, are ar bit r ar y d ist inct coord inat e axes. For
id ent it ies t hat d epend on t he hand ed ness of t he coord inat e fr ame, t wo
ver sions are given wit h t he r ight -hand ver sion labelled (#R) and t he
left -hand ver sion labelled (#L). Since we are assuming t hat t he xyz axes
are a r ight -hand ed coord inat e fr ame, u, v, w is a r ight -hand ed coord inat e
fr ame for (u = x, v = y, w = z), (u = y, v = z, w = x), and (u = z,
v = x, w = y). The t hree ot her combinat ions of axes, (u = z, v = y,
w = x), (u = y, v = x, w = z), and (u = x, = z, w = y) ar e left -
hand ed coord inat e fr ames.
487
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 487
Rever sing t he ord er of t r anslat ion and scaling is simple and int uit ive:
T
u,v,w
(A,B,C)S
u,v,w
(D,E,F) = S
u,v,w
(D,E,F)T
u,v,w
(AD,BE,DF)
(1)
S
u,v,w
(A,B,C)T
u,v,w
(D,E,F)
= T
u,v,w
(D/A,E/B,F/C)S
u,v,w
(A,B,C),
A 0, B 0, C 0. (2)
The following r ules for exchanging t he ord er of t r anslat ion and rot at ion
are relat ed t o for mulas for rot at ing a 2D point :
T
u,v,w
(A,B,C)R
u
(D)
=R
u
(D)T
u,v,w
(A,B cos(D) C sin(D), C cos(D) + B sin(D))
(3R)
T
u,v,w
(A,B,C)R
u
(D)
= R
u
(D)T
u,v,w
(A,B cos(D) + C sin(D) , C cos(D) B sin(D))
(3L)
R
u
(A)T
u,v,w
(B,C,D)
= T
u,v,w
(B,C cos(A) + D sin(A), D cos(A) C sin(A))R
u
(A)
(4R)
R
u
(A)T
u,v,w
(B,C, D)
=T
u,v,w
(B,C cos(A) D sin(A), D cos(A) + C sin(A))R
u
(A).
(4L)
488
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 488
The ord er of t r anslat ion and skewing can be easily rever sed :
T
u,v,w
(A,B,C)K
u,v
(D)
= K
u,v
(D)T
u,v,w
(A + B t an(D), B,C), / 2 < D < / 2 (5)
K
u,v
(A)T
u,v,w
(B,C,D)
= T
u,v,w
(B C t an(A),C,D)K
u,v
(A), / 2 < A < / 2. (6)
Rot at ion and isot ropic scaling commut e:
R
u
(A)S
x,y,z
(B, B, B) = S
x,y,z
(B, B, B)R
u
(A). (7)
This id ent it y shows t hat even when angles are nor malized t o a st and ard
int er val (say, t o ), t here are always at least t wo par amet er set s of
rot at ion angles t hat gener at e t he same rot at ion:
R
u
(A) R
v
(B) R
w
(C) = R
u
( + A) R
v
( B) R
w
( + C). (8)
Special cases involving rot at ion of or / 2 (see also Rule 20).
R
u
() = S
u,v,w
(1,1,1) (9)
R
u
(

)R
v
() = R
w
() (10)
R
u
(A)R
v
() = R
v
()R
u
(A) (11)
R
u
(/ 2)R
v
(A) = R
w
(A)R
u
(/ 2) (12R)
R
u
(/ 2)R
v
(A) = R
w
(A)R
u
(/ 2) (12L)
R
u
(/ 2)R
v
(A) = R
w
(A)R
u
(/ 2) (13R)
R
u
(/ 2)R
v
(A) = R
w
(A)R
u
(/ 2) (13L)
489
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 489
A rot at ion can be expressed as t hree skews:
R
u
(A) =K
v,w
(A/ 2)K
w,v
(at an(sin(A)))K
v,w
(A/ 2),


2
< A <

2
(14R)
R
u
(A) = K
v,w
(A/2)K
wv
(at an(sin(A)))K
v,w
(A/ 2),


2
< A <

2
(14L)
A paper on image rot at ion by Paet h (1986) is based on t his id ent it y which
was ind epend ent ly d iscovered by Heckber t (Greene, 1983).
A rot at ion can also be expressed as a combinat ion of skews and scales:
R
u
(A) = S
u,v,w
(1,1/ cos(A),cos(A))K
w,v
(B)K
v,w
(A)
= S
u,v,w
(l,cos(A), l/ cos(A))K
v,w
(B))K
w,v
(A),


2
< A <

2
B = at an(sin(A)cos(a)) (15R)
R
u
(A) = S
u,v,w
(1,1/ cos(A), cos(A))K
w,v
(B)K
v,w
(A)
= S
u,v,w
(1,cos(A), 1/ cos(A))K
v,w
(B))K
w,v
(A),

2
< A <

2
B = at an(sin(A)cos(A)) (15L)
Skewing and isot ropic scaling commut e:
S
x,y,z
(A,A,A)K
u,v
(B) = K
u,v
(B)S
x,y,z
(A,A,A). (16)
The ord er of skewing and anisot ropic scaling can be rever sed :
S
u,v,w
(A,B,C,)K
u,v
(D) = K
u,v
(E)S
u,v,w
(A,B,C),
A 0, /2 < D < /2
E = at an(B t an(D)/ A). (17)
490
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 490
A special case for which skewing and anisot ropic scaling commut e
follows:
S
u,v,w
(A, 1, 1)K
v,w
(B) =K
v,w
(B)S
u,v,w
(A,1,1) (18).
The following id ent it y shows t hat anisot ropic scaling following rot at ion
int rod uces skew:
R
u
(A)S
u,v,w
(B,C,D)
= K
w,v
(H)K
v,w
(G)S
u,v,w
(B,E,F)R
u
(A), A 0,


2
< A <


2
Q = (C D)/ (t an(A) + cot (A))
F = C + (D C)/ (1 + t an
2
(A))
E = (Q(D C)sin(A)cos(A)
+F(D sin
2
(A) + C cos
2
(A)))/ F
G = at an((C D)/ (E(t an(A) + cot (A))))
H = at an((C E)cos(A)/ (Qcos(A) + Fsin(A)))) (19R)
For a left -hand ed coord inat e fr ame, t he signs of G and H are rever sed .
Two special cases follow, for which rot at ion and anisot ropic scaling
commut e:
S
u,v,w
(A,B,C)R
u
( ) = R
u
( )S
u,v,w
(A,B,C) (20)
S
u,v,w
(A,1,1)R
u
(B) = R
u
(B)S
u,v,w
(A, 1, 1). (21)
491
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 491
A skew can be expressed as t wo rot at ions and a scale:
K
u,v
(A) = R
w
(B)S
u,v,w
(C,D,1)R
w
(E), /2<A< /2
C =

tan(A) t 4 + tan
2
(A)
( )
/ 2
D = 1/ C
B = at an(C)
E = B + / 2 (22R)
For a l eft -h a n d ed coor d i n a t e fr a me, t h e si gn of A i s r ever sed .
Id ent it ies for exchanging t he ord er of skews follow:
K
u,v
(A)K
u,w
(B)
= K
u,w
(B)K
u,v
(A), /2 < A < / 2, /2 < B < /2 (23)
K
u,v
(A)K
u,v
(B)
= K
u,v
(B)K
u,v
(A) = K
u,v
(at an(t an(A) + t an(B))),


2
< A <

2
,


2
< B <

2
. (24)
Expressions involving per spect ive follow:
P(A, B, C) = P(D, B, C)S
x,y,z
(E, E, 1) = S
x,y,z
(E, E, 1)P(D, B, C)
E = t an(D/ 2)/ t an(A/ 2), 0 < A < , 0 < D < , 0 < B < C.
(25)
The previous id ent it y shows t hat changing camer a angle is equivalent t o
a 2D zoom.
DP(A, B, C) = S
s,y,z
(D, D, D)P(A, BD, CD), D > 0. (26)
Mat r ix expressions on t he t wo sid es of t he equat ion d iffer by scale fact or
D, so from t he st and point of t r ansfor ming homogeneous coord inat es,
P(A, B, C) is equivalent t o S
x,y,z
(D, D, D)P(A, BD, CD).
492
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 492
Appendi x Appendi x Appendi x Appendi x Appendi x
Id ent it ies are based on t he following mat r ix represent at ions of pr imit ive
t r ansfor mat ions. We assume a r ight -hand ed coord inat e fr ame, rot at ion
accord ing t o t he r ight -hand ru le, t hat point s are represent ed as row
mat r ices, and t hat point s rot at e in a xed coord inat e fr ame.
t r anslat ion scaling
T
x,y,z
(A,B,C) S
x,y,z
(A,B,C)

1 0 0 0
0 1 0 0
0 0 1 0
A B C 1

1
]
1
1
1

A 0 0 0
0 B 0 0
0 0 C 0
0 0 0 1

1
]
1
1
1

1 0 0 0
0 cos A sin A 0
0 sin A cos A 0
0 0 0 1

1
]
1
1
1

cos A 0 sin A 0
0 1 0 0
sin A 0 cos A 0
0 0 0 1

1
]
1
1
1

cos A sin A 0 0
sin A cos A 0 0
0 0 1 0
0 0 0 1

1
]
1
1
1

1 0 0 0
tan A 1 0 0
0 0 1 0
0 0 0 1

1
]
1
1
1

1 0 0 0
0 1 0 0
tan A 0 1 0
0 0 0 1

1
]
1
1
1

1 0 0 0
0 1 0 0
0 tan A 1 0
0 0 0 1

1
]
1
1
1

a 0 0 0
0 a 0 0
0 0 b 1
0 0 c 0

1
]
1
1
1
x rot at ion y rot at ion
R
x
(A) R
y
(A)
z rot at ion
R
z
(A)
skew x wit h skew y wit h
respect t o y respect t o x
K
x,y
(A) K
y,x
(A)
skew x wit h skew z wit h
respect t o z respect t o x
K
x,z
(A) K
x,z
(A)
skew y wit h skew z wit h
respect t o z respect t o y
K
y,z
(A) K
z,y
(A)
per spect ive
P(A,B,C)

1 tan A 0 0
0 1 0 0
0 0 1 0
0 0 0 1

1
]
1
1
1

1 0 tan A 0
0 1 0 0
0 0 1 0
0 0 0 1

1
]
1
1
1

1 0 0 0
0 1 tan A 0
0 0 1 0
0 0 0 1

1
]
1
1
1
493
X.1 TRANSFORMATION IDENTITIES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 493
where a = 1/ t an(A/ 2)
b = (C + B)/ (C B)
c = 2BC/ (B C).
The per spect ive mat r ix t r ansfor ms t he viewing volumet he region of
t he viewing pyr amid bet ween t he clipping planest o a cube ext end ing
from 1 t o 1 on each axis.
Acknowl edgement s Acknowl edgement s Acknowl edgement s Acknowl edgement s Acknowl edgement s
Most of t he research for t his paper was d one at t he New Yor k Inst it ut e of
Technology in 1983. I wou ld like t o t hank for mer colleagu es Pau l
Heckber t and Pat Hanr ahan for t heir help at t hat t ime. In ad d it ion,
Paul Heckber t cont r ibut ed id ent it ies (14) and (22) and reviewed t his
manuscr ipt .
See also Mat r ix Id ent it ies (453)
494
X.2 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 494
X. 2 X. 2 X. 2 X. 2 X. 2
F FF FFIXED-POINT IXED-POINT IXED-POINT IXED-POINT IXED-POINT
T TT TTRIGONOMETRY WITH RIGONOMETRY WITH RIGONOMETRY WITH RIGONOMETRY WITH RIGONOMETRY WITH
C CC CCORDIC ITERATIONS ORDIC ITERATIONS ORDIC ITERATIONS ORDIC ITERATIONS ORDIC ITERATIONS
Ken Turkowski
Apple Comput er
Cupert ino, Calif ornia
Int r oduct i on t o t he Int r oduct i on t o t he Int r oduct i on t o t he Int r oduct i on t o t he Int r oduct i on t o t he CORDIC CORDIC CORDIC CORDIC CORDIC Techni que Techni que Techni que Techni que Techni que
CORDIC is an acronym t hat st and s for COord inat e Rot at ion Dlgit al
Comput er, and was coined by Vold er (1959). It s concept s have been
fur t her d eveloped t o includ e calculat ion of t he Discret e Four ier Tr ans-
for m (Despain, 1974), exponent ial, logar it hm, for ward and inver se circu-
lar and hyper bolic funct ions, r at ios and square root s (Chen, 1972; Walt her,
1971), and has been applied t o t he ant i-aliasing of lines and polygons
(Tur kowski, 1982).
It is an it er at ive xed -point t echnique t hat achieves approximat ely one
mor e bit of accu r acy wit h each it er at ion. In sp it e of merely linear
convergence, t he inner loop is ver y simple, wit h ar it hmet ic t hat consist s
only of shift s and ad d s, so it is compet it ive wit h (and even out per for ms)
oat ing-point t echniques wit h quad r at ic convergence, for t he accur acy
t ypically required for t wo-d imensional r ast er gr aphics.
C CC CCORDIC ORDIC ORDIC ORDIC ORDIC Vect or Rot at i on Vect or Rot at i on Vect or Rot at i on Vect or Rot at i on Vect or Rot at i on
To rot at e a vect or [x, y] t hrough an angle , we per for m t he linear
t r ansfor mat ion


x y
[ ]
x y
[ ]

cos sin
sin cos

1
]
1
.
(1)
495
X.2 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 495
If t his t r ansfor mat ion is accomplished by a sequence of rot at ions

i
such
t hat
(


i
,
i

(2)
t 22
we t hen have


x y
[ ]
x y
[ ]
cos
i
sin
i
sin
i
cos
i

1
]
1
i
,
(3)
where t he prod uct is per for med on t he r ight wit h increasing i. We next
fact or out a cos

i
from t he mat r ix:


x y
[ ]
x y
[ ]
cos
i
i

1 tan
i
tan
i
1

1
]
1

'



cos
j
j

'

x y
[ ]

1 tan
i
tan
i
1

1
]
1
i
.
(4)
Then we const r ain t he

i
s such t hat
t an
i

i
2
i
, (5)
where t he sign is chosen for each i so t hat Eq. 2 is fullled t o t he d esired
accur acy. We ar r ive at


x y
[ ]
cos(tan
1
2
j
)
j

'

x y
[ ]

m
i
1 t
i
2
i
2
i
1

1
]
1
i

'




1
1+ 2
2 j
j

'

x y
[ ]

1 t
i
2
i
m
i
2
i
1

1
]
1
i
.
(6)
496
X.2 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 496
Figure 1
Eq. 6 is even simpler t han it looks. The fact or in br aces is a const ant for
a xed number of it er at ions, so it may be precomput ed . The mat r ix mult i-
plicat ions are not hing more t han shift s and ad d s (or subt r act s).
The convergence of t his prod uct is guar ant eed in an int er val great er
t han 90 <

< 90 when i st ar t s out at 0, alt hough it converges fast er


when i begins at 1 (t hat is, t he r st shift is upward , t he rest d ownward ).
Typical convergence is illust r at ed in Fig. 1 using approximat ely 16 bit
regist er s. Not e t hat no new bit s of accur acy are ad d ed aft er about 16
it er at ions, but unt il t hat point , approximat ely one bit of accur acy is ad d ed
per it er at ion.
The scale fact or converges t wo bit s per it er at ion and is accur at e t o 16
bit s aft er 9 it er at ions, so t he limit value could be safely used for all
pr act ical number s of it er at ions, and has t he value 0.2715717684432241
when i st ar t s at 1, and t he value 0.6072529350088813 when i st ar t s
at 0.
The recommend ed algor it hm is t hen

x y
[ ]
0. 27157177 x y
[ ]
i 1
N 2

1 t
i
2
i
m
i
2
i
1

1
]
1
,
(7)
or in word s, st ar t wit h t he largest subangle and eit her ad d it or subt r act
it in such a way as to bring the desired angle closer to zero, and perform the
497
X.2 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 497
mat r ix mult iplicat ion cor respond ing t o t hat subrot at ion wit h shift s and
ad d s (or subt r act s). Cont inue on in t he manner for t he specied number
of it er at ions. Mult iply t he result by t he const ant scale fact or.
Not e t hat t wo ext r a bit s are need ed at t he high end d ue t o t he scale
fact or. It is d ifcult t o assess t he er ror d ue t o nit e word lengt h, but
32-bit regist er s wor k well in pr act ice. Expect t o have about 23 LSBs of
noise, even if 32 it er at ions are per for med .
If t he or iginal vect or was [r 0], rot at ion by

per for ms a polar-t o-rect -


angular conver sion. If r is 1, it gener at es sines and cosines. Rect angular-
t o-polar conver sion can be accomplished by d et er mining t he sense of t he
rot at ion by t he sign of y at each st ep.
See also Tr ig Summar y (12); Rot at ion Mat r ix Met hod s Sum-
mar y (455); Bit Pat t er ns for Encod ing Angles (442); Fast 2D3D
Rot at ion (440); Using Quat er nions for Cod ing 3D Tr ansfor ma-
t ions (498)
See Append ix 2 for C Implement at ion (773)
498 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 498
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
X. 3 X. 3 X. 3 X. 3 X. 3
U UU UUSING QUATERNIONS SING QUATERNIONS SING QUATERNIONS SING QUATERNIONS SING QUATERNIONS
F FF FFOR CODING 3D OR CODING 3D OR CODING 3D OR CODING 3D OR CODING 3D
T TT TTRANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS
Pat rick-Gilles Maillot
Sun Microsyst ems, Inc.
Mount ain View, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
This paper present s some fund ament als of a mat hemat ical element called
a quat ernion. A t heoret ical st ud y emphasizes t he char act er ist ic proper-
t ies of t hese element s and t he analogy t hat can be mad e bet ween t he
geomet r ical int er pret at ion of 3D t r ansfor mat ions and t he subgroup of
unit quat er nions.
We also give a pr act ical applicat ion of quat er nions for t he cod ing of 3D
t r ansfor mat ions and some basic algor it hms for or t hogonal coord inat e
syst ems d isplacement s.
Quat er nions were int r od uced in 1843 by Sir William R. Hamilt on. They
are useful pr imar ily in t he cod ing of nat ur al movement s in a 3D space, as
it has been realized in t he PATK3D soft ware (Maillot , 1983), or t o
per for m movement s along par amet r ic cur ves (Shoemake, 1985). Quat er-
nions may also be used when cod ing t r ansfor mat ions in Const ruct ive
Solid Geomet r y (CSG) t rees as used in MCAD syst ems or applicat ions, as
well as when cod ing t he hier archical t r ansfor mat ion t rees of visualizat ion
soft ware, such as proposed by t he PHIGS (PHIGS, 1987) st and ard .
This paper d escr ibes a way t o cod e and evaluat e 3D t r ansfor mat ions
using a d ifferent met hod t han cod ing 4 4 mat r ices. The init ial re-
searches on t he mat er ial present ed here have been cond u ct ed at t he
Univer sit y Claud e Ber nard , Lyon I, Fr ance. In 1983, t he gr aphic group
was wor king on for malizat ion of scene composit ion and nat ur al move-
ment s cod ing in space. Some mat hemat ical d emonst r at ions p resent ed
499 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 499
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
here are d ue t o E. Tosan (Tosan, 1982), who is a mat hemat ician re-
searcher of t he comput ing sciences group.
A mat hemat ical d enit ion of t he quat er nion d ivision r ing, Q, is pro-
posed t o d emonst r at e some of t he par t icular it ies and proper t ies of t he
quat er nions, as well as t he d enit ion of t he oper at ions t hat can be applied
t o t hem.
The analogy t hat can be mad e bet ween t his algebr aic element and t he
geomet r y of 3D t r ansfor mat ions will be d escr ibed . This is const ruct ed
over a subgroup of quat er nions having a par t icular proper t y.
Some examples of cod e are proposed t hat show t wo basic t r ansfor ma-
t ions involving only some simple algebr aic comput at ions.
De ni t i on of t he Quat er ni ons De ni t i on of t he Quat er ni ons De ni t i on of t he Quat er ni ons De ni t i on of t he Quat er ni ons De ni t i on of t he Quat er ni ons
A quat er nion is a mat hemat ical element not ed
q = c + xi + yj + zk,
wit h c, x, y, z: real number s, and i, j, K: imaginar y number s. It is also
wr it t en in cond ensed not at ion:
q = c + u,
wit h u = xi + yj + zk called t he pure par t of t he quat er nion, and c t he
real par t of t he quat er nion.
Let us supply Q, t he set of quat er nions, wit h t wo oper at ions: ad d it ion,
q + q = (c + c) + (x + x)i + (y + y)j + (z + z)k
and mult iplicat ion, d ened on t he base {1, i, j, k}:
i
2
= j
2
= k
2
= 1
ij = k, ji = k; jk = i, kj = i; ki = j, ik = j.
500 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 500
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
If we d evelop t he mult iplicat ion, we obt ain:
qq = (c + xi + yj + zk)(c + xi + yj + zk)
= (cc xx yy zz) + (yz yz + cx + cx)i
+ (zx zx + cy + cy) j + (xy xy + cz + cz)k.
Or, if we use t he cond ensed not at ion,
qq = (c + u)(c + u)
= (cc u u) + (u u + cu + cu),
wit h
u u = xx + yy + zz: inner prod uct .
cu = cxi + cyj + czk
u u = (yz zy, zx xz, xy yz): cross prod uct .
Not es Not es Not es Not es Not es
If we wr it e u = sN and u = sN, wit h s and s being real number s,
and N = (N
x
, N
y
, N
z
) a unit vect or of R
3
, so t hat | N|
2
= 1, we nd t he
prod uct of complex number s, wit h N inst ead of t he imaginar y num-
ber i.
Then
qq = (c + sN)(c + sN)
= (cc ssN N) + (N N + csN + csN)
= (cc ss) + (cs + cs)N.
501 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 501
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
If we wr it e q = q = N, or c = c = 0 and s = s = 1, we nally nd
N
2
= ssN N = 1.
Pr oper t i es of t he Quat er ni ons Pr oper t i es of t he Quat er ni ons Pr oper t i es of t he Quat er ni ons Pr oper t i es of t he Quat er ni ons Pr oper t i es of t he Quat er ni ons
The ad d it ion of quat er nions has an ident it y element ,
0 = 0 + 0i + 0j + 0k,
and an inverse element ,
q = c xi yj zk.
The mult iplicat ion of quat er nions has an ident it y element ,
1 = 1 + 0i + 0j + 0k.
The quat er nions have some conjugat e element s,
q = c + u;

q
= c u.
It is possible t o d ene a magnit ud e for q by applying t he hermit ian
inner prod uct of q int o

q:
q

q = (c
2
+ u u) + (u u cu + cu)
= c
2
+ x
2
+ y
2
+ z
2
=

qq = q
2
, magnit ud e of q.
The quat er nions have some inverse element s:

q
1

1
q
2
q.
The mult iplicat ion is not commut at ive. If it was commut at ive, we would
502 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 502
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
have had
qq = qq.
Then,
(c + u)(c + u) = (c + u)(c + u)
u u = u u u u = 0.
The mult iplicat ion is dist ribut ive over t he ad d it ion:
q(v + w) = qv + qw (v + w)q = vq + wq.
In conclusion, we may wr it e t hat
Q is closed und er quat er nion ad d it ion.
Q is closed und er quat er nion mult iplicat ion.
(Q, +, ) is a d ivision r ing.
Pr oper t i es of t he Set of Uni t Quat er ni ons Pr oper t i es of t he Set of Uni t Quat er ni ons Pr oper t i es of t he Set of Uni t Quat er ni ons Pr oper t i es of t he Set of Uni t Quat er ni ons Pr oper t i es of t he Set of Uni t Quat er ni ons
This par agr aph focuses on t he subset quat er nions of unit magnit ud e.
These are t he par t icular quat er nions t hat are of int erest in t his applica-
t ion.
Let us consid er t he quat er nions so t hat q = l, or c
2
+ u u = 1, and
c
2
+ s
2
= l, (if u = sN); t hen we can wr it e any unit qu at er nion as
q = cos() + sin()N.
Then, for t his par t icular set of quat er nions,
The mult iplicat ion of t he conjugat e of t wo quat er nions is t he conjugat e
of t he mult iplicat ion of t he quat er nions.


q q
= (cc u u) (u u + cu + cu)


q q = (c u)(c u)
= (cc u u) + ((-u) (-u) cu cu)
= (cc u u) (u u + cu + cu)
=

q q
503 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 503
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS


The magnit ud e of t he mult iplicat ion of t wo quat er nions is t he mult ipli-
cat ion of t he magnit ud es of t he quat er nions.
qq
2
= (qq)(

q q )
= qq

q q = qq
2

q
= q
2
q
2
If t wo quat er nions have a magnit ud e of 1, t hen t he prod uct of t hese
t wo quat er nions will also have a magnit ud e of 1.
q = q = 1 qq = 1
(cos() + sin()N)(cos() + sin()N) = cos( + ) + sin( + )N
The conjugat e of a unit quat er nion is it s inver se.
q = 1 q
1
= q
The set of unit quat er nions is a mult iplicat ive subgroup of t he non-null
quat er nions.
Rot at i ons i n a Rot at i ons i n a Rot at i ons i n a Rot at i ons i n a Rot at i ons i n a 3 D 3 D 3 D 3 D 3 D Space Space Space Space Space
Rot at ions can be expressed using a geomet r ical for mulat ion. Figure 1
gr aphically present s t he rot at ion of a vect or around a given axis.
Let P = Rot
(a,N)
(P) be t he t r ansfor mat ion of point P by t he rot at ion
of angle around t he axis N. We can wr it e:
H = OH
P = OP = OH + HP = H + U
P = OP = OH + HP = H + U,


504 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 504
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
Figure 1. Geomet r ical represent at ion of a rot at ion.
and t hen,
U = P H.
In t he plane for med by (PHP), we can set
U = cos()U + sin()V,
wit h U V, V N, and V = U, t hat is t o say, V = N U =
N (P H) = N P. On t he (O, N) axis, OH is t he project ion of OP
on N, and t hen OH = H = N (N P), which gives t he for mulas
OP = H + U = N(N P) + (cos()U + sin()V)
= N(N P) + cos()(P N(N P) + sin()N P
= cos()P + (1 cos())N(N P) + sin()N P.

505 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 505


X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
This can be wr it t en in a mat r ix for mulat ion:
Rot
(,N)
= cos()I
3
+ (1 cos())N
T
N + sin()A
N
,
wit h

N N
1
, N
2
, N
3
[ ]
, I
3

1 0 0
0 1 0
0 0 1

1
]
1
1
1
, A
N

0 N
3
N
2
N
3
0 N
1
N
2
N
1
0

1
]
1
1
1
The geomet r y for mu lat ion p r esent ed above can be exp ressed in an
algebr aic for mulat ion:
Let p = (0, v) = xi + yj + zk be a pure quat er nion.
Let q = (c, u) be a unit quat er nion.
We can d ene
R
q
(p) = qp

q
= (c, u)p(c, u)
= (c, u)(0, v)(c, u)
= (c, u)(v u, v u + cv)
= (c(v u) u (v u) c(u v), u (v u)
+ c(u v) + c(v u + cv) + (v u)u)
= (0, u (u v) + 2c(u v) + c
2
v + (v u)u)
= (0, (u v)u (u u)v + 2c(u v)
+ c
2
v + (v u)u)
= (0, (c
2
u u)v + 2(v u)u + 2c(u v)).
506 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 506
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
And because q is a unit quat er nion, t hat is, q = (c, u) = (c, sN) wit h
c = cos() and s = sin(),
R
q
(p) = (0,(c
2
s
2
)v + (2s
2
(N v)N,) + 2sc(N v))
= (0, cos(2)v + (1 cos(2))(N v)N
+ sin(2)(N v))
R
q
can be int er p ret ed as t he r ot at ion of angle 2 arou nd t he axis
N = N
l
i + N
2
j + N
3
k. As a reciprocit y, Rot
(q,N)
is represent ed by t he
quat er nion
q = cos(/ 2) + sin(/ 2)N
N = N
l
i + N
2
j + N
3
k
Not es Not es Not es Not es Not es
R
qq
= R
q
R
q
R
qq
(p) = (qq)p(

q q )
= q(qp

q)

q
= R
q
(R
q
(p))
R
qq
= R
q
R
q
.
If we st at e t hat , t hen / 2 / 2 / 2, and t hen
q = cos(/ 2) sin(/ 2)N = cos(/ 2) + sin(/ 2)(N)
R
q
= Rot
(,N)
= Rot
(,N)
.
507 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 507
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
The algebr aic for mulat ion can be d irect ly used in comput ing t he d is-
placement of or t honor mal (or t hogonal and nor malized ) coord inat e sys-
t ems.
If we make an analogy bet ween t he point s P = [x, y, z] of a nonhomo-
geneous space wit h t he pure quat er nions p = xi + yj + zk, it is possible
t o r ep r esent t he d isp lacemen t s Tr
(U)
+ Rot
(,N)
of t h e sp ace wit h t h e
following funct ions:
p p = M
(u, r)
(p) = u + rp

r ,
wit h p, p, u being pure quat er nions, and r a unit quat er nion.
We can wr it e
Tr
(U)
= M
(u, 1)
: p = p + u
Rot
(,N)
= M
(0, r)
= M
(0, cos(/ 2) + sin(/ 2)N)
Id = M
(0, 1)
: Ident it y t ransform.
We can also d ene t he proper t y
M
(u, r)
M
(u,r)
(p) = u + r(u + rp

r)

r
M
(u, r)
M
(u,r)
(p) = (u + ru

r) + (rr)p(

r r )
M
(u, r)
M
(u,r)
(p) = M
(u + ru

r , rr)
.
We have d ened a mult iplicat ion * over t he element s (u, r):
(u, r) (u, r) = (u + ru

r, rr)
M
(u, r)
M
(u,r)
= M
(u, r)
M
(u,r)
.
We can now evaluat e d isplacement s in a 3D space using only algebr aic
element s (u, r).
508 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 508
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
Figur e 2. Movement from one or t honor mal base t o anot her.
By set t ing t he init ial coord inat e syst em t o Id, we can represent t he
movement s of or t honor mal bases (see Fig. 2).
R = R Tr
(V)
Rot
(, N)
(u, r) = (u, r) (v, cos(/ 2) + sin(/ 2)N
We can also represent ser ies of d isplacement s as explained in Fig. 3.
(u, r) = (u
0
, r
0
) (v
l
, s
l
) (v
2
, s
2
) . . . (v
n
, s
n
),
where (v
i
, s
i
) = (v
i
, cos(
i
/ 2) + sin(
i
/ 2)N) represent s t he t r ansfor ma-
t ion Tr
(V)
Rot
(, N)
.
Figur e 3. Ser ies of movement s from one or t honor mal base t o anot her.
509 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 509
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
Not es Not es Not es Not es Not es
Let R
0
and R
1
be t wo given or t honor mal bases. The t r ansfor mat ion from
R
0
t o R
1
has an algebr aic represent at ion:
(u
1
, r
1
) = (u
0
, r
0
) * (v, s)
(v, s) = (u
0
, r
0
)
1
(u
1
, r
1
)
(v, s) = (

r
0
u
0
r
0
,

r
0
)*(u
1
, r
1
)
(v, s) = (

r
0
(u
1
u
0
)r
0
,

r
0
, r
1
)
Al gor i t hmi c Impl ement at i on Al gor i t hmi c Impl ement at i on Al gor i t hmi c Impl ement at i on Al gor i t hmi c Impl ement at i on Al gor i t hmi c Impl ement at i on
The use of quat er nions is a good way of cod ing 3D t r ansfor mat ions. It
requires less space t han a 4 4 mat r ix (only 7 element s inst ead of 16)
and t he funct ions t hat are necessar y for t he implement at ion of a pre-
/ post -t r anslat ion or a pre-/ post -rot at ion are shor t and simple t o cod e.
The pseud o-cod e examples t hat follow have been implement ed in t he
PATK3D soft ware, and have alread y been present ed in a FORTRAN-like
cod e (Maillot , 1986). A C language ver sion is given in t he append ix
sect ion. In t he PATK3D p ackage, t he obser ver s p osit ion is init ially
[1,0,0], looking at t he p oint [0, 0, 0]. Ther e ar e t hree p r imit ives t hat
cont rol t he posit ion and visualizat ion d irect ion of t he obser ver.
The r st one set s t he obser ver at a given p osit ion, [x, y, z ], and
init ializes t he visualizat ion d irect ion t o be in t he X d ecreasing, t hat is,
looking in t he X decreasing direct ion.
Anot her pr imit ive can be used t o set t he visualizat ion d irect ion while
not changing t he obser ver posit ion. It only need s a point in 3D, which
is equivalent t o saying: look at t he point [x, y ,z].
The last pr imit ive is more gener al and gives t o t he PATK3D user t he
capabilit y of moving in t he 3D space using a d isplacement d escr ipt or.
510 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 510
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
This d escr ipt or is cod ed as a st r ing where t he user species a ser ies of
movemen t s su ch a s: forward 10, t urn right 20 degrees, pit ch 30
degrees, and so on.
Once all t he obser ver s movement s are d escr ibed by t he user, a 4 4
mat r ix is calculat ed using t he obser ver s r esult ing quat er nion. This mat r ix
is accumulat ed wit h t he ot her mat r ices of t he visualizat ion pipeline t o
prod uce t he nal mat r ix used in t he single mod el-coord inat e-t o-d evice-
coord inat e t r ansfor mat ion.
The funct ions present ed here d eal only wit h t he ker nel par t of t he
obser ver s posit ion and visualizat ion d irect ion pr imit ives. We d ene t he
following st ruct ures:
P: point [ 1.0, 0.0, 0.0] is used to keep trace of the observer s position. It
actually codes the orthonormal world coordinate base in respect to the
obser ver.
Q: array [0..3] of real [1.0, 0.0, 0.0, 0.0] is the quaternion itself. The rst
element, Q[0], keeps the real part of the quaternion, while the three other
elements represent the components of the pure part of the quaternion.
M: matrix4 is the matrix that will be calculated from P and Q. M will be
cod ed like t his:

M
1 tx ty tz
0 m
1,1
m
1, 2
m
1, 3
0 m
2,1
m
2, 2
m
2, 3
0 m
3,1
m
3, 2
m
3, 3

1
]
1
1
1
1
The set _obs_posit ion pr imit ive set s new values for t he obser ver s eyes
posit ion in t he space.
name: set _obs_posit ion(posit ion: poi nt)
begi n
Set t he values of t he eyes posit ion.
The posit ion here represent s t he posit ion of t he ort honormal base
in respect t o t he observer.
P posit ion;
Set t he visualizat ion t o be in t he decreasing x-axis.
Q [1.0, 0.0, 0.0, 0.0];
end;
511 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 511
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
P and Q (Fig. 4) have t he following values:
P: [ 2.0,0.0,0.0] ,
Q: [1.0,0.0,0.0,0.0].
The t ranslat e_quart ernion fu nct ion comp u t es t r anslat ion movement s
for a given quat er nion. i is t he axis of t he t r anslat ion, while x is t he
par amet er t hat char act er izes t he t r anslat ion for t he quat er nion. w should
be set t o 1 if t he obser ver moves in respect t o t he scene, or t o 1 if t he
scene moves in respect t o t he obser ver.
name: t r anslat e_quat er nion(x: real , i, w: i nteger)
begi n
j, k: i nteger;
A,B,D,E,F:real ;
Does t he observer move in respect t o t he scene?
i f w < 0 then P[i 1] P[i 1] x;
el se begi n
The scene moves in respect t o t he observer.
Comput e t he successor axis of i [1, 2, 3];
and t hen t he successor axis of j [1, 2, 3];
j i + 1;
i f j > 3 then j 1;
k j + 1;
i f k > 3 then k 1;
A Q[j]; B Q[k]; F Q[0]; E Q[i];
P[i 1] P[i 1] + x*(E*E + F*F A*A B*B);
D x + x;
P[j 1] P[j 1] + D*(E*A + F*B);
P[k 1] P[k 1] + D*(E*B + F*A);
end;
end;
512 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 512
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
Figure 4. A simple gr aphic scene, and t he user s scr een.
Figure 5. The result of a t r anslat ion of vect or [0, 0, 1].
513 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 513
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
P and Q (Fig. 5) get t he following values:
P: [ 2.0, 0.0, 1.0] ,
Q: [1.0, 0.0, 0.0, 0.0].
The rot at e_quat ernion fu nct ion comp u t es r ot at ion based movement s
for a given quaternion. i is the axis number of the rotation, while x and y
are t he par amet er s t hat char act er ize t he rot at ion for t he quat er nion. w
follows t he same rule as for t ranslat e_quat ernion. x and y are t ypically
t he cosine and sine of t he half-rot at ion angle.
name: rot at e_quat er nion(x, y: real , i , w: i nteger)
begi n
j, k: i nteger;
E, F, R1 : real ;
Comput e t he successor axis of i [1, 2, 3,] and j [1, 2, 3];
j i + 1;
i f j 3 then j 1;
k j + 1;
i f k > 3 then k 1;
E Q[i];
Q[i] E*x + w*y*Q[0];
Q[0] Q[0]*x w*y*E;
E Q[j];
Q[j] E*x + y*Q[k];
Q[k] Q[k]*x y*E;
i f w < 0 then begi n
Compute a new position if the observer moves in respect to the scene.
j j 1; k k 1;
R1 x*x y*y;
F 2.*x*y;
E P[j];
P[j] E*R1 + F*P[k];
P[k] P[k]*R F*E;
end;
end;
514 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 514
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
P and Q (Fig. 6) get t he following values:
P: [2.24, 0.0, 0.0],
Q: [0.97, 0.0, 0.23, 0.0].
The evaluat e_mat rix p r imit ive (r e)comp u t es t he mat r ix cor r esp ond -
ing t o t he obser ver s posit ion and visualizat ion d irect ion given by P and
Q. The met hod present ed here is t he d irect applicat ion of t he mat hemat i-
cal for mulae. Fast er ways t o evaluat e t he mat r ix can be found .
name: evaluat e_mat r ix()
begi n
e, f: real ;
r :array [0..3] of real ;
i, j, k: i nteger;
We will need some square values!
for i: i nteger 0, i < 4 do
r [i] Q[i]*Q[i];
i i + l;
endl oop;
Comput e each element of t he mat rix.
j is t he successor of i (in 1, 2, 3), where k is t he successor of j.
for i: i nteger 1, i < 4 dO
j i + l;
i f j > 3 then j 1;
k j + l;
i f k > 3 then k 1;
e 2.*Q[i]*Q[j];
f 2.*Q[k]*Q[0];
M[j][i] e f;
M[i][j] e + f;
M[i][i] r [i] + r [0] r [j] r [k];
M[0][i] P[i 1];
M[i][0] 0.0;
endl oop;
M[0][0] 1.0;
end;
515 GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 515
X.3 USING QUATERNIONS FOR CODING 3D TRANSFORMATIONS
Figure 6. The result of a r ot at ion angle 26.57 d egrees around t he yaxis t o look at t he
point [0, 0, 0].
As an example, t he mat r ix result ing from P and Q values of Fig. 6 is
given below.

M
1.0 2.24 0.0 0.0
0.0 0.89 0.0 0.45
0.0 0.0 1.0 0.0
0.0 0.45 0.0 0.89

1
]
1
1
1
See also Rot at i on Mat r i x Met h od s Su mmar y (455); Bi t Pat t er n s
for En cod i n g An gl es (442); Fa st 2D3D Rot a t i on (440); Fi xed -
Point Tr igonomet r y wit h CORDIC It er at ions (494)
See Append ix 2 for C Implement at ion (775)
516
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 516
X. 4 X. 4 X. 4 X. 4 X. 4
3 33 33 D VIEWING AND D VIEWING AND D VIEWING AND D VIEWING AND D VIEWING AND
R RR RROTATION USING OTATION USING OTATION USING OTATION USING OTATION USING
O OO OORTHONORMAL BASES RTHONORMAL BASES RTHONORMAL BASES RTHONORMAL BASES RTHONORMAL BASES
St eve Cunningham
Calif ornia St at e Universit y, St anislaus
Tur lock, Calif or nia
This not e d iscusses t he gener al viewing t r ansfor mat ion and rot at ions
about a gener al line. It cont r ast s t he common t ext book approach t o t his
p r oblem wit h an ap p roach based on or t honor mal bases from linear
algebr a and shows t hat a st r aight for war d p iece of mat hemat ics can
improve t he implement at ions of viewing and rot at ion t r ansfor mat ions in
act ual wor k.
The common ap p roach t o viewing and gener al r ot at ion op er at ions
t reat s t hese as a t r anslat ion followed by a sequence of rot at ions about t he
coord inat e axes in mod el space, and seems t o go back t o Newman and
Sproull (1979). The viewing t r ansfor mat ion requires t hree of t hese rot a-
t ions, whereas t he gener al rot at ion requires four. The ent r ies in t hese
rot at ion mat r ices are t r igonomet r ic funct ions of angles which are not
read ily seen, t hough t he act ual comput at ions r arely use t he t r ig funct ions;
t he ent r ies are comput ed from t he component s of vect or s d er ived from
t he t r anslat ed eye vect or as t he rot at ions proceed . See Newman and
Sproull for more d et ails; t his same approach has been used in Hear n and
Baker (1986) and ot her books. Some books, such as Foley and Van Dam
(1982) d iscuss a (U, V, N) viewplane coord inat e syst em such as we build
below, but st ill use rot at ions t o build t he act ual viewing t r ansfor mat ion.
The ap p roach we su ggest was d evelop ed ind ep end ent ly bu t is not
or iginal t o t his not e. It appear s in Berger (1986), where it seems a bit
obscure, as well as in Salmon and Slat er (1987), where it is d eveloped
more for mally, but is not wid ely known. No gr aphics t ext s seem t o use
t his approach t o gener al rot at ions in 3-space. The approach is st r aight for -
ward : aft er t he usual t r anslat ion of t he view reference point t o t he or igin,
517
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 517
use t he eye point and up point in t he viewing infor mat ion t o comput e an
or t honor mal basis (U, V, N) for 3-space for which t he eye vect or is one
component (t he z-axis analogue) and t he up vect or is project ed ont o
anot her (t he y-axis analogue). Then t he viewing t r ansfor mat ion is simply
a change of basis and it s mat r ix is d irect ly wr it t en from t he or t honor mal
(U, V, N) basis. The gener al rot at ion is much t he same, wit h t he up vect or
t aken r and omly, and t he d esired rot at ion applied aft er t he init ial viewing
t r ansfor mat ion; t he inver se of t he viewing t r ansfor mat ion is t hen applied .
The ad vant ages of t he or t honor mal basis approach are t wofold : t here is
a logical basis for t he ap p roach, which comes nat u r ally from linear
algebr a, and t he comput at ion involves fewer st eps. I have found t hat
st ud ent s can apply t hese id eas in t heir own cod e more quickly and
accur at ely t han t hey can using t he t r ad it ional approach.
The New Appr oach The New Appr oach The New Appr oach The New Appr oach The New Appr oach
Consid er a st and ard set up for 3D viewing (t here are var iat ions, but t his
uses st and ard infor mat ion common t o t hem all):
A view reference point VRP
An eye point EP
An up point UP
From t hese we comput e t wo vect or s:
An eye vect or EV as t he vect or from VRP t o EP
An up vect or UV as t he vect or from VRP t o UP,
and we have t he sit uat ion shown in Fig. 1.
The new process st ar t s in t he same way as t he st and ard process: by
d ening a t r anslat ion t o move t he view reference point t o t he or igin. This
518
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 518
Figure 1. A st and ard viewing environment .
has t he st and ard mat r ix for m
T
0
=

1 0 0 0
0 1 0 0
0 0 1 0
xv yv zv 0

1
]
1
1
1
1
Next comp u t e t he or t honor mal basis for t he sp ace as seen by t he
viewer, as follows:
1. Nor malize EV and call t he result N.
2. Nor ma l i z e UV a n d con t i n u e t o ca l l t h e r esu l t UV.
3. Comput e V
l
or t hogonal t o N by set t ing V
l
= UV (N UV)N, (as
shown in Fig. 2).
4. Nor malize V
l
and call t he result V.
5. Comput e t he cross prod uct U = V

N.
This creat es a new coord inat e syst em wit hin t he or iginal mod el space
t hat represent s t he coord inat es of t he d esired viewing space. This coord i-
nat e syst em is shown in Fig. 3.
519
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 519
Figure 2. Or t hogonalizing UV and N.
Then t he change of basis from (X, Y, Z)-sp ace t o (U, V, N)-sp ace
(assuming t hat mat r ices mult iply on t he r ight of t heir oper and s) has U as
it s r st row, V as it s second row, and N as it s t hird row. The inver se of
t his mat r ix is t he t r ansfor mat ion from (U, V, N)-space t o (X, Y, Z)-space
and provid es t he const ruct ion of t he rot at ion par t of t he mat r ix M of t he
viewing t r ansfor mat ion. Since U, V, and N are or t honor mal, t his inver se
is t he t r anspose of t he mat r ix above; t he r st t hree rows of M have U as
t he r st column, V as t he second column, N as t he t hird column, and t he
rest are zero. Finally, t he viewing t r ansfor mat ion is T
0
M. It is easier t o
comput e t han t he st and ard t r ansfor mat ion and is as efcient t o apply (if
all t hree rot at ions are comput ed and mult iplied t oget her ) or more efcient
Figure 3. The UVN coord inat e.
520
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 520
(if t he rot at ions are applied separ at ely). The act ual mat r ix of t his t r ansfor-
mat ion is t he out put of t he BuildViewTransform funct ion below.
Rot at i ons about a Gener al Li ne Rot at i ons about a Gener al Li ne Rot at i ons about a Gener al Li ne Rot at i ons about a Gener al Li ne Rot at i ons about a Gener al Li ne
The usual approach t o a rot at ion by an angle about a gener al line is t o
move t he line so it goes t hrough t he or igin (a t r anslat ion), rot at e t he line
int o t he XZ-plane, rot at e t he resu lt ing line t o align wit h t he z-axis,
per for m t he d esired rot at ion by now, rever se t he t wo ear lier rot at ions,
and t hen rever se t he t r anslat ion. This requires ve rot at ions and t wo
t r anslat ions, and suffer s from t he same d ifcult ies as t he viewing t r ansfor-
mat ion: t he angles are not easy t o see and t he comput at ions are obscure.
The approach above t o t he viewing t r ansfor mat ion ext end s easily t o
t h ese r ot at i on s. Th e l i n e i s assu med t o be gi ven by a p oi n t P =
(xp, yp, zp) and a d irect ion vect or D = A,B,C. Then t he plane per-
pend icular t o t he line at t he given point has equat ion
A(x xp) + B(y yp) + C(z zp) = 0.
Let T be t he t r anslat ion t hat moves P t o t he or igin. Pick any point
Q = (x,y,z ) in t he p lane and let UP = (x xp, y yp, z zp), as
shown in Fig. 4. Let N be t he result when D is nor malized , and let V be
Figure 4. The set up for gener al rot at ions.
521
X.4 3D VIEWING AND ROTATION USING ORTHONORMAL BASES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 521
.
append ix) if t he last t r anslat ion st ep is omit t ed .
Build ViewTr ansfor m(VRP, EP, UP, T)
Input : point s VRP, EP, UP as in t he t ext
Out put : t r ansfor mat ion mat r ix T
Comput e vect or N EP VRP and nor malize N
Comput e vect or V VP VRP
Make vect or V or t hogonal t o N and nor malize V
Comput e vect or U V N (cross prod uct )
Wr it e t he vect or s U, V, and N as t he r st t hree rows of t he
r st , second , and t hird columns of T, respect ively,
Comput e t he four t h row of T t o includ e t he t r anslat ion of
VRP t o t he or igin
See Append ix 2 for C Implement at ion (778)
t he result when UP is nor malized . Then comput e U as in t he viewing
t r ansfor mat ion t o complet e t he (U, V, N) t r iple, and build t he change-of-
basis mat r ix M wit h U, V, and N as t he r st , second , and t hird columns,
respect ively.
Now let R be t he mat r ix of t he rot at ion by about t he z-axis, and let
N and S be t he (t r ivially comput ed ) inver ses of M and T, respect ively.
Then t he mat r ix of t he rot at ion by Q about t he line is TMRNS, which
requires fewer mat r ices and less comput at ion t han t he t r ad it ional met hod
of r ot at ion abou t t he coor d inat e axes. Ther e is anot her benet for
st ud ent s and for oat ing-point speed : it uses no t r igonomet r ic funct ions
except t hose in t he mat r ix R.
Pseudo- Code f or Const r uct i ng t he Pseudo- Code f or Const r uct i ng t he Pseudo- Code f or Const r uct i ng t he Pseudo- Code f or Const r uct i ng t he Pseudo- Code f or Const r uct i ng t he
Vi ewi ng Tr ansf or mat i on Vi ewi ng Tr ansf or mat i on Vi ewi ng Tr ansf or mat i on Vi ewi ng Tr ansf or mat i on Vi ewi ng Tr ansf or mat i on
The following pseud o-cod e prod uces t he mat r ix for t he viewing t r ansfor-
mat ion wit h t he t hree st and ard view specicat ion point s as input . It is
expand ed int o act ual C cod e in an append ix. No pseud o-cod e is given for
t he gener al rot at ion, since t he mat r ices M and N of t he previous sect ion
are easily comput ed by t he pseud o-cod e above (and t he cod e in t he
522
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 522
X. 5 X. 5 X. 5 X. 5 X. 5
T TT TTHE USE OF HE USE OF HE USE OF HE USE OF HE USE OF
C CC CCOORDINATE FRAMES IN OORDINATE FRAMES IN OORDINATE FRAMES IN OORDINATE FRAMES IN OORDINATE FRAMES IN
C CC CCOMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS OMPUTER GRAPHICS
Ken Turkowski
Apple Comput er
Cupert ino, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Coord inat es have no meaning wit hout reference t o t heir cor respond ing
basis vect or s (and or igin). When we express one set of basis vect or s (and
or igin) wit h respect t o anot her, we est ablish a new coordinat e frame in
t er ms of t he old . This coord inat e fr ame can also be used t o t r ansfor m t he
coord inat es in one fr ame t o t hose of anot her.
We will see t hat coor d inat e fr ames ar e easy t o const ru ct and can
simplify t he const ruct ion of coord inat e t r ansfor mat ions wit hout t he ex-
plicit use of t r igonomet r y.
Vect or s and Poi nt s Vect or s and Poi nt s Vect or s and Poi nt s Vect or s and Poi nt s Vect or s and Poi nt s
We will need t o d ist inguish bet ween 3D vect or s and 3D point s. A vect or
has a magnit ud e and a d irect ion, but d oes not have a locat ion, whereas a
point has only a locat ion, and no magnit ud e or d irect ion. A vect or cannot
be moved , but it can be scaled and rot at ed . A point cannot be scaled or
rot at ed , alt hough it can be moved ; a group of point s can, however, be
rot at ed or moved relat ive t o each ot her. A linear t r ansfor mat ion is
appropr iat e for vect or s, whereas an afne t r ansfor mat ion is appropr iat e
for point s. There is a unique or igin in a vect or space, but an or igin is
ar bit r ar y in an afne (point ) space. These proper t ies are summar ized in
523
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 523
When point s and vect or s are represent ed by t hree component s, t hey
can be d ist inguished only by cont ext . We may somet imes represent t hem
by four coord inat es (x, y, z, w), in what is called homogeneous coordi-
nat es. We int er pret a 4-vect or as a point in 3D by it s project ion ont o t he
hyp er p lane w = 1, t hat is, (x/w, y/w, z/w). The ind et er minat e for m at
w = 0 is resolved by t aking t he limit as w approaches 0 from above: t he
p oint ap p r oaches innit y in a p ar t icu lar d ir ect ion; hence t he vect or
int er pret at ion.
By convent ion, we will represent point s as homogeneous 4-vect or s wit h
w = 1 whenever possible. For example, we represent t he point 1 unit
along t he x-axis as
[1 0 0 1],
whereas t he x-axis (a vect or ) it self is represent ed as
[1 0 0 0].
In nonhomogeneous coord inat es, t hey are bot h represent ed as
[1 0 0],
so only cont ext can d ist inguish t hem.
t he t able below:
At t r ibut e Vect or Point
represent s magnit ud e and d irect ion locat ion
or igin unique ar bit r ar y
t r ansfor mat ion linear affine
scale move
rot at e
524
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 524
Coor di nat e Fr ames Coor di nat e Fr ames Coor di nat e Fr ames Coor di nat e Fr ames Coor di nat e Fr ames
We shall u su ally r ep resent a coor d inat e fr ame for t hree-d imensional
point s wit h a 4 3 mat r ix:


F
X
Y
Z
O

1
]
1
1
1
1

X
x
X
y
X
z
Y
x
Y
y
Y
z
Z
x
Z
y
Z
z
O
x
O
y
O
z

1
]
1
1
1
1
.
This est ablishes a local reference fr ame wit hin a more global fr ame by
represent ing t he local or igin and t he x-, y-, and z-axes in t er ms of t he
global coord inat es in t he rows of t he mat r ix. In par t icular,
O = [ O
x
O
y
O
z
] ,
(2)
is a p oint t hat r ep r esent s t he or igin of t he local coor d inat e fr ame,
r ep r esen t ed i n t h e coor d i n a t es of t h e gl oba l r efer en ce fr a me.
The local x-axis,
X = [ X
x
X
y
X
z
] , (3)
is a vect or (not a point ), wit h bot h magnit ud e and d irect ion.
Similar sor t s of int er pret at ions are appropr iat e for t he y- and z-axes.
There is no r equ ir ement t hat t he vect or s X, Y, and Z be mu t u ally
or t hogonal, alt hough t hey should be linear ly ind epend ent .
The mat r ix represent at ion of t his coord inat e fr ame is more t han just a
convenient represent at ion; it is in fact relat ed t o t he more familiar 4 4
gr aphics t r ansfor mat ion mat r ix (Newman and Sproull, 1979):


F
4

0
F 0
0
1

1
]
1
1
1
1

X 0
Y 0
Z 0
0 1

1
]
1
1
1
1

X
x
X
y
X
z
0
Y
x
Y
y
Y
z
0
Z
x
Z
y
Z
z
0
O
x
O
y
O
z
1

1
]
1
1
1
1
,
(4)
where t he 0' s and 1 in t he r ight column und er score t he int er pret at ion
X, Y, and Z as vect or s, and O as a point .
(1)
525
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 525
A 4-vect or wit h w = 0, will not be affect ed by t he t r anslat ion por t ion
(bot t om row) of a 4 4 mat r ix t r ansfor mat ion, whereas t he 4-vect or wit h
w 0 will.
We illust r at e t he effect of a coord inat e t r ansfor mat ion on t he homoge-
neous represent at ion of t he x-axis (a vect or ) wit h a 4 4 mat r ix mult i-
plicat ion:


1 0 0 0
[ ]

X
x
X
y
X
z
0
Y
x
Y
y
Y
z
0
Z
x
Z
y
Z
z
0
O
x
O
y
O
z
1

1
]
1
1
1
1
X
x
X
y
X
z
0
[ ]
.
Wit h t he four t h component zero, we see t hat a vect or t r ansfor ms int o a
vect or, and not just any vect or : t he x-axis t r ansfor ms int o t he vect or
[X
x
X
y
X
z
0], t he t op row of t he t r ansfor mat ion mat r ix. It is easy t o
see t hat t he y-and z-axes t r ansfor m int o t he second and t hird rows of t he
mat r ix, respect ively.
The t r ansfor mat ion of t he or igin (a point ) yield s


0 0 0 1
[ ]

X
x
X
y
X
z
0
Y
x
Y
y
Y
z
0
Z
x
Z
y
Z
z
0
O
x
O
y
O
z
1

1
]
1
1
1
1
O
x
O
y
O
z
1
[ ]
,
t he bot t om row of t he mat r ix. This is consist ent wit h t he d efinit ion we
gave it ear lier.
Since t he t r ansfor mat ion of an ar bit r ar y vect or


V
x
V
y
V
z
0
[ ]

X
x
X
y
X
z
0
Y
x
Y
y
Y
z
0
Z
x
Z
y
Z
z
0
O
x
O
y
O
z
1

1
]
1
1
1
1
=[V
x
X
x
+ V
y
Y
x
+ V
z
Z
x
V
x
X
y
+ V
y
Y
y
+ V
z
Z
y
V
x
X
z
+ V
y
Y
z
+ V
z
Z
z
0] (7)
(6)
(5)
526
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 526
d oesnt d epend at all on t he last row (t r anslat ion), we will gener ally omit
it , as well as t he last column, t o obt ain t he familiar 3 3 linear t r ansfor-
mat ion mat r ix for 3D vect or s:

V
x
V
y
V
z [ ]

X
x
X
y
X
z
Y
x
Y
y
Y
z
Z
x
Z
y
Z
z

1
]
1
1
1
= [V
x
X
x
+ V
y
Y
x
+ V
z
Z
x
V
x
X
y
+ V
y
Y
y
+ V
z
Z
y
V
x
X
z
+ V
y
Y
z
+ V
z
Z
z
]
(8)
We will u se t his as t he int r insic coord inat e fr ame for 3D vect or s,
whereas t he 4

3 mat r ix (see Eq. 1) will be u sed as t he int r insic


coord inat e fr ame for 3D point s, where we ext end t he oper at ions of linear
algebr a t o afne algebr a as follows:


P
x
P
y
P
z [ ]

X
x
X
y
X
z
Y
x
Y
y
Y
z
Z
x
Z
y
Z
z
O
x
O
y
O
z

1
]
1
1
1
1
= [P
x
X
x
+ P
y
Y
x
+ P
z
Z
x
+ O
x
P
x
X
y
+ P
y
Y
y
+P
z
Z
y
+O
y
P
x
X
z
+P
y
Y
z
+ P
z
Z
z
+ O
z
]. (9)
This d enit ion is consist ent wit h t he t reat ment of 3D point s as homoge-
neous 4-vect or s, as in Eq. 6.
We now show some examples of how t he use of coord inat e fr ames
can simplify t remend ously problems in comput er gr aphics.
527
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 527
Exampl es: Usi ng Coor di nat e Fr ames Exampl es: Usi ng Coor di nat e Fr ames Exampl es: Usi ng Coor di nat e Fr ames Exampl es: Usi ng Coor di nat e Fr ames Exampl es: Usi ng Coor di nat e Fr ames
t o Sol ve Pr obl ems t o Sol ve Pr obl ems t o Sol ve Pr obl ems t o Sol ve Pr obl ems t o Sol ve Pr obl ems
Exampl e 1: Find t he simple plane rot at ion t hat aligns t he x-axis t o t he
y-axis.
If t he x-axis [1, 0] rot at es int o t he y-axis [0, 1], t he y-axis rot at es int o
t he negat ive x-axis [ 1, 0]. Therefore, t he d esired t r ansfor mat ion is

R
90

X
Y

1
]
1

X
x
X
y
Y
x
Y
y

1
]
1

0 1
1 0

1
]
1 (10)
Exampl e 2: Find t he simple plane rot at ion t hat aligns t he x-axis t o t he
d irect ion [1, 1].
From t he previous example, we know t hat t he new y-axis must be
aligned wit h [1 1]R
90
= [1 1], so t hat t he d esired mat r ix is


R
45

X
Y

1
]
1

1
2
1 1
1 1

1
]
1
.
The nor malizat ion fact or come about because t he Euclid ean nor m of
each of t he t wo rows is

2 . Wit hout t his, t he t r ansfor mat ion would
enlarge vect or s as well as rot at e t hem.
Exampl e 3: Find t he simple plane rot at ion t hat rot at es an ar bit r ar y
nor malized vect or V int o anot her nor malized vect or W.
We approach t his by r st rot at ing V t o align it t o t he x-axis, t hen
rot at ing it t o align it t o W. In ord er t o d et er mine t he r st rot at ion, it is
easier t o specify t he rot at ion from t he x-axis t o V, and t hen inver t it .
Rot at ing t he x-axis t o V is accomplished by t he mat r ix


R
V

V
VR
90

1
]
1

V
x
V
y
V
y
V
x

1
]
1
.
Since t his is an or t hogonal mat r ix (all pur e rot at ions are or t hogonal), it s
528
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 528
(11)

1
4

M =
N V
N V

0 1 1
[ ]
1 1 1
[ ]
0 1 1
[ ]
1 1 1
[ ]

1
6
2 1 1
[ ]
,
inver se is equal t o it s t r anspose:


R
V
1

V
VR
90

1
]
1
1
T

V
x
V
y
V
y
V
x

1
]
1
.
The rot at ion from t he x-axis t o W is found similar y:


R
W

W
WR
90

1
]
1

W
x
W
y
W
y
W
x

1
]
1
.
Therefore, t he d esired t r ansfor mat ion is t he concat enat ion

R
VW
R
V
1
R
W

V
VR
90

1
]
1
T
W
WR
90

1
]
1

V
x
V
y
V
y
V
x

1
]
1

V
x
V
y
W
y
W
x

1
]
1

V
x
W
x
+ V
y
W
y
V
x
W
y
V
y
W
x
(V
x
W
y
V
y
W
x
) V
x
W
x
+ V
y
W
y

1
]
1
Exampl e 4: Find t he skewing t r ansfor mat ion suit able for it alicizing
let t er s by

1
4 unit in x for ever y unit in y.
We basically just want t o remap t he y-axis t o [

1
4
1] while t he x-axis
remains t he same:


K
1 0
1

1
]
1
.
Exampl e 5: Find t he rot at ion t hat t akes t he vect or (1/

3 )[1 1 1] ont o
t he x-axis, t hrough t he plane t hat cont ains t hem bot h.
The axis of rot at ion can be obt ained as

N =
V X
N V

1 1 1
[ ]
1 0 0
[ ]
1 1 1
[ ]
1 0 0
[ ]

1
2
0 1 1
[ ]
,
where X is t he x-axis. An or t hogonal t hird vect or can be obt ained by
crossing t his wit h t he given vect or :
529
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 529


R
z 90

0 1 0
1 0 0
0 0 1

1
]
1
1
1
1
1
(12)
These t hree vect or s t hen make up an or t honor mal coord inat e fr ame:


Q =
V
M
N

1
]
1
1

1
3
1
3
1
3
2
6

1
6

1
6
0
1
2

1
2

1
]
1
1
1
1
1
1
.
This t r ansfor mat ion t akes t he x-axis ont o t he vect or (1/

3
)[1 1 1],
and t akes t he z-axis ont o t he axis of rot at ion. In ord er t o simplify t he
rot at ion, we would like t o have t he inver se of t his, namely, t o t r ansfor m t he
axis of rot at ion ont o t he z-axis. Since Q is an or t hogonal mat r ix, it s
inver se is simply t he t r anspose:


Q
1
Q
T

1
3
2
6
0
1
3

1
6
1
2
1
3
1
6

1
2

1
]
1
1
1
1
1
1
.
It can be ver ied t hat t he vect or (1/

3
)[1 1 1] maps ont o t he x-axis,
t hat t he axis of rot at ion, (1/

2 )[0 1 1], maps ont o t he z-axis, and
t hat t he x-axis maps ont o


X'
1
3
2
6
0

1
]
1
.
A simple rot at ion about t he z-axis in t his fr ame would have t he y-axis
map int o


Y' X' R
z 90
=
2
6
1
3
0

1
]
1
,
where
530
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 530
is t he 3D analog of Eq. 10, and rot at es t he x-axis ont o t he y-axis around
t he z-axis. Put t ing t his t oget her int o a t r ansfor mat ion, we have


R =
1
3
2
6
0

2
6
1
3
0
0
0 1

1
]
1
1
1
1
1
1
We t hen need t o go back t o our or iginal fr ame by using Q; t he composit e
t r ansfor mat ion is

T = Q
T
RQ
1
3
2
6
0
1
3

1
6
1
2
1
3

1
6

1
2

1
]
1
1
1
1
1
1

1
3
2
6
0

2
6
1
3
0
0
0 1

1
]
1
1
1
1
1
1



1
3
1
3
1
3
2
6

1
6

1
6
0
1
2

1
2

1
]
1
1
1
1
1
1
1



1
3

1
3

1
3
1
3
1
2 3
+
1
2
1
2 3

1
2
1
3
1
2 3

1
2
1
2 3
+
1
2

1
]
1
1
1
1
1
1
,
.
531
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 531
a somewhat for mid able expression. No t r igonomet r y per se has been used
for t his d er ivat iononly cross prod uct s, vect or nor malizat ion, and mat r ix
mult iplicat ion. We gener alize t his in t he next example.
Exampl e 6: Find t he rot at ion t hat t akes an ar bit r ar y nor malized vect or V
t o anot her nor malized vect or W, t hrough t he plane t hat cont ains t hem
bot h.
Gener alizing our exper ience wit h Example 3 and Example 5, we have
t he mat r ix t hat t r ansfor ms V ont o t he x-axis, and t he axis of t he plane of
rot at ion ont o t he z-axis:

Q
T

V
M
N

1
]
1
1
T
,
where

N
V W
V W
is t he axis of rot at ion, and

M
N V
N V
is t he t hird vect or t hat complet es a d ext r al or t hogonal basis. The image of
W in Q
T
is:
W = WQ
T
. (16)
The t r ansfor mat ion t hat rot at es t he x-axis (t hat is, t he image of V) ont o
W (t he image of W)is

R
W
W R
z90
Z

1
]
1
1
,
where
Z = [0 0 1]
(13)
(14)
(15)
(17)
532
X.5 THE USE OF COORDINATE FRAMES IN COMPUTER GRAPHICS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 532
is t he z-axis. The d esired t r ansfor mat ion is t hen
R
v w
= Q
T
RQ. (18)
Exampl e 7: For a gener al t r ansfor mat ion on a set of point s, suppose t hat
we would like t o scale t he x-axis by 2, t he y-axis by 3, t he z-axis by 4,
and we want t o reor ient t he object d escr ibed in t er ms of t hose point s so
t hat t he new x-axis point s in t he d irect ion (1/

3 )[1 1 1], t he y-axis
point s in t he d irect ion [1 0 0], and t he z-axis point s in t he d irect ion
(1

2 )[0 1 1], a n d fu r t h er, t h a t t h e wh ol e object be sh i ft ed i n
posit ion by [10 20 27].
Just copying t hese specicat ions int o a 4 3 mat r ix, we get


T =
2
3
2
3
2
3
3 0 0
0
4
2

4
2
10 20 27

1
]
1
1
1
1
1
1
.
Not e t hat t his t r ansfor mat ion is more complex t han ot her s t hat we have
encount ered before, and would be vir t ually impossible t o d escr ibe in
t er ms of element ar y rot at ion, scaling, skewing, and t r anslat ion oper a-
t ions, yet it was ext remely simple t o d escr ibe and implement in t er ms of a
coord inat e fr ame.
533
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 533
X. 6 X. 6 X. 6 X. 6 X. 6
F FF FFORMS ORMS ORMS ORMS ORMS, ,, ,,
V VV VVECTORS, AND ECTORS, AND ECTORS, AND ECTORS, AND ECTORS, AND
T TT TTRANSFORMS RANSFORMS RANSFORMS RANSFORMS RANSFORMS
Bob Wallis
Weit ek Corporat ion
Sunnyvale, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
The mod er n concept of a t ensor as a linear machine t hat cont r act s for ms
wit h vect or s t o prod uce scalar s is an excellent ment al mod el for d ealing
wit h cer t ain t ypes of gr aphics problems. It helps t o eliminat e much of t he
confu sion t hat exist s in d ealing wit h qu est ions su ch as how su r face
nor mals, Bzier cont rol point s, and so on, are affect ed by geomet r ical
t r ansfor mat ions.
Many gr aphics progr ammer s t end t o get confused when at t empt ing t o
per for m oper at ions such as t he rend er ing of bicubic pat ches, light ing
calculat ions, and so for t h, in d ifferent coord inat e syst ems. The following
is a t ut or ial t hat d escr ibes a way of t hinking about mat hemat ical quant i-
t ies, su ch as polynomial coefcient s and su r face nor mals, which t he
aut hor has found useful in problems involving coord inat e syst em t r ansfor-
mat ions. The key point is t hat all arrays of numbers are not necessar-
ily vect ors.
The Moder n Vi ew of a Tensor The Moder n Vi ew of a Tensor The Moder n Vi ew of a Tensor The Moder n Vi ew of a Tensor The Moder n Vi ew of a Tensor
Old er books on t ensor s seem obsessed wit h t heir t r ansfor mat ion proper-
t ies, classifying t hem as covar iant and cont r avar iant . The mod er n concept
(Misner et al., 1973) is t hat a t ensor is sor t of a d ot prod uct machine t hat
accept s as input s t wo d ifferent t ypes of mat hemat ical quant it ies, vect or s
and for ms. It cont r act s t hese via an inner prod uct t o prod uce a scalar
534
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 534
quant it y, which should be invar iant t o coord inat e syst em changes. While
t he full-blown gener alit y of mod er n d ifferent ial geomet r y is d enit ely
over kill for d ealing wit h simple gr aphics problems, t he concept of sepa-
r at ing for ms and vect or s is ver y helpful in avoid ing mat hemat ical blun-
d er s when d ealing wit h coord inat e syst em t r ansfor mat ions. For example,
in evaluat ing t he homogeneous line equat ion
p = x + y + = [x y 1] point on line if p = 0
= w
j
; j = 1,2,3 1-form of plane equat ion coefcient s
[x y 1] = v
j
; j = 1,2,3 vect or, (1)
It is impor t ant t o recognize t hat t he sur face nor mal, or ar r ay of plane
equat ion coefcient s (Greek let t er s) is a d ifferent sor t of mat hemat ical
object t han t he [x y 1] (Roman let t er s) vect or. When we change coord i-
nat e syst ems, t he vit al t hing t o remember is t hat t he scalar out put of t he
t ensor, p in Eq. l, should remain invar iant . Different d isciplines have
d ifferent not at ions for segregat ing for ms and vect or s. Quant um mechan-
ics u ses t he br a-ket not at ion; some t ensor books u se su bscr ip t s and
super scr ipt s. Most gr aphics progr ammer s use column vect or s and row
vect or s, but in t he nomenclat ure of t his paper, what is nor mally called a
column vect or really isnt a vect or at all, but a 1-for m. For most pr act ical
problems in gr aphics, it is sufcient t o use convent ional subscr ipt not a-
t ion. However, one must be ver y careful about which ind ex is summed
over which. For example, expressing Eq. 1 in ind ex not at ion we have
p =

w
j
v
j

w
i
I
ij
v
j
w
i
T
ij
1
( )
T
jk
v
k
( )
p =

w
j
v
j
invariant in primed coords
v
j
=

T
jk
v
k
how t o t ransform vect or
w
j
=

w
i
T
ij
1
how t o t ransform 1-form.
In t he above, I is t he id ent it y t r ansfor m (Kroneker s d elt a), and t here is
an implied summat ion over any repeat ed ind ex. In most lit er at ure on
535
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 535
t ensor s, t he summat ion sign is also implied . The ad vant age of wr it ing
ever yt hing d own wit h ind ex not at ion (inst ead of row and column vect or s,
for inst ance) is t hat it ext end s easily t o higher-d imensional problems,
minimizes t he likelihood of blund er s such as an inappropr iat e mat r ix
t r ansp ose, and can be t r anslat ed d ir ect ly t o nest ed for loop s when
cod ing up t he result .
Tr i angul ar Int er pol ant s Tr i angul ar Int er pol ant s Tr i angul ar Int er pol ant s Tr i angul ar Int er pol ant s Tr i angul ar Int er pol ant s
As a fur t her example of t he d ifference bet ween 1-for ms and vect or s,
consid er t he problem of linear int er polat ion wit hin a t r iangle. This t ypi-
cally comes up in t iling sur faces t hat have been t esellat ed int o t r iangles.
Here, it becomes necessar y t o int er polat e z-d ept h and / or int ensit y at
each pixel wit hin t he t r iangle. If t he int er polat or is viewed as a cont r ac-
t ion bet ween a group of t hree for ms and a d ual group of t hree vect or s, it
is possible t o wr it e d own t he cor rect answer by inspect ion.
Figure 1 illust r at es one way of viewing 1-for ms. Consid er t he d ensit y of
a 1-for m at a par t icular point x, y to be t he scalar you get when you
Figure 1.
536
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 536
cont r act it wit h [x, y, 1]. These are ind icat ed in t he gure as waves of
increasing d ensit y (d ar kness), which sweep across t he t r iangle whose
ver t ices (in t his example) are at [6, 11], [17, 6], [2, 1]. The 1-for ms are t he
d uals of t he basis vect or s for t he t r iangle, and as such, t he it h 1-for m
st ar t s wit h a d ensit y of 0, and hit s a d ensit y of exact ly 1 when it rolls over
t he it h ver t ex of t he t r iangle. It is 0 at t he ot her t wo ver t ices. This is
really just a fancy way of saying t hat t he ver t ices of t he t r iangle and it s
associat ed 1-for ms are inver ses of one anot her. That is, inver t t he mat r ix
whose rows are t he t r iangles ver t ices, and you get a mat r ix whose
columns are t he d ual l-for ms.
v
ij
=

6 11 1
17 6 1
2 1 1

1
]
1
1

ij
= (1/ 130)

5 10 5
15 4 11
5 16 151

1
]
1
1
I
ik
=

w
ij
v
jk
Ort hogonalit y
w
ij
= v
ij
1
Inverse of 3 3 mat rix
These 3 for ms (which always sum t o 0, 0, 1) are precisely t he blend -
ing funct ions required for our int er polat or. All one has t o d o is weight t he
it h for m by t he value we would like t o have at t he it h ver t ex of t he
t r iangle, and ad d t he blend ing funct ions t oget her :
z
i
desired value at it h vert ex

i
=

ij
z
j
form t o int erpolat e [x, y, 1].
The problem could have been red uced t o t wo d imensions by locat ing
t he coord inat e syst em or igin at one of t he t r iangles ver t ices, but t he
geomet r ical int er pret at ion given here would be t he same in eit her case.
The above int er polat or is somet imes seen und er t he name of barycent ric
coordinat es.
537
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 537
The d et er minant of t he v mat r ix t ells us t he hand ed ness and area of t he
t r iangle. If a 3D t r iangle is viewed near ly ed ge-on, t he 2D project ion
becomes a sliver, and t he d et er minant approaches 0. This would seem t o
cause numer ical r ange problems for t he int er polat or, but if it is imple-
ment ed wit h int eger mat h, and it s out put is ignored for point s out sid e t he
t r iangle, 2s complement over ows and und er ows d ont mat t er. How-
ever, if t he d et er minant d oes reach 0, t he problem has t o be recast as
int er polat ing along a line.
As a fur t her example, consid er t he problem of subd ivid ing par amet r ic
polynomial cur ves:

j
= [1 u u
2
u
3
. . . ] paramet er of curve, u = [0, 1]
M
ij
= blending funct ions of curve
Z
ij
= jt h compoenet of it h cont rol point
p
j
= [x y z] out put point
p
m
=

i
M
ij
Z
jm


i
T
ij
( )
T
jk
1
M
kl
Z
lm
( )

i
M
ij
Z
jm
Z
im
=

M
ij
1
T
jk
1
M
kl
( )
Z
lm
S
il
Z
lm
For t he specic case of a cubic Bzier subd ivid ed in t wo, t he upper half
is a repar amet r izat ion of t he u par amet er such t hat
u = 2u 1 maps [0.5, 1] [0, 1]
The T, M, S mat r ices are:
T
ij
=

1 1 1 1
0 2 4 6
0 0 4 12
0 0 0 8

1
]
1
1
1
M
ij
=

1 0 0 0
3 3 0 0
3 6 3 0
1 3 3 1

1
]
1
1
1
S
ij
= (1/ 8)

1 3 3 1
0 2 4 2
0 0 4 4
0 0 0 8

1
]
1
1
1
538
X.6 FORMS, VECTORS, AND TRANSFORMS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 538
Alt hough t his well-known result can be d er ived by more elegant means,
t he brut e-force mat r ix approach is just as easy for any t ype or d egree of
cur ve, and for any linear subd ivision. All t he algebr aic wor k is in t he
mat r ix inver se.
It should be emphasized t hat unless t he M blend ing funct ions of t he
cur ve ad d t o unit y (as was t he case for t he t r iangular int er polant ), t he
cont rol point s d ont have any geomet r ical vect or signicance, since t hey
lose invar iance wit h t he or igin of t he coord inat e syst em. See Gold man
(1985) for an excellent d iscussion of t hese t ypes of issues.
Cramer s Rule and Adjoint Mat rices
A ver y useful t ool in d ealing wit h problems such as t hose d iscussed here
is a ut ilit y for inver t ing mat r ices using exact int eger ar it hmet ic. The
ap p roaches d escr ibed here can t hen be ap p lied d irect ly wit hou t any
t ed ious algebr a, and used t o provid e insight t oward a more closed -for m
solut ion (if need ed ).
Since comput at ional efciency is of no concer n, t he much-maligned
Cr amer s Rule is id eally suit ed for t his. If d one in int eger ar it hmet ic, it is
free of round off er ror, is easy t o cod e, and will always prod uce an ad joint ,
even if t he mat r ix is singular. A sample collect ion of rout ines set up t o
solve t he Bzier subd ivision example is provid ed in t he append ix.
See Append ix 2 for C Implement at ion (780)
539
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 539
X. 7 X. 7 X. 7 X. 7 X. 7
P PP PPROPERTIES OF ROPERTIES OF ROPERTIES OF ROPERTIES OF ROPERTIES OF
S SS SSURFACE-NORMAL URFACE-NORMAL URFACE-NORMAL URFACE-NORMAL URFACE-NORMAL
T TT TTRANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS RANSFORMATIONS
Ken Turkowski
Apple Comput er
Cupert ino, Calif ornia
Why Is a Nor mal Vect or Not Just a Di f f er ence Why Is a Nor mal Vect or Not Just a Di f f er ence Why Is a Nor mal Vect or Not Just a Di f f er ence Why Is a Nor mal Vect or Not Just a Di f f er ence Why Is a Nor mal Vect or Not Just a Di f f er ence
bet ween Two Poi nt s? bet ween Two Poi nt s? bet ween Two Poi nt s? bet ween Two Poi nt s? bet ween Two Poi nt s?
In Fig. 1a, we illust r at e a rect angle and it s nor mals, N
i
, t hat have been
mod eled as st r aight line segment s. Figure 1b shows an afne t r ansfor ma-
t ion of t he rect angle and it s nor mals, where t he end point s of t he st r aight
line segment s represent ing t he nor mals have been t r ansfor med in t he
same way as ot her point s t hat make up t he rect angle. Not e t hat t hese
so-called nor mal vect or s are no longer per pend icular t o t he sur face. This
occur s because we applied an anisot ropic t r ansfor mat ion, t hat is, one
t hat is not equal in all d irect ions.
Is t his t he t ype of behavior t hat we expect from nor mal vect or s? I t hink
not . This lead s u s t o believe t hat nor mals behave d iffer ent ly u nd er
t r ansfor mat ions t han t he sur faces t o which t hey cor respond . The t r ansfor-
mat ions are relat ed , t hough, by t he requirement t hat t he sur faces and
t heir nor mals remain or t hogonal. We use t his proper t y t o d er ive t he
nor mal t r ansfor mat ion mat r ix from t he point t r ansfor mat ion mat r ix.
Tr ansf or mat i on of Nor mal Vect or s under Af ne Tr ansf or mat i on of Nor mal Vect or s under Af ne Tr ansf or mat i on of Nor mal Vect or s under Af ne Tr ansf or mat i on of Nor mal Vect or s under Af ne Tr ansf or mat i on of Nor mal Vect or s under Af ne
Model i ng Tr ansf or mat i ons Model i ng Tr ansf or mat i ons Model i ng Tr ansf or mat i ons Model i ng Tr ansf or mat i ons Model i ng Tr ansf or mat i ons
Given a planar sur face, a t angent vect or can be expressed as t he d iffer-
ence bet ween t wo point s on t he sur face
T
1
= P
1
P
0
. (1)
540
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 540
Figur e 1.
The nor mal t o t he sur face can be calculat ed from
N = T
1
T
2
, (2)
for any t wo noncolinear t angent vect or s T
1
and T
2
.
If a sur face is not planar, t he t angent s and nor mal can st ill be d ened
syst emat ically. For a par amet r ic sur face (t hat is, x, y, and z as a funct ion
of u and v), we have
T
u
=

x, y, z
( )
u
, T
v
=

x, y, z
( )
v
, N = T
u
T
v
, (3)
while for an implicit sur face d escr ibed by f(x, y, z) = 0, we have
N = f (x, y, z), (4)
wit h a t angent represent ed by any nonzero vect or such t hat
T N = 0. (5)
Regard less of t he t ype of sur face, we know t hat Ihe nor mal is or t hogonal
t o t he t angent by const ruct ion, so t hat
T N = TN
T
= 0. (6)
541
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 541
When a sur face is t r ansfor med by t he afne mod eling mat r ix M,

p pM, (7)
where
M =

M
00
M
01
M
02
0
M
10
M
11
M
12
0
M
20
M
21
M
22
0
M
30
M
31
M
32
1

1
]
1
1
1
,
(8)
and

p is a represent at ive of a set of point s (in homogeneous coord inat es)
t hat d ene t he sur face, t hen t he t angent vect or is t r ansfor med by t he
mat r ix M
T
:
T = TM
T
, (9)
where
M
T
=

M
00
M
01
M
02
M
10
M
11
M
12
M
20
M
21
M
22

1
]
1
1
(10)
is t he submat r ix of M t hat exclud es t r anslat ions, as can be ver ied by
applying Eq. 1, Eq. 8, and Eq. 7.
In ord er t o have t he relat ion bet ween t angent and nor mal vect or s hold ,
we apply Eq. 6:
T N = TM
T
M
T
1
N
T
= (TM
T
) (NM
T
1T
) = T N = 0, (11)
or
N = NM
N
= NM
T
1T
, (12)
which implies t hat t he nor mal vect or is t r ansfor med by t he t ranspose of
t he inverse of t he t angent s t r ansfor mat ion mat r ix. This is a well-known
t heorem of t ensor algebr a, where T is called a covar iant t ensor of r ank 1,
and N is a cont r avar iant t ensor of r ank 1.
542
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 542
Cer t ain t ypes of mod eling t r ansfor mat ions give r ise t o simple represen-
t at ions of t he nor mal t r ansfor mat ion mat r ix. Tr anslat ions d o not affect t he
nor mal or t angent t r ansfor mat ions, so t hese t wo are t he id ent it y t r ansfor-
mat ion. Below, we give examples of ot her special t r ansfor mat ions.
Isot r opi c Tr ansf or mat i ons Isot r opi c Tr ansf or mat i ons Isot r opi c Tr ansf or mat i ons Isot r opi c Tr ansf or mat i ons Isot r opi c Tr ansf or mat i ons
Oft en, a mod eling t r ansfor mat ion consist s of unifor m (isot ropic) scaling.
This t yp e of t r ansfor mat ion has t he following rep resent at ive t angent
mat r ix
M
T
=

s 0 0
0 s 0
0 0 s

1
]
1
1
,
(13)
where s is a nonzero scalar. The inver se t r anspose of t his mat r ix (suit able
for t r ansfor ming nor mals) is
M
N
= M
T
1T
=

1
s
0 0
0
1
s
0
0 0
1
s

1
]
1
1
1
1
1
1
=

1
s
2
M
T
, (14)
a scalar mult iple of t he t angent t r ansfor mat ion mat r ix.
Or t hogonal Tr ansf or mat i ons Or t hogonal Tr ansf or mat i ons Or t hogonal Tr ansf or mat i ons Or t hogonal Tr ansf or mat i ons Or t hogonal Tr ansf or mat i ons
In t he case where M
T
is an or t hogonal mat r ix (t hat is, t he mod eling
mat r ix M is composed only of rot at ions and t r anslat ions), it s inver se is
simply t he t r anspose; since t he t r anspose of t he t r anspose of a mat r ix is
t he or iginal mat r ix it self, nor mals are t r ansfor med by t he same mat r ix as
543
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 543
t he t angent s, and hence,
M
N
= M
T
. (15)
Composi t i on of Tr ansf or mat i ons Composi t i on of Tr ansf or mat i ons Composi t i on of Tr ansf or mat i ons Composi t i on of Tr ansf or mat i ons Composi t i on of Tr ansf or mat i ons
If a mod eling t r ansfor mat ion is composed of a concat enat ion of simpler
t r ansfor mat ions, t he composit e nor mal t r ansfor mat ion is just t he concat e-
nat ion of t he cor respond ing simpler nor mal t r ansfor mat ions. That is, if
M
T
= M
T1
M
T2
. . .M
Tk
, (16)
t hen
M
N
= M
N1
M
N2
. . .M
Nk
. (17)
This can be ver ied by subst it ut ion of Eqs. 16, 17, and 12 int o Eq. 11.
Tr ansf or mat i ons of Met r i c Pr oper t i es Tr ansf or mat i ons of Met r i c Pr oper t i es Tr ansf or mat i ons of Met r i c Pr oper t i es Tr ansf or mat i ons of Met r i c Pr oper t i es Tr ansf or mat i ons of Met r i c Pr oper t i es
In Euclid ean space, t he L
2
nor m of a vect or is given by


V
2
V V VV
T
.
(18)
Und er t he t r ansfor mat ion
V = WA, (19)
we have

W
2
WA ( ) WA ( )
T
WAA
T
W
T
WGW
T
,
(20)
where t he mat r ix
G = AA
T
(21)
544
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 544
is known as t he rst fundament al mat rix (Fau x and Pr at t , 1979) or
met ric t ensor, an d r ep r esen t s t h e fact t h at t h e sp ace i s i n gen er al
non-Euclid ean, t hat is, t he lengt h of a vect or in t his space is not simply
t he square root of t he sum of t he squares of t he component s of t he
vect or. Such a space is called a Riemannian space, and comes about
because t he mod eling mat r ix A is not in gener al isot ropic (scaling is not
equal in all d imensions).
In a Eu clid ean sp ace, G = 1 (t he id ent it y mat r ix), and t he nor m
becomes t he familiar Euclid ean one.
Appl i cat i ons t o Comput er Gr aphi cs: Appl i cat i ons t o Comput er Gr aphi cs: Appl i cat i ons t o Comput er Gr aphi cs: Appl i cat i ons t o Comput er Gr aphi cs: Appl i cat i ons t o Comput er Gr aphi cs:
Back- Face Cul l i ng Back- Face Cul l i ng Back- Face Cul l i ng Back- Face Cul l i ng Back- Face Cul l i ng
Per for mance in a comput er gr aphics d isplay syst em can be improved if
polygons facing away from t he view point are removed before scan-con-
ver sion. This is facilit at ed wit h an oper at ion involving t he plane equat ion
of t he polygon in quest ion:
N
x
x + N
y
y + N
z
z d = N P d = 0, (22)
where
N = [N
x
N
y
N
z
] (23)
is t he out ward -facing nor mal t o t he polygon,
P = [x y z] (24)
is any point on t he plane of t he polygon, and d is t he closest d ist ance of
t he plane t o t he or igin, measured in t he d irect ion of t he nor malposit ive
if t he nor mal point s t oward t he or igin from t he plane, and negat ive if it
point s away.
Back-facing polygons can be id ent ied by
N E d < 0, (25)
545
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 545
where E is t he view point . This can also be represent ed as

E < 0, (26)
where

N = [N
x
N
y
N
z
d] and

E = [E
x
E
y
E
z
1] (27)
represent t he plane and view point in homogeneous coord inat es.
Su p p ose now t hat t he p olygon is d ened in a coord inat e syst em
d ifferent t han t he wor ld , coord inat e syst em. This is commonly called
mod eling space, and usually has an afne t r ansfor mat ion M relat ing it t o
wor ld space as in Eq. 7 by

P
w
=

P
m
M. (28)
If t he plane equat ion is given in mod eling space, we can t r ansfor m t he
view point int o mod eling space and d o t he back-face culling t here:

EM
1
< 0. (29)
We can alt er nat ively d o t he culling in wor ld space by not ing t hat

EM
1
=

N (

EM
1
)
T
=

NM
1T

E
T
= (

NM
1T
)

E, (30)
so we can t r ansfor m t he plane from mod eling t o wor ld space wit h t he
inver se t r anspose of t he full mod eling mat r ix.
Eit her way, we need t o inver t t he mod eling mat r ix, whet her we d o t he
back-face culling in mod eling or wor ld space.
Appl i cat i ons t o Comput er Gr aphi cs: Shadi ng Appl i cat i ons t o Comput er Gr aphi cs: Shadi ng Appl i cat i ons t o Comput er Gr aphi cs: Shadi ng Appl i cat i ons t o Comput er Gr aphi cs: Shadi ng Appl i cat i ons t o Comput er Gr aphi cs: Shadi ng
When comput ing shad ing in a simple t hree-d imensional gr aphics syst em,
it is somet imes ad vant ageous t o t r ansfor m a direct ional light vect or I
from t he wor ld space int o t he mod eling space. Cent r al t o t he Lamber t
546
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 546
shad ing comput at ion, for example, is t he calculat ion of t he d ot prod uct :
d =

N
w
I
w
T
N
w
I
w
.
(31)
In a t ypical scan-conver sion syst em, t he light vect or is nor malized once
when t he light is placed in t he scene, and d oesnt need t o be nor malized
again. The sur face nor mal, however, is usually int er polat ed from t he
polygons ver t ex nor mals, so it need s t o be renor malized at ever y pixel in
t he polygon. (This is t rue if t he nor mals are linear ly int er polat ed in a
Car t esian space. If t he nor mals are int er polat ed on t he su r face of a
sphere, using spher ical coord inat es or quat er nions, no renor malizat ion is
required .)
Applying t he nor mal t r ansfor mat ion rule (Eq. 12), we get t he equivalent
relat ion in mod eling space:
d =

N
w
I
w
T
N
w
I
w

N
m
M
N
I
w
T
N
m
M
N
I
w

N
m
I
w
M
N
T
( )
N
m
M
N
I
w
,
(32)
which implies t hat we need t o t r ansfor m only one vect or (t he light vect or )
inst ead of all of t he polygons ver t ex nor mals. But wait ! Theres a pesky
t r ansfor mat ion in t he d enonominat or as well. Looking back t o Eq. 20, we
nd t hat

N
m
M
N
N
m
M
N
M
N
T
N
m
T
N
m
GN
m
T
,
(33)
where G is t he r st fund ament al mat r ix of t he Riemannian mod eling
space.
Nor malizing t he sur face nor mal at each pixel cost s:
Mod eling space: one 3 3 mat r ixvect or mult iplicat ions
and one 3-vect or d ot prod uct
Wor ld space: one 3-vect or d ot prod uct
Since one mat r ix-vect or mult iplicat ion is composed of t hree d ot prod -
u ct s, it cost s t hr ee ext r a d ot p rod u ct s p er p ixel t o d o t he shad ing
calculat ions in mod eling space. Thus, it seems like it is always ad vant a-
geous t o d o t he shad ing calculat ions in wor ld space.
547
X.7 PROPERTIES OF SURFACE-NORMAL TRANSFORMATIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 547
There is anot her reason not t o comput e shad ing in mod el space: t he
component s of t he vect or as nor malized by Eq. 33 d o not necessar ily
have a magnit u d e less t han or equ al t o one. This is an imp or t ant
consid er at ion if t he shad ing calculat ions are t o be d one in xed -point
because t he magnit ud es need t o be bound ed . For a given unit of energy
(power consumpt ion mult iplied by t ime), xed -point comput at ions are
always fast er t han oat ing-point , so high-per for mance gr aphics syst ems
especially need t o per for m shad ing in wor ld space.
If one can be assured , however, t hat all scaling will be isot ropic, it is
p ossible t o save a few mat r ix-vect or mu lt ip licat ions p er p olygon by
subsuming t he scale fact or in Eq. 14 int o t he t r ansfor med light vect or,
t hereby guar ant eeing a Euclid ean nor m and bound ed vect or magnit ud es.
However, t he increased exibilit y afford ed by nonr est r ict ed mod eling
mat r ices far out weighs any per for mance improvement afford ed by shad -
ing in mod eling space.
Concl usi ons Concl usi ons Concl usi ons Concl usi ons Concl usi ons
Nor mals are t r ansfor med by t he inver se t r anspose of t he mod eling t r ans-
for mat ion.
An i sot r op i c t r a n sfor ma t i on s ma ke met r i c comp u t a t i on s (d i st a n ce,
lengt h, nor m) more complex: t hree ext r a 3-vect or d ot prod uct s.
It is slight ly more ad vant ageous t o per for m back-face culling in mod el-
ing space. The ext r a cost in wor ld space is one mult iplicat ion and one
4 4 mat r ix t r anspose.
It cost s t hree ext r a 3-vect or d ot prod uct s per pixel t o per for m shad ing
comput at ions in mod eling r at her t han wor ld space if anisot ropic scaling
is allowed . It cost s (n 1) ext r a 3 3 mat r ix-vect or mult iplicat ions per
polygon in wor ld space.
Unless t he int er face t o t he gr aphics libr ar y prevent s anisot r opic scaling
(none cur rent ly d o), t he shad ing soft ware should accommod at e it . This is
accomplished most efcient ly and robust ly in wor ld space.
548
X.8 TRANSFORMING AXIS-ALIGNED BOUNDING BOXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 548
X. 8 X. 8 X. 8 X. 8 X. 8
T TT TTRANSFORMING RANSFORMING RANSFORMING RANSFORMING RANSFORMING
A AA AAXIS-ALIGNED XIS-ALIGNED XIS-ALIGNED XIS-ALIGNED XIS-ALIGNED
B BB BBOUNDING BOXES OUNDING BOXES OUNDING BOXES OUNDING BOXES OUNDING BOXES
James Arvo
Apollo Syst ems Division
of Hewlet t -Packard
Chelmsf ord, Massachuset t s
A ver y common t ype of t hree-d imensional bound ing volume is t he axis-
aligned box, a par allelepiped wit h each face per pend icular t o one coord i-
nat e axis. The appeal of t his shape is it s simplicit y. It is ubiquit ous in r ay
t r acing because it is among t he simplest object s t o t est for r ay int er sec-
t ion. It is also wid ely used t o acceler at e t he rend er ing of d isplay list s by
facilit at ing quick visibilit y t est s for collect ions of d r awing pr imit ives.
In bot h cont ext s it is frequent ly necessar y t o const ruct a bound ing box
of an object t o which an afne t r ansfor mat ion has been applied , t ypically
by means of a 3 3 mod eling mat r ix, M, followed by a t r anslat ion, T.
A simple and frequent ly accept able means of const ruct ing such a box is
t o t r ansfor m t he bound ing box of t he or iginal object and enclose t he
result ing ar bit r ar y par allelepiped by an axis-aligned box. This is equiva-
lent t o t r ansfor ming t he eight ver t ices of t he or iginal box and nd ing t he
ext rema of t he result ing coord inat es. Since each point t r ansfor mat ion
requires nine mult iplies and nine ad d s, t his would ent ail 144 ar it hmet ic
op er at ions and a minimu m of 21 comp ares. This naive ap p roach is
wast efu l becau se it ignores t he infor mat ion embod ied in t he cu bes
symmet r y. We will show how t o t ake ad vant age of t his infor mat ion.
We ad d ress t wo common met hod s of encod ing a bound ing box, B. The
r st i s t h e u se of t h r ee i n t er va l s, [B
x
min
, B
x
max
], [B
y
min
, B
y
max
]
[B
z
min
, B
z
max
]. Asid e fr om or d er in g, t h is is equ ivalen t t o st or in g t wo
op p osi n g ver t i ces. Th e secon d met h od i s t o st or e t h e box cen t er,
(B
x
cent
, B
y
cent
, B
z
cent
), and t he box half-diagonal, (B
x
diag
,B
y
diag
, B
z
diag
), which
is t he posit ive vect or from t he cent er of t he box t o t he ver t ex wit h
t he t hree largest component s. Bot h of t hese represent at ions are amenable
t o ver y efcient t r ansfor mat ion.
549
X.8 TRANSFORMING AXIS-ALIGNED BOUNDING BOXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 549
The algor it hm shown in Fig. 1 t r ansfor ms box A, encod ed as int er vals,
int o anot her axis-aligned box, B, of t he same for m. The algor it hm is
based on t he following obser vat ion. To comput e a component of t he
t r ansfor med box, say, t he maximum along t he it h axis, we need only
consid er which of t he eight ver t ices prod uces t he maximal prod uct wit h
t he it h row of t he mat r ix. There are t wo possibilit ies for each component
of t he pot ent ial ver t ex: t he minimum or t he maximum of t he int er val for
t hat axis. By for ming bot h prod uct s for each component and summing t he
largest t er ms, we ar r ive at t he maximal value. The minimal value is found
by summing t he smaller t er ms. The t r anslat ion component of t he mat r ix
d oes not inuence t hese choices and is simply ad d ed in.
The algor it hm shown in Fig. 2 t r ansfor ms box A, now encod ed as a
cent er and half-d iagonal vect or, int o anot her axis-aligned box, B, of t he
same for m. In t his for m t he new cent er, B
cent
, is obt ained by simply
applying t he affine t r ansfor mat ion t o A
cent
. The it h component of t he
n ew h al f-d i agon al , B
i
diag
, i s obt ai n ed by sel ect i n g t h e si gn ed h al f-
procedure Tr ansfor m_Int er val_Box(M,T,A,B)
begi n
for i = 1...3 do
St ar t wit h a d egener at e int er val at T
i
t o account for t r anslat ion.
B
i
min
T
i
;
B
i
max
T
i
;
Ad d in ext reme values obt ained by comput ing t he prod uct s of t he
mins and maxes wit h t he element s of t he it h row of M
for j = 1. . .3 do
a M
i,j
A
j
min
;
b M
i,j
A
j
max
;
B
i
min
B
i
min
+ min(a, b);
B
i
max
B
i
max
+ max(a, b);
endl oop;
endl oop;
end;
Figure 1. An algor it hm for t r ansfor ming an axis-aligned bound ing box, A, st ored as
t hree int er vals int o anot her box, B, of t he same for m.
550
X.8 TRANSFORMING AXIS-ALIGNED BOUNDING BOXES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 550
procedure Tr ansfor m_Cent er Diag_Box(M,T,A,B)
begi n
for i = 1. . .3 do
Init ialize t he out put var iables by zeroing t he new half-d iagonal
and set t ing t he new cent er equal t o t he t r anslat ion T.
B
i
cent
T
i
;
B
i
d iag
0;
Compute the ith coordinate of the center by adding M
i,
, A
cent
,
and the ith coordinate of the half-diagonal by adding M
i,
A
diag
.
for j = 1. . .3 do
B
i
cent
B
i
cent
+ M
i, j
A
j
cent
;
B
i
d iag
B
i
d iag
+ M
i,j
A
j
d iag
;
endl oop;
endl oop;
end;
Figure 2. An algor it hm for t r ansfor ming an axis-aligned bound ing box, A, st ored as
a cent er and a half-d iagonal int o anot her box, B, of t he same for m.
d iagonal of A, which result s in t he maximal prod uct wit h t he it h row of
M. Here signed means allowing each component t o be eit her posit ive
or negat ive ind epend ent ly. This gener at es all eight half-d iagonals of box
A, point ing from A
cent
t o each ver t ex. We achieve t he maximum prod uct
wit h t he row of M by making each of it s t hree t er ms posit ive, negat ing
t he negat ive element s of M. Because A
cent
is a posit ive vect or, t his is
equivalent t o t aking t he absolut e value of each element of M, as shown in
Fig. 2.
The cost of bot h of t hese algor it hms is only 36 ar it hmet ic oper at ions
and 9 comp ar es. Not e t hat in t he r st algor it hm bot h min(a,b) and
max(a, b) ca n be comp u t ed wi t h on e comp a r e, a n d i n t h e secon d
algor it hm each absolut e value is count ed as one compare.
See Append ix 2 for C Implement at ion (785)
551
I.2 USEFUL TRIGONOMETRY
GRAPHIC GEMS I Edit ed by ANDREW S. GLASSNER 551
C CC CCONSTRUCTING ONSTRUCTING ONSTRUCTING ONSTRUCTING ONSTRUCTING
SHAPES SUMMARY SHAPES SUMMARY SHAPES SUMMARY SHAPES SUMMARY SHAPES SUMMARY
Th er e a r e ma n y a p p r oa ch es t o con st r u ct i n g geomet r i c sh a p es.
On e may begin wit h a small set of geomet r ic p r imit ives, su ch as
p ol ygon s a n d qu a d r i c su r fa ces, a n d combi n e t h em u si n g some
or a l l of t h e con st r u ct i ve sol i d geomet r y op er a t or s. On e ma y
cr ea t e a sh a p e u si n g fr ee-for m d efor ma t i on of a si mp l er i n i t i a l
sh a p e. Ot h er t ech n i qu es for cr ea t i n g 3D object s a bou n d i n t h e
lit er at ure and commercial prod uct s.
Th e Gems i n t h i s sect i on p r ovi d e some t ech n i qu es u sefu l for
some of t h ese a p p r oa ch es. Ra t h er t h a n a l gor i t h ms for sh a p e
con st r u ct i on , t h ese Gems p r ovi d e some of t h e essen t i a l p i eces
u sed by su ch a l gor i t h ms t o cr ea t e sh a p es fr om con t ou r s,
d et er mi n e su r fa ce n or ma l s, or or i en t r efer en ce fr a mes. Th ese
met h od s ma y t h en be cou p l ed wi t h ot h er p r oced u r es t o for m
power ful shape synt hesis t ools.
552
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 552
X. 9 X. 9 X. 9 X. 9 X. 9
D DD DDEFINING EFINING EFINING EFINING EFINING
S SS SSURFACES FROM URFACES FROM URFACES FROM URFACES FROM URFACES FROM
S SS SSAMPLED DATA AMPLED DATA AMPLED DATA AMPLED DATA AMPLED DATA
Mark Hall
Rice Universit y
Houst on, Texas
The Pr obl em The Pr obl em The Pr obl em The Pr obl em The Pr obl em
This ar t icle will d escr ibe how t o creat e sur face d escr ipt ions from regu-
lar ly spaced d at a values on a t hree-d imensional gr id .
Many d isciplines d eal wit h a scalar d at a eld whose values are known
on a regular t hree-d imensional gr id as d epict ed in Fig. 1. Examples are
seismic d at a, elect ron microscopy d at a, NMR and CT scan d at a, and
baromet r ic pressure. It is oft en useful t o look at sur faces d ened as a
collect ion of point s wit h a given value. The sur face is called a level set .
For example, CT scan d at a record s mat er ial d ensit ies. Bone has a d iffer-
ent d ensit y t han t he soft er t issu es arou nd it . Find ing t he bou nd ar y
bet ween bone and soft t issue result s in a sur face represent at ion of t he
bone. Anot her example of a familiar level set is a single line of baromet r ic
pressure on a weat her map.
Assumpt i ons Assumpt i ons Assumpt i ons Assumpt i ons Assumpt i ons
The or iginal d at a is consid ered t o be samp les of a cont inu ou s eld
funct ion over space. Having a cont inuous funct ion is impor t ant because it
allows inferences abou t t he su r face locat ion. If t he d at a valu es are
oat ing point number s, t he chance t hat a d at a value will be exact ly equal
t o t he d esired level is negligible. If t wo samples of a cont inuous funct ion
br acket t he level set value (one is more and one is less), t here must be a
member of t he level set on any pat h bet ween t he t wo sample point s.
553
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 553
Figure 1. Funct ion values known at cor ner nod es.
A second assumpt ion about t he d at a values is t hat t he d ist ance bet ween
samples is small enough t o avoid aliasing. In simple t er ms t he samples
must be spaced closely enough t o d et ect t he smallest feat ure in which you
are int erest ed . This is t he t hree-d imensional equivalent of t he Nyquist
sampling t heorem for one-d imensional signals. If t he samples are t oo far
ap ar t , accu r at e r econst r u ct ion of t he su r faces gener ally will not be
possible.
Met hods Met hods Met hods Met hods Met hods
Three examples of sur face-nd ing t echniques are found in Wyvill et al.
(1986), Lorenson and Cline (1987), and Blooment hal (1988). This ar t icle
will d escr ibe similar it ies in t he t echniques.
The basic id ea is t o look at a cube for which we know t he funct ion
valu e at each cor ner. We comp are t he cor ner valu es t o t he d esired
sur face value, t he level. For any cube wit h at least one cor ner value above
554
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 554
Figure 2. Ad jacent nod es d ene cont our s in one, t wo, and t hree d imensions.
and one cor ner value below t he level, we know t hat t he sur face exist s
insid e t he cube.
Consid er scaling t he cor ner values by subt r act ing t he d esired level from
each sampled value. Then t he d esired level set is composed of point s wit h
value zero. For a given cube ed ge, if t he end point s are of opposit e sign,
t he sur face must pass t hrough t he ed ge. The exact locat ion of t he sur face
ver t ex is d et er mined by assuming t hat t he eld values along an ed ge
linear ly int er polat e t he cor ner values. For a cube face, if t he sur face
int er sect s ed ges on t he face, t he sur face must int er sect t he face as well.
The cube face int er sect ions can be combined t o from a represent at ion of
t he sur face insid e t he cube, as shown in Fig. 2. All t hree met hod s per for m
a var iat ion of t hese st eps t o d ene polygonal represent at ions of a sur face
wit hin each cube.
Figure 3. An example.
555
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 555
Figur e 4. Blooment hals solut ion of t he example.
As an example, t he cube in Fig. 3 has seven posit ive ver t ices and a
single negat ive ver t ex. Blooment hals (1988) met hod would be given one
of t he sur face ver t ices as a st ar t ing point , as in Fig. 4. From t hat point ,
t he algor it hm proceed s t oward t he posit ive cor ner, and t hen clockwise on
t he face t o t he r ight , as d ened by t he t r avel d irect ion. When anot her
sur face ver t ex is encount ered , a polygon ed ge is ad d ed and t he process
repeat s unt il all sur face ver t ices have been found .
Blooment hals met hod d iffer s fr om t he ot her t wo in t hat it is an
algor it hmic approach while t he ot her s are t able-d r iven. It has t he ad d i-
Figur e 5. Wyvills seven cases (from Wyvill et al., 1986).
556
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 556
Figure 6. Wyvill solut ion of t he example.
t ional benet of allowing ad apt ive renement of t he cubic mesh around
regions of high sur face cur vat ure. That is, more sample point s can be
used around cer t ain areas of t he sur face. For cases where ad d it ional eld
values can be gener at ed , t his allows a bet t er sur face t .
Wyvill et al.s met hod (1986) is based on consid er ing t he cor ner values
of each face. There are only seven possible face congur at ions, as shown
in Fig. 5. Not e t hat t he d ifference bet ween case F and case G is t he value
at t he cent er of t he face. An aver age of t he cor ner values is usually
sufcient for t he cent er value. Und er Wyvills met hod , each visible face of
t he cube would be an example of case D, as shown in Fig. 6. The t hree
ed ges creat e t he cor ner s of a t r iangle in t he cube.
Lor enson and Clines met hod (1987) looks at t he signs of all t he
cor ner s in a cube. They number t he cube ver t ices and ed ges as in Fig. 7.
Figure 7. Lorenson and Clines (1987) ed ge and ver t ex number ing.
557
X.9 DEFINING SURFACES FROM SAMPLED DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 557
The ver t ex signs creat e an ind ex int o a look-up t able. The t able d escr ibes
t he t r iangles for med by each pat t er n of ver t ex signs. Our example would
be d escr ibed by t he ind ex 11111011 binar y, or 251 d ecimal. Ent r y 251
would d escr ibe a single t r iangle for med by ver t ices on ed ges 2, 3, and 12.
There is a pr oblem wit h t he last met hod . It is possible for ad joining
cubes t o have an inconsist ent int er pret at ion of t heir shar ed face (Dr st ,
1988). An example is t he d ifference bet ween cases F and G in Wyvills
not at ion. A hole in t he sur face can result from t hese d ifferent int er pret a-
t ions. In pr act ice, t his problem seld om ar ises.
558
X.10 DEFINING SURFACES FROM CONTOUR DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 558
X.10 X.10 X.10 X.10 X.10
D DD DDEFINING EFINING EFINING EFINING EFINING
S SS SSURFACES FROM URFACES FROM URFACES FROM URFACES FROM URFACES FROM
C CC CCONTOUR DATA ONTOUR DATA ONTOUR DATA ONTOUR DATA ONTOUR DATA
Mark Hall
Rice Universit y
Houst on, Texas
Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement
Given a set of t wo-d imensional cont our s of an object , creat e a set of
polygons in t hree d imensions d escr ibing t he object .
The problem wit h nd ing polyhed r a from a select ion of t wo-d imen-
sional cont our s is t hat t here is in gener al no way t o join cont our s t oget her
cor rect ly. When t he ind ivid ual cont our s are d ened , all t he connect ivit y
infor mat ion is limit ed t o t he cont our plane. There is no infor mat ion about
t he relat ionship bet ween ad jacent cont our s. At best , heur ist ic rules can be
used t o recreat e t he connect ivit y bet ween successive cont our layer s. But
t here is no way t o use t he cont our s t o recreat e t he cor rect connect ions in
all cases.
Exampl es of Di f cul t i es Exampl es of Di f cul t i es Exampl es of Di f cul t i es Exampl es of Di f cul t i es Exampl es of Di f cul t i es
The hard est problem in reconst r uct ing a sur face from planar cont our s
concer ns how t o d eal wit h branching. Find ing polygons t o connect a
single cont our t o sever al closed cont our s on t he next level is d ifcult . In
more gener al t er ms, t he cont our s or t heir enclosed regions on successive
levels may not have t he same t opology.
Figure 1 d emonst r at es t he br anching problem. One level cont ains a
single cont our. The next cont our level cont ains t hree d ist inct cont our s. It
is not obvious how t o joint t he t wo cont our levels wit h polygons. Figure 2
d ep ict s anot her p roblem. One cont ou r level cont ains a single closed
559
X.10 DEFINING SURFACES FROM CONTOUR DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 559
Figur e 1.
Figur e 2.
cur ve. The ad jacent cont our cont ains sever al, one enclosing t he ot her.
Figu re 3 shows anot her d ifcu lt y t hat can ar ise. One cont ou r level
cont ains a closed cur ve. The next cont our level cont ains a single cont our
line. There is no obvious met hod of d ening a sur face bet ween t he t wo
cont our levels.
Met hods Met hods Met hods Met hods Met hods
There are sever al met hod s t hat wor k for some sit uat ions. A relat ively
simple congur at ion occur s when each cont our level cont ains a single
Figur e 3.
560
X.10 DEFINING SURFACES FROM CONTOUR DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 560
Figur e 4. Simple casea single closed cont our on each level.
closed cur ve. The algor it hms of Fuchs et al. (1977) or Ganapat hy and
Dennehy (1982) wor k well for t his simple case. They seek posit ions on
t he ad jacent cont ou r s t hat ar e near each ot her t o d ene cor ner s of
polygons spanning t he cont our levels, as shown in Fig. 4.
In more complicat ed cases, t he met hod of Anjyo et al. (1987) oft en
wor ks, but not always. It t r ies t o t r ansfor m each pair of ad jacent cont our
levels int o a number of t he simple sit uat ions solved above. The algor it hm
assumes t hat each level d enes a number of closed areas called regions.
For a pair of levels, t he upper and lower regions are t est ed against each
ot her t o d et er mine how t he regions over lap. Each region has associat ed
wit h it a value, which is t he number of regions from t he ot her level
over lapping t he region. Call t his number t he count of a region. A region
wit h count zero is a local minimum or maximum, so t he sur face is d ened
as t he enclosed region. A pair of regions t hat over lap only each ot her
d ene t he simple sit uat ion of Fig. 4. Regions t hat have count great er t han
one ar e involved in br anching, as d ep ict ed in Fig. 5. The region L
over laps t wo regions U1 and U2 in t he next cont our level. The algor it hm
seeks t o d ivid e region L int o regions L1 and L2 as in Fig. 6. Each pair
(L1, U1) and (L2, U2) can t hen be hand led easily. The cr u x of t he
problem is how t o nd a good place t o split L. The ar t icle is vague about
how t o go about t he split t ing process.
Figure 5. An example of br anching.
561
X.10 DEFINING SURFACES FROM CONTOUR DATA
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 561
Figure 6. Br anching conver t ed t o sever al simple cases.
For d at a in t he for m of a scalar quant it y known over a t hree-d imen-
sional gr id , t he met hod s d eveloped t o nd polygonal approximat ions t o
implicit sur faces wor k well.
See also Dening Sur faces from Sampled Dat a (552)
562
X.11 COMPUTING SURFACE NORMALS FOR 3D MODELS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 562
X.11 X.11 X.11 X.11 X.11
C CC CCOMPUTING SURFACE OMPUTING SURFACE OMPUTING SURFACE OMPUTING SURFACE OMPUTING SURFACE
N NN NNORMALS FOR 3D MODELS ORMALS FOR 3D MODELS ORMALS FOR 3D MODELS ORMALS FOR 3D MODELS ORMALS FOR 3D MODELS
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
In t his not e we focus on polygonal approximat ions t o smoot h sur faces.
Such sur faces may be mad e t o appear smoot h r at her t han facet ed , using
smoot h-shad ing t echniques such as t hose proposed by Gour aud (1971b)
and Phong (1973). These t echniqu es requ ire a su r face nor mal t o be
d ened at each ver t ex in t he mod el. This not e d iscusses some met hod s
for gener at ing such nor mals.
Swept Cont our s Swept Cont our s Swept Cont our s Swept Cont our s Swept Cont our s
We begin wit h an imp or t ant sp ecial class of shap e: swep t cont ou r s.
Examples of t hese shapes are pr isms and sur faces of revolut ion. Such
shapes are d ened by a planar cur ve (or cont our ), which is t hen t r ans-
lat ed along a pat h or rot at ed about an axis, as shown in Fig. 1. If our
input consist s only of t he cont our, how might we nd a sur face nor mal for
point s on t he swept -out sur face?
Red ucing t he d imension of a problem is oft en a good way t o simplify it s
analysis. An easy way t o eliminat e one d imension for t his problem is t o
gener at e nor mals for just t he 2D cont our cur ve, and t hen t r ansfor m t hose
nor mals wit h t he cu r ve as t he cont ou r is swep t (see Prop er t ies of
563
X.11 COMPUTING SURFACE NORMALS FOR 3D MODELS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 563
Figure 1. A cont our, and t he result s of t r anslat ion along a st r aight line and rot at ion
about an axis.
Sur face-Nor mal Tr ansfor mat ions in t his volume). We now need only nd
planar nor mals t o t he planar cont our.
Figure 2 shows a cont our and a d ist inguished point P for which we
wish t o nd a nor mal; we d iscuss t hree approaches. Technique A nd s t he
nor mals of t he t wo segment s ad jacent t o P, and aver ages t hose (Fig. 2a).
Technique B nd s t he line joining t he t wo ver t ices ad jacent t o P, and uses
t he nor mal of t hat line as t he nor mal at P (Fig. 2b). Bot h of t hese
approaches are implicit ly using t he Mean Value Theorem, which guar an-
t ees t hat somewhere bet ween t wo point s on a cont inuous cur ve, t he cur ve
obt ains a slope par allel t o t he line t hrough t hose point s. We can t ake a
564
X.11 COMPUTING SURFACE NORMALS FOR 3D MODELS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 564
more d irect , const r uct ive approach t o t his result . Recall t hat t he d er iva-
t ive of a cur ve at a point may be comput ed as t he limit of t he slope of a
line t hat passes on eit her sid e of t hat point ; t hat is,

df
dx
x
0
( )

f x
0
+
( )
f x
0

( )
x
0
+
( )
x
0

( )
.
Let us d irect ly comput e x
0
+

and x
0


as t hose point s t hat are
some small d ist ance

away from P along t he lines joining P; t hese are
on t he circle of r ad ius

cent ered at P. Then t he slope of t hat line may be
used as t he aver age slope at P, as shown in Fig. 2c. The choice of may
be mad e somewhat ar bit r ar ily; about 1/ 2 t he d ist ance along t he shor t er
of t he t wo ed ges has wor ked well for me. Not e t hat t his is a d ist ance
along t he nonzero ed ges r ad iat ing from P; t he next d ened point aft er P
may be P it self (t his can happen when one wishes t o d ene a d ouble
point for an int er polat ed cur ve).
Figur e 2. Different met hod s for comput ing t he ver t ex nor mal on a cont our.
565
X.11 COMPUTING SURFACE NORMALS FOR 3D MODELS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 565
3 33 33 D DD DD Pol yhedr a Pol yhedr a Pol yhedr a Pol yhedr a Pol yhedr a
The more gener al problem of comput ing nor mals at ver t ices of ar bit r ar y
3D polyhed r a may also be approached in sever al ways. By far t he most
common approach is t hat suggest ed by Gour aud (1971), which simply
aver ages t he nor mal of each polygon t hat shares t hat ver t ex (see Fig. 3).
We may ad apt t he lat t er t wo t echniques of t he previous sect ion, but
t here is a problem. In 2D, we found t wo point s t hat t oget her d et er mined
a unique line; t his is because only t wo ed ges could leave a ver t ex in a
prole cur ve. In 3D, we need exact ly t hree point s t o d et er mine a plane. If
a ver t ex has t hree ed ges r ad iat ing from it , we may nd t he ver t ex on t he
far end of each ed ge, pass a plane t hrough t hese point s, and use t he plane
nor mal as t he ver t ex nor mal (see Fig. 4a). The problem wit h t his ap-
proach is t hat many ver t ices will have more or less t han t hree ed ges. If a
ver t ex has only t wo ed ges, t he t wo neighbor ing ver t ices d o not d et er mine
a unique plane; if a ver t ex has more t han t hree ed ges, t here t ypically will
be no single plane t hat will pass t hrough all t he neighbor ver t ices. One
Figure 3. Comput ing a ver t ex nor mal by aver aging neighbor polygon nor mals.
566
X.11 COMPUTING SURFACE NORMALS FOR 3D MODELS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 566
Figure 4. Different met hod s for comput ing t he ver t ex nor mal on a polyhed ron.
approach in t his lat t er case is t o nd t he least -squares solut ion t o t he
point s, result ing in t he plane t hat most near ly int er polat es t he point s (Fig.
4b). Techniques for nd ing t his plane are well-known; see Lawson and
Hanson (1974).
This approximat e solut ion may also be applied t o a more local approxi-
mat ion of t he d er ivat ive, found by t r aveling only some d ist ance along
each ed ge shar ing P (Fig. 4c).
In pr act ice, t he simple aver aging of t he nor mals of ad jacent polygons
wor ks quit e well, as long as t he polygons int er polat e t he sur face rea-
sonably. (It is int erest ing t o consid er just what t hat word means in t his
cont ext .) If t he polygons are a poor approximat ion t o t he sur face, t hen I
t hink t hat at t ened silhouet t es and bad ly approximat ed int er sect ions wit h
ot her sur faces will usually be more object ionable sympt oms t han poor
shad ing. These lat t er effect s may be alleviat ed wit h t he t echnique of
Max (1989).
567
X.12 CALCULATION OF REFERENCE FRAMES ALONG A SPACE CURVE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 567
C CC CCALCULATION OF ALCULATION OF ALCULATION OF ALCULATION OF ALCULATION OF
R RR RREFERENCE FRAMES EFERENCE FRAMES EFERENCE FRAMES EFERENCE FRAMES EFERENCE FRAMES
A AA AALONG A SPACE CURVE LONG A SPACE CURVE LONG A SPACE CURVE LONG A SPACE CURVE LONG A SPACE CURVE
Jules Blooment hal
Xerox PARC
Palo Alt o, Calif ornia
Three-d imensional space cur ves can represent t he pat h of an object or
t he bound ar y of a sur face pat ch. They can also par t icipat e in var ious
fr ee-for m geomet r ic const r u ct ions. For examp le, t he generalized cylin-
der (a cylind er wit h ar bit r ar y cross-sect ions along a cent r al, space cur ve
axis) is used in comput er gr aphics t o good effect . Est ablishing reference
fr ames for t he cross-sect ions of a gener alized cylind er, or for any ot her
geomet r ic use, is t he subject of t his Gem.
We rest r ict t he cent r al axis t o t he familiar t hree-d imensional cu bic
cur ve, which we represent by it s polynomial coefcient s, t he t hree-d imen-
sional vect or s A, B, C, and D. A point P on t he cur ve is comput ed
accord ing t o it s par amet r ic posit ion, t :
P = At
3
+ Bt
2
+ Ct + D.
When const ruct ing a polygonal gener alized cylind er, each cr oss-sect ion
must be aligned proper ly wit h it s neighbor s so t hat t he st ruct ure d oes not
t wist . This alignment is usually provid ed by a reference frame, a point
and t hree or t hogonal vect or s t hat d ene posit ion and or ient at ion along
t he cent r al axis of t he cylind er (see Fig. 1).
One of t he more int uit ive reference fr ames is d ue t o Frenet (see Fig. 2);
t he fr ame consist s of a unit lengt h t angent , T, t o t he cent r al axis; a
pr incipal nor mal, N; and a binor mal, B. T is comput ed simply as t he unit
lengt h velocit y vect or, V; V is t he d er ivat ive of t he cur ve
V = 3At
2
+ 2Bt + C.
X.12 X.12 X.12 X.12 X.12
568
X.12 CALCULATION OF REFERENCE FRAMES ALONG A SPACE CURVE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 568
Figure 1. Polygons result ing from t wist ing refer ence fr ames.
The pr incipal nor mal is oft en d ened t o be in t he d irect ion of cur vat ure,
K = V Q V/ V
4
. Q is t he acceler at ion of t he cur ve, t hat is, t he
d er ivat ive of velocit y, 6At + 2B. Thus,
T = V/ V, N = K/ K, and B = T N.
The Frenet fr ame is convenient because it can be analyt ically comput ed
at ar bit r ar y point s along t he cur ve. Unfor t unat ely, it is und ened wher-
ever t he cur vat ure is d egener at e, such as at point s of inect ion or along
st r aight sect ions of cu r ve. Wor se, t he cu r vat u re vect or can su d d enly
rever se d irect ion on eit her sid e of an inect ion point , inict ing a violent
t wist in a progression of Frenet fr ames.
This problem was d iscussed by Shani and Ballard (1984), who pro-
posed an it er at ive solut ion t o minimize t or sion, t hat is, rot at ion around
t he t angent t o a cur ve. This t echnique was used t o comput e reference
fr ames for t he t ree br anches in J. Blooment hal (1985).
Figure 2. Cur vat ure (left ) and a Frenet fr ame (r ight ).
569
X.12 CALCULATION OF REFERENCE FRAMES ALONG A SPACE CURVE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 569
Pap er s by Klok (1986) and M. Blooment hal (1988) d iscu ss rot at ion
minimizing fr ames in some d et ail. They bot h obser ve t hat a rot at ion
minimizing fr ame d oes not necessar ily prod uce t he int uit ively d esired
result ; in t he case of a helical cur ve, for example, t he Frenet fr ame
appear s more d esir able.
The id ea behind rot at ion minimizing fr ames is t o d ene an init ial
reference fr ame at t he beginning of t he cur ve and t hen propagat e t he
fr ame along t he cur ve using small, local rot at ions. This met hod is immune
t o d egener acies in t he cur vat ure vect or ; it d oes not , unfor t unat ely, per mit
analyt ical comput at ion of a reference fr ame.
The r st fr ame usually can be comput ed using cur vat ure, as illust r at ed
in Fig. 2. If t he cur vat ure is d egener at e, t hen N can be any unit lengt h
vect or per pend icular t o T. Given t he init ial fr ame, subsequent fr ames are
gener at ed , in order, by comput ing P and T at t he new locat ion on t he
cur ve. The old reference fr ame is t hen rot at ed such t hat t he old T aligns
it self wit h t he new T. The rot at ion creat es a new N and B, which, wit h t he
new P and T, d ene a new reference fr ame. The axis of rot at ion is
gi ven by T0 T1 an d = cos
1
((T0 T1)/ ( T0 T1 ). In Fi g. 3,
{P0, T0, N0, B0} becomes {P1, T1, N1, B1}.
As t he cur ve becomes relat ively st r aight , t he d ifference bet ween T0 and
T1 becomes small. If T0 = T1, t heir cross-prod uct is und ened and no
axis is available t o per for m t he rot at ion: t his is not a problem, because
t he amount of rot at ion is zero.
Figure 3. Comput ing a refer ence fr ame from t he pr evious fr ame.
570
X.12 CALCULATION OF REFERENCE FRAMES ALONG A SPACE CURVE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 570
Alt hough t he t angent is need ed t o comput e t he reference fr ame, only
t he point P, nor mal N, and binor mal B ar e need ed t o t r ansfor m t he
cross-sect ion int o t he plane d ened by N and B.
If (C
x
, C
y
) is on t he cross-sect ion (Fig. 4), (P
x
+ C
x
N
x
+ C
y
B
x
, P
y
+
C
x
N
y
+ C
y
B
y
, P
z
+ C
x
N
z
+ C
y
B
z
) is a t hree-d imensional point proper ly
posit ioned on t he sur face of t he gener alized cylind er. This is convenient ly
expressed in mat r ix for m:
P
surface
= [C
x
, C
y
, 1][M], where M =

N
x
N
y
N
z
B
x
B
y
B
z
P
x
P
y
P
z

1
]
1
1
1
.
Figur e 4. Posit ioning and or ient ing a cross-sect ion.
Not e t hat t he r esu lt s d ep end on t he d ist ance bet ween su ccessive
reference fr ames. Reference fr ames a small d ist ance apar t will, nat ur ally,
follow t he pat h of t he cur ve more closely. Wit h large d ist ances it is
possible t o miss t ur ns of t he cur ve; such an er ror is t hen propagat ed
along t he cur ve. Implement or s may nd it ad vant ageous t o creat e sever al
int er med iat e reference fr ames in or d er t o est ablish one at a d esired
locat ion. Also, a cross-sect ion at t he beginning of a closed cur ve will not
necessar ily align wit h t he cross-sect ion at t he end of t he cur ve.
Figure 5 was creat ed wit h t he t echnique d escr ibed here; not e t hat t he
cross-sect ions change as t hey progress along t he cur ve. Also, t here are
more cross-sect ions where t he cur vat ure is relat ively high. The number of
cross-sect ions can also d epend on t he change in cross-sect ions (whet her
r ad ius or shape).
571
X.12 CALCULATION OF REFERENCE FRAMES ALONG A SPACE CURVE
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 571
Figur e 5. A gener alized cylind er wit h changing cross-sect ions.
11
CURVES AND
SURFACES
575
XI.1 PLANAR CUBIC CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 575
XI.1 XI.1 XI.1 XI.1 XI.1
P PP PPLANAR CUBIC CURVES LANAR CUBIC CURVES LANAR CUBIC CURVES LANAR CUBIC CURVES LANAR CUBIC CURVES
Andrew S. Glassner
Xerox PARC
Palo Alt o, Calif ornia
I nd t hat planar cubic cur ves pop up all over t he place in comput er
gr aphics. They' re useful for all sor t s of int er polat ion and blend ing in
mod eling and animat ion. For example, a good slow-in/ slow-out cur ve for
mot ion cont rol may be built from a cubic. Simple cubics ar e also useful
t ools for int er act ive d r awing progr ams, and t hey can be used as lt er s t o
mod ulat e some scalar par amet er.
Let ' s focus just on 1D cur ves. Suppose t hat we wr it e our cubic as
y = f(x). To d ene a cubic you need t o provid e four par amet er st hese
can t h en be t u r n ed i n t o t h e fou r coefci en t s of f so f(x) can be
evaluat ed at any x. The way I specify such a cubic is t o provid e t he (x, y)
coord inat es and t he slope of t he cur ve at t he st ar t and t he end of an
int er val. Suppose t he int er val goes from x
L
(low) t o x
H
(high). The input
consist s of t he point s (x
L
, y
L
) and (x
H
, y
H
) and t he associat ed slopes m
L
and m
H
at t hose point s (see Fig. 1):
f(x
L
) = y
L
f(x
H
) = y
H
f' (x
L
) = m
L
f' (x
H
) = m
H
,
where f is t he d er ivat ive of f. Calculat ion of t he coefcient s is much
easier if we map t he input int er val [x
L
, x
H
] t o t he int er val [0, 1]. Since
t his will change t he scaling of t he axes relat ive t o each ot her, we need t o
ad just t he values of t he slopes as well. We will d ene a new funct ion g,
576
XI.1 PLANAR CUBIC CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 576
Figur e 1. Specifying a cubic cur ve wit h t wo point s and cor respond ing slopes.
which will mimic f over t he unit int er val (see Fig. 2):
g(0) = y
L
= y
0
g(1) = y
H
= y
1
g' (0) = m
L

x
y
= m
0


g' 1
( )
m
H
x
y
m
1
,
where x = x
H
x
L
and y = y
H
y
L
. (We have renamed t he y and
m values wit h t he subscr ipt s 0 and 1 t o emphasize t hat t hey are t he
values for g, not f.) Our funct ion g and it s d er ivat ive g look like t his:
g(x) = ax
3
+ bx
2
+ cx + d
g' (x) = 3ax
2
+ 2bx + c
Examinat ion shows t hat g(0) = d, and since by d enit ion g(0) = y
0
,
we have d = y
0
. Similar reasoning hold s for g(0) = c = m
0
. We can nd
Figure 2. Remapping t he cur ve int o t he unit int er val [0, 1] requires scaling t he slopes.
577
XI.1 PLANAR CUBIC CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 577
a and b by simult aneously solving g(1) and g(1). The result ing coef-
cient s are summar ized below:
a = m
0
+ m
1
+ 2(y
0
y
1
)
b = 3(y
1
y
0
) m
1
2m
0
c = m
0
d = m
1
.
An impor t ant special case is when y
0
= 0 and y
1
= 1; t his ar ises when
one is int er polat ing bet ween t wo values smoot hly. The above coefficient s
t hen simplify t o
a = m
0
+ m
1
2
b = 3 m
1
2m
0
c = m
0
d = y
0
.
An even more special case is when m
0
= m
1
= 0. This is useful when
you want r st -ord er cont inuit y bet ween a ser ies of int er polat ed values
t hat are held const ant bet ween t r ansit ions (see Fig. 3). To blend from
value v
0
t o v
1
, let go fr om 0 t o 1, and calculat e v

= lerp( f(), v
0
, v
1
).
Figure 3. Int er polat ing bet ween const ant segment s uses end point d er ivat ives of 0.
578
XI.1 PLANAR CUBIC CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 578
Figure 4. Clamping t he input r ange t o (x
L
, x
H
) is equivalent t o hold ing t he end point
values const ant out sid e t he int er val, so t he cur ve d oesnt y off t o ext reme values.
The coefcient s for t he process are given by
a = 2
b = 3
c = d = 0
Incid ent ally, anot her good way t o d o t his sor t of int er polat ion is t o use
a piece of t he cosine cur ve. (Again using t he int er val [0, 1], you could use
g(x) = 1 ([cos( x) + 1]/ 2.)
To evaluat e f(x), we remap t he input t o t he unit int er val and nd t he
value of g:

f x ( ) g
x x
L
x
H
x
L

_
,

.
This ind ir ect ap p r oach is mu ch easier t han solving for t he cu bic
d irect ly on t he inp u t r ange. If you evalu at e f ou t sid e t he int er val
[x
L
, x
H
], you may nd t hat it quickly shoot s off int o huge posit ive or
negat ive number s. Typically one clamps t he input d omain in t his case
(see Fig. 4):

f x ( ) g clamp x
L
, x
H
,
x x
L
x
H
x
L

_
,

_
,

.
579
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 579
XI.2 XI.2 XI.2 XI.2 XI.2
E EE EEXPLICIT CUBIC SPLINE XPLICIT CUBIC SPLINE XPLICIT CUBIC SPLINE XPLICIT CUBIC SPLINE XPLICIT CUBIC SPLINE
I II IINTERPOLATION FORMULAS NTERPOLATION FORMULAS NTERPOLATION FORMULAS NTERPOLATION FORMULAS NTERPOLATION FORMULAS
Richard Rasala
Nort heast ern Universit y
Bost on, Massachuset t s
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
This ar t icle will show how t o comput e t he unifor m cubic spline t hat
passes t hrough a sequence of point s: P
0
, P
l
,. . .,P
i
, P
i+1
,. . . . For each i, ,
we will const r uct ad d it ional Bzier cont rol point s Q
i
and R
i
such t hat
t he cubic Bzier cur ve d ened by t he four cont rol point s P
i
, Q
i
, R
i
, P
i+1
is t he spline segment bet ween P
i
and P
i+1.
For background infor mat ion
on splines and on t he Bzier represent at ion of a cubic polynomial, see
Bar t els et al. (1987) or Far in (1988).
The spline smoot hness cond it ions (based on agreement of r st and
second d er ivat ives) yield t wo set s of equat ions:
P
i
R
i-1
= Q
i
P
i
(1)
P
i
2R
i-1
+ Q
i-l
= R
i
2Q
i
+ P
i
. (2)
The r st equat ions suggest t hat we int rod uce as fund ament al quant it ies
t he d ifference vect or s or t angent s:
D
i
= P
i
R
i-1
= Q
i
P
i
(3)
Then, using simple algebr a, we can eliminat e t he Qs and Rs from Eqs. 1
and 2 t o obt ain a set of equat ions t hat relat e t he Ds d irect ly t o t he Ps:
D
i+l
+ 4D
i
+ D
i1
= P
i+l
P
i1
(4)
580
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 580
The hear t of t he comput at ion becomes nd ing expressions for t he Ds
given t he Ps. We must break up t he d iscussion int o t wo separ at e cases:
closed loops and open cur ves.
Cl osed Loops Cl osed Loops Cl osed Loops Cl osed Loops Cl osed Loops
The key t o nd ing expressions in t he closed loop case is t o exploit t he
symmet r i es i n t h e equ a t i on s. We a ssu me t h a t t h er e a r e n p oi n t s
P
0
. . . P
n1
in t he loop and t hat t he spline cur ve wr aps around from P
n1
t o P
0
. We ext end t he not at ion so t hat for 0 i < n: P
i-n
= P
i+n
= P
i
and D
in
= D
i+n
= D
i
. The equat ions (4) are valid for 0 i < n. Thus,
t her e are n equ at ions for t he n u nknowns D
i
. These equ at ions ar e
invar iant und er rot at ional per mut at ion of t he ind ices. Thus, wit h proper
not at ion, t he expression for D
i
in t er ms of t he P
j
s must involve coef-
cient s t hat are ind epend ent of i. In ad d it ion, reect ion of t he ind ices
about i will rever se t he d irect ion of each vect or D
i
while int erchanging
symmet r ic point s P
j
. Thus, in t he expression for D
i
, t he coefcient s of
point s P
j
symmet r ic relat ive t o i must have opposit e signs.
To exploit t hese remar ks on symmet r y, we need one ad d it ional not a-
t ion. If n is od d , let n = 2m + 1, and , if n is even, let n = 2m + 2.
Then, we can comput e each D
i
by an expression of t he following for m:

a
k
k 1
m

P
i +k
P
i k
( )
.
It r emains t o exp lain how t o comp u t e t he coefcient s a
k
. The r u le
d epend s on whet her n is od d or even. If n is od d ,
a
k
= f
mk
/ f
m
where
f
0
= 1, f
1
= 3, and f
j
= 4f
j-1
f
j-2
for j 2.
If n is even,
a
k
= g
mk
/ g
m
D
i
581
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 581
Table 1. The Quant it ies f
j
and g
j
.
Tables 2. Int er polat ion Coefficient s for Closed Loops of n Point s wit h n Od d .
Table 3. Int er polat ion Coefficient s for Closed Loops of n Point s wit h n Even.
where
g
0
= 1, g
1
= 4, and g
j
= 4g
j-1
g
j2
for j 2.
The values of t he sequences f
j
and g
j
for small j are given in Table 1.
The numer ical values of t he cor respond ing coefcient s a
k
are given in
Tables 2 and 3. These t ables can be st ored in a spline compu t at ion
rou t ine and u sed for t he special cases 3 < n < 14. By n = 15, t he
582
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 582
coefcient s a
k
for 1 k 7 have converged wit hin four-d ecimal accu-
r acy t o const ant valu es t hat ar e ind ep end ent of t he p ar it y of n. In
ad d it ion, t he coefcient a
7
is alread y so small t hat higher coefcient s can
be ignored in comput at ion of splines for gr aphics d isplays. Thus, for
n 15, D
i
can be comput ed by a xed formula, which uses only t he
seven d ifference vect or s (P
i+k
P
ik
) cor respond ing t o 1 k 7.
Open Cur ves Open Cur ves Open Cur ves Open Cur ves Open Cur ves
The key t o nd ing expressions in t he open cur ve case is t o red uce t he
comput at ion t o t he closed loop case. We will d ene a d egener at e closed
loop t hat fold s back upon it self in such a way t hat cer t ain t angent s in t he
closed loop are equal t o t he d esired t angent s in t he or iginal open cur ve.
For convenience of not at ion, we assume t hat t here are n + 1 point s
P
0
. . . P
n
in t he open cur ve. The end point s P
0
and P
n
are special and
t here are only n 1 const r aint equ at ions cor r esp ond ing t o D
i
wit h
0 < i < n. Thus, t he init ial and nal t angent vect or s D
0
and D
n
are
u nd et er mined and may be fr eely chosen. In p r act ice, t hese t angent
vect or s are oft en t aken t o be zero but we need not assume t his.
It t ur ns out t hat t he vect or s D
i
for 0 < i < n can be expressed simply
in t er ms of t he point s P
i
for 0 < i < n and t he point s Q
0
= P
0
+ D
0
and
R
n1
= P
n
D
n
. To capt ure t his we int rod uce t he following not at ion:
T
0
= P
0
+ D
0
T
n
= P
n
D
n
T
i
= P
i
for 0 < i < n.
We ext end t he point s T
0
. . . T
n
t o a d egener at e closed loop of 2n point s
by reect ing back from T
n
using t he d enit ion
T
2ni
= T
i
for 0 < i < n.
By compar ison of t he equat ions for t he or iginal open cur ve and t he new
d egener at e closed loop, it is easy t o see t hat D
i
is also t he t angent at T
i
in t he d egener at e closed loop. Thus, t he for mula of t he preced ing sect ion
583
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 583
Table 4. Int er polat ion Coefficient s for Open Cur ves of n + 1 Point s
may be used . To d o t his, set
T
i
= T
i
for 0 < i < n,
and set m = n 1. Then, we can comput e each D
i
by an expression of
t he following for m:

D
i
a
k
k 1
m

T
i +k
T
i k
( )
.
In t his for mula, 2m = 2n 2 point s appear even t hough t here are only
n + 1 d ist inct Ts. Thus, most point s occur in t his for mula t wice, once as
or iginal p oint s and once as reect ed p oint s. The coefcient s a
k
ar e
alread y familiar from t he even-ord er closed loop case and may be t abu-
lat ed as shown in Table 4. Of cour se, Table 4 is id ent ical t o Table 3
except for t he r st column. Thus, in pr act ice, one should probably st ore
t he coefcient t ables ind exed by m r at her t han by n.
Remar ks Remar ks Remar ks Remar ks Remar ks
The for mulas in t his ar t icle are relat ed t o for mulas of Schoenber g (1973).
There, Schoenberg t reat s only t he open cur ve case and uses a B-spline
basis r at her t han Bzier cubics. His for mulas are more complex t han our s
because Bzier cubics appear t o be a bet t er choice of basis and because
584
X1.2 EXPLICIT CUBIC SPLINE INTERPOLATION FORMULAS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 584
no ad vant age is t aken of red ucing t he open cur ve case t o t he closed loop
case.
The coefcient s a
k
in t he expr ession for D
i
d ecrease by roughly a
fact or of four as k increases. This d ecrease is so r apid t hat for pr act ical
pur poses D
i
is inuenced by at most seven pair s of point s on eit her sid e
of P
i
. This est imat e is in fact somewhat pessimist ic. If all point s P
i
lie
wit hin a square of size 1024, t hen each D
i
is inuenced by at most ve
pair s of point s on eit her sid e of P
i
. Thus, int er polat ing splines exhibit
semilocal cont rol . . . a change in one P
j
affect s only a relat ively small
number of near by D
i
.
The for mulas for D
i
ar e quit e easy t o progr am. In ad d it ion, if one P
j
is
lat er changed , it is t r ivial t o increment ally upd at e each affect ed D
i
.
585
XI.3 FAST SPLINE DRAWING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 585
XI.3 XI.3 XI.3 XI.3 XI.3
F FF FFAST SPLINE DRAWING AST SPLINE DRAWING AST SPLINE DRAWING AST SPLINE DRAWING AST SPLINE DRAWING
Julian Gomez
MacroMind, Inc.
San Francisco, Calif ornia
Splines are oft en expressed as t he mat r ix prod uct
[T] [C] [G], (1)
wher e [T] = [t
3
t
2
t 1], [C] is t he coefcient mat r ix for t he sp line
basis, and [G] is t he geomet r y mat r ix of cont rol point s. For cubic splines,
bot h [C] and [G] are 4 4 mat r ices. A common met hod of gener at ing t he
spline involves r st evaluat ing t over t he r ange [0, 1] in equal increment s
t o gener at e an ar r ay of vect or s; t hat is, t he it h ent r y of t he ar r ay would
be t he prod uct [

t
i
3
t
i
2
t
i
1] [C] [G]. Gener ally, t
i
= i t. The second
st ep is hand ing t he ar r ay t o t he gr aphics soft ware t o be d r awn as a vect or
list t hrough t he cur rent t r ansfor mat ion mat r ix [M].
A fast er met hod involves ext end ing Eq. 1 t o includ e [M] in t he prod uct
[T] [C] [G] [M]. (2)
To d r aw t he spline it is t hen necessar y simply t o calculat e [T] for t
i
and
d r aw t hat vect or. In pseud o-cod e t his would look like t he following:
Push( );
PreMult iply(G);
PreMult iply(C);
for i 0; i nChord s; i i + 1
T[0] 1;
T[1] t ;
T[2] T[1] t ;
586
XI.3 FAST SPLINE DRAWING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 586
T[3] T[2] t ;
i f i = 0 then MoveTo(T); el se Dr awTo(T);
t t + t ;
endloop;
Pop( );
This t echnique combines t he spline d r awing oper at ion wit h t he geomet -
r ical t r ansfor mat ion, at t he cost of t wo mat r ix mult iplies, for which t he
hard ware is presumably opt imized , anyway. Since all oper at ions are d one
at t he same t ime it eliminat es t he t ime, space, and cod e required t o
gener at e t his t empor ar y ar r ay.
See also Line Dr awing Summar y (98)
587
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 587
XI.4 XI.4 XI.4 XI.4 XI.4
S SS SSOME PROPERTIES OF OME PROPERTIES OF OME PROPERTIES OF OME PROPERTIES OF OME PROPERTIES OF
B B B B BZIER CURVES ZIER CURVES ZIER CURVES ZIER CURVES ZIER CURVES
Ronald Goldman
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
Most of t h e p r op er t i es of Bzi er cu r ves can be d er i ved fr om t h e
d e Cast eljau evaluat ion algor it hm (see Fig. 1). Here we show how quickly
t o d ifferent iat e Bzier cur ves and how easily t o conver t bet ween t he
Bzier and monomial for m.
Not at i on Not at i on Not at i on Not at i on Not at i on
Let B(t) be t he Bzier cur ve wit h cont rol point s P
0
, . . . , P
n
. Then by
d enit ion

B t ( )
k
n
( ) k

t
k
1 t ( )
nk
P
k
.
Let M(t ) be t he monomial cur ve wit h coefcient s C
0
, . . . , C
n
. Then we
d ene

M t ( )
k
n
( ) k

C
k
t
k
.
Not ice t hat t o t he st and ard monomial for m, we have ad d ed t he binomial
coefcient

k
n
( )
. This will make all our algor it hms simpler lat er on. To
conver t from t his ver sion of t he monomial for m t o t he st and ard monomial
for m is ver y easy; simply mult iply C
k
by

k
n
( )
.
588
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 588
Figure 1. De Cast eljau algor it hm.
Remar ks Remar ks Remar ks Remar ks Remar ks
We shall illust r at e all of our algor it hms in gures for cubic cur ves, but
t hese algor it hms wor k qu it e gener ally for p olynomials of ar bit r ar y
d egree n.
Much t he same algor it hms (evaluat ion, d ifferent iat ion, and conver sion)
will wor k for B-spline cur ves if we replace t he d e Cast eljau algor it hm for
Bzier cur ves by t he d e Boor algor it hm for B-spline cur ves.
Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves
1. The d e Cast eljau Evaluat ion Algor it hm for Bzier Cur ves (Fig. 1).
Let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) 1 t ( )p
k 1
r 1
t ( ) + tp
k
r 1
t ( ) k = r, . . . , n
589
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 589
Then t he Bzier cur ve wit h cont rol point s P
0
, . . . , P
n
is given by
B(t ) =

p
n
n
(t ).
We illust r at e t his algor it hm for cubic Bzier cur ves in Fig. 1. Her e t he
cont rol p oint s are p laced at t he base of a t r iangu lar ar r ay and t he
recur sion is illust r at ed by labeling t he ar rows wit h t he coefcient s (1 t )
and t . The nal result , t hat is, a point on t he cur ve at par amet er value t,
emerges at t he apex of t he t r iangle.
2. Different iat ion of Bzier Cur ves (Fig. 2).
Let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) p
k 1
r 1
t ( ) + p
k
r 1
t ( ) r = 1, . . . , m
p
k
r
t ( ) 1 t ( )p
k 1
r 1
t ( ) + tp
k
r 1
t ( ) r = m + 1, . . . , n
Then

B
m ( )
t ( ) n!/ n m ( )!
{ }
p
n
n
t ( )
Here t he point is t hat t o d ifferent iat e a Bzier cur ve, all we need t o d o is
Figur e 2. Different iat ion algor it hm.
590
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 590
Figure 3. Monomial evaluat ion algor it hm.
t o d ifferent iat e t he d e Cast eljau algor it hm. But t o d ifferent iat e t he d e
Cast eljau algor it hm m t imes, we need only d ifferent iat e t he coefcient s t
and (1 t) in m levels of t he algor it hm and mult iply t he nal result by
n!/ (n m)!; we need not d ifferent iat e t he t er ms

p
k
r
t ( )
at all! In fact ,
t hough we chose t o d ifferent iat e t he r st m levels of t he algor it hm, we
could act ually d ifferent iat e any m levels and st ill get t he cor rect answer.
We illust r at e t his d ifferent iat ion algor it hm by nd ing t he r st d er ivat ive of
a cubic Bzier cur ve in Fig. 2. (Remember t hat t he result at t he apex of
t he t r iangle must be mult iplied by n = 3.) Not ice t hat all we d id is
d ifferent iat e t he labels on t he r st levels of t he d e Cast eljau algor it hm of
Fig. 1.
Cur ves i n Monomi al For m Cur ves i n Monomi al For m Cur ves i n Monomi al For m Cur ves i n Monomi al For m Cur ves i n Monomi al For m
3. Evalu at ion Algor it h m for Cu r ves in Mon omial For m (Fig. 3).
Let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) p
k 1
r 1
t ( ) + tp
k
r 1
t ( ) k = r, . . . , n
591
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 591
Then t he monomial cur ve wit h coefcient s P
0
, . . . , P
n
is given by

M t ( ) p
n
n
t ( ).
Not ice t hat t his algor it hm is just t he d e Cast eljau algor it hm for Bzier
cur ves wit h t he coefcient (1 t ) replaced by 1. This algor it hm wor ks
because t he monomials

k
n
( )
t
k
are t he same as t he Ber nst ein basis func-
t ions

k
n
( )
t
k
1 t ( )
nk
, except t hat (1 t ) is replaced by 1. This evaluat ion
algor it hm is much less efcient t han evaluat ion by Hor ner s met hod . We
illust r at e it here only because our conver sion algor it hms are based on t his
evaluat ion t echnique.
4. Different iat ion Algor it hm for Cur ves in Monomial For m.
Let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) p
k
r 1
t ( ) r = 1, . . . , m
p
k
r
t ( ) p
k 1
r 1
t ( ) + tp
k
r 1
t ( ) r = m + 1, . . . , n
Then

M
m ( )
t ( ) n!/ n m ( )!
{ }
p
n
n
t ( ).
This d ifferent iat ion algor it hm mimics t he d ifferent iat ion algor it hm for
Bzier cur ves. That is, only t he coefcient s 1 and t are d ifferent iat ed . This
t echnique wor ks because
t
n
= t t
n-1
and (t
n
) = nt
n-1
.
That is, t o d ifferent iat e t
n
, we d ifferent iat e t and mult iply t he result by n.
Conver si on bet ween Bzi er and Monomi al For m Conver si on bet ween Bzi er and Monomi al For m Conver si on bet ween Bzi er and Monomi al For m Conver si on bet ween Bzi er and Monomi al For m Conver si on bet ween Bzi er and Monomi al For m
5. Conver sion from Monomial t o Bzier For m (Fig. 4).
Let t he monomial coefcient s be P
0
, . . . , P
n
, and let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) p
k 1
r 1
t ( ) + p
k
r 1
t ( ) k = r, . . . , n
592
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 592
Figure 4. Conver sion fr om monomial t o Bzier for m.
Then t he Bzier cont rol point s Q
0
, . . . , Q
n
are given by

Q
k
p
k
k
t ( ).
Not ice t hat t his algor it hm is just t he evaluat ion algor it hm for monomial
for m wit h 1 subst it ut ed for t . Also t he d iagr am is just Pascals t r iangle in
rever se (Fig. 4).
6. Conver sion from Bzier t o Monomial For m (Fig. 5).
Let t he Bezier cont rol point s be Q
0
, . . . , Q
n
, and let

p
k
0
t ( ) P
k
k = 0, . . . , n
p
k
r
t ( ) p
k 1
r 1
t ( ) + p
k
r 1
t ( ) k = r, . . . , n
Then t he monomial coefcient s P
0
, . . . , P
n
are given by

P
k
p
k
k
t ( ).
Not ice t hat t his algor it hm is just t he d ifferent iat ion algor it hm for Bzier
cur ves where we have d ifferent iat ed ever y level of t he recur sion. Also t he
d iagr am is just for ward d ifferencing (Fig. 5).
593
XI.4 SOME PROPERTIES OF BZIER CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 593
Figur e 5. Conver sion from Bzier t o monomial for m.
Obser vat i on Obser vat i on Obser vat i on Obser vat i on Obser vat i on
We can also conver t fr om Bzier t o monomial for m by d ivid ing a Bzier
cur ve by eit her t
n
or (1 t)
n
. That is, if we let u = t / (1 t ), t hen

B t ( ) / 1 t ( )
n

k
n
( )
k

t
k
1 t ( )
nk
P
k
/ 1 t ( )
n

k
n
( )
k

u
k
P
k
.
Thus, we can evaluat e B(t )/ (1 t )
n
by applying Hor ner s met hod t o

k
n
( )
k

u
k
p
k
, and we can t hen ret r ieve B(t ) by mult iplying t he result by
(1 t)
n
. This proced ur e is fast er t han conver t ing d irect ly t o monomial
for m using t he algor it hm d escr ibed above. Again t his met hod illust r at es
t he close connect ion bet ween t he Bzier and monomial for m. This t ech-
nique can be used t o d er ive proper t ies of t he Ber nst ein polynomials
a n d Bz i er cu r ves. For exa mp l e, t h a t t h e Ber n st ei n p ol yn omi a l s

k
n
( ) t k(1t )
nk
, k=0, . . . , n for m a ba si s a n d sa t i sfy Desca r t es La w of
Signs follows easily from t he cor respond ing fact s about t he monomial
basis

k
n
( ) u
k
,k=0,...,n, by applying t his conver sion proced ure.
See also A Bzier Cur veBased Root -Find er (408)
594
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 594
XI.5 XI.5 XI.5 XI.5 XI.5
T TT TTUTORIAL ON UTORIAL ON UTORIAL ON UTORIAL ON UTORIAL ON
F FF FFORWARD DIFFERENCING ORWARD DIFFERENCING ORWARD DIFFERENCING ORWARD DIFFERENCING ORWARD DIFFERENCING
Bob Wallis
Weit ek Corporat ion
Sunnyvale, Calif ornia
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
A collect ion of mat hemat ical t ools and t r icks for reasoning about for ward
d ifferencing algor it hms is present ed .
For ward d ifferencing p rovid es a ver y fast t echniqu e for evalu at ing
polynomials at unifor mly spaced int er vals. Alt hough recur sive subd ivision
has many ad vant ages, it is less efcient when implement ed on RISC
processor s because of all t he load / st ore t r afc involved in t he st ack
oper at ions. The basic t ools for d er iving for ward d ifferencing DDAs come
from t he eld of numer ical analysis. They are Newt ons for ward for mula,
St ir lings number s, and a t echnique known as subt abulat ion.
Del Del Del Del Del Oper at or and t he Oper at or and t he Oper at or and t he Oper at or and t he Oper at or and t he DDA DDA DDA DDA DDA
If we evaluat e t he cubic polynomial

y
k
5 + 4k + 3k
2
+ 2k
3
a
i

k
i
(1)
at k = 0, 1, 2.. and t abulat e t he forward differences:

1
y
k
= y
k+1
y
k
rst forward difference

2
y
k
=
1
y
k+1

1
y
k
second forward difference

3
y
k
=
2
y
k+1

2
y
k
, t hird forward difference
595
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 595
we obt ain t he t able:
k y
1

3
0 5 9 18 12
1 14 27 30 12
2 41 57 42 12
3 98 99 54 12
4 197 153 66
5 350 219
6 569
The DDA algor it hm exploit s t his pat t er n by running t he above const r uc-
t ion backwards t o reconst r uct t he polynomial from it s for ward d iffer -
ences. For examp le, t he cod e fr agment below will r ep r od u ce t he y
column of t he above t able exact ly.
y
0
5

1
y
0
9

2
y
0
18

3
y
0
12
k 0
unti l nished do
y
k+l
y
k
+
1
y
k

1
y
k+l

1
y
k
+
2
y
k

2
y
k+l

2
y
k
+
3
y
0
k k + 1
endl oop
Newt on s For mul a and Fact or i al Pol ynomi al s Newt on s For mul a and Fact or i al Pol ynomi al s Newt on s For mul a and Fact or i al Pol ynomi al s Newt on s For mul a and Fact or i al Pol ynomi al s Newt on s For mul a and Fact or i al Pol ynomi al s
Ther e is an easy way t o conver t bet ween t he p olynomial coefcient s of
Eq. (1) (5, 4, 3, 2) and t he associat ed DDA coefcient s (5, 9, 18, 12). It
i n v ol v es con v er t i n g or d i n a r y p ol yn omi a l s t o f act ori al pol y nomi al s,
596
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 596
which are d ened as follows:
k
(l)
= k
k
(2)
= k( k 1)
k
(3)
= k(k 1)(k 2)
k
(n)
= k(k 1) . . . (k n + 1)
Polynomials in t his for m per mit d iscret e analogs of Taylor s for mula,
d er ivat ives, and so on. For examp le, t he del op er at or ap p lied t o a
fact or ial power behaves exact ly like a cont inuous d er ivat ive:
k
(n)
= nk
(n l)
.
The count er par t of Taylor s for mula is Newt ons for mula:

y
k
k
j ( )
/ j !
( )

j
y
0
.
j 0
n

For example, conver t ing t he polynomial in Eq. 1 t o t his for m yield s


y
k
= 5 + 9k + 18k (k 1)/ 2 + 12k (k 1) (k 2)/ 6.
The mat hemat ics of nit e d ifferences is full of elegant recur rences, and
sur pr ising inver sion for mulas. Since t hese are well-d escr ibed elsewhere
(Scheid , 1968; Ralst on, 1965; Gr aham et al., 1989), t his t ut or ial will use
a d ifferent approach. This is basically t he br u t e-force mat r ix inver se
met hod p resent ed in For ms, Vect or s, and Tr ansfor mat ions in t his
volume.
597
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 597
We can conver t from ord inar y power s t o k t o fact or ial polynomials of
k using St ir lings number s of t he r st kind . The r st six of t hese ex-
pressed as mat r ix t r ansfor ms are

1 k
1
k
2
k
3
k
4
k
5
[ ]

1 0 0 0 0 0
0 1 1 2 6 24
0 0 1 3 11 50
0 0 0 1 6 35
0 0 0 0 1 10
0 0 0 0 0 1

1
]
1
1
1
1
1
1
1

k
1

S
ij
1 k
1 ( )
k
2 ( )
k
3 ( )
k
4 ( )
k
5 ( )
[ ]
.
The inver se relat ionship uses St ir lings number s of t he second kind :

1 k
1 ( )
k
2 ( )
k
3 ( )
k
4 ( )
k
5 ( )
[ ]

1 0 0 0 0 0
0 1 1 1 1 1
0 0 1 3 7 15
0 0 0 1 6 25
0 0 0 0 1 10
0 0 0 0 0 1

1
]
1
1
1
1
1
1
1

k
i ( )

S
ij
1
1 k
1
k
2
k
3
k
4
k
5
[ ]
.
If we use t his t o conver t from a polynomial in power ser ies for mat ,
such as Eq. (1), t o one in t he for m of Newt ons for mula (Eq. (2)), we can
just read off t he DDA coefcient s we want . Int rod ucing a d iagonal fact o-
r ial mat r ix F, whose ii ent r y is i!, we can d er ive t he d esired result by
st ar t ing wit h a power ser ies, and int rod ucing mat ched pair s of t r ansfor ms
t hat keep t he relat ionship invar iant . However, one must be ver y careful
which ind ices are su mmed over rows, and which are su mmed over
columns:

y
k

m
k
m
k
m

I
mn

n
k
m

S
mn
F
no
1
( ) F
op
S
pq
1

q
( )
y
k
k
i ( )
/ i ! ( )

C
ij

j
( )
.
598
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 598
Compar ing t his t o Ralst on (1965), we have

i
y
0
C
ij

j
F
ij
S
jk
1
( )

k
.
The result ing C mat r ix, up t o N = 5, is


1 0 0 0 0 0
0 1 1 1 1 1
0 0 2 6 14 30
0 0 0 6 36 150
0 0 0 0 24 240
0 0 0 0 0 120

1
]
1
1
1
1
1
1
1

1

1
]
1
1
1
1
1
1
1

1

1
y
0

2
y
0

3
y
0

4
y
0

5
y
0

1
]
1
1
1
1
1
1
1
Using t his, we can easily conver t t he (5, 4, 3, 2) 1-for m (see For ms,
Vect or s, and Tr ansfor mat ions in t his volume) of power ser ies coef-
cient s in Eq. 1 t o t he (5, 9, 18, 12) 1-for m of DDA coefcient s. The same
met hod can be used t o d er ive conver sions bet ween any pair of for mat s
t hat are relat ed by linear weight s of t heir coefcient s.
In t he case of Bzier for mat , t he del oper at or becomes involved in a
r at her amazing fashion. It t ur ns out t hat if we conver t a cur ve from Bzier
for mat t o power ser ies for mat , t he coefcient s end up being relat ed t o t he
for ward d ifferences of t he cont rol point s (vect or s in bold t ype):
Z
j
= jt h cont rol point
f(t ) =

j
N
( )
j 0
N
t
j
(1 t )
Nj
Z
j
Bzier form
=

t
j
j
N
( )
j 0
N

j
Z
0
power series form
so it is an easy manner t o conver t Bzier t o power ser ies, and t hen apply
t he C mat r ix.
.
599
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 599
Det er mi ni ng t he St ep Si ze Det er mi ni ng t he St ep Si ze Det er mi ni ng t he St ep Si ze Det er mi ni ng t he St ep Si ze Det er mi ni ng t he St ep Si ze
If we have a polynomial in a cont inuous var iable t = [0, 1], t hen t o use
t he DDA met hod , we have t o d ecid e how many samples we want in t he
int er val. If t his is N, our d iscret e var iable becomes
h = 1/ N N st eps from 0 1
k = t / h = Nt . cont inuous discret e
An example of t he effect of d ecreasing st ep sizes on t he rend er ing of an
out line font is shown in Fig. 1.
A simple geomet r ical argument can be used t o d er ive a relat ionship
bet ween st ep size and er ror. It is based on t he assumpt ion t hat t he cur ve
can be t reat ed as if it were locally circular (see Fig. 2).
A reasonable st r at egy for select ing h is t o pick a value such t hat t he
maximum er ror in pixel space bet ween t he exact cur ve and it s piecewise
linear approximat ion is less t han some t oler ance:
R
2
= (ds/ 2)
2
+ (R e)
2
. Pyt hagorean t heorem
Ignor ing e
2
, t his red uces t o
e = ds
2
/ (8R). error from nit e st ep size (3)
Figur e 1.
600
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 600
Figur e 2.
Denot ing vect or quant it ies in bold t ype,
v = df/dt rst derivat ive = velocit y of curve
ds/dt = | v | change in arc lengt h wit h respect t o t (4)
a = dv / dt second derivat ive = accelerat ion of curve
1/ R = | v |
3
/ | v a| from different ial geomet ry (5)
dt = h t ime st ep same as st ep size (6)
e = h
2
| v a| / (8| v ) combining (3), (4), (5), (6) (7)
Equat ion 7 gives us a fair ly t ight er ror bound as long as t he locally
circular assumpt ion hold s. However, when cur ves have cusps t he veloc-
it y goes t o zero, and Eq. 5 becomes meaningless. A more conser vat ive
bound t hat wor ks wit h cusps can be d er ived as
| v a| | v | | a| (8)
e = h
2
| a| / 8. combining (7), (8) (9)
Equat ion 9 t ur ns out t o be a st and ard result from numer ical analysis
(Scheid , 1986; Ralst on, 1965). Applying t his t o t he case of a cubic Bzier
601
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 601
cur ve is easy:
Z
j
= jt h cont rol point
a(t ) = 6[(1 t )
2
Z
0
+ t
2
Z
1
] accelerat ion
Since t he magnit ud e of a is a linear funct ion of t , t he max must occur at
eit her t = 0 or t = 1.
e = (3/ 4)h
2
max(|
2
Z
0
| ,|
2
Z
1
| ) for a cubic Bzier (10)
Subdi vi di ng For war d Di f f er ences Subdi vi di ng For war d Di f f er ences Subdi vi di ng For war d Di f f er ences Subdi vi di ng For war d Di f f er ences Subdi vi di ng For war d Di f f er ences
Pr ior t o t he publicat ion of Lien et al. (1987), it was not wid ely recognized
t hat you could change t he st ep size wit hout complet ely recalculat ing t he
for ward d ifferences, t hus giving DDA algor it hms some of t he ad vant ages
of recur sive subd ivision. The t echnique has act ually been known for a
long t ime in t he eld of numer ical analysis (Scheid , 1986; Ralst on, 1965),
but was d isguised und er t he name of subt abulat ion.
In Lien et al., t he st ep size is halved unt il t he pixel space increment is
less t han a pixel. This sect ion will d escr ibe a slight ly d ifferent approach,
in which t he st ep size is red uced unt il t he er ror of Eq. 9 is less t han some
t oler ance, yield ing a piecewise linear approximat ion t o t he cur ve. The
ad vant age of combining t he st ep est imat or wit h t he calculat ion of t he
DDA coefcient s is t hat t he same quant it ies are involved in bot h comput a-
t ions.
The subd ivision for mulae may be d er ived using t he same mat r ix t ech-
nique as used in sect ion 4:

y
k
k
j ( )
/ j !
( )

y
0
k
l

S
lm
F
mn
1

n
y
0
.
If we want t o t r ansfor m t he st ep size, t his is t he same as t r ansfor ming t he
k par amet er :

k
j
k
i
T
ij

.
602
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 602
Some simple manipulat ions give us a t r ansfor mat ion mat r ix t hat con-
ver t s DDA coefcient s from one st ep size t o anot her :

t
i
C

ij

j
F
mn
S
no
1
T
op
S
pq
F
qr
1
( )

r
.
For t he case of a cubic, and a halving of h, t he relevant mat r ices are

S
ij

1 0 0 0
0 1 1 2
0 0 1 3
0 0 0 1

1
]
1
1
1
1
F
ij

1 0 0 0
0 1 0 0
0 0 2 0
0 0 0 6

1
]
1
1
1
1
T
ij

1 0 0 0
0 2 0 0
0 0 4 0
0 0 0 8

1
]
1
1
1
1
C
ij
1/ 16 ( )
16 0 0 0
0 8 2 1
0 0 4 2
0 0 0 2

1
]
1
1
1
1
.
For a quint ic polynomial, t he result is

C
ij
1/ 256 ( )
256 0 0 0 0 0
0 128 32 16 10 7
0 0 64 32 20 14
0 0 0 32 24 18
0 0 0 0 16 16
0 0 0 0 0 8

1
]
1
1
1
1
1
1
1
.
For t he cubic case, t he nal result is ver y simple, and can be expressed in
t er ms of del oper at or s as

1/ 2
3

3
/ 8

1/ 2
2

2
/ 4
1/ 2
3

1/ 2
1

1

1/ 2
2
( )
/ 2.
(11)
It should be point ed out t hat t here exist much more elegant ways t o
d er ive t he above r elat ionship s. They involve t he calcu lu s of t he del
oper at or, and r esult in ver y nice closed for m solut ions. See Scheid (1968)
and Ralst on (1965). The ad vant age of t he mat r ix approach is t hat it put s
603
XI.5 TUTORIAL ON FORWARD DIFFERENCING
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 603
all t he algebr a int o t he mat r ix inver se rout ine, and provid es a unifor m
t ool t o at t ack a wid e var iet y of problems.
Impl ement at i on f or Bzi er Cubi cs Impl ement at i on f or Bzi er Cubi cs Impl ement at i on f or Bzi er Cubi cs Impl ement at i on f or Bzi er Cubi cs Impl ement at i on f or Bzi er Cubi cs
Once t he for ward d ifferences of t he Bzier cont rol point s have been
calculat ed , t he conver sion from Bzier for mat t o DDA for mat , for h = 1,
is easy:

1
f
0
= 3(
1
Z
0
=
2
Z
0
) +
3
Z
0
rst DDA forward diff.

2
f
0
= 6(
2
Z
0
+
3
Z
0
) second DDA forward diff.

3
f
0
= 6
3
Z
0
. t hird DDA forward diff.
Equat ion 10 may t hen be used t o calculat e t he er ror for t his (unit y) st ep
size. The recur sion of Eq. 11 can t hen be applied repeat ed ly, each t ime
red ucing t he er ror by a fact or of (1/ 4). When t his is below t he er ror
t oler ance (or we hit a min st ep size), we can d rop d irect ly t o t he inner
DDA loop.
A major concer n wit h any DDA scheme is t he cont rol of accumulat ed
round off er ror (Chang et al, (1989). The binar y subd ivision of Eq. 11 is
ver y at t r act ive in t his regard because all t he d ivid es are by power s of t wo,
so t he result ant coefcient s can be represent ed wit hout er ror (given a
binar y fr act ion wit h enough bit s t o t he r ight of t he d ecimal point ).
If cod ed in assembler on a 32-bit CPU wit h lot s of regist er s, t he most
st r aight for ward t echnique is t o use 64-bit d ouble precision ad d s for t he
DDA, p u t t ing t he binar y d ecimal p oint bet ween t he most and least
signicant word s. This resu lt s in a ver y fast inner loop , cap able of
prod ucing up t o 1024 point s per cur ve, wit h no round off.
Anot her alt er nat ive is t o use t hree linked Bresenham-t ype DDAs, main-
t aining separ at e fr act ional par t s. This has t he ad vant age t hat it will wor k
for nonbinar y subd ivisions, but result s in a much bigger inner loop.
For a small number of st eps, round off is less of a concer n, and a single
precision DDA is usually sufcient .
604
XI.6 INTEGRATION OF BERNSTEIN BASIS FUNCTIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 604
XI.6 XI.6 XI.6 XI.6 XI.6
I II IINTEGRATION OF NTEGRATION OF NTEGRATION OF NTEGRATION OF NTEGRATION OF
B BB BBERNSTEIN BASIS FUNCTIONS ERNSTEIN BASIS FUNCTIONS ERNSTEIN BASIS FUNCTIONS ERNSTEIN BASIS FUNCTIONS ERNSTEIN BASIS FUNCTIONS
Ronald Goldman
Universit y of Wat erloo
Wat erloo, Ont ario, Canada
We can use t he d e Cast eljau algor it hm t o int egr at e t he Ber nst ein basis
funct ions

B
k
n
t ( )
k
n
( )
t
k
1 t ( )
nk
.
If we read t he d e Cast eljau algor it hm from t he apex d own, t he funct ions
at t he nod es ar e t he Ber nst ein basis fu nct ions, where t he d egree n
funct ions resid e in ord er on t he nt h level of t he d iagr am. We illust r at e
t his pr inciple in Fig. 1 for n = 3.
This d iagr am is just a consequence of t he well-known recur rence

B
k
n
t ( ) 1 t ( )B
k
n1
t ( ) + tB
k 1
n1
t ( ).
In fact Fig. 1 is simply an illust r at ion of t his recur rence.
Now suppose t hat we wish t o int egr at e some basis funct ion

B
k
n
t ( )
. The
proced ure is simply t o ext end t he d iagr am d own one more level from

B
k
n
t ( )
t o where

B
k
n+1
t ( )
and

B
k +1
n+1
t ( )
would ord inar ily r esid e. Then t hrough t he
t wo new nod es d r aw lines par allel t o t he t wo new opposing ed ges and clip
t he d iagr am below t hese ed ges. Summing t he leaf nod es eit her t o t he upper
left or t he upper r ight and d ivid ing by (n + 1) yield s t he d esired int egr al.
We illust r at e t his algor it hm in Figs. 2 and 3 for t he basis funct ion

B
1
3
t ( )
. Refer r ing t o Fig. 3, we have

B
1
3

t ( )dt
0
3
t ( ) +
1
4
t ( ) { }
/ 4,
605
XI.6 INTEGRATION OF BERNSTEIN BASIS FUNCTIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 605
Figure 1. Ber nst ein basis funct ions.
Figur e 2. Int egr at ion, st ep 1: Ext end t he d iagr am and d r aw t he clipping lines.
606
XI.6 INTEGRATION OF BERNSTEIN BASIS FUNCTIONS
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 606
Figur e 3. Int egr at ion, st ep 2: Clip t he d iagr am.
and

B
1
3

t ( )dt
2
2
t ( ) +
2
3
t ( ) +
2
4
t ( ) { }
/ 4.
Not ice, t herefore, t hat

a
0
3
t ( ) + a
1
4
t ( )
{ }

2
2
t ( ) +
2
3
t ( ) +
2
4
(t)
{ }
const ant
Much t he same t echnique wor ks for int egr at ing t he B-splines.
607
XI.7 SOLVING THE NEAREST-POINT-ON-CURVE PROBLEM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 607
XI.7 XI.7 XI.7 XI.7 XI.7
S SS SSOLVING TH OLVING TH OLVING TH OLVING TH OLVING THE EE EE
N NN NNEAREST-POINT-ON-CURVE EAREST-POINT-ON-CURVE EAREST-POINT-ON-CURVE EAREST-POINT-ON-CURVE EAREST-POINT-ON-CURVE
P PP PPROBLEM ROBLEM ROBLEM ROBLEM ROBLEM
Philip J. Schneider
Universit y of Geneva
Geneva, Swit zerland
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
Consid er t he following scenar io: you have an int er act ive cur ve-d r awing
syst em t hat maint ains a piecewise Bzier represent at ion of t he cur ves
appear ing on t he screen. The user can manipulat e t he cur ves by select ing
cont rol point s and moving t hem about , by ad d ing or d elet ing cur ves, and
so on. However, t he user has no d irect hand le ont o t he cur ve it selfhe
or she cannot select point s on t he cur ve d irect ly. The abilit y t o manipu-
lat e cur ves freely would allow t he user t o select point s on t he cur ve at
which t o fur t her subd ivid e t he cur ve, int er act ively t r ace along t he cur ve
wit h a mouse, nd t he point on a cur ve closest t o some ot her point on t he
screen (for example, a ver t ex of a t r iangle), nd t he d ist ance from such a
point t o t he cur ve, and so on. Unfor t unat ely, t he space t o which t he
user has access is t he wor ld of Car t esian coord inat es (wind ow coord i-
nat es, or some real t wo-d imensional space mapped ont o t he screen),
while t he space of t he Bzier cur ve is t hat of t he par amet r ic d omain of
t he cur ve. This Gem present s an algor it hm for solving t his problem.
This algor it hm was d eveloped in t he cont ext of an int er act ive t wo-
d imensional cur ve-d r awing syst em (Schneid er, 1988), which maint ained a
piecewise cubic Bzier represent at ion of free-for m cur ves; t hese were
gener at ed by t t ing t he par amet r ic cur ves t o d igit ized point s input (t hat
is, sket ched in) by t he user. (See An Algor it hm For Aut omat ically
Fit t ing Digit ized Cur ves in t his volume.)
608
XI.7 SOLVING THE NEAREST-POINT-ON-CURVE PROBLEM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 608
Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement Pr obl em St at ement
The basic problem can be st at ed in t his manner : given a par amet r ic cur ve
Q and a point P, bot h in t he plane, nd t he point on t he cur ve closest t o
P (t hat is, nd t he par amet er value t such t hat t he d ist ance from P t o
Q(t ) is a minimum). Our approach begins wit h t he geomet r ic obser vat ion
t hat t he line segment (whose lengt h we wish t o minimize) from P t o Q(t )
is per pend icular t o t he t angent of t he cur ve at Q(t ), as shown in Fig. 1.
The equat ion we wish t o solve for t is
[Q(t ) P] Q(t) = 0. (1)
In our par t icular case, cur ve Q is a cubic Ber nst ein-Bzier polynomial,

Q t ( ) V
i
i 0
n

B
i
n
t ( ), t 0, 1
[ ]
,
where t he V
i
, are t he cont rol point s. The Ber nst ein polynomials of d egree
n are d ened by

B
i
n
t ( )
i
n
( )t
i
1 t ( )
ni
, i = 0, . . . , n,
where

i
n
( )
is t he binomial coefcient n!/ (n i)!i!. The t angent cur ve Q
Figur e 1. Dist ance from an ar bit r ar y point t o a par amet r ic cur ve.
609
XI.7 SOLVING THE NEAREST-POINT-ON-CURVE PROBLEM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 609
for t he above cur ve Q can also be r epresent ed in Ber nst ein-Bzier for m:
Conver si on of Bzi er For m Conver si on of Bzi er For m Conver si on of Bzi er For m Conver si on of Bzi er For m Conver si on of Bzi er For m
We are d ealing wit h cubics, so polynomial Q is of d egree t hree, and Q is
of d egree t wo. Q P is also of d egree t hree, so t he polynomial d escr ibed
by Eq. 1 is (in gener al) of d egree ve. Thus, we can rest at e our problem
as one of nd ing t he root s of t his ft h-d egree polynomial. There is no
closed -for m solut ion for solving t his problem, so we must t ur n t o some
ot her t echniqu e t o nd t he root s (see A Bzier Cu r ve-Based Root -
Find er in t his volu me). The basic t echniqu e is r st t o conver t t he
equat ion t o Ber nst ein-Bzier for m, and t hen t o use a recur sive algor it hm
t o nd t he root s. The remaind er of t his Gem d escr ibes t he conver sion of
t his ft h-d egree equat ion t o Ber nst ein-Bzier for m, t o which one can
t hen apply t he previously ment ioned root -nd er.
We can rewr it e Eq. 1 as a single polynomial of d egree ve. Let
Q
1
(t ) = Q(t ) P (2)
Q
2
(t ) = Q(t ). (3)
Then, Eq. (1) can be wr it t en
Q
1
(t ) Q
2
(t ) = 0. (4)
Expand ing Eq. (2),
Q
1
(t ) = Q(t ) P

V
i
i 0
n

B
i
n
t ( ) P
c
i
i 0
n

B
i
n
t ( ),
where c
i
= V
i
P.

Q (t ) n (V
i +1
V
i
)B
i
ni
(t ).
i 0
n1

610
XI.7 SOLVING THE NEAREST-POINT-ON-CURVE PROBLEM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 610
Expand ing Eq. (3),

Q
2
t ( ) n V
i +1
V
i
( )
i 0
n1

B
i
n1


d
i
i 0
n1

B
i
n1
,
where d
i
= n(V
i+1
V
i
). Then, Eq. (4) can be wr it t en as

c
i
i 0
n

B
i
n
t ( ) d
j
j 0
n1

B
j
n1
t ( )

c
i
j 0
n1

i 0
n

d
j
B
i
n
t ( )B
j
n1
t ( )

c
i
j 0
n1

i 0
n

d
j i
n
( ) 1 t ( )
ni
t
i
j
n1
( )
1 t ( )
n1 j
t
j

c
i
j 0
n1

i 0
n

d
j i
n
( )
j
n1
( )
1 t ( )
2n1 ( ) i + j ( )
t
i + j

c
i
j 0
n1

i 0
n

d
j
i
n
( )
j
ni
( )
i+ j
2n1
( )
B
i + j
2n1
t ( )

c
i
j 0
n1

i 0
n

d
j
z
i , j
B
i + j
2n1
t ( )

w
i , j
j 0
n1

i 0
n

B
i + j
2n1
t ( ),
where z
i,j
=

i
n
( )
j
ni
( )
/
i+ j
2n1
( )
, and w
i,j
= c
i
d
j
z
i,j
.
0
611
XI.7 SOLVING THE NEAREST-POINT-ON-CURVE PROBLEM
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 611
Not e t hat t he W
i,j
are not t wo-d imensional coord inat est hey are real
number s, and t hus represent only one component of t he Bzier cont rol
point s for t his funct ion (t he y component , if you will). The ot her compo-
nent , x, for each cont rol point w
i,j
is i + j/ n + (n 1); t his is ex-
plained fur t her in A Bzier Cur ve-Based Root -Find er. At t his point , we
have t he equat ion in t he d esired Ber nst ein-Bzier for m, and can apply
t he met hod found in t he Gem ment ioned ear lier.
C cod e implement ing t he conver sion d escr ibed here, and t he root -nd -
ing algor it hm associat ed wit h it , may be found in t he append ix in t he le
point _on_curve.c. For simp licit y, t his samp le imp lement at ion is for
cubic cur ves onlygener alizat ion should be relat ively st r aight for ward .
The example case species a single cubic Bzier cur ve and an ar bit r ar y
point , and comput es t he point on t he cur ve nearest t hat ar bit r ar y point .
The r ou t ine Convert ToBezierForm creat es t he ft h-d egr ee Bzier equ a-
t ion, which is p assed t o t he r ou t ine FindRoot s. FindRoot s will t hen
ret ur n from zero t o t hree root s, which we evaluat e t o nd t he point s on
t he cur ve. By compar ing t he d ist ances from t hose point s on t he cur ve t o
t he ar bit r ar y point , and also consid er ing t he end point s of t he cur ve, we
can nd our d esired result t he point on t he cur ve closest t o t he ar bit r ar y
point , and also it s par amet er value.
See also A Fast 2D Point -on-Line Test (49)
See Append ix 2 for C Implement at ion (787)
612
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 612
XI.8 XI.8 XI.8 XI.8 XI.8
A AA AAN ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR N ALGORITHM FOR
A AA AAUTOMATICALLY FITTING UTOMATICALLY FITTING UTOMATICALLY FITTING UTOMATICALLY FITTING UTOMATICALLY FITTING
D DD DDIGITIZED CURVES IGITIZED CURVES IGITIZED CURVES IGITIZED CURVES IGITIZED CURVES
Philip J. Schneider
Universit y of Geneva
Geneva, Swit zerland
Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on Int r oduct i on
A new cur ve-t t ing met hod is int rod uced . This ad apt ive algor it hm aut o-
mat ically (t hat is, wit h no user int er vent ion) t s a piecewise cubic Bzier
cur ve t o a d igit ized cur ve; t his has a var iet y of uses, from d r awing
p r ogr ams t o creat ing ou t line font s from bit map font s. Of p ar t icu lar
int erest is t he fact t hat it t s geomet r ically cont inuous (G
1
) cur ves, r at her
t han par amet r ically cont inu ou s (C
1
) cu r ves, as d o most previou s ap-
proaches.
Cur ve t t ing has been t he subject of a fair amount of at t ent ion, even
befor e comp u t er gr ap hics came along. The t er m spline comes fr om
d r aft ing jargon: t o d r aw a smoot h cur ve passing t hrough a set of point s,
t he d r aft sman would place a weight (also a t er m t hat has sur vived int o
CAGD met hod s) on each point , and t hen place a exible wood en st r ip
(t he spline) ont o t he weight s. The spline had slot s running lengt hwise,
which t t ed ont o t he t op of t he weight s, allowing t he spline t o assume a
nat u r al and smoot h shap e. Pavlid is (1983) not es t hat t heor ies of
mechanical elast icit y can be used t o show t hat such spline cur ves exhibit
C
2
cont inuit y, and are equivalent t o piecewise cubic polynomial cur ves.
Because of t his, piecewise polynomial cur ves are refer red t o as splines,
and such cur ves have been used t o mat hemat ically int er polat e d iscret e
d at a set s. Read er s int erest ed in int er polat ion should consult any numer i-
cal analysis t ext , su ch as Cont e and d eBoor (1972), or Bar t els et al.
(1987).
613
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 613
This ar t icle d iscusses a met hod for approximat ion of d igit ized cur ves
wit h piecewise cubic Bzier segment s. Such an algor it hm is useful in
int er act ive d r awing syst ems, conver t ing bit mapped or ot her wise d igit ized
gures (such as font s) t o a par amet r ic cur ve represent at ion, and t he like.
Many t echniques have been brought t o bear on t he problem of t his t ype
of cur ve-t t ing: splines (Reinsch, 1967; Grossman, 1970); purely geomet -
r ic met hod s (Flegal cit ed in Reeves, 1981); B-splines (Yamaguchi, 1978;
Wu et al., 1977; Giloi, 1978; Lozover and Preiss, 1981; Yang et al.,
1986; Dierckx, 1982; Vercken et al., 1987; Ichid a et al., 1977; Chong,
1980); her mit e polynomials (Plass and St one, 1983); hyper bolic splines in
t ension (Kozak, 1986; Schweiker t , 1966; Cline, 1974); cubic splines in
t en sion (Clin e, 1974; Du be, 1987; Sch weiker t , 1966); con ic sect ion s
(Bookst ein, 1979); conic splines (Pavlid is, 1983); conic arcs and st r aight -
line segment s (Albano, 1974); and circular arcs and st r aight -line seg-
ment s (Piegl, 1986). A mor e d et ailed d escr ipt ion of t hese solut ions may
be found in Schneid er (1988) and Reeves (1981).
However, each of t hese approaches has some shor t comingsome of
t he ear lier met hod s apply only t o scalar funct ions, many require a great
d eal of int er vent ion by t he user, some prod uce represent at ions t hat are
i n ap p r op r i at e for fr ee-for m cu r ves (for examp l e, ci r cu l ar ar cs an d
st r aight -line segment s), and all of t he par amet r ic polynomial met hod s but
Plass and St ones (1983) prod uce cur ves t hat are par amet r ically cont inu-
ous, but t hey not e of t heir met hod t hat . . . it somet imes d oes not
converge at all.
Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves Bzi er Cur ves
The cur ve represent at ion t hat is used in t his algor it hm in approximat ing
t he d igit ized cur ve is t he Bzier cur ve. Accord ingly, we br iey review t he
basics: t he cur ves known as Bzier cur ves were d eveloped (ind epen-
d ent ly) in 1959 by P. d e Cast eljau and in 1962 by P. Bzier. Numerous
references exist ; see Boehm et al., (1984) and Davis (1975). A Bzier
cur ve of d egree n is d ened in t er ms of Ber nst ein polynomials:

Q t ( ) V
i
B
i
n
i 0
n

t ( ), t 0, 1
[ ]
,
614
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 614
Figure 1. A single cubic Bzier segment .
where t he V
i
are t he cont rol point s, and t he

B
i
n
t ( )
are t he Ber nst ein
polynomials of d egree n.

B
i
n
t ( )
i
n
( )
t
i
1 t ( )
ni
, i=0,...,n,
where

i
n
( )
is t he binomial coefcient n!/ (n i)!i!. See Fig. 1 for an
example of a cubic Bzier cur ve. Bzier cur ves gener ally are evaluat ed
using a recur sive algor it hm d ue t o d e Cast eljau. The algor it hm is based on
t he recursion proper t y of Ber nst ein polynomials:

B
i
n
t ( ) 1 t ( )B
i 1
n1
+ tB
i 1
n1
t ( ).
The kt h d er ivat ive of a Bzier cur ve is given by

d
k
dt
k
Q t ( )
n!
n k ( )!

k
i 0
nk
V
i
B
i
nk
t ( ),
where
1
V
i
= V
i
= V
i+1
V
i
, and where
k
V
i
=
k
V
i+1

k-1
V
i
,
615
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 615
Figure 2. G
1
cont inuit y cond it ion for cubic Bzier cur ves.
(Wat kins, 1987; Boehm et al, 1984). Thus, for t = 0 (t he left end point ),

d
k
dt
k
Q 0 ( )
n!
n k ( )!

k
V
0
.
For k = 1,

dQ
dt
Q' t ( ) n V
i +1
V
i
( )
i 0
n1
B
i
n1
t ( ),
which makes more obvious t he fact t hat t he t angent vect or d irect ion (at
t he left of t he segment ) is d et er mined by t he line segment from V
0
t o V
1
.
A similar cond it ion hold s for t he r ight end .
The implicat ion of t his fact is t hat t o enforce cont inuit y at t he joint s,
t he second -t o-last cont rol point of one segment must be collinear wit h t he
last cont rol point (which is also t he r st cont rol point of t he ad joining
segment ) and t he second cont rol point of t he ad joining segment . See Fig.
2 for an example; t he second cont rol point of t he r ight -hand segment
(t he one wit h cont rol point s W
i
) must be locat ed along t he half-line
labeled WV.
616
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 616
Any sor t of complet e t reat ment of cont inuit y cond it ions is well beyond
t he scope of t his ar t icleint erest ed read er s may wish t o consult DeRose
(1985). Br iey, we not e t hat if t he d ist ance bet ween t he cont rol point on
t he left sid e of t he joint and t he shared cont rol point is equal t o t he
d ist ance bet ween t he shared cont rol point and it s neighbor on t he r ight ,
t he t angent vect or s will be equal in magnit ud e and d irect ion. This cond i-
t ion is known as paramet ric cont inuit y, d enot ed C
1
. However, for many
applicat ions, t his is t oo rest r ict ivenot ice t hat for t he joint t o appear
smoot h, all t hat is r equ ir ed is t hat only t he t angent direct ions be
equ i val en t , a con d i t i on kn own as geomet ric cont inuit y, d en ot ed G
1
.
Get t ing back t o t he gure, t his implies t hat t he shared cont rol point and
it s t wo neighbor s need only be colinear t he respect ive d ist ances d o not
affect t he appear ance of smoot hness at t he joint . The cur ve-t t ing algo-
r it hm exploit s t his ext r a d egree of freed omwe employ a least -squares
t t ing met hod , which set s t hese d ist ances so t hat t he er ror (t hat is,
d ist ance) bet ween t he d igit ized point s of t he t t ed cur ve is minimized .
This has sever al ad vant ages: r st , we can t t he cu r ve wit h fewer
segment s; second , par amet r ically cont inuous cur ves cor respond t o t he
family of cur ves d r awn by t he mot ion of a par t icle t hat moves at a
cont inuous velocit y. This is a t oo much of a rest r ict ion for bit mapped
font s, for example; for hand -d r awn cur ves, t he rest r ict ion is pat ent ly
wrong.
Fi t t i ng t he Cur ve Fi t t i ng t he Cur ve Fi t t i ng t he Cur ve Fi t t i ng t he Cur ve Fi t t i ng t he Cur ve
Followin g Plass an d St on e (1983), a p ar amet r ic cu r ve Q(t ) can be
t hought of as t he project ion of a cur ve in X, Y, t space ont o t he XY
plane. Then, we can t hink of t he problem as nd ing t he cur ve in X, Y, t
space whose project ion best approximat es t he d igit ized cur ve in t he XY
lane. Best is d ened in t er ms of minimizat ion of t he sum of squared
d ist ances from t he d igit ized cur ve t o t he par amet r ic cur ve.
We st at e wit hout proof t hat t he cur ves d ened by t he project ions of t he
3-sp ace cu r ve on t he Xt and Yt p lanes ar e single-valu ed (scalar )
cur ves. Thus, if one could d evise some scheme relat ing t he X and Y
coord inat es of each point , one could apply a convent ional least -squares
funct ion-t t ing t echnique, wit h t he ad d it ion const r aint s of t angent vect or
d irect ion consid ered , in X and Y simult aneously. As we are wor king wit h
617
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 617
Figure 3. The d ist ance from P t o Q(t ).
par amet r ic represent at ions, t he scheme relat ing t he X and Y coord i-
nat es is t he par amet r ic value t associat ed wit h each point . As none are
provid ed , we must look for a way t o est imat e accur at ely a value of t for
each d igit ized point . A t r ad it ional approach t o t his problem is t o use
chord-lengt h paramet erizat ion.
Once each point has an associat ed t -value, we can t a cubic Bzier
segment t o t he set of point s (a process d escr ibed lat er ), and comput e t he
er ror by compar ing t he d ist ances bet ween each d igit ized point p
k
and t he
point wit h par amet r ic value t
k
on t he gener at ed cur ve.
The square d ist ance bet ween a given point P and a point Q(t ) on a
par amet r ic cur ve Q is
dist = P Q(t ). (1)
Refer t o Fig. 3 for a d iagr am of t his. The gener al problem can be st at ed in
t his manner : given a set of point s in t he plane, nd a cur ve t hat t s t hose
point s t o wit hin some given t oler ance. In our case, t he cur ve wit h which
we wish t o approximat e t he point s is a cubic Ber nst ein-Bzier cur ve, and
our t t ing cr it er ion is t o minimize t he sum of t he squared d ist ances from
t he point s t o t heir cor respond ing point s on t he cur ve. For mally, we wish
t o minimize a funct ion S, where S is d ened by

S d
i
Q u
i
( )
[ ]
2
i 1
n
(2)


d
i
Q u
i
( )
[ ]
i 1
n

d
i
Q u
i
( )
[ ]
,
(3)
618
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 618
where d
i
are t he (x, y) coord inat es of t he given point s, and u
i
is t he
par amet er value associat ed wit h d
i
.
In t he next set of equat ions, t he following d enit ions hold :
V
0
and V
3
, t he r st and last cont rol point s, are givent hey are set t o
be equal t o t he r st and last d igit ized point s in t he region we are t r ying
t o t .

t
l
and

t
2
are t he unit t angent vect or s at V
0
and V
3
, respect ively.
V
l
=
1

t
2
+ V
0
, and V
2
=
2

t
2
+ V
3
; t hat is, t he t wo inner cont rol
point s are each some d ist ance from t heir t he nearest end cont rol
point , in t he t angent vect or d irect ion.
Recall t hat as we are enforcing G
1
cont inuit y, V
1
and V
2
can be placed
anywhere along t he half-lines d ened by

t
1
and

t
2
, respect ively. Our
problem can be d ened as nd ing
1
, and
2
t o minimize S. That is, we
wish t o solve t hese t wo equat ions for
1
, and ,
2
t hereby d et er mining t he
remaining t wo cont rol point s (t hat is, V
1
and V
2
) of t he cubic Bzier
segment :

S

1
0
(4)

S

2
0.
(5)
Expand ing Eq. (4),

S

1
2 d
i
Q u
i
( )
[ ]
i 1
n


Q u
i
( )

1
.
Expand ing t he second t er m, we get

Q u
i
( )

1
V
0
B
0
3
u
i
( ) +
1

t
1
+ V
0
( )
B
1
3
u
i
( )
(

+
2

t
2
+ V
3
( )
B
2
3
u
i
( ) + V
3
B
3
3
u
i
( ))


t
1
B
1
3
u
i
( ).
619
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 619
Thus,

S

1
2 d
i
Q u
i
( )
[ ]
i 1
n

t
1
B
1
3
u
i
( ) 0.
Rear r anging, we get

B
1
3
i 1
n

u
i
( )Q u
i
( )

t
1

t
1
i 1
n

B
1
3
u
i
( ) d
i
.
For convenience, d ene

A
i ,1

t
1
B
1
3
u
i
( ).
Then,

Q
i 1
n

u
i
( ) A
i ,1
d
i
i 1
n

A
i ,1
.
(6)
Expand ing Q(u
i
),

Q
i 1
n

u
i
( ) A
i ,1

A
i ,1
i 1
n

V
0
B
0
3
u
i
( ) +
1
A
i ,1
+ V
0
B
1
3
u
i
( ) (

+
2
A
i , 2
+ V
3
B
2
3
u
i
( ) + V
3
B
3
3
u
i
( ))

A
i ,1
i 1
n

V
0
B
0
3
u
i
( ) +
1
A
i ,1
2
i 1
n

+ A
i ,1
i 1
n

V
0
B
1
3
u
i
( )

+
2
A
i ,1
i 1
n

A
i , 2
+ A
i ,1
i 1
n

V
3
B
2
3
u
i
( ) + A
i ,1
V
3
B
3
3
u
i
( )
i 1
n

Equat ion (6) becomes



A
i ,1
2
i 1
n

_
,

1
+ A
i ,1
i 1
n

A
i , 2

_
,

2

d
i
V
0
B
0
3
u
i
( ) + V
0
B
1
3
u
i
( ) + V
3
B
2
3
u
i
( ) + V
3
B
3
3
u
i
( ) ( ) ( )
i 1
n

A
i ,1
620
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 620
Similar ly, for

S /
2
,

A
i ,1
A
i , 2
i 1
n

_
,

1
+ A
i , 2
2
i 1
n

_
,

2

d
i
V
0
B
0
3
u
i
( ) + V
0
B
1
3
u
i
( ) + V
3
B
2
3
u
i
( ) + V
3
B
3
3
u
i
( ) ( ) ( )
i 1
n

A
i , 2
If we represent t he previous t wo equat ions by


c
1,1

1
+ c
1, 2

2
X
1


c
2,1

1
+ c
2, 2

2
X
2
,
we need only solve

c
2,1
c
1,1

c
2,2
c
1,2
( )

1
( )

X
2
X
1

_
,
for
1
and
2
. If we let


c
2,1
c
1,1

c
2,2
c
1,2
( )
C
1
C
2
( )


X
2
X
1

_
,
,
Then, using Cr amer s Rule, t he solut ion t o

1
( )

1

is

1

det C
2
( )
det C
1
C
2
( )

1

det C
1
( )
det C
1
C
2
( )
621
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 621
Our algor it hm t hat at t empt s t o t a single cubic Bzier segment t o a set
of point s appear s in Fig. 4. We begin by comput ing approximat e t angent s
at t he end point s of t he d igit ized cur ve. This can be accomplished by
t t ing a least -squ ares line t o t he p oint s in t he neighborhood of t he
end point s, or by aver aging vect or s from t he end point s t o t he next n
point s, and so on. Next , we assign an init ial par amet er value u
i
t o each
point d
i
, using chord -lengt h par amet er izat ion. At t his point , we use t he
t echnique d escr ibed t o t a cubic Bzier segment t o t he point st he r st
and last cont rol point s are posit ioned at t he r st and last d igit ized point s
in t he region we are wor king on, and t he inner t wo cont rol point s are
placed a d ist ance
1
and
2
away from t he r st and last cont rol point s, in
t he d irect ion of t he unit t angent vect or s previously comput ed . We t hen
comput e t he er ror bet ween t he Bzier cur ve and t he d igit ized point s,
not ing t he point t hat is t he far t hest d ist ance from t he cur ve. If t he t is
accept able, we d r aw or ot her wise out put t he cur ve. If t he t is not
accept able, we could break t he d igit ized point s int o t wo subset s at t he
point of great est er ror, comput e t he unit t angent vect or at t he point of
sp lit t ing, and recu r sively t r y t o t Bzier cu r ves t o t hese t wo new
subcur ves. Consid er, t hough, t hat our init ial chord -lengt h par amet er iza-
t ion is only a ver y rough appr oximat ion; if we had a bet t er par amet er iza-
t ion of t he point s, we might have been able t o t t he cur ve wit hout fur t her
recur sive processing. For t unat ely, t here is a t echnique available t o us.
Refer r ing back t o Eq. 1, our t is t hat chord -lengt h-gener at ed approximat e
par amet er. We can use Newt on-Raphson it er at ion t o get a bet t er t in
gener al, t he for mula is

t t
f t ( )
f ' t ( )
.
(7)
Refer r ing back t o Fig. 3, we wish t o solve
[Q(t ) P] Q(t ) = 0 (8)
for t . In our case, t hen, we red uce t he t for each point by

Q
1
t Q
2
t
Q
1
t ( ) Q
2
t ( )
[ ]
'
.
(9)
622
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 622
Fit Cu r ve(d , )
d :array[ ] of poi nt; Array of digit ized point s
:doubl e; User-specied error
begi n
Comp u t e

t
l
and

t
2
, t he u nit t angent
vect or s at t he end s of t he d igit ized p oint s;
Fit Cu bic(d ,

t
l
,

t
2
, );
end
Fit Cu bic(d ,

t
l
,

t
2
, )
d : array[] of poi nt; Array of digit ized point s

t
l
,

t
2
: vector; Endpoint t angent s
: doubl e; User-specied error
begi n
Comp u t e chord -lengt h p ar amet er izat ion of d igit ized p oint s:
Fit a single cu bic Bezier cu r ve t o d igit ized p oint s:
Comp u t e t he maximu m d ist ance from p oint s
t o cu r ve:
i f er ror <
then begi n
Dr awBezier Cu r ve:
return;
end;
i f er ror <
then begi n
f or i: i nteger 0. i 1. whi l e i < maxIt er at ions do
begi n
Rep ar amet er ize t he p oint s:
Fit a single cu bic Bezier cu r ve t o d igit ized p oint s:
Comp u t e t he maximu m d ist ance from p oint s t o
cu r ve:
i f er ror <
then begi n
Dr awBezier Cu r ve:
return;
end
end
endl oop
el se begi n
Comp u t e u nit t angent at p oint of maximu m er ror :
Call Fit Cu bic on t he left sid e:
Call Fit Cu bic on t he r ight sid e:
end
end
end
Figure 4.
623
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 623
Figure 5. Use of t he it er at ive pr ocess t o improve t he t t ing of t he cubic.
This t echnique was r st used by Grossman (1970) and lat er by Plass and
St one (1983) in t heir algor it hms. This it er at ion can great ly improve t he t
of t he cur ve t o t he point s: see Fig. 5, for an example of t he process.
Newt on-Raphson it er at ion is fair ly inexpensive and t end s t o converge
r at her quickly. Thus, one might want t o at t empt t his improvement ever y
t ime. However, if t he init ial t is ver y poor, it may be best not even t o
at t empt t he improvement . So, we compare t he er ror t o some value ,
which we set t o some small mult iple or power of t he user-specied er ror
. This value is implement at ion-d epend ent , but is easy t o d et er mine
empir ically. Ad d it ionally, since t he increment al improvement d ecreases
quickly wit h each successive it er at ion, we set a limit on t he number of
at t empt s we make (t he aut hor found t hat a value of four or ve is
appropr iat e). Finally, we not e t hat while t his Newt on-Raphson it er at ion is
cheap, t he associat ed t t ing at t empt s are not . The ast ut e read er may
not ice, t hen, t hat we have placed more emphasis on minimizing t he
number of segment s gener at ed t han on execut ion speed . Even so, t he
algor it hm is gener ally more t han fast enough for int er act ive use, even
wit h a ver y large number of point s t o t . In ad d it ion, it is ver y st able
t he aut hor has not seen a case when t he algor it hm failed t o converge quickly
on a sat isfact or y and cor rect t .
One nal not e: t he least -squares mat hemat ics fails when t here are only
t wo point s in t he d igit ized subcur ve t o be t t ed . In t his case, we ad opt a
Chord -Lengt h
par amet er izat ion
One It er at ion
Two It er at ions Three It er at ions
624
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 624
Figure 6. A d igit ized glyph, showing t he or iginal samples and t he t t ed cur ves.
met hod from Schmit t et al., (1986), and plane t he inner t wo cont rol
point s at a d ist ance from t he out er t wo cont rol point s equal t o one-t hird
of t he d ist ance bet ween t he t wo point s, along t he unit t angent s at each
end point .
Examples of t he t t ing algor it hm being applied t o a d igit ized font and
t o a hand -sket ched cur ve appear in Figs. 6 and 7. The large d ot s ind icat e
t he cubic Bzier cont rol point st he lled d ot s are t he r st and last
cont rol point s in each cur ve (which are shared wit h t he cur ves neighbor s),
and t he hollow d ot s are t he inner cont rol point s.
Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es Impl ement at i on Not es
Sever al point s should be mad e wit h respect t o implement at ion. Fir st , t he
implement or may want t o preprocess t he d igit ized d at a pr ior t o calling
t he t t ing rout ine. Such preprocessing might includ e: removing coinci-
625
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 625
Figure 7. A hand -sket ched cur ve, showing t he or iginal samples and t he t t ed cur ves.
d ent and / or near ly coincid ent d at a point s, lt er ing t he point s wit h a lit t le
convolu t ion (makes t angent comp u t at ion more reliable), and locat ing
cor ner s. By cor ner s we mean regions in t he d igit ized cur ve where t here
should be a d iscont inuit y; such point s can be locat ed by looking at t he
angle creat ed by a point and it s neighbor s. These cor ner s d ivid e t he
or iginal cur ve int o a number of d ist inct subcur ves, each of which can be
t t ed ind epend ent ly. Second , negat ive values occasionally are gener-
at ed when t he point s are ver y ir regular ly spaced . In t his case, one can
eit her split t he point s and t r y t o t each subcur ve, or employ t he heur ist ic
ment ioned ear lier.
A sample C implement at ion of t he algor it hm is includ ed in t he ap-
p end ix, in t he le t _cubic.c. Inp u t s t o t he r ou t ine Fit Curve are t he
ar r ay of d igit ized point s, t heir number, and t he d esired (squared ) er ror
value. When Bzier cur ves are gener at ed , t he rout ine DrawBezierCurve
is called t o ou t pu t t he cu r ve; t his rou t ine mu st be su pplied by t he
implement or, and simply consist s of d r awing or ot her wise out put t ing t he
cur ve d ened by t he cont rol point s wit h which it is called .
626
XI.8 AN ALGORITHM FOR AUTOMATICALLY FITTING DIGITIZED CURVES
GRAPHICS GEMS I Edit ed by ANDREW S. GLASSNER 626
Concl usi on Concl usi on Concl usi on Concl usi on Concl usi on
We have present ed an ad apt ive, aut omat ic algor it hm for t t ing piecewise
cubic Bzier cur ves t o d igit ized cur ves. This algor it hm elegant ly par allels
ad apt ive subd ivision for cur ve evaluat ion, and shares wit h t hat t echnique
t hat char act er ist ic t hat regions of lower cu r vat u re are more coar sely
rened t han t hose of higher cur vat ure. Ad vant ages over previous ap-
proaches are complet e aut omat icit y, geomet r ic cont inuit y, st abilit y, and
ext reme ease of implement at ion.
See Append ix 2 for C Implement at ion (797)
627
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 627
REFERENCES REFERENCES REFERENCES REFERENCES REFERENCES
ACM Transact ions on Graphics (1987). Color Plat es 1518. 6(3), 235. (Pr op er Tr eat -
ment of Pixels as Int eger s)
Albano, A. (1974). Represent at ion of Digit ized Cont ou r s in Ter ms of Conic Arcs and
St r ai gh t -Li n e Segmen t s, Comput er Graphics and Image Processing. 3, 2333. (An
Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Anjyo, K., Ochi, T., and Usami, Y. (1987). A Pr act ical Met hod of Const ru ct ing Su r faces in
Thr ee-d imensional Digit ized Sp ace, The Visual Comput er. 3, 412. (Dening Su r -
faces from Cont ou r Dat a)
Ar vo, J. (1986). Ba ckwa r d Ra y Tr a ci n g, SIGGRA PH Course Not es, 12. (Sh a d ow
At t enu at ion for Ray Tr acing Tr ansparent Object s)
Ar vo, J., and Kir k, D. (1987). Fair Play, SIGGRAPH 87 Film Show Cont r ibu t ion,
Mid night Movie Grou p, Apollo Compu t er, Inc. (Efcient Gener at ion of Sampling Jit t er
Using Look-u p Tables)
Ar vo, J., and Kir k, D. (1987). Fast Ray Tr acing by Ray Classicat ion, Comput er
Graphics. 21(4), 5564. (Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Bao, P. G., and Rokne, J. G. (1990). Qu ad r u ple-St ep Line Gener at ion, Comput ers and
Graphics. 13(4). (Symmet r ic Dou ble St ep Line Algor it hm)
Bar r y, C. D., Ellis, R. A., Gr aesser, S. M., and Mar shall, G. R. (1971). A Compu t er
Pr ogr am for Mod ellin g Molecu lar St r u ct u r es, Proceedings 1991 IFIP, 15521558.
(Rot at ion Tools)
Bar t els, R. H., Beat t y, J. C., and Bar sky, B. A. (1987). An Int roduct ion t o Splines for Use
in Comput er Graphics and Geomet ric Modeling. Mor gan Kau fman, Los Alt os, Calif.
(Exp licit Sp line Int er polat ion For mu las; Fast Spline Dr awing; An Algor it hm for Au t o-
mat ically Fit t ing Digit ized Cu r ves)
Beeler, M., Gosper, R. W., and Schropp el, R. (1972). HAKMEM, Massachu set t s Inst it u t e
of Technology Ar t icial Int elligence Labor at or y Repor t AIM-239. (A Fast Algor it hm for
Gener al Rast er Rot at ion)
628
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 628
Ber ger, M. (1986). Comput er Graphics wit h Pascal. Benjamin/ Cu mmings, Menlo Par k,
Calif. (3D Viewing and Rot at ion Using Or t honor mal Bases)
Bergman, L., Fu chs, H., Gr ant , E., and Spach, S. (1986). Image Rend er ing by Ad apt ive
Renement , Comput er Graphics (SIGGRAPH). 20(4), 2937. (Recor d ing Animat ion
in Binar y Ord er for Progressive Tempor al Renement )
Blaker, J. W. (1971). Geomet ric Opt icsThe Mat rix Theory. Marcel Dekker, New Yor k.
(A Fast Algor it hm for Gener al Rast er Rot at ion)
Blinn, J. F. (1977). Mod els of Light Reect ion for Comp u t er Synt hesized Pict u res,
Comput er Graphics. 11(2). (Fast Dot Prod u ct s for Shad ing)
Blinn, J. F. (1982). A Gener alizat ion of Algebr aic Su r face Dr awing, ACM Transact ions
on Graphics. 1(3).
Blinn, J. F. (1982). Light Reect ions Fu nct ions for Simu lat ion of Clou d s and Du st y
Su r faces, Comput er Graphics (SIGGRAPH). 16(3). (Simu lat ing Fog and Haze)
Blinn, J. F. (1989). Op t imal Tu bes-Jim Blinns Cor ner, IEEE Comput er Graphics and
Applicat ions. Sept ., 813. (Mat r ix Inver sion)
Bloch, N. J. (1987). Abst ract Algebra wit h Applicat ions. Pr ent ice-Hall, Englewood Cliffs,
N.J. (Per iod ic Tilings of t he Plane on a Rast er Gr id )
Blooment hal, J. (1985). Mod eling t he Might y Maple, Proceedings of SIGGRAPH 1985,
Comput er Graphics 19(3), 305311. (Calcu lat ion of Refer ence Fr ames along a Sp ace
Cu r ve)
Blooment hal, J. (1988). Approximat ion of Sweep Su r faces by Tensor Prod u ct B-Splines,
Univer sit y of Ut ah Technical Repor t WCS-88-008. (Calcu lat ion of Reference Fr ames
along a Sp ace Cu r ve)
Blooment hal, J. (1988). Polygonalizat ion of Implicit Su r face, Comput er Aided Geomet -
ric Design. 5, 341355. (Dening Su r faces from Sampled Dat a)
Boehm, W., Far in, G., and Kahman, J. (1984). A Su r vey of Cu r ve and Su r face Met hod s in
CAGD, Comput er-Aided Geomet ric Design. 1, 160 (A Bzi er Cu r ve-Based Root -
Find er ; An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Bookst ein, F. L. (1979). Fit t ing Conic Sect ions t o Scat t ered Dat a, Comput er Graphics
and Image Processing. 9, 5671. (An Algor it hm for Au t omat ically Fit t ing Digit ized
Cu r ves)
Bowyer, A. , a n d Wood wa r k, J. (1983). A Programmer s Geomet ry. Bu t t er wor t h s ,
Lond on. (Usefu l 2D Geomet r y; Usefu l 3D Geomet r y)
Bresenham, J. E. (1965). Algor it hm for Compu t er Cont rol of a Digit al Plot t er, IBM
Sy st ems Journal. 4(1), 2530. Rep r i n t ed i n Int eract iv e Comput er Graphics, ed i t ed
by H. Fr eeman, IEEE Comp u t er Societ y, 1980. (Digit al Line Dr awing; Symmet r ic
Dou ble St ep Line Algor it hm)
Bresenham, J. E. (1977). A Linear Algor it hm for Increment al Digit al Display of Circu lar
Arcs, CACM. 20, 100106. (Spheres-t o-Voxels Conver sion)
Br it t on, E. G., Lip scomb, J. S., and Piqu e, M. E. (1978). Making Nest ed Rot at ions
629
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 629
Convenient for t he User, Comput er Graphics (SIGGRAPH). 12(3), 222227. (Rot a-
t ion Tools)
Bronsvoor t , W. F., van Wijk, J. J., and Jansen, F. W. (1984). Two Met hod s for Improving
t he Efciency of Ray Cast ing in Solid Mod elling, Comput er-Aided Design. 16(1),
51-55. (A Simple Ray Reject ion Test )
Bu r n , R. P. (1982). A Pat hway i nt o Number Theory. Ca mbr i d ge Un i v er s i t y Pr es s ,
Cambr id ge, England . (Per iod ic Tilings of t he Plane on a Rast er Gr id )
Bu r n, R. P. (1985). Groups, A Pat h t o Geomet ry. Cambr id ge Univer sit y Press, Cam-
br id ge, England . (Per iod ic Tilings of t he Plane on a Rast er Gr id )
Camp bell, G., De Fant i, T. A., et al. (1986). Two Bit / Pixel Fu ll Color Encod ing,
Comput er Graphics (SIGGRAPH). 16(3), 297307. (A Simple Met hod for Color Qu ant i-
zat ion: Oct ree Qu ant izat ion)
Car p ent er, L. (1984). The A-Bu ffer, an Ant ialiased Hid d en Su r face Met hod , Comput er
Graphics. (Fast Ant i-Aliasing Polygon Scan Conver sion)
Cat mu ll, E. (1978). A Hid d en-Su r face Algor it hm wit h Ant i-Aliasing, Comput er Graph-
ics. (Fast Ant i-Aliasing Polygon Scan Conver sion)
Cat mu ll, E., and Smit h, A. R. (1980). 3-D Tr ansfor mat ions of Images in Scanline Ord er,
ACM Comput er Graphics (SIGGRAPH). 14(3), 279285. (A Fast Algor it hm for Gener al
Rast er Rot at ion)
Chang, S., Shant z, M., and Rocchet t i, R. (1989). Rend er ing Cu bic Cu r ves and Su r faces
wit h Int eger Ad apt ive For ward Differencing, Comput er Graphics (SIGGRAPH). 23(3).
(Tu t or ial on For ward Differencing)
Chen, T. C. (1972). Au t omat ic Comp u t at ion of Exponent ials, Logar it hms, Rat ios and
Squ are Root s, IBM J. Res. Dev. 380388. (Tr igonomet r y wit h CORDIC It er at ions)
Cheng, F., and Jiaan, W. C. (1986). Find ing t he Int er sect ion Point s of a Line wit h a
Pol ygon an d It s Ap p l i cat i on s, Proceedings of 1986 A CM 14t h A nnual Comput er
Science Conference: CSC86. (Two-Dimensional Clipping: A Vect or-Based Appr oach)
Chong, W. L. (1980). Au t omat ic Cu r ve Fit t ing Using an Ad apt ive Local Algor it hm, ACM
Transact ions on Mat hemat ical Soft ware. 6(1), 4557. (An Al gor i t h m for Au t omat i -
cally Fit t ing Digit ized Cu r ves)
Clear ly, J. G., Wyvill, B., Bir t wist le, G. M., and Vat t i, R. (1983). Mu lt iprocessor Ray
Tr acing, Technical Repor t No. 83/ 128/ 17, Dept . of Compu t er Science, Univer sit y of
Calgar y. (Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Cline, A. K. (1974). Scalar - and Planar -Valu ed Cu r ve Fit t ing Using Sp lines u nd er
Tension, Communicat ions of t he ACM. 17, 218223. (An Algor it hm for Au t omat ically
Fit t ing Digit ized Cu r ves)
Collins, G. E., and Loos, R. (1976). Polynomial Root Isolat ion by Different iat ion, ACM
Symposium on Symbolic and Algebraic Comput at ion, 1525. (A Bzier Cu r ve-Based
Root -Find er )
Cont e, S. D., and d eBoor, C. (1972). Element ary Numerical Analysis. McGr aw-Hill. (An
Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
630
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 630
Cook, R. L. (1983). Ant ialiased Point Sampling, Technical Memo No. 94, Lu caslm, Lt d .
(Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Cook, R. L. (1986). St ochast ic Sampling in Compu t er Gr aphics, ACM Transact ions on
Graphics. 5(1), 5172. (Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Cook, R. L., Car pent er, L., and Cat mu ll, E. (1987). The Reyes Image Rend er ing Archit ec-
t u r e, Comput er Graphics. 21(4), 95102. (Efci en t Gen er at i on of Samp l i n g Ji t t er
Using Look-u p Tables)
Cook, R. L., Por t er, T., and Car pent er, L. (1984). Dist r ibu t ed Ray Tr acing, Comput er
Graphics. 18(3), 137145. (Efcient Gener at ion of Samp ling Jit t er Using Look-u p
Tables)
Cook, R. L., and Tor r ance, K. E. (1982). A Reect ance Mod el for Compu t er Gr aphics,
ACM Trans. Graph. 1(1), 724. (Fast Dot Prod u ct s for Shad ing)
Coxet er, H. S. M. et al. (1987). M. C. Escher: A rt and Science. El s evi er Sci en ce.
(Per iod ic Tilings of t he Plane on a Rast er Gr id )
Coxet er, H. S. M. (1948). Regular Polyt ropes. Met hu en and Co., Lt d ., Lond on. (A Digit al
Dissolve Effect )
Cr ow, F. C. (1977). The Aliasing Problem in Comp u t er-Gener at ed Shad ed Images,
Communicat ions of t he ACM. (Fast Ant i-Aliasing Polygon Scan Conver sion)
Crow, F. C. (1984). Su mmed -Ar ea Tables for Text u re Map p ing, Comput er Graphics
(SIGGRAPH). 18(3). (Int er p ret at ion of Text u re Map Ind ices; Mu lt id imensional Su m
Tables)
Davis, P. J. (1975). Int erpolat ion and Approximat ion. Dover Pu blicat ions, New Yor k.
(An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
DeRose, A. D. (1985). Geomet r ic Cont inu it y: A Par amet er izat ion Ind epend ent Measu re of
Cont inu it y of Compu t er Aid ed Geomet r ic Design, Ph.D. t hesis, Univer sit y of Califor nia,
Ber keley. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Desp ain, A. M. (1974). Fou r ier Tr ansfor m Compu t er s Using CORDIC It er at ions, IEEE
Trans. Comput . C-23(10), 993-1001. (Tr igonomet r y wit h CORDIC It er at ions)
Dierckx, P. (1982). Algor it hms for Smoot hing Dat a wit h Per iod ic and Par amet r ic Splines,
Comput er Graphics and Image Processing . 20, 171184. (An Algor it hm for Au t omat -
ically Fit t ing Digit ized Cu r ves)
Dipp , M. A. Z., and Wold , E. H. (1985). Ant ialiasing t hrou gh St ochast ic Sampling,
Comput er Graphics. 19(3), 6167. (Efci en t Gen er a t i on of Sa mp l i n g Ji t t er Usi n g
Look-u p Tables)
Dipp , M. E., and Swensen, J. (1984). An Ad apt ive Su bd ivision Algor it hm and Par allel
Ar ch i t ect u r e for Rea l i st i c Ima ge Syn t h esi s, Comput er Graphics. 18(3), 149158.
(Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Du be, R. P. (1987). Preliminar y Specicat ions of Spline Cu r ves, IEEE Transact ions on
Comput ers. C-28(4), 286-290. (An Al gor i t h m for Au t omat i cal l y Fi t t i n g Di gi t i zed
Cu r ves)
Du ff, T. (1985). Comp osit ing 3D Rend ered Images, Comput er Graphics. (Fast Ant i-
Aliasing Polygon Scan Conver sion)
631
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 631
D r st , M. J. (1988). Ad d it ional Refer ence t o Mar ching Cu bes, Comput er Graphics.
22(2), 7273. (Dening Su r faces from Sampled Dat a)
Fa r i n , G. (1988). Curv es and Surf aces f or Comput er A i ded Geomet ri c Desi gn. Aca -
d emic Press. (A Bzier Cu r ve-Based Root -Find er ; Explicit Cu bic Spline Int er polat ion
For mu las)
Fau x, I., an d Pr at t , M. (1979). Comput at ional Geomet ry for Design and Manufact ure.
Ellis Hor wood Lt d ., Chichest er, West Su ssex, England . (Proper t ies of Su r face Nor mal
Tr ansfor mat ions)
Fishkin, K. P., and Bar sky, B. A. (1985). An Analysis and Algor it hm for Filling Propaga-
t ion, Proc. Graphics Int erface 85, 203212. (A Seed Fill Algor it hm; Filling a Region
in a Fr ame Bu ffer )
Floyd , R. W., and St einberg, L. (1975). An Ad apt ive Algor it hm for Spat ial Gr ay Scale,
Societ y for Informat ion Displays. Int er nat ional Symp osiu m Digest of Technical Pa-
p er s, p . 36. (A Digit al Dissolve Effect )
Foley, J. D., and Van Dam, A. (1982). Fundament als of Int eract ive Comput er Graphics.
Ad d ison-Wesley, Read ing, Mass. (Fast Scan Conver sion of Ar bit r ar y Polygons; A Digit al
Dissolve Effect ; Efcient Post -Concat enat ion of Tr ansfor mat ion Mat r ices; 3D Viewing
and Rot at ion Using Or t honor mal Bases)
Fu chs, H., Ked em, Z. M., and Uselt on, S. P. (1977). Opt imal Su r face Reconst r u ct ion from
Planar Cont ou r s, Communicat ions of t he ACM. 20(10), 693702. (Dening Su r faces
from Cont ou r Dat a)
Ganap at hy, S., and Dennehy, T. G. (1982). A New Gener al Tr iangu lat ion Met hod for
Planar Cont ou r s, Comput er Graphics. 16(3), 6975. (Dening Su r faces from Cont ou r
Dat a)
Gasson, P. C. (1983). Geomet ry of Spat ial Forms. Ellis Hor wood Lt d ., Chichest er, West
Su ssex, England . (Efcient Post -Concat enat ion of Tr ansfor mat ion Mat r ices)
Giloi, W. K. (1978). Int eract ive Comput er Graphics. Pr en t ice-Hall, En glewood Cliffs,
N.J. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Glassner, A. S. (1984). Ad ap t ive Precision in Text u r e Map p ing, Comput er Graphics
(SIGGRAPH). 20(4). (Int er pret at ion of Text u re Map Ind ices)
Glassner, A. S., et al. (1989). An Int roduct ion t o Ray Tracing. Acad emic Press, Lond on.
(Fast Dot Prod u ct s for Shad ing)
Gold man, R. N. (1985). Illicit Exp ressions in Vect or Algebr a, ACM Transact ions on
Graphics. 4(3). (For ms, Vect or s, and Tr ansfor ms)
Gou r au d , H. (1971a). Cont inu ou s Shad ing of Cu r ved Su r faces, IEEE Transact ions on
Comput er. (Rep r i n t ed i n H. Fr eema n , ed . , Int eract i v e Comput er Graphi cs, IEEE
Comp u t er Societ y.) (Compu t ing Su r face Nor mals for 3D Mod els)
Gou r a u d , H. (1971b). Comp u t er Di sp l a y of Cu r ved Su r fa ces, IEEE Transact ions
C-20(6), 623. (Compu t ing Su r face Nor mals for 3D Mod els)
Gr aham, R. L., Knu t h, D. E., and Pat ashnik, O. (1989). Concret e Mat hemat ics. Ad d ison-
Wesley, Read ing, Mass. (Rend er ing Fat Lines on a Rast er Gr id ; Tu t or ial on For ward
Differencing)
632
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 632
Gr ap hics St and ard s Planning Commit t ee (1979). St at u s Repor t of t he Gr aphics St an-
d ard s Commit t ee, Comput er Graphics. 13(3). (A Fast HSL-t o-RGB Tr ansfor m)
Gr eene, N. (1983). Tr ansfor mat ion Id ent it ies, Technical Rep or t No. 14, New Yor k
Inst it u t e of Technology Compu t er Gr aphics Lab. (Tr ansfor mat ion Id ent it ies)
Greene, N. (1986). Environment Mapp ing and Ot her Applicat ions of Wor ld Project ion,
IEEE Comput er Graphics and A pplicat ions. 6(11), 2129. (Di gi t al Car t ogr ap h y for
Comp u t er Gr aphics)
Greene, N., and Heckber t , P. (1986). Creat ing Rast er Omnimax Images from Mu lt ip le
Per sp ect ive Views Using t he Ellip t ical Weight ed Aver age Filt er, IEEE Comput er
Graphics & Applicat ions. (Int er pret at ion of Text u re Map Ind ices)
Gr os s ma n , M. (1970). Pa r a met r i c Cu r v e Fi t t i n g, The Comput er Journal . 14(2),
169172. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Gu p t a, S., and Sprou ll, R. F. (1981). Filt er ing Ed ges for Gr ay-Scale Displays, Comput er
Graphics. 15(3), 15. (Area of Int er sect ion: Circle and a Half-Plane; Ver t ical Dist ance
from a Point t o a Line)
Hall, R. A. (1989). Illuminat ion and Color in Comput er Generat ed Imagery. Sp r inger -
Ver lag, New Yor k. (Fast Dot Prod u ct s for Shad ing)
Hear n, D., and Baker, M. P. (1986). Comput er Graphics. Pr ent ice-Hall, Englewood Cliffs,
N.J. (3D Viewing and Rot at ion Using Or t honor mal Bases)
Heckber t , P. S. (t o appear ). An Algor it hm for In-Place Filt ered Zoom. (A Fast Algor it hm
for Gener al Rast er Rot at ion)
Heckber t , P. S. (1982). Color Image Qu ant izat ion for Fr ame Bu ffer Disp lay, ACM
Comput er Graphics (SIGGRAPH). 16(3), 297307. (A Digit al Dissolve Effect ; Prop er
Tr eat ment of Pixels as Int eger s; A Simp le Met hod for Color Qu ant izat ion: Oct ree
Qu ant izat ion)
Heckber t , P. S. (1984). Techniqu es for Real-Time Fr ame Bu ffer Animat ion, Compu t er
FX 84 Confer ence, Lond on. (Mapping RGB Tr iples ont o Fou r Bit s)
Heckber t , P. S. (1989). Fu nd ament als of Text u re Mapping and Image War ping, Mast er s
t hesis, UCB/ CSD 89/ 516, Dep t . of Comp u t er Science, Univer sit y of Califor nia at
Ber keley. (Gener ic Convex Polygon Scan Conver sion and Clipping)
Higgins, T. M., and Boot h, K. S. (1986). A Cell-Based Mod el for Paint Syst ems,
Proceedi ngs, Graphi cs Int erf ace, 86, Ca n a d i a n In for ma t i on Pr oces s i n g Soci et y,
82-90. (A Fast Algor it hm for Gener al Rast er Rot at ion)
Hobby, J. D. (1985). Digit ized Br u sh Tr aject or ies, Repor t No. STAN-CS-85-1070, Dept .
of Comp u t er Science, St anford Univer sit y. (Rend er ing Fat Lines on-a Rast er Gr id )
Hol d en , A. (1971). Shapes, Space, and Symmet ry. Col u mbi a Un i ver si t y Pr ess, New
Yor k. (A Digit al Dissolve Effect )
Hollad ay, T. M. (1980). An Opt imu m Algor it hm for Halft one Gener at ion for Displays and
Har d Cop ies, Proc. Soc. Inf. Display. 21, 185192. (Per iod ic Tilings of t he Plane on
a Rast er Gr id )
Hu ang, T. S., ed . (1980). Two-Dimensional Digit al Signal Processing II, Tr ansfor ms and
633
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 633
Med ian Filt er s, in Topics in Applied Physics (43). Sp r inger -Ver lag, Ber lin. (Med ian
Find ings on a 3 3 Gr id )
Ichid a, K., Kiyono, T., and Yoshimot o, F. (1977). Cu r ve Fit t ing by a One-Pass Met hod
wit h a Piecewise Cu bic Polynomial, ACM Transact ions on Mat hemat ical Soft ware.
3(2), 164174. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Ingalls, D. H. H. (1978). The Smallt alk-76 Progr amming Syst em: Design and Implemen-
t at ion, Fift h ACM Symp. Prin. Prog. Lang., 916. (A Fast Algor it hm for Gener al
Rast er Rot at ion)
Jakson, C. L., and Tanimot o, S. L. (1980). Oct rees and Their Use in Rep r esent ing
Th r ee-Di men s i on a l Object s , Comput er Graphi cs and Image Processi ng. 14(3),
249270. (A Simple Met hod for Color Qu ant izat ion: Oct ree Qu ant izat ion)
Jar vis, J. F., Ju d ice, N., and Nike, N. H. (1976). A Su r vey of Techniqu es for t he Display
of Con t in u ou s Tone Pict u r es on Bilevel Disp lays, Comput er Graphics and Image
Processing. 5(1), 1340 (A Simple Met hod for Color Qu ant izat ion: Oct ree Qu ant izat ion)
Joblove, G. H., and Greenberg, D. (1978). Color Spaces for Compu t er Gr aphics, ACM
Comput er Graphics (SIGGRAPH). 12(3), 2025. (A Digit al Dissolve Effect )
Johnson, J. R. (1989). Int roduct ion t o Digit al Signal Processing. Pr ent ice Hall, Engle-
wood Cliffs, N.J. (A Fast Algor it hm for Gener al Rast er Rot at ion)
Kajiya, J., and Von Her zen, B. P. (1984). Ray Tr acing Volu me Densit ies, Comput er
Graphics (SIGGRAPH). 18(3). (Simu lat ing Fog and Haze)
Kaji ya, J. T. (1986). Th e Ren d er i n g Equ at i on , Comput er Graphics. 20(4), 269-278.
(Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Kau fman, A., and Bakalash, R. (1988). Memor y and Processing Archit ect u re for 3D
Voxel -Ba s ed Ima ger y, IEEE Comput er Graphi cs and A ppl i cat i ons. 18(11), 10-23.
(Sp heres-t o-Voxels Conver sion)
Kind le, J. (1950). Analyt ic Geomet ry. Schau ms Ou t line Ser ies, McGr aw-Hill, New Yor k.
(Usefu l 2D Geomet r y)
Klok, F. (1986). Two Moving Coord inat e Fr ames for Sweeping along a 3D Tr aject or y,
Comput er A ided Geomet ric Design. 3. (Ca l cu l a t i on of Refer en ce Fr a mes a l on g a
Sp ace Cu r ve)
Knowlt on, K. (1980). Progressive Tr ansmission of Grey-Scale and Binar y Pict u res by
Si mp l e, Efci en t , a n d Lossl ess En cod i n g Sch emes, Proc. IEEE. 68(7), 885896.
(Record ing Animat ion in Binar y Ord er for Progressive Tempor al Renement )
Kn u t h , D. E. (1981). The A rt of Comput er ProgrammingSeminumerical A lgorit hms,
Vol. 2, (2nd ed.). Ad d ison-Wesley, Read ing, Mass. (Efcient Gener at ion of Sampling
Jit t er Using Look-u p Tables)
Kn u t h , D. E. (1981). Art of Comput er Programming, Vol. 2, Fundament al Algorit hms.
Ad d ison-Wesley, Read ing, Mass. (Per iod ic Tilings of t he Plane on a Rast er Gr id )
Kor nfeld , C. (1977). The Image Pr ism: A Device for Rot at ing and Mir ror ing Bit map
Images, IEEE Comput er Graphics and Applicat ions. 7(5), 25. (A Fast Algor it hm for
Gener al Rast er Rot at ion)
634
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 634
Koz a k, J. (1986). Sh a p e Pr es er v i n g Ap p r oxi ma t i on , Comput ers i n Indust ry. 7,
435-440. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Kr ieger, R. A. (1984). 3-D Environment s for 2-D Animat ion, Mat h Essay, Univer sit y of
Wat er loo, Ont ar io. (A Fast Algor it hm for Gener al Rast er Rot at ion)
Lane, J. (1989). Per sonal commu nicat ion. (A Bzier Cu r ve-Based Root -Find er )
Lawson, C. L., and Hanson, R. J. (1974). Solving Least -Squares Problems. Pr ent ice-Hall,
Englewood Cliffs, N.J. (Compu t ing Su r face Nor mals for 3D Mod els)
LEcu yer, P. (1988). Efcient and Por t able Combined Rand om Nu mber Gener at or s,
Communicat ions of t he ACM. 31(6), 742749. (Efcient Gener at ion of Sampling Jit t er
Using Look-u p Tables)
Lee, M. E., Red ner, R. A., and Uselt on, S. P. (1985). St at ist ically Opt imized Sampling for
Dist r ibu t ed Ray Tr acing, Comput er Graphics. 19(3), 5172. (Efcient Gener at ion of
Samp ling Jit t er Using Look-u p Tables)
Levoy, M. S. (1981). Ar ea Flood ing Algor it hms, p r esent ed at SIGGRAPH 82 2D
Animat ion Tu t or ial. (Filling a Region in a Fr ame Bu ffer )
Lien, S., Shant z, M., and Pr at t , V. (1987). Ad apt ive For ward Differencing for Rend er ing
Cu r ves and Su r faces, Comput er Graphics (SIGGRAPH). 21(4). (Tu t or ial on For ward
Differencing)
Limb, J. O., Ru binst ein, C. D., and Thompson, J. E. (1977). Digit al Cod ing of Color Vid eo
Si gn a l s A Rev i ew, IEEE Trans. Communi cat i on. 25(11), 13491385. (A Di gi t a l
Dissolve Effect )
Lorenson, W., and Cline, H. (1987). Marching Cu bes: A High Resolu t ion 3D Su r face
Con s t r u ct i on Al gor i t h m, Comput er Graphics. 21(4), 163-169. (Den i n g Su r fa ces
from Samp led Dat a)
Lozover, 0., and Preiss, K. (1981). Au t omat ic Gener at ion of a Cu bic B-Spline Represen-
t at ion for a Gener al Digit ized Cu r ve. In Encar ancao, J. L., ed ., Eurographics 81.
Nor t h-Holland . (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Ma i l l ot , P. -G. (1983). PATK3D: Logi ci el i nt ract i f de v i sual i sat i on dobj et s en 3
dimensions, Univer sit Clau d e Ber nard , Lyon I, Lyon, Fr ance. (Using Qu at er nions for
Cod ing 3D Tr ansfor mat ions)
Ma i l l ot , P. -G. (1986). Cont ri but i on l t ude des sy st nes graphi ques, archi t ect ures
logicielle et mat rielle, Ph.D. Thesis. Univer sit Clau d e Ber nard , Lyon I, Lyon, Fr ance.
(Using Qu at er nions for Cod ing 3D Tr ansfor mat ions)
McKeown, K. R., and Bad ler, N. I. (1980). Cr eat ing Polyhed r al St ellat ions, ACM
Comput er Graphics (SIGGRAPH). 14(3), 19-24. (A Digit al Dissolve Effect )
Max, N. (1989). Smoot h Appear ance for Polygonal Su r faces, Visual Comput er. Vol. 5,
160-173. (Compu t ing Su r face Nor mals for 3D Mod els)
Meagher, D. (1982). Geomet r ic Mod elling Using Oct ree Encod ing, Comput er Graphics
and Image Processing. 19(2), 129147. (A Simp le Met hod for Color Qu ant izat ion:
Oct ree Qu ant izat ion; Spheres-t o-Voxels Conver sion)
Misner, C. W., Thor ne, K. S., and Wheeler, J. A. (1973). Gravit at ion. Freeman. (For ms,
Vect or s, and Tr ansfor ms)
635
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 635
Mor t on , (1986). A Digit al Dissolve for Bit -Map p ed Gr ap hics Scr eens, Dr. Dobbs
Journal. (A Digit al Dissolve Effect )
Newell, M. E., and Sequ in, C. H. (1980). The Insid e St or y on Self-Int er sect ing Polygons,
Lambda. 1(2), 2024. (Fast Scan Conver sion of Ar bit r ar y Polygons)
Newman, W. M., and Sp r ou ll, R. F. (1979). Principles of Int eract ive Comput er Graph-
ics. McGr aw-Hill, New Yor k. (A Fast Algor it hm for Gener al Rast er Rot at ion (r st ed.); A
Digit al Dissolve Effect ; 3D Viewing and Rot at ion Using Or t honor mal Bases (second
ed .); The Use of Coord inat e Fr ames in Compu t er Gr aphics)
Nicholl, T. M., Nicholl, R. A., and Lee, D. T. (1987). An Efcient New Algor it hm for 2-D
Line Clip p ing, ACM Comput er Graphics. 21(4), 253261. (Two-Dimensional Clip -
p ing: A Vect or-Based Approach)
Niimi, H., et al. (1984). A Par allel Pr ocessor Syst em for Thr ee-Dimensional Color
Gr ap hics, Comput er Graphics (SIGGRAPH). 20(4), 95101. (Scanline Dept h Gr ad ient
of a Z-Bu ffered Tr iangle)
Nishimu r a, H., Ohno, H., Kawat a, T., Shir akawa, I., and Omu r a, K. (1983). LINKS-1: A
Par allel Pip elined Mu lt imicrocompu t er Syst em for Image Creat ion, Conference Pro-
ceedi ngs of t he 10t h A nnual Int ernat i onal Sy mposi um on Comput er A rchi t ect ure,
SIGGARCH, 387394. (Efcient Gener at ion of Sampling Jit t er Using Look-u p Tables)
Op p enheim, A. V., and Schaeffer, R. W. (1975). Digit al Signal Processing. Pr ent ice-Hall,
Englewood Cliffs, N.J. (Filt er s for Common Resampling Tasks)
Paet h , A. W. (1986). Desi gn an d Exp er i en ce wi t h a Gen er al i zed Rast er Tool ki t ,
Proceedings, Graphics Int erface 86, Canad ian Infor mat ion Pr ocessing Societ y, Van-
cou ver. (Med ian Find ing on a 3 3 Gr id ; A Digit al Dissolve Effect ; Proper Tr eat ment
of Pixels as Int eger s)
Paet h, A. W. (1986a). A Fast Algor it hm for Gener al Rast er Rot at ion, Proceedings,
Graphics Int erface 86, Canad ian Infor mat ion Processing Societ y, Vancou ver. 77-81.
(A Fast Algor it hm for Gener al Rast er Rot at ion; Tr ansfor mat ion Id ent it ies)
Paet h, A. W. (1986b). The IM Rast er Toolkit Design, Imp lement at ion, and Use,
Univer sit y of Wat er loo Technical Repor t CS-86-65. (Med ian Find ing on a 3 3 Gr id ; A
Fast Algor it hm for Gener al Rast er Rot at ion; A Digit al Dissolve Effect ; Proper Treat -
ment of Pixels as Int eger s)
Paet h, A. W. (1987). The IM Rast er Toolkit Design, Implement at ion, and Use, Inst i-
t ut e for Comput er Research Repor t UW/ ICR 8703. (Median Finding on a 3 3 Gr id; A
Fast Algor it hm for Gener al Rast er Rot at ion; A Digit al Dissolve Effect ; Proper Treat -
ment of Pixels as Int eger s)
Paet h, A. W. (1988). Lemming Ed it or, IRIS Soft ware Exchange. Su mmer 1988 (1), 17.
(A Fast 2D Point -on-Line Test ; A Fast Appr oximat ion t o t he Hypot enu se)
Paet h, A. W. (1989a). Algor it hms for Fast Color Cor rect ion, Proceedings of t he SID
(Societ y for Infor mat ion Disp lay). 30(3), 169175. (Pr op er Tr eat ment of Pixels as
Int eger s)
Paet h, A. W. (1989b). Algor it hms for Fast Color Cor rect ion, Univer sit y of Wat er loo
Technical Repor t CS-89-42. (Proper Treat ment of Pixels as Int eger s)
636
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 636
Paet h, A. W. (t o ap p ear ). Color Cu bes and Color Polyhed r a and Color Map p ing.
(A Digit al Dissolve Effect )
Paet h, A. W. (t o appear ). Fast Med ian Find ing Using Exchange Net wor ks, Univer sit y of
Wat er loo Technical Repor t CS-90. (Med ian Find ing on a 3 3 Gr id )
Par s, L. A. (1965). A Treat ise on Analyt ical Dynamics. John Wiley and Sons, New Yor k.
(Rot at ion Tools)
Pavlid is, T. (1983). Cu r ve Fit t ing wit h Conic Splines, ACM Transact ions on Graphics.
2, 131. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
PHIGS. (1987). Programmer s Hi erarchi cal Int eract i v e Graphi cs Sy st em (PHIGS)
ISO, DIS 9592-1. (Using Qu at er nions for Cod ing 3D Tr ansfor mat ions)
Piegl, L. (1986). Cu r ve Fit t ing Algor it hm for Rou gh Cu t t ing, Comput er-Aided Design.
18(2), 7982. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Pinker t , J. R. (1976). An Exact Met hod of Find ing Root s of a Complex Polynomial,
ACM Transact ions on Mat hemat ics. 2(4). (Using St u r m Sequ ences t o Br acket Real
Root s of Polynomial Equ at ions)
Piqu e, M. E. (1987). Semant ics of Int er act ive Rot at ions, Proceedings of 1986 Work-
shop on 3D Graphics, 259269. (Rot at ion Tools)
Plass, M., and St one, M. (1983). Cu r ve-Fit t ing wit h Piecewise Par amet r ic Cu bics,
Comput er Graphics. 17(3), 229239. (An Algor it hm for Au t omat ically Fit t ing Digit ized
Cu r ves)
Phong, B.-T. (1973). Illu minat ion for Comp u t er-Gener at ed Images, Univ. of Ut ah
Comp u t . Sci. Dept . UTEC-CSC 73, 129. (Compu t ing Su r face Nor mals for 3D Mod els)
Por t er, T., and Du ff, T. (1984). Composing Digit al Images, ACM Comput er Graphics
(SIGGRAPH). 18(3), 253259. (A Fast Algor it hm for Gener al Rast er Rot at ion; Alpha
Blend ing)
Pr es s , W. H. (1988). Numeri cal Reci pes i n C: The A rt of Sci ent i c Comput i ng.
Cambr id ge Univer sit y Press. (Efcient Gener at ion of Sampling Jit t er Using Look-u p
Tables)
Ralst on, A. (1965). A First Course in Numerical Analysis. McGr aw-Hill. (Tu t or ial on
For ward Differencing)
Reeves, W. T. (1981). Qu ant it at ive Rep resent at ions of Comp lex Dynamic Shap es for
Mot ion Analysis, Ph.D. t hesis, Univer sit y of Toront o. (An Algor it hm for Au t omat ically
Fit t ing Digit ized Cu r ves)
Reinsch, C. H. (1967). Smoot hing by Sp line Fu nct ions, Numerische Mat hemat ik. 10,
177183. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Roger s, D. F. (1985). Procedural Element s for Comput er Graphics. McGr aw-Hill, New
Yor k. (Sp heres-t o-Voxels Conver sion; Concave Polygon Scan Conver sion)
Roger s, D. F., and Ad ams, J. A. (1976). Mat hemat ical Element s for Comput er Graph-
ics. McGr aw-Hill, New Yor k. (Rot at ion Tools)
Rokne, J. G., Wyvill, B., and Wu , X. (1990). Fast Line Scan-Conver sion, ACM Transac-
t ions on Graphics. In press. (Symmet r ic Dou ble St ep Line Algor it hm)
637
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 637
Ross, S. (1976). A First Course in Probabilit y. Ma cmi l l a n . (Mu l t i d i men si on a l Su m
Tables)
Sa l mon , R. , a n d Sl a t er, M. (1987). Comput er Graphi cs: Sy st ems and Concept s.
Ad d ison-Wesley, Read ing, Mass. (3D Viewing and Rot at ion Using Or t honor mal Bases)
Scheid , F. (1968). Schaums Out line of Numerical Analysis. McGr aw-Hill. (Tu t or ial on
For ward Differencing)
Schmit t , F. J. M., Bar sky, B. A., and Du , W. H. (1986). An Ad apt ive Su bd ivision Met hod
for Su r face-Fit t ing from Sampled Dat a, Proceedings of SIGGRAPH 86, 179188. (An
Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Schneid er, P. J. (1988). Phoenix: An Int er act ive Cu r ve Design Syst em Based on t he
Au t omat ic Fit t ing of Hand -Sket ched Cu r ves, Mast er s t hesis, Univer sit y of Washingt on.
(A Bzier Cu r ve-Based Root -Find er ; Solving t he Nearest -Point -on-Cu r ve Problem; An
Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Schoenber g, I. J. (1973). Cardinal Spline Int erpolat ion. SIAM. (Exp licit Cu bic Sp line
Int er p olat ion For mu las)
Schu maker, R. A., Br and , R., Gilliland , M., and Shar p, W. St u d y for Applying Compu t er
Gener at ed Images t o Visu al St imu lat ion, AFHRL-TR-69-14, U.S. Air Force Hu man
Resou rces Labor at or y. (A Digit al Dissolve Effect )
Schweiker t , D. G. (1966). An Int er polat ion Cu r ve Using a Spline in Tension, J. Mat h.
Phys. 45, 312317. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Sed gewick, R. (1983). Algorit hms. Ad d ison-Wesley, Read ing, Mass. (Two-Dimensional
Clip p ing: A Vect or-Based Approach)
Shani, U. (1980). Filling Regions in Binar y Rast er Images: A Gr aph-Theoret ic Appr oach,
SIGGRA PH 80 Conf erence Proceedi ngs, 321327. (Fi l l i n g a Regi on i n a Fr a me
Bu ffer )
Shani, U., and Ballard , D. H. (1984). Splines as Embed d ings for Gener alized Cylind er s,
Comput er Vi si on, Graphi cs, and Image Processi ng. 27. (Ca l cu l a t i on of Refer en ce
Fr ames along a Space Cu r ve)
Sh a p i r a , A. (1990). Sou r ce Cod e for Ter r a i n Vi si bi l i t y Pr ogr a ms, Comput at ional
Geomet ry Lab. Techni cal Report #90-0418, ECSE Dep t . , Ren s s el a er Pol yt ech n i c
Inst it u t e. (Fast Line-Ed ge Int er sect ions on a Unifor m Gr id )
Shinya, M., Sait o, T., and Takahashi, T. (1989). Rend er ing Techniqu es for Tr ansparent
Object s, Proc. Graphics Int erface, 173182. (Shad ow At t enu at ion for Ray Tr acing
Tr ansp arent Object s)
Shinya, M., Takahashi, T., and Nait o, S. (1987). Pr inciples and Applicat ions of Pencil
Tr acing, Comput er Graphics (SIGGRAPH). 21(4), 4554. (Shad ow At t enu at ion for
Ray Tr acing Tr ansparent Object s)
Shoemake, K. (1985). Animat ing Rot at ion wit h Qu at er nion Cu r ves, ACM, Siggraph
85. 19(3) 245254. (Using Qu at er nions for Cod ing 3D Tr ansfor mat ions)
Sloan, K. R., Jr., and Tanimot o, S. L. (1979). Progressive Renement of Rast er Images,
IEEE Trans. on Comput ers. C-28(11), 871874. (Recor d i n g An i ma t i on i n Bi n a r y
Ord er for Pr ogressive Tempor al Renement )
638
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 638
Sloan, K. R., Jr., and Brown, C. M. (1979). Color Map Techniqu es, Comput er Graphics
and Image Processing. 10(4), 297317. (Mapping RGB Tr iples ont o Fou r Bit s)
Smi t h , A. R. (1978). Col or Ga mu t Tr a n s for m Pa i r s , A CM Comput er Graphi cs
(SIGGRAPH). 12(3), 1219. (A Digit al Dissolve Effect ; Proper Treat ment of Pixels as
Int eger s; A Fast HSL-t o-RGB Tr ansfor m)
Smit h , A. R. (1979). Tin t Fill, SIGGRAPH 79 Proceedings, 279283. (A Seed Fill
Algor it hm)
Smit h, A. R. (1982). Fill Tu t or ial Not es, present ed at SIGGRAPH 82 2D Animat ion
Tu t or ial. (Filling a Region in a Fr ame Bu ffer )
Smit h, A. R. (1987). Planar 2-Pass Text u re Map p ing and War p ing, ACM Comput er
Graphics (SIGGRAPH). 21(4), 263272. (A Fast Algor it hm for Gener al Rast er Rot at ion)
Sn yd er, J. P. (1984). Map Proj ect i ons Used by t he U. S. Geol ogi cal Surv ey. U. S.
Gover nment Pr int ing Ofce, Washingt on, D.C. (Alber s Equ al-Area Conic Map Projec-
t ion)
Snyd er, J. M., and Bar r, A. H. (1987). Ray Tr acing Complex Mod els Cont aining Su r face
Tessellat ions, ACM Comput er Graphics. 21(4). (An Efcient Ray-Polygon Int er sec-
t ion)
St u r m, C. (1835). Mm. Pr sent s par Diver s Savant s, lAcad . Royale d es Sciences d e
lInst it u t d e Fr ance, t . 6, Par is. (Using St u r m Sequ ences t o Br acket Real Root s of
Polynomial Equ at ions)
Su t her land , I. E., and Hod gman, G. W. (1974). Reent r ant Polygon Clipping, Communi-
cat ions of t he A CM. 17(1), 3242. (Gen er i c Con vex Pol ygon Scan Con ver si on an d
Clip p ing; Two-Dimensional Clipping: A Vect or-Based Appr oach; Fast Ant i-Aliasing Poly-
gon Scan Conver sion)
Swanson, R. W., and Thayer, L. J. (1986). A Fast Shad ed -Polygon Rend erer, Comput er
Graphics (SIGGRAPH). 20(4), 95101. (Scan l i n e Dep t h Gr ad i en t of a Z-Bu ffer ed
Tr iangle)
Sweeney, M. (1984). The Wat er loo CGL Ray Tr acing Package, Mast er s t hesis, Dept . of
Comp u t er Science, Univer sit y of Wat er loo. (Shad ow At t enu at ion for Ray Tr acing Tr ans-
p arent Object s)
Tosa n , E. (1982). Quat ernions et rot at ions dans lespace. Tu t or i a l n ot es. Un i ver si t
Clau d e Ber nard , Lyon I, Lyon, Fr ance. (Using Qu at er nions for Cod ing 3D Tr ansfor ma-
t ions)
Tou nt as, C., and Kat z, L. (1971). Int er act ive Gr aphics in Molecu lar Biology: Real-t ime
Three-d imensional Rot at ions of Images and Image Fr agment s, Proceed ings Su mmer
Comp u t er Simu lat ion Conference. 1, 241247. (Rot at ion Tools)
Tu r kowski, K. E. (1982). Ant i-Aliasing t hrou gh t he Use of Coord inat e Tr ansfor mat ions,
ACM Trans. Graphics 1(3), 215234. (Tr igonomet r y wit h CORDIC It er at ions)
Tu r kowski, K. E. (1986). Ant i-Aliasing in Topological Color Spaces, ACM Comput er
Graphics. (SIGGRAPH). 20(3), 307314. (A Digit al Dissolve Effect )
Tu r nbu ll, M. A. (1957). Theory of Equat ions, ft h ed it ion (repr int of or iginal 1927 t ext ).
639
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 639
Oliver and Boyd , Ed inbu rgh. (Using St u r m Sequ ences t o Br acket Real Root s of Polyno-
mial Equ at ions)
Van Wyck, C. J. (1984). Clipping t o t he Bou nd ar y of a Circu lar-Arc Polygon, Comput er
Vision, Graphics, and Image Processing. 25, 383392. (Two-Dimensional Clip p ing: A
Vect or-Based Approach)
Vercken, C., Pot ier, C., and Vignes, S. (1987). Spline Cu r ve Fit t ing for an Int er act ive
Desi gn En vi r on men t , i n Theoret ical Foundat ions of Comput er Graphics and CA D.
NATO. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Vold er, J. E. (1959). The CORDIC Tr igonomet r ic Comp u t ing Techniqu e, IRE Trans.
Elect ron. Comput . EC-8(3), 330334. (Tr igonomet r y wit h CORDIC It er at ions)
Walt her, J. S. (1971). A Unied Algor it hm for Element ar y Fu nct ions. In Proceedings of
A PS 1971 Spring Joint Comput er Conference, Vol. 38, p p . 379385. APS Pr ess,
Ar lingt on, Virginia. (Tr igonomet r y wit h CORDIC It er at ions)
Wat kins, M. A. (1987). Degr ee Red u ct ion of Bzier Cu r ves and Su r faces, Mast er s
t hesis, Univer sit y of Ut ah. (An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Weiman, C. F. R. (1989). Cont inu ou s Ant i-Aliased Rot at ion.and Zoom of Rast er Images,
ACM Comput er Graphics (SIGGRAPH). 23(3), 291. (A Fast Algor it hm for Gener al
Rast er Rot at ion)
Whit t ed , T., and Weimer, D. M. (1981). A Soft ware Test bed for t he Development of 3D
Rast er Gr ap hics Syst ems, 1(1), 4358. (Gener ic Convex Polygon Scan Conver sion and
Clip p ing)
Wi l ki n son , J. H. (1988). The Algebraic Eigenvalue Problem (r ep r i n t of or i gi n al 1965
ed it ion). Clared on Press, Oxford . (Using St u r m Sequ ences t o Br acket Real Root s of
Polynomial Equ at ions)
Williams, L. (1978). Cast ing Cu r ved Shad ows on Cu r ved Su r faces, Comput er Graphics
(SIGGRAPH). 12, 270274. (Mat r ix Inver sion)
Williams, L. (1983). Pyr amid al Par amet r ics, Comput er Graphics (SIGGRAPH). 17(3).
(Int er p ret at ion of Text u re Map Ind ices)
Wolberg, G., and Bou lt , T. E. (1989). Separ able Image War ping wit h Spat ial Looku p
Tables, ACM Comput er Graphics (SIGGRAPH). 23(3), 369377. (A Fast Algor it hm
for Gener al Rast er Rot at ion)
Wu , S. C., Abel, J. F., and Greenbu rg, D. P. (1977). An Int er act ive Compu t er Gr aphics
Ap p roach t o Su r face Repr esent at ion, Communicat ions of t he ACM. 20(10), 703712.
(An Algor it hm for Au t omat ically Fit t ing Digit ized Cu r ves)
Wu , X., and Rokne, J. G. (1987). Dou ble-St ep Increment al Gener at ion of Lines and
Ci r cl es, Comput er Vision, Graphics and Image Processing. 37, 331334. (Symmet -
r ic Dou ble St ep Line Algor it hm)
Wyvill, G., McPheet er s, C., and Wyvill, B. (1986). Dat a St ru ct u r e for Soft Object s, The
Visual Comput er. 2, 227-234. (Dening Su r faces from Sampled Dat a)
Yamagu chi, F. (1978). A New Cu r ve Fit t ing Met hod Using a CRT Compu t er Display,
640
REFERENCES
GRAPHIC GEMS I Edit ed by DAVID KIRK 640
Graphics and Image Processing, 425437. (An Algor it hm for Au t omat ically Fit t ing
Digit ized Cu r ves)
Yang, M., Kim, C., Cheng, K., Yang, C., and Liu , S. (1986). Au t omat ic Cu r ve Fit t ing wit h
Qu ad r at ic B-Spline Fu nct ions and It s Applicat ions of Compu t er-Aid ed Animat ion,
Comput er Vision, Graphics, and Image Processing. 33, 346363. (An Algor it h m for
Au t omat ically Fit t ing Digit ized Cu r ves)
Yellot , J. I., Jr. (1983). Spect r al Consequ ences of Phot orecept or Sampling in t he Rhesu s
Ret ina, Science. No. 221, 382385. (Efcient Gener at ion of Samp ling Jit t er Using
Look-u p Tables)
Zwi kker, C. (1963). A dv anced Geomet ry of Plane Curv es. Dover Pu bl i ca t i on s, New
Yor k. (Usefu l 2D Geomet r y)
641 641
INDEX
Gau ssian lt er, 150153
Gaussian 1/ 2 lter frequency response, 163
Gau ssian 1/

2
lt er frequ ency resp onse,
164
half-p hase lt er fr equ ency r esp onse,
162163
int er p olat ion and d ecimat ion, 147148
interpolation by factor of two with Lanczos2
sinc fu nct ion, 158159
int er p olat ion wit h Gau ssian 1/ 2 lt er,
152154, 156
int er p olat ion wit h Gau ssian 1/

2
lt er,
154156
Lanczos2 sinc functions frequency response,
164165
Lanczos-windowed sinc functions, 156158
sinc fu nct ion, 156157
t ent lt er, 149150
zer o-p hase lt er frequ ency r esp onse, 162
cone p lu s cosine, 145146
Gau ssian lt er, 144145
minimizing bu mp y samp ling, 144146
Ar ea, t r iangles, 20
Ar ea of int er sect ion
cir cle and half-p lane, 3839
cir cle and t hick line, 4042
t wo cir cles, 4346
Axes, t r ansfor ming, 456459
Axis-aligned bou nd ing boxes, t r ansfor mat ion,
548550
Axis-amou nt r ep resent at ion, conver sion wit h
mat r ix, 466467
Azimu t hal equ al-area p r oject ion, 317
A-bu ffer, 76
Act ive ed ge list , 9293
Ad d resses, p r ecalcu lat ing, 285286
Ad joint mat r ices, 538
Afne modeling transformations, normal vectors,
539542
Albers equalarea conic map projection, 321325
Alp ha blend ing, 210211
Alp ha bu ffer, 218
Alp ha comp osit ing op er at or s, 211
Alt it u d es, t r iangles, int er sect ion, 22
Angles
encod ing, bit p at t er ns, 442
su m and d ifference r elat ions, 16
Animat ion, record ing, 265269
d ou ble-fr amed ord er, 265266
Ant i-aliased lines, rend er ing, 105106
Ant i-aliasing, 37, see also Ar ea of int er sect ion
ad vanced , algor it hm, 194195
p ixel, 73
p olygon scan conver sion, 7683
Ant i-aliasing lt er s, 143
common resamp ling t asks, 147165
box lt er, 149
choices of lt er s, 148
comp ar at ive frequ ency resp onses, 161
comp ar ison of lt er s, 151152
cont inuous, sampled , and d iscret e signals,
147
d ecimat ion wit h Lanczos2 sinc fu nct ion,
160161
A
I II IINDEX NDEX NDEX NDEX NDEX
642 642
INDEX
Bu mp y samp ling, ant i-aliasing lt er s t hat
minimize, 144146
C
Canonical ll algor it hm, 279
Car d anos For mu la, 405
Car t esian color cu bes, 254255
Car t esian p rod u ct s
color p ixels, 254256
ext ensions, 256
Car t ogr ap hy, see Digit al car t ogr ap hy
Car t esian t r ip le, fact or ing int o, 255256
Cell occu p ancy, for d ifferent gr id sizes, 262
Cent er of gr avit y, t r iangles, 2021
Cent r al p r oject ion, 315
Change mat r ix, int er at ion, 468469
Channel, fr ame bu ffer, 217218
Chor d -lengt h p ar amet er izat ion, 617, 621
Cir cle
ar ea of int er sect ion
wit h half-p lane, 3839
wit h t hick line, 4042
t wo cir cles, 4346
Br esenhams algor it hm, 327329
d r awing, shear algor it hm, 192193
int egr al r ad iu s, on int egr al lat t ices, 5760
int er sect ion
wit h line, 2D, 56
wit h r ect angle, fast checking, 5153
t angent line
p er p end icu lar t o line, 89
t o t wo circles, 2D, 78
2D, 5
2D, 4-5
Cir cu mcent er, t r iangles, 2023
Cir cu mr ad iu s, t r iangles, 2023
Class nu mber s, 115116
Clip p ing
gener ic convex p olygons, 8486
2D, see Two-d imensional clip p ing
Closed loops, cubic spline interpolation formulas,
580582
Color cu be, 233234, 254255
Color map , 216218
manip u lat ion, l-t o-l p ixel t r ansfor ms,
270274
Color p ixels, Car t esian p rod u ct s, 254256
Color qu ant izat ion, see Oct ree qu ant izat ion
Color solid , fou r -bit , 235236
Azimu t hal equ id ist ant p roject ion, 316317
Azimu t hal p roject ions, 314317
B
Backface cu lling, 346347, 544545
Band ing, 263
Ber nst ein basis funct ions, int egr at ion, 604606
Ber nst ein-Bzier, equ at ion conver sion t o,
409411
Ber nst ein p olynomials, 613614
Beveling, 107113
Bzier cu r ve-based root -nd er, 408415
bou nd ing box, 413414
conversion to Bernstein-Bzier form, 409411
nd ing r oot s, 411415
root -nd ing algor it hm, 412413
Bzier cu r ves, 613616, see also Cu bic Bzier
cu r ves
d e Cast elJau Evaluat ion Algor it hm, 587589
d ifferent iat ion, 589590
t t ing t o d igit ized cu r ve, 616624
monomial evalu at ion algor it hm, 590591
not at ion, 587
p rop er t ies, 587593
in t er ms of Ber nst ein p olynomials, 613614
Bzier for m, conver sion, 609611
fr om monomial for m, 591592
t o monomial for m, 592593
Binar y ord er, animat ion recor d ing, 266269
Bisect ion, St u r m sequ ences, 420421
Bit Blt , gener alized , algor it hm, 193194
Bit s
conver sion wit h d igit s, 435
int er leaving, qu ad and oct rees, 443447
p at t er ns, encod ing angles, 442
Bou nd ing box
axis-aligned , t r ansfor mat ion, 548550
ft h-d egree p olynomial, 413414
Bou nd ing sp here, 301303
Box, 326
int er sect ion wit h r ay, fast , 395396
Box lt er, 149, 180
Box-sp here int er sect ion t est ing, 335339
Br anching, 558561
Bresenhams algor it hm, 101
sp heres-t o-voxels conver sion, 327329
Bresenhams Line Algor it hm, 105106
643 643
INDEX
Color t able, lling, 290291
Comp lexit y analysis, RGB t r ip les, 244
Comp u t at ional cost , jit t er gener at ion, 6768
Concave p olygon scan conver sion, 8791
Cone-ellip soid int er sect ion, 321322
Cone p lu s cosine, 145146
Const ant s, fu ll p recision, 434
Cont inu it y cond it ions, cu bic Bzier cu r ves,
615616
Cont inu ou s image, 246
Cont inu ou s signals, 147
Cont our d at a, d ening sur faces from, 558561
Cont ou r s
d ening, 554
swep t , 562564
Cont r ast enhancement t r ansfor m, 197198,
201202, 270271, 274
Convex d ecomp osit ions, p olygons, 97
Coord inat e fr ames, 522532
mat r ix rep resent at ion, 524
p roblem solving examp les, 527532
vect or s and p oint s, 522523, 526
Coord inat e Rot at ion Digit al Comp u t er, see
CORDIC
Coord inat es, homogeneou s and
nonhomogeneou s, 523
CORDIC, vect or rot at ion, 494497
Cor ner valu e, 553554
Cosine
angles bet ween lines, 2D, 11
in exp onent ials, 15
Cr amer s Ru le, 538
Cross p rod u ct , mat r ix, 473
Cross-sect ion, p osit ioning and or ient ing, 570
Cu bic Bzier cu r ves, 579, 614
cont inu it y cond it ions, 615616
for ward d ifferencing, 600601, 603
Cu bic cu r ves, p lanar, 575578
Cu bic root s, 404407
Cu bic sp line int er p olat ion for mu las, 579584
closed loop s, 580582
op en cu r ves, 582583
Cu boct ahed r on, 237
Cu lling, backface, 346347, 544545
Cu r vat u re vect or, 568
Cu r ves
t t ing, see Digit ized cu r ve t t ing
op en, cu bic sp line int er p olat ion for mu las,
582583
Cylind er
wit h changing cross-sect ions, 570571
gener alized , r eference fr ames, 567
Cylind r ical equ al ar ea, 318319
Cylind r ical equ ir ect anglu ar map , 310
Cylind r ical map s, 310311
D
Dat abases, d ir ect char t ing, 309310
Dat a valu e, 30
DDA algor it hm, 595
d e Cast eljau Evalu at ion Algor it hm, 587589,
604605
Decimat ion, 148
Gaussian 1/ 2 lter, frequency response, 163
Gaussian 1/

2
lter, frequency response, 164
Lanczos2 sinc functions, frequency response,
164165
by fact or of fou r, 161
by fact or of t hree, 160
by fact or of t wo, 158159
by t wo, fr equ ency resp onse
half-p hase lt er s, 162163
Lanczos2 sinc fu nct ions, 164165
zer o-p hase lt er s, 162
Del op er at or, 594595, 598
DeMoivr es Theorem, 15
Dep t h bu ffer, 218
Dep t h cu ing, 365
Different iat ion algor it hm, Bzier cu r ves,
589590
Digit al car t ogr ap hy, 307320
azimu t hal p roject ions, 314317
cylind r ical equ al ar ea, 318319
cylind r ical equ irect anglu ar map , 310
cylind r ical map s, 310311
d ir ect char t ing of d at abases, 309310
r st p r incip les, 308309
gener al p r oject ions, 318
Mer cat or p roject ion, 311313
or t hogr ap hic p roject ion, 309, 316
p r act ical ap p licat ion, 319320
p roject ion p rop er t ies, 307308
Sanson-Flamst eed sinu soid al p roject ion,
312314
spher ical coord inat e t r ansfor mat ion, 317318
Digit al d issolve effect , 221232
case st u d y, 229231
fast er map p ing, 227228
r st at t emp t , 223224
fu r t her resear ch, 231232
644 644
INDEX
Embedding plane, intersection with ray, 390391
Enlargement , monochrome images, smoot hing,
166170
Eu clid ian d ist ance, 3D, fast ap p roximat ion,
432433
F
Fact or ial p olynomials, 595596
Fast ll algor it hm, p recalcu lat ing ad d r esses,
285286
Fast lines, rend er ing on r ast er gr id , 114120
Hobbys p olygonal p ens, 114117
soft war e imp lement at ion, 117120
Fat lines, r end er ing on r ast er gr id , 114120
Fer mat p r imes, 18
Fill algor it hms, 278284
canonical, 279
op t imal, 281282
p rocessing shad ows, 280281
Filt er wind ows, 194195
Fir st fu nd ament al mat r ix, 543544
Fixed -p oint t r igonomet r y, CORDIC, 494497
Fog, simu lat ing, 364365
Font r end er ing, t hree-p ass algor it hm, 193
For ms
d ifferences wit h vect or s, 533535
t r iangu lar int er p olant s, 535538
For war d d ifferencing, 594603
Bzier cu bics imp lement at ion, 603
DDA algor it hm, 595
Del op er at or, 594595, 598
fact or ial p olynomials, 595596
locally cir cu lar assu mp t ion, 599600
Newt ons for mu la, 596598
st ep size d et er minat ion, 599601
su bd ivid ing, 601603
Fr ame bu ffer, 215216
associat ed color map , 217
ll algor it hms, 278284
p lane, 217218
Fr enet fr ame, 567568
Fu ll-p r ecision const ant s, 434
G
Gamma cor rect ion funct ion, 199, 203206, 270,
273
Digit al d issolve effect (Cont d )
op t imizat ions, 230231
r and omly t r aver sing 2D ar r ay, 221222
scr ambling int eger s, 222223
Digit al line d r awing, 99100
Digit ized cu r ve t t ing, au t omat ic, algor it hm,
612626
chor d -lengt h p ar amet er izat ion, 617, 621
imp lement at ion not es, 624625
Newt on-Rap hson it er at ion, 621623
scalar cu r ves, 616
Digit s, conver sion wit h bit s, 435
Direct ion r at ios, 456457
Discr et e image, 246
Discr et e signals, 147
Discr iminat or, 101
Dissolve algor it hm, 225227
Dist ance, ap p roximat e, 423
Dist ance measu res
ap p roximat e vect or lengt h, 429
equ at ions of u nit d ist ance, 428
fast approximation to 3D Euclidian distance,
432433
fast ap p roximat ion t o hyp ot enu se, 427
fu ll-p r ecision const ant s, 434
high sp eed , low p recision squ are r oot ,
424426
Dist ance var iable, 105
Dit her ing, ord ered , 176178
Dit her ing mat r ix, 177
Dot p r od u ct s, for shad ing, 348360
d ir ect algor it hm, 351352, 359
d ir ect ing vect or s, 348
new algor it hm, 351-352, 359360
reect ion of light , 349352
refr act ion, 353354
Snells law, 353354
Dou ble-angle relat ions, 17
Dou ble-fr amed ord er, 265266
Dou ble sp eed Bresenhams, 101102
Du al solid s, 236237
Dymaxion gnomonic p roject ion, 316
E
Ellip soid
int er sect ion wit h cone, 321322
box-sphere int er sect ion t est ing, gener alizing,
338-339
645 645
INDEX
Gau ssian lt er, 144145, 150153
Gau ssian 1/ 2 lt er
fr equ ency resp onse, 163
int er p olat ion, 152154, 156
Gau ssian 1/

2
lt er
fr equ ency resp onse, 164
int er p olat ion, 154156
Gener al d irect ion r at ios, 457
Geod esics, 315
Geomet r ic cont inu it y, 615616
Gnomonic p r oject ion, 315
n-gon, 18
Gou r au d shad ing, 84
Gr ap h labels, nice nu mber s, 6163
Gr ay int er ior p oint s, 244
H
Half-angle relat ions, 16
Half-p hase lt er s, frequ ency r esp onse,
d ecimat ion by t wo, 162
Half-p lane, area of int er sect ion, wit h circle,
3839
Half-sp ace t est ing, 240241
Har d ware, scr ambling int eger s in, 222223
Hashing fu nct ion, 3D gr id , 343345
Haze, simu lat ing, 364365
Hexagonal const r u ct ion, 238
Hobbys p olygonal p ens, 114117
Hollow objects, box-sphere intersection testing,
337338
Homogeneou s coord inat es, 523
HSL Sat u r at ion, 449
HSL-t o-RGB t r ansfor m, fast , 448449
HSV Sat u r at ion, 448
Hu e Sat u r at ion Valu e, 239240
Hyp ot enu se
fast ap p roximat ion, 427431
d er ivat ion, 427429
er ror analysis, 429431
t r iangles, 5759
I
Id ent it y, mat r ix, 473
In cent er, t r iangles, 2021
In r ad iu s, t r iangles, 2021
Int eger s, scr ambling, 222223
Int er p olat ion, 148
by fact or of t wo, Lanczos2 sinc fu nct ion,
158159
for mu las, cu bic sp line, 579584
Gau ssian lt er, 150153
Gau ssian 1/ 2 lt er, 152154, 156
Gau ssian 1/

2
lt er, 154156
t ent lt er, 149150
Int er p olat ion coefcient s
closed loop s, 581
op en cu r ves, 583
Isot r op ic t r ansfor mat ions, nor mal vect or, 542
It er at ion, r ot at ion t ools, 468
J
Jit t er, gener at ion, 6474
comp u t at ional cost , 6768
er r or analysis, 72
samp ling p r op er t ies evalu at ion, 6972
Jit t er fu nct ion
u se in r ay t r acing, 7274
u sing look-u p t ables, 6567
L
Lanczos2 sinc fu nct ion
d ecimat ion, 160161
fr equ ency resp onse, 164165
int er p olat ion by fact or of t wo, 158159
Lanczos-wind owed sinc fu nct ions, 156158
Lattice, integral, integral radius circle on, 5760
Law of Cosines, 13
Law of Sines, 13
Law of Tangent s, 13
Level set , 552
Line
area of int er sect ion, t hick, wit h circle, 4042
d ist ance t o p oint , 10
int er sect ion
calcu lat ion, p olygons, 128
wit h cir cle, 56
p oint of, 2D, 11
3D, 304
t angent
t o circle, 5
646 646
INDEX
Line (Cont d)
t o circle and p er p end icu lar t o line, 89
t o t wo circles, 78
ver t ical d ist ance t o p oint , 4748
Linear congru ent ial gener at or s, 67
Linear feed back shift regist er, 222
Line d r awing, 98
ant i-aliasing lines, 105106
d igit al, 99100
fat lines on r ast er gr id , 114120
lling in bevel joint s, 107113
symmet r ic d ou ble st ep line algor it hm,
101104
t wo-d imensional clip p ing, 121128
Line-ed ge int er sect ions, u nifor m gr id , 2936
Line st r u ct u res, 2D, 34
Locally circu lar assu mp t ion, 599600
Look-u p t able, nonlinear p ixel map p ings, 253
M
Manhat t an d ist ance, 432
Map p ing, see Digit al Car t ogr ap hy
nonlinear p ixels, 251253
or iginal color s ont o rep resent at ives, 291
3D, 306
Map s, cylind r ical, 310311
Mat r ix id ent it ies, 453454
Mat r ix inver sion, 470471
Mat r ix mu lt ip licat ion, fast , 460461
Mat r ix or t hogonalizat ion, 464
Med ian cu t algor it hm, 288
Med ian lt er ing, 171172
Med ian nd ing, 3 3 gr id , 171175
element exchange, 172175
minmax, 173174
Mercat or p r oject ion, 311313
Mer id ians, 310311
Met r ic p rop er t ies, t r ansfor mat ions, 543544
Met r ic t ensor, 543544
Mir ror image, t r ansfor mat ion mat r ices, 474
Mod eling, afne t r ansfor mat ions, 539542
Modied cylindrical equidistant projection, 311
Mollweid es For mu la, 13
Monochrome enlargements, smoothing, 166170
p at t er n wit hin r u les, 169
ru les, 166167
Monomial evaluation algorithm, Bzier curves,
590591
Monomial for m, conver sion
fr om Bzier for m, 592593
t o Bzier for m, 591592
Mu lt id imensional su m t ables, 376381
d -d imensional, 380381
t hree-d imensional, 378380
t wo-d imensional, 376377
Mu lt ip le-angle r elat ions, 17
N
Near est -p oint -on-cu r ve p roblem, 607611
Bzier for m conver sion, 609611
p roblem st at ement , 608609
Nest ing, 467468
Newt on-Rap hson it er at ion, d igit ized cu r ve
t t ing, 621623
Newt ons For mu la, 14, 596598
Nice nu mber s, for gr ap h labels, 6163
Nolid , 238
Nonhomogeneou s coord inat es, 523
Nonlinear p ixel map p ings, 251253
Nor mal bu ffer, 257258
Nor mal cod ing, 257264
encod ing met hod s, 258263
imp roving, 263264
ind ex nu mber, 260
p r incip les, 258259
nor mal bu ffer, 257258
Nor mal map , 260
Nor mal vect or, 539540
Nor mals, see Su r face nor mals
Nu ll t r ansfor m, 196197
O
Oct ree, 288
bit int er leaving, 443447
Oct ree qu ant izat ion, 287293
algor it hm, 289
color t able lling, 290291
evalu at ion of rep resent at ives, 289290
imp rovement s, 291292
map p ing ont o r ep resent at ives, 291
memory and computational expense, 292293
p r incip le, 288
1-t o-1 p ixel t r ansfor ms, 196209
color -map manip u lat ion. 270274
647 647
INDEX
cont r ast enhancement t r ansfor m, 197198,
201202, 270271, 274
gamma cor rect ion fu nct ion, 199, 203206,
270, 273
nu ll t r ansfor m, 196197
phot o-inver sion t r ansfor m, 196, 198, 270271
qu ant izat ion t r ansfor m, 196197, 199, 270,
272
sawt oot h t r ansfor m fu nct ion, 203, 207209
Open curves, cubic spline interpolation formulas,
582583
Or d ered d it her ing, 176178
Or t hogonalizat ion, mat r ix, 464
Or t hogonal loop s, 105
Orthogonal projection, transformation matrices,
475
Or t hogonal t r ansfor mat ions, nor mal vect or s,
542543
Or t hogr ap hic p roject ion, 309, 316
Or t honor mal base
movement from one t o anot her, 508
3D viewing and r ot at ion u sing, 516521
gener al rot at ions, 520521
new ap p roach, 517520
UVN coord inat e syst em, 518519
viewing t r ansfor mat ion, pseud o-cod e, 521
P
Par allelogr am ap p roximat ion, 183184
Parallel projection, transformation matrices, 475
Par amet r ic cont inu it y, 616
Peano cu r ve, 28
Per imet er, t r iangles, 20
Per iod ic p lane t esselat ion, 129130
Per pend icular bisect or s, int er sect ion, t r iangles,
2223
Perspective projection, transformation matrices,
475
Phot o-inver sion t r ansfor m, 196, 198, 270271
Pixel coord inat es, 246248
cont inu ou s, 246
conver t ing, 247248
d iscret e, 246
along u nit axis, 250
Pixel int eger s, 249256
Car t esian p r od u ct ext ensions, 256
color pixels and Cartesian products, 254256
fact or ing int o Car t esian t r ip le, 255256
nonlinear p ixel map p ings, 251253
p rop er int er p ret at ion, 249251
Pixel map p ings, nonlinear, 251253
Planar cu bic cu r ves, 575578
Plane
cr yst allogr ap hic gr ou p s, 129133
embed d ing, int er sect ion wit h r ay, 390391
fr ame bu ffer, 217218
int er sect ion of t hr ee, 305
p er iod ic t ilings on r ast er gr id , 129139
wallp ap er gr ou p s, 129133
Point
d ist ance t o line, 10
ver t ical, 4748
gener at ing r and om, t r iangles, 2428
mu t u al visibilit y, 3031
3D, 522523, 526
Point -on-line t est , 4950
Polygonal p ens, 114117
Polygons
int er sect ion
calcu lat ion, 128
fast scan conver sion, 96
wit h r ay, 390394
r and om p oint , 2428
t ext u re-sp ace images, 366367
fr om t wist ing reference fr ames, 567568
Polygon scan conver sion, 7683
algor it hm, 7782
backgrou nd , 7677
concave, 8791
fast , 9297
act ive ed ge list s, 9293
convex d ecomp osit ions, 97
int er sect ing p olygons, 96
t r afc bet ween r egist er s and memor y, 92
x-t r ansit ion t able, 9395
y extrema and memory requirements, 9596
gener ic, and clip p ing, 8486
imp lement at ion not es, 8283
red u cing cod e r ed u nd ancy, 84
ver t ices d u r ing scan conver sion, 7879
Polygon st ret ching, 127128
Polyhed r a, 3D, 565566
Polynomial equations, bracketing real roots, see
St u r m sequ ences
Pop u lar it y algor it hm, 288
Post -concat enat ion, t r ansfor mat ion mat r ices,
476481
Power r elat ions, 15
Prod u ct r elat ions, 16
648 648
INDEX
Project ion
Alber s equ al-area conic map , 321325
azimu t hal, 314317
gener al, 318
Mercat or, 311313
p rop er t ies, d igit al car t ogr ap hy, 307308
Sanson-Flamst eed sinu soid al, 312314
t r ansfor mat ion mat r ices, 475
Proximit y t est ing, 237239
Pyt hagorean relat ion, 57
Pyt hagorean t heorem, 599
Pyt hagorean t r iangles, p r ime, 58
Q
Qu ad t rees, bit int er leaving, 443447
Qu ant izat ion t echniqu es, comp ar ison, 293
Quant izat ion t r ansfor m, 196197, 199, 270, 272
Qu ar t ic root s, 406407
Qu at er nions, 498515
algor it hmet ic imp lement at ion, 509515
d enit ion, 499500
movement s fr om one or t honor mal base t o
anot her, 508
p rop er t ies, 501502
rot at ions in 3D sp ace, 503506
set of u nit , p rop er t ies, 502503
R
Rand om, see also Jit t er
int eger s, gener at ion, 438439
p oint s in t r iangles, 2428
Rast er gr id
per iod ic t ilings of plane, see Plane, per iod ic
t ilings on r ast er gr id
rend er ing fat lines, 114120
Rast er rot at ion, fast algor it hm, 179195
ad vanced ant i-aliasing, 194195
ar bit r ar y rot at ion, 186187
cir cle d r awing, 192193
comp ar isons, 190191
font rend er ing, 193
fu r t her wor k, 195
gener alized Bit Blt , 193194
hist or y, 191192
imp lement at ion, 187190
p ar allelogr am ap p roximat ion, 183184
r at ional r ot at ion, 184186
rot at ion t hrou gh shear ing, 181183
st at ement of p r oblem, 180
Rast er shear ing, 179, 183184
Rat ional r ot at ion, 184186
Ray, int er sect ion wit h sp here, 388389
Ray-box int er sect ion, fast , 395396
Ray-object int er sect ion, 387
Ray-p olygon int er sect ion, 390394
Ray r eject ion t est , 385386
Ray t r acing
algor it hm, 64
int er sect ion of r ay and sp her e, 388389
jit t er fu nct ion u se, 7274
r ay-object int er sect ion, 387
r ay-p olygon int er sect ion, 390394
r ay r eject ion t est , 385386
t r ansp ar ent object s, shad ow at t enu at ion,
397399
Ray-t r iangle int er sect ion, 393
Real r oot s, br acket ing, see St u r m sequ ences
Rectangle, intersection with circle, fast checking,
5153
Recursion property, Bernstein polynomials, 614
Refer ence fr ames
calcu lat ion along sp ace cu r ve, 567571
rot at ion minimizing fr ames, 569
t wist ing, p olygons-fr om, 567568
Refer ence geoid , 309
Refr act ion, Snells law, 353354
Regions, 560
Resamp ling, see Ant i-aliasing
RGB, t r ansfor m from HSL, 448449
RGB t r ip les, map p ing ont o fou r bit s, 233245
algor it hm d esign, 241242
Car t esian qu ant izat ion ver su s p olyhed r a,
244245
comp lexit y analysis, 244
cu boct ahed r on, 237
d u al solid s, 236237
eight -p oint color cu be, 233234
fou r-bit color solid , 235236
gr ay int er ior p oint s, 244
half-sp ace t est ing, 240241
hexagonal const r u ct ion, 238
nolid , 238
p roximit y t est ing, 237239
relat ed met hod s, 239240
rhombic d od ecahed ron, 236
t hree ver su s fou r bit s, 243244
649 649
INDEX
Rhombic d od ecahed ron, 236
Root nd ing, 403
Bzier cu r ve-based , see Bzier cu r ve-based
root -nd er
cu bic, 404407
qu ar t ic, 406407
Root -nd ing algor it hm, 412413
Rot at ion
bit p at t er ns for encod ing angles, 442
fast 2D-3D, 440441
abou t gener al line, or t honor mal bases,
520521
geomet r ical r ep resent at ion, 503504
r ast er, see Rast er r ot at ion
3D sp ace, see also Or t honor mal base
qu at er nions, 503506
t r ansfor mat ion mat r ices, 474
Rot at ion mat r ix, 180
Rot at ion mat r ix met hod s, 455
fast mat r ix mu lt ip licat ion, 460461
mat r ix inver sion, 470471
mat r ix or t hogonalizat ion, 464
rot at ion t ools, 465469
t r ansfor ming axes, 456459
vir t u al t r ackball, 462463
Rot at ion minimizing fr ames, 569
Rot at ion t ools, 465469
conver t ing bet ween mat r ix and axis-amount
rep resent at ions, 466467
it er at ion, 468
nest ing, 467468
t r ansfor mat ion inver ses, 468
S
Sampled d at a, d ening sur faces from, 552557
Samp led signals, 147
Samp ling Theorem, 147
Sanson-Flamst eed sinu soid al p roject ion,
312314
Sawt oot h t r ansfor m fu nct ion, 203, 207209
Scaling, t r ansfor mat ion mat r ices, 474475
Scan conver sion, 75
algor it hm, p olygon, 7782
ar bit r ar y p olygons, 9297
concave p olygon, 8791
gener ic p olygon and clip p ing, 8486
p olygon, see Polygon scan conver sion
Scanline d ep t h gr ad ient , Z-bu ffered t r iangle,
361363
Seed ll algor it hm, 275277
Shad ing, fast d ot p r od u ct s, 348360
Shad ows, 278
attenuation for ray tracing transparent objects,
397399
lling, 280281
Shap e const r u ct ion, 551
Shar ed chord , 44
Shear ing
algor it hm, 188
r ast er rot at ion t hrou gh, 181183
scan-line, 187190
Shear mat r ices, 181
Shu fe gener at or, 66
Sinc fu nct ion, 156157
Lanczos2
d ecimat ion by, 160161
int er p olat ion by fact or of t wo, 158159
Lanczos-wind owed , 156158
Sine, in exp onent ials, 15
Smoot hing algor it hm, monochrome
enlar gement s, 166170
Snells law, r efr act ion, 353354
Soft ware, scr ambling int eger s in, 223
Solid object s, box-sp here int er sect ion t est ing,
335337
Sp ace cu r ve, r eference fr ame calcu lat ion,
567571
Sp an, 278
Sp ecial effect s
cont r ast enhancement , 197198, 201202,
270271, 274
d issolve, see Digit al d issolve effect
p hot o-inver sion, 196, 198, 270271
Sp her e, 326
box-sp her e int er sect ion t est ing, 335339
int er sect ion wit h r ay, 388389
Sp her es-t o-voxels conver sion, 327334
Sp her ical coor d inat e t r ansfor mat ion, 317318
Sp lines, 585586
Square root, high speed, low-precision, 424426
St er eogr ap hic map , 316
St ir lings nu mber s, 597
St or age-free swap p ing, 436437
St u r m sequ ences, 416422
char act er ist ics, 420
cou nt ing sign changes, 419
d r iving algor it hm, 418419
650 650
INDEX
St u r m sequ ences (Cont d)
examp le, 417418
met hod of bisect ion, 420421
p seu d o-d ivision of p olynomials, 419
St u r ms Theorem, 416417
St u r ms Theorem, 416417
Su bp ixel coord inat es, 7778
Su bt abu lat ion, 601
Su m t ables, mu lt id imensional, see
Mu lt id imensional su m t ables
Su r face nor mals, 3D mod els, 562566
Su r face-nor mal t r ansfor mat ions, 539547
afne mod eling t r ansfor mat ions, 539542
backface cu lling, 544545
comp osit ion, 543
isot rop ic t r ansfor mat ions, 542
or t hogonal t r ansfor mat ions, 542543
shad ing, 545547
t r ansfor mat ions of met r ic p rop er t ies, 5435
Su r faces, d ening
fr om cont ou r d at a, 558561
fr om samp led d at a, 552557
assu mp t ions, 552553
met hod s, 553557
Swap p ing, st or age-fr ee, 436437
Swep t cont ou r s, 562564
Symmetric double step line algorithm, 101104
d ou ble sp eed Bresenhams, 101102
line d r awing, 101
u sing symmet r y, 102104
T
Temp or al renement
p rogressive, record ing animat ion in binar y
or d er, 265269
Tensor
mod er n view, 533535
p rod u ct , mat r ix, 473
Tent lt er, 149150
Text u re cell, 366367
t yp es, 371372
Text u red cylind er, 366367
Text u re map ind ices, int er p ret at ion, 366376
algor it hm, 373375
d ecision t r ee, 369371
replicat ing cells t o creat e larger t ext ure, 369
r igid t r ansfor mat ion of squ ar e cell, 374375
t ext u re sp ace as t wo-t or u s, 367368
t yp es of cells, 371372
Text u re-sp ace images, p olygons, 366367
Thr ee-d imensional geomet r y, 297300, see also
Digit al car t ogr ap hy
backface cu lling, 346347
boxes, 326
int er sect ion of t hr ee p lanes, 305
int er sect ion of t wo lines, 304
map p ing, 306
sp heres, 326
sp heres-t o-voxels conver sion, 327334
3D gr id hashing fu nct ion, 343345
Three-dimensional grid, dening surfaces from
samp led d at a, 552557
Thr ee-d imensional mod els, su r face nor mals,
562566
Tick mar ks, 6163
Tilings, periodic, plane on raster grid, 129139
Tr ackball, vir t u al, 462463
Tr ansfor mat ion
axis-aligned bou nd ing boxes, 548550
3D, cod ing, see Qu at er nions
Tr ansfor mat ion id ent it ies, 485493
anisot r op ic scaling following rot at ion, 490
commu t ing
rot at ion and anisot r op ic scaling, 490
rot at ion and isot rop ic scaling, 488
skewing and isot r op ic scaling, 489
exchanging or d er
of skews, 491
of t r anslat ion and r ot at ion, r u les, 487
mat r ix r ep resent at ions of p r imit ive
t r ansfor mat ions, 492493
rever sing ord er
skewing and anisot r op ic scaling, 489
t r anslat ion and scaling, 487
t r anslat ion and skewing, 488
rot at ion exp ressed as
combinat ion of skews and scales, 489
t hree skews, 489
skew expressed as two rotations and a scale,
491
Tr ansfor mat ion inver ses, 468
Tr ansfor mat ion mat r ix, 472475
DDA coefcient conversion between step sizes,
602
mir r or image, 473
651 651
INDEX
not at ion, 472, 485486
obser vat ions, 472
p ost -concat enat ion, 476481
comp u t at ional cost comp ar ison, 479481
d ir ect , 478479
imp lement at ion, 476477
p r imit ive t r ansfor mat ions, 492493
p roject ion, 474
rot at ion, 473
scaling, 473474
t r anslat ion, 472
Tr ansfor ming axes, 456459
Tr ansit ion t able, 9395
Tr anslat ion, t r ansfor mat ion mat r ices, 473
Tr ansp arent object s, r ay t r acing, shad ow
at t enu at ion, 397399
Tr ansp ose of t he inver se, 541
Tr iangles, 2023
ar ea, 20
in cent er, 2021
cent er of gr avit y, 2021
cir cu mcent er, 2023
cir cu mr ad iu s, 2023
gener at ing r and om p oint s, 2428
hyp ot enu se, 5759
int er sect ion
of alt it u d es, 22
of p er p end icu lar bisect or s, 2223
wit h r ay, 393
p er imet er, 20
p r ime p yt hagorean, 58
in r ad iu s, 2021
Tr iangu lar int er p olant s, 535538
Tr igonomet r y
angle su m and d ifference r elat ions, 16
DeMoivres Theorem, 15
d ou ble-angle relat ions, 17
xed -p oint , CORDIC, 494497
fu nct ions, 1819
su ms and d ifferences, 14
half-angle relat ions, 16
inver se fu nct ions, 14
Law of Cosines, 13
Law of Sines, 13
Law of Tangent s, 13
Mollweid es For mu la, 13
mu lt ip le-angle relat ions, 17
Newt ons For mu la, 14
p ower relat ions, 15
p rod u ct r elat ions, 16
sines and cosines in exp onent ials, 15
Two-d imensional ar r ay, r and omly t r aver sing,
d igit al d issolve effect , 221222
Two-d imensional clip p ing, 121128
algor it hm, 124126
ap p roximat ion er r or, 186
basic consid er at ions, 123124
imp lement at ion, 126127
int eger s and vect or s, 121122
Two-d imensional geomet r y, 311, see also
Tr iangles
cir cles, 45
cosine of angle bet ween lines, 11
d ist ance fr om p oint t o line, 10
int er sect ion of cir cle and line, 56
lines t angent
t o circle and p er p end icu lar t o line, 89
t o t wo circles, 78
line st r u ct u res, 34
p oint of int er sect ion bet ween lines, 11
p oint -on-line t est , 4950
t r iangles, 2023
Two-d imensional rend er ing, circles of int egr al
r ad iu s on int eger lat t ices, 5760
U
Unifor m gr id , line-ed ge int er sect ions, 2936
Unifor m qu ant izat ion, 288
Unimod u lar t r ansfor ms, 135
Unit qu at er nions, set , p r op er t ies, 502503
UVN coord inat e syst em, 518519
V
Vect or rot at ion, CORDIC, 494497
Vect or s
d ifferences wit h for ms, 533535
nor mal, see Nor mal vect or s
3D, 522523, 526
t r iangu lar int er p olant s, 535538
Ver t ex nor mal, comp u t ing, 563565
Ver t ical d ist ance, p oint t o line. 4748
652 652
INDEX
Viewing, 3D, see Or t honor mal base
Viewing t r ansfor mat ion, p seu d o-cod e, 521
Vir t u al t r ackball, 462463
Visibilit y algor it hm, 3031
Voxel subdivision, 3D grid hashing function, 343
W
Wallpaper groups, tiling in raster grids, 133139
Wide line bevel joints, algorithm for lling in,
107113
Wind ing nu mber t r ansit ions, 9495
Wr it e-only wr it e mask, read ing, 219220
X
X-t r ansit ion t able, 9395
Y
Y extrema, polygon fast scan conversion, 9596
Z
Z-bu ffered t r iangle, scanline d ep t h gr ad ient ,
361363
Zer o-p hase lt er s, fr equ ency resp onse,
d ecimat ion by t wo, 162

S-ar putea să vă placă și