Table Of ContentLeen Ammeraal · Kang Zhang
Computer
Graphics
for Java
Programmers
Third Edition
Computer Graphics for Java Programmers
Leen Ammeraal (cid:129) Kang Zhang
Computer Graphics for Java
Programmers
Third Edition
LeenAmmeraal KangZhang
Kortenhoef,TheNetherlands DepartmentofComputerScience
TheUniversityofTexasatDallas
Richardson,TX,USA
ISBN978-3-319-63356-5 ISBN978-3-319-63357-2 (eBook)
DOI10.1007/978-3-319-63357-2
LibraryofCongressControlNumber:2017947160
©SpringerInternationalPublishingAG2017
1stedition:©JohnWiley&SonsLtd1998
2ndedition:©JohnWiley&SonsLtd2007
Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartof
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilarmethodologynowknownorhereafterdeveloped.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publicationdoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexempt
fromtherelevantprotectivelawsandregulationsandthereforefreeforgeneraluse.
Thepublisher,theauthorsandtheeditorsaresafetoassumethattheadviceandinformationinthis
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, express or implied, with respect to the material contained
hereinor for anyerrors oromissionsthat may havebeenmade. Thepublisher remainsneutralwith
regardtojurisdictionalclaimsinpublishedmapsandinstitutionalaffiliations.
Printedonacid-freepaper
ThisSpringerimprintispublishedbySpringerNature
TheregisteredcompanyisSpringerInternationalPublishingAG
Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland
Preface
Ithasbeen10yearssincethepublicationofthesecondedition.Theprogramming
language,Java,hasnowdevelopedintoitsmaturity,beingthelanguageofchoicein
many industrial and business domains. Yet the skills of developing computer
graphics applications using Java are surprisingly lacked in the computer science
curricula. Though no longer active in classroom teaching, the first author has
developed and published several Android applications using Java, the main lan-
guageforAndroiddevelopers.ThesecondauthorhastaughtComputerGraphicsat
his current university for the past 17 years using the first and second editions of
thistextbook,apartfromhispreviousyearsinAustraliausingdifferenttextbooks.
Wefeelstronglyaneedforupdatingthebook.
This third edition continues the main theme of the first two editions, that is,
graphicsprogramminginJava,withallthesourcecode,exceptthoseforexercises,
availabletothereader.Majorupdatesinthisneweditionincludethefollowing:
1. The contents of all chapters are updated according to the authors’ years of
classroomexperiencesandrecentfeedbackfromourstudents.
2. Hidden-line elimination and hidden-face elimination are merged into a single
chapter.
3. Anewchapteroncolor,texture,andlightingisadded,asChap.7.
4. The companion software package, CGDemo, that demonstrates the working of
differentalgorithmsandconceptsintroducedinthebook,isenhancedwithtwo
newalgorithmsaddedandafewbugsfixed.
5. A set of 37 video sessions (7–11 min each) in MOOC (Massive Open Online
Course)style,coveringallthetopicsofthetextbook,issupplemented.
6. A major exercise, split into four parts, on implementing the game of Tetris is
addedattheendoffourrelevantchapters.
Many application examples illustrated in this book could be readily
implementedusingJava3DorOpenGLwithoutanyunderstandingoftheinternal
working of the implementation, which we consider undesirable for computer
science students. We therefore believe that this textbook continues to serve as an
indispensable introduction to the foundation of computer graphics, and more
v
vi Preface
importantly, how various classic algorithms are designed. It is essential for com-
putersciencestudentstolearntheskillsonhowtooptimizetime-criticalalgorithms
andhowtodevelopelegantalgorithmicsolutions.
TheexampleprogramscanbedownloadedfromtheInternetat:
http://home.kpn.nl/ammeraal/
orat:
http://www.utdallas.edu/~kzhang/BookCG/
Finally,wewouldliketothanktheUT-DallascolleaguePushpaKumar,whohas
been using this textbook to teach undergraduate Computer Graphics class and
providedvaluablefeedback.WearegratefultoSusanLagerstrom-FifeofSpringer
forherenthusiasticsupportandassistanceinpublishingthisedition.
Kortenhoef,TheNetherlands LeenAmmeraal
Richardson,TX,USA KangZhang
Contents
1 ElementaryConcepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 PixelsandDeviceCoordinates. . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 LogicalCoordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 AnisotropicandIsotropicMappingModes. . . . . . . . . . . . . . . . . 12
1.4 DefiningaPolygonThroughMouseInteraction. . . . . . . . . . . . . 19
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2 AppliedGeometry. . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . 29
2.1 Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2 InnerProductandVectorProduct. . . . . . . . . . . . . . . . . . . . . . . 31
2.3 TheOrientationofThreePoints. . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 PolygonsandTheirAreas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.5 Point-in-PolygonTest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.6 TriangulationofPolygons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.7 Point-on-LineTest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.8 ProjectionofaPointonaLine. . . . . . . . . . . . . . . . . . . . . . . . . 55
2.9 DistanceBetweenaPointandaLine. . . . . . . . . . . . . . . . . . . . . 57
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3 GeometricalTransformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.1 MatrixMultiplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2 LinearTransformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3 Translations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.4 HomogeneousCoordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.5 InverseTransformationsandMatrixInversion. . . . . . . . . . . . . . 72
3.6 RotationAboutanArbitraryPoint. . . . . . . . . . . . . . . . . . . . . . . 73
3.7 ChangingtheCoordinateSystem. . . . . . . . . . . . . . . . . . . . . . . . 78
3.8 RotationsAbout3DCoordinateAxes. . . . . . . . . . . . . . . . . . . . 79
3.9 RotationAboutanArbitraryAxis. . . . . . . . . . . . . . . . . . . . . . . 80
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
vii
viii Contents
4 Classic2DAlgorithms. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . .. 91
4.1 BresenhamLineDrawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.2 DoublingtheLine-DrawingSpeed. . .. . . . . . . . .. . . . . . . . .. . 97
4.3 CircleDrawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.4 Cohen–SutherlandLineClipping. . . . . . . . . . . . . . . . . . . . . . . . 106
4.5 Sutherland–HodgmanPolygonClipping. . . . . . . . . . . . . . . . . . 112
4.6 Be´zierCurves.. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . 118
4.7 B-SplineCurveFitting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5 Perspectiveand3DDataStructure. . . . . . . . . . . . . . . . . . . . . . . . . 137
5.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.2 ViewingTransformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
5.3 PerspectiveTransformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
5.4 ACubeinPerspective. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
5.5 SpecificationandRepresentationof3DObjects. . . . . . . . . . . . . 149
5.6 SomeUsefulClasses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
5.7 AProgramforWireframeModels. . . . . . . . . . . . . . . . . . . . . . . 172
5.8 AutomaticGenerationofObjectSpecification. . . . . . . . . . . . . . 177
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6 Hidden-LineandHidden-FaceRemoval. . . . . . . . . . . . . . . . . . . . . 191
6.1 Hidden-LineAlgorithm. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. 191
6.2 BackfaceCulling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
6.3 Painter’sAlgorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
6.4 Z-BufferAlgorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
7 Color,Texture,andShading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.1 ColorTheories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.2 AdditiveandSubtractiveColors. . . . . . . . . . . . . . . . . . . . . . . . 227
7.3 RGBRepresentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
7.4 HSVandHSLColorModels. . . . . . . . . . . . . . . . . . . . . . . . . . . 234
7.5 Transparency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.6 Texture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
7.7 SurfaceShading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8 Fractals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
8.1 KochCurves. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
8.2 StringGrammars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
8.3 MandelbrotSet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
8.4 JuliaSet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Contents ix
AppendixA:Interpolationof1/z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
AppendixB:ClassObj3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
AppendixC:Hidden-LineTestsandImplementation. . . . . . . . . . . . . . 293
AppendixD:Several3DObjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
AppendixE:HintsandSolutionstoExercises. . . . . . . . . . . . . . . . . . . . 349
Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Chapter 1
Elementary Concepts
This book is primarily about computer graphics programming and related mathe-
matics.Ratherthandiscussinggeneralgraphicssubjectsforendusersorhowtouse
graphicssoftware,wewillcovermorefundamentalsubjects,requiredforgraphics
programming.Inthischapter,wewillfirstunderstandandappreciatethenatureof
discretenessofdisplayedgraphicsoncomputerscreens.Wewillthenseethatx-and
y-coordinatesneednotnecessarilybepixelnumbers,alsoknownasdevicecoordi-
nates.Inmanyapplications,logicalcoordinatesaremoreconvenient,providedwe
canconvertthemtodevicecoordinatesbeforedisplayingonthescreen.Withinput
from a mouse, we would also need the inverse conversion, i.e. converting device
coordinatestologicalcoordinates,aswewillseeattheendofthischapter.
1.1 Pixels and Device Coordinates
The most convenient way ofspecifying a line segment on a computer screen is by
providingthecoordinatesofitstwoendpoints.Inmathematics,coordinatesarereal
numbers,butprimitiveline-drawingroutinesmayrequirethesetobeintegers.Thisis
thecase,forexample,intheJavalanguage,tobeusedthroughoutthisbook.TheJava
AbstractWindowsToolkit(AWT)providestheclassGraphicscontainingthemethod
drawLine,whichweuseasfollowstodrawthelinesegmentconnectingAandB:
g.drawLine(xA, yA, xB, yB);
Thegraphicscontextginfrontofthemethodisnormallysuppliedasaparameter
of the paint method in the program, and the four arguments of drawLine are
integers, ranging from zero to some maximum value. The above call to drawLine
producesexactlythesamelineonthescreenasthisone:
g.drawLine(xB, yB, xA, yA);
©SpringerInternationalPublishingAG2017 1
L.Ammeraal,K.Zhang,ComputerGraphicsforJavaProgrammers,
DOI10.1007/978-3-319-63357-2_1
Description:This third edition covers fundamental concepts in creating and manipulating 2D and 3D graphical objects, including topics from classic graphics algorithms to color and shading models. It maintains the style of the two previous editions, teaching each graphics topic in a sequence of concepts, mathema