Previous Next

Computer graphics using Java 2D and 3D (Liang, Y. Daniel Zhang, Hong) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Liang, Y. Daniel, Zhang, Hong

Java

This Java handbook makes a practical tutorial on Java 2D and Java 3D for computer professionals. It contains in-depth coverage of basic computer graphics concepts and techniques, and introduces advanced graphic features to an audience mostly trained in the Java language.KEY TOPICS:Chapter topics include mathematical background for computer graphics, .geometric transformation, views, lighting and texturing, behavior and interaction, and animation.MARKET:For computer programmers and engineers, data analysts, graphic designers/animators, and game developers.

📄 File Format: PDF
💾 File Size: 4.3 MB
15
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
COMPUTER GRAPHICS USING JAVA™ 2D AND 3D
📄 Page 3
This page intentionally left blank
📄 Page 4
COMPUTER GRAPHICS USING JAVA™ 2D AND 3D Hong Zhang Y. Daniel Liang Armstrong Atlantic State University Upper Saddle River, NJ 07458
📄 Page 5
Library of Congress Cataloging-in-Publication Data Zhang, Hong. Computer graphics using Java 2 and 3D / Hong Zhang, Y. Daniel Liang p. cm. Includes index. ISBN 0-13-035118-0 1. Java (Computer program language) 2. Computer graphics. I. Liang, Y. Daniel. II. Title QA76.73.J38Z43 2006 005.13'3—dc22 2006049804 Vice President and Editorial Director, ECS: Marcia J. Horton Executive Editor: Tracy Dunkelberger Associate Editor: Carole Snyder Editorial Assistant: Christianna Lee Executive Managing Editor: Vince O’Brien Managing Editor: Camille Trentacoste Production Editor: Donna Crilly Director of Creative Services: Paul Belfanti Creative Director: Juan Lopez Art Director and Cover Manager: John Christiana Interior Design: JMC Desig Cover Design: Kiwi Design Managing Editor, AV Management and Production: Patricia Burns Art Editor: Xiaohong Zhu Director, Image Resource Center: Melinda Reo Manager, Rights and Permissions: Zina Arabia Manager, Visual Research: Beth Brenzel Manager, Cover Visual Research and Permissions: Karen Sanatar Manufacturing Manager, ESM: Alexis Heydt-Long Manufacturing Buyer: Lisa McDowell Executive Marketing Manager: Robin O’Brien Marketing Assistant: Mack Patterson Cover Image: Albert J Copley / Photodisc Green / Getty Images, Inc. Chapter Opener Image: Philip Colmentz / Brand X Pictures © 2007 Pearson Education, Inc. Pearson Prentice Hall Pearson Education, Inc. Upper Saddle River, NJ 07458 All rights reserved. No part of this book may be reproduced in any form or by any means, without permission in writing from the publisher. Pearson Prentice Hall™ is a trademark of Pearson Education, Inc. All other trademarks or product names are the property of their respective owners. The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and pro- grams to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation con- tained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN 0-13-035118-0 Pearson Education Ltd., London Pearson Education Australia Pty. Ltd., Sydney Pearson Education Singapore, Pte. Ltd. Pearson Education North Asia Ltd., Hong Kong Pearson Education Canada, Inc., Toronto Pearson Educación de Mexico, S.A. de C.V. Pearson Education—Japan, Tokyo Pearson Education Malaysia, Pte. Ltd. Pearson Education, Inc., Upper Saddle River, New Jersey
📄 Page 6
To My Parents, Xuemei Sun and Zhongyi Zhang —HZ To Samantha, Michael, and Michelle —YDL
📄 Page 7
This page intentionally left blank
📄 Page 8
vii Chapter 1 Overview of Computer Graphics 1 1.1 Introduction 2 1.2 Evolution of Computer Graphics Programming 3 1.3 Java Programming Language 16 1.4 Java 2D 20 1.5 Java 3D 23 1.6 Related Fields 26 1.7 Resources 27 Chapter 2 2D Graphics: Basics 31 2.1 Introduction 32 2.2 2D Rendering Process 32 2.3 2D Geometry and Coordinate Systems 33 2.4 The Graphics2D Class 35 2.5 Graphing Equations 38 2.6 Geometric Models 40 2.7 Constructive Area Geometry 49 2.8 General Path 51 Chapter 3 2D Graphics: Rendering Details 59 3.1 Introduction 60 3.2 Colors and Paints 60 3.3 Strokes 67 3.4 Affine Transformation 70 3.5 Compositions of Transformations 78 3.6 Transparency and Compositing Rules 81 3.7 Clipping 85 3.8 Text and Font 87 Chapter 4 2D Graphics: Advanced Topics (Optional) 97 4.1 Introduction 98 4.2 Spline Curves 98 4.3 Custom Primitives 104 4.4 Image Processing 107 4.5 Creating Fractal Images 115 4.6 Animation 118 4.7 Printing 127 CONTENTS
📄 Page 9
viii Contents Chapter 5 Basic 3D Graphics 135 5.1 Introduction 136 5.2 3D Rendering Process 136 5.3 Java 3D API Overview 138 5.4 Java 3D Scene Graphs 141 5.5 The Superstructure 144 5.6 The Nodes 145 5.7 The Node Components 149 5.8 The Structure of a Java 3D Program 150 5.9 Backgrounds and Bounds 154 5.10 Compiling Scene Graphs and Capability Bits 160 Chapter 6 Graphics Contents 169 6.1 Introduction 170 6.2 Points and Vectors 170 6.3 Geometry 172 6.4 GeometryInfo 185 6.5 Primitives 193 6.6 Fonts and Texts 196 6.7 Appearance and Attributes 197 Chapter 7 Geometric Transformation 209 7.1 Introduction 210 7.2 3D Affine Transformations 210 7.3 Transformations in Scene Graphs 225 7.4 Composite Transforms 229 7.5 Constructing Geometries with Transformations 234 Chapter 8 Views 247 8.1 Introduction 248 8.2 Projections 248 8.3 Specification of a View 251 8.4 Java 3D View Model 253 8.5 Picking 265 8.6 Head Tracking 271 Chapter 9 Lighting and Texturing 281 9.1 Introduction 282 9.2 Lights 282 9.3 Illumination Models 288 9.4 Java 3D Lighting Models 290 9.5 Atmospheric Attenuation and Depth Cueing 294 9.6 Texture Mapping 297
📄 Page 10
Contents ix Chapter 10 Behavior and Interaction 315 10.1 Introduction 316 10.2 Behavior 316 10.3 Interaction 323 10.4 Behavior and Picking 334 Chapter 11 Animation 345 11.1 Introduction 346 11.2 Alpha Objects 346 11.3 Interpolators 350 11.4 Morphing 361 11.5 LOD 366 11.6 Billboard 370 Chapter 12 Additional 3D Topics 379 12.1 Introduction 380 12.2 3D Curves 380 12.3 Surfaces 384 12.4 Sound 390 12.5 Shadows 394 12.6 Geometry Change 399 12.7 Off-Screen Rendering 405 12.8 3D Textures 409 APPENDIXES Appendix A 419 Appendix B 443 Index 455
📄 Page 11
This page intentionally left blank
📄 Page 12
audience Java 2D and 3D why Java? high-level approach On March 3, 2001, the first Oscar of the new millennium was awarded to three computer sci- entists, Rob Cook, Loren Carpenter, and Ed Catmull, “for their significant advancements to the field of motion picture rendering as exemplified in Pixar’s ‘RenderMan.’” This incredible event symbolizes the emergence of computer graphics and its applications, once an esoteric research subject, as an essential part of the digital community. The rapid development of com- puter hardware, graphical applications, and network technologies has made computer graph- ics indispensable in mainstream computing. Modeling and rendering virtual graphics objects with digital computers are the main objec- tives of computer graphics. The topics involved in this process span a wide range of disci- plines from mathematics and computer science to psychology and arts. From the big model of the universe to the small details of rasterizing a graphical primitive, sophisticated and ingenious methods, algorithms, and paradigms have been developed to address the problems of modeling and rendering in computer graphics. A thorough treatment of this subject, there- fore, would require a broad and deep coverage of many related areas. A traditional computer graphics course, due to its complexity and mathematical sophistication, is usually beyond the scope of a standard undergraduate computer science curriculum. However, we believe that the new technological developments and the availability of well- designed and easy-to-use graphics programming packages have made an elementary graphics course feasible. This development is analogous to the evolution of programming languages. Programming used to be a very tedious task when only low-level languages such as machine instructions were available. The development of high-level languages freed programmers from the low-level technical details. Programs at the more abstract levels are much more man- ageable and logical. The performance loss due to the abstraction has become negligible with the improvement of hardware and compiler technologies. Most programmers today may never need to program in machine or assembly languages. Computer graphics programming is going through a similar process. High-level, portable systems are rapidly replacing the tedious low-level approaches in many aspects of computer graphics. The rapid development of hardware technologies is also erasing the performance gap. The primary focus of this book is the fundamental concepts of computer graphics and applications of Java 2D and Java 3D to graphics programming. Rather than studying the tech- nical details of low-level implementations, we will emphasize the techniques of developing practical applications using existing graphics packages. This approach enables us to provide an introductory computer graphics text that is accessible to undergraduate computer science and engineering students and most computer professionals. Java 2D and Java 3D are the ideal graphics packages for such a purpose. They are high- level comprehensive graphics packages that offer a much-needed layer of abstraction. They are also platform independent and provide state-of-the-art graphics programming capabilities. Java 2D and Java 3D are parts of the Java platform. Java is a relatively new programming language, yet it has quickly gained popularity because of its unique characteristics and fea- tures, such as platform independence, simplicity, and object-oriented programming support. Java 2D and Java 3D provide powerful, natural, and object-oriented interfaces for graphics modeling and rendering. This book is intended for students and computer professionals who want to learn basic computer graphics concepts and techniques and to get started in programming with Java 2D and Java 3D. However, it is not just another tutorial on Java 2D or Java 3D. Its purpose is to introduce the fundamentals of computer graphics to the readers, and the powerful Java pack- ages serve as useful and convenient tools to achieve the goal. xi PREFACE
📄 Page 13
xii Preface The prerequisite of this book is a basic knowledge of programming with Java, including GUI programming (AWT and Swing). Appendix B, “GUI Programming with AWT and Swing,” illustrates the differences in programming with AWT and Swing components. Basic mathematical courses in geometry and linear algebra will be helpful but are not required. Appendix A, “Mathematical Background for Computer Graphics,” provides a brief review of some relevant mathematical concepts. Companion Website The companion website at www.cs.armstrong.edu/liang/graphics contains the following resources: ■ Answers to review questions ■ Solutions to even-numbered programming exercises ■ Source code for the examples in the book ■ Resource links ■ Errata Instructor Resource Website The Instructor Resource website accessible from www.prenhall.com/liang or www.cs.armstrong.edu/liang/graphics contains the following resources: ■ Microsoft PowerPoint slides with interactive buttons to view full-color, syntax-highlight- ed source code and to run programs without leaving the slides. ■ Sample exams. ■ Solutions to all the exercises. Students will have access to the solutions of even-numbered exercises in the book’s companion website. Some readers have requested the materials from the Instructor Resource Website. Please understand that these are for instructors only. Such requests will not be answered. Acknowledgments The computer science department at Armstrong Atlantic State University is a great place to work. We thank Ray Greenlaw and our colleagues at Armstrong Atlantic State University for their support of this book. Our thanks to anonymous reviewers and our students for their critics, comments, and sug- gestions, which have greatly improved this book. It is a great pleasure and privilege to work with the legendary computer science team at Prentice Hall. We thank Marcia Horton, Tracy Dunkelburger, Robin O’Brien, Christianna Lee, Jennifer Cappello, Barrie Reinhold, Mack Patterson, Vince O’Brien, Camille Trentacoste, Donna Crilly, Xiaohong Zhu, and their colleagues for organizing, managing, and promoting this proj- ect, and Robert Lentz for copy editing. Hong Zhang (hong@armstrong.edu) Y. Daniel Liang (liang@armstrong.edu) www.cs.armstrong.edu/liang/graphics prerequisite
📄 Page 14
1 OVERVIEW OF COMPUTER GRAPHICS Objectives ■ To understand the basic objectives and scope of computer graphics. ■ To identify computer graphics applications. ■ To understand the basic structures of 2D and 3D graphics systems. ■ To understand evolution of graphics programming environments. ■ To identify common graphics APIs. ■ To understand the roles of Java language and the Java 2D and Java 3D packages. ■ To identify fields related to computer graphics. CHAPTER 1
📄 Page 15
2 Chapter 1 Overview of Computer Graphics Rendering Virtual world model Image of a scene FIGURE 1.1 Main tasks of computer graphics: modeling a virtual world and rendering a scene. modeler renderer world space view 1.1 Introduction Computer graphics studies the theory and techniques of modeling, processing, and rendering of graphical objects in computers. The basic objective of computer graphics is to build a vir- tual world of graphics objects and to render a scene of the virtual model from a specific view onto a graphic device, as shown in Figure 1.1. A graphics system typically consists of two major components: a modeler and a renderer. The modeler is responsible for the construction of the virtual world models and the renderer performs the rendering of a scene. A “retained-mode” system maintains a persistent model of graphics objects and the modeler’s function is explicit. An “immediate-mode” system renders the objects immediately and the model is more transient. This view of the modeling–rendering paradigm is convenient for studying graphics systems, even if the separation may not be clear in some systems. Typically the graphics objects to be modeled are in either a 2D or a 3D space. This com- mon space to host all the graphics objects is often called the world space. A rendered scene of the world space, the main output of a graphics system, is typically in a 2D form. Consequently the techniques involved in 2D and 3D graphics are considerably different. Because 3D graphics problems are significantly more complex, 2D and 3D graphics are often treated as separate topics. The graphics objects to be modeled in a world space are usually geometric entities such as lines and surfaces, but they also include other special objects such as lights, texts and images. The graphics objects may possess many characteristics and properties such as color, trans- parency and texture. Various mathematical representations are used to model geometric objects. Straight-line segments and simple polygon meshes provide simple and compact representations. Only the vertices of the structures need to be stored and they are easy to implement. More sophisticat- ed representations include spline curves and surfaces. They are versatile and require only the storage of relatively few control points. Geometric transformations are applied to the objects to achieve the proper placement of the objects in the virtual space. Transformations of this type are called object transformations. Transformations are also used for the viewing; these are known as viewing transformations. A useful family of the geometric transforms is the affine transforms, which include the most common types such as translations, rotations, scalings and reflections. A more general set of transforms, the projective transformations, are useful for 3D viewing. A view is used to see the model in the virtual world from a specific perspective. A 2D viewing process is relatively simple. The viewing transformation is usually indistinguish- able from the object transformation. Rendering features such as composition rules and clipping 2D viewing object transformations viewing transformations projective transformations affine transforms
📄 Page 16
1.2 Evolution of Computer Graphics Programming 3 hardware devices animation paths may be applied. A 3D view is much more complicated. Like eyes or cameras, 3D viewing involves a projection process that maps 3D objects to a 2D plane. Many parameters such as the projection, view position, orientation, and field of view could affect the 3D rendering. In order to achieve a realistic rendering of the virtual world, numerous rendering issues need to be addressed. Relative locations of the objects need to be correctly reflected in the rendered images. For example, an object may be hidden behind another object, and the hid- den portion should not be shown in the image. Light sources of various characteristics should be considered. The properties of the materials of the objects will affect the appearance. Many of the methods for solving the problems have significant computational demands. The capabilities and characteristics of hardware devices have great impact on graphics sys- tems. The most common output devices for displaying the results of the graphics rendering are video monitors and printers. Other output devices include plotters and holographic pro- jectors. For input devices, mice, joysticks and tablets with pens are widely available. There are also more sophisticated input devices and sensors such as six-degree-of-freedom trackers. Animation is also an important part of computer graphics. Instead of still images, anima- tion produces dynamic graphics contents and rendering. In applications such as movie-scene rendering and gaming, animation plays a crucial role. Another dynamic aspect of computer graphics is interaction. In response to user inputs, the graphics model may change accord- ingly. The fundamental principle of GUI (graphical user interface) is based on the user inter- actions with graphics systems. Another example of extensive application of interaction is video games. Computer graphics has a wide range of applications. The popularity of GUI environments has made graphics an integral part of ordinary user programs. CAD (computer-aided design) and other engineering applications depend heavily on graphics systems. Data visualization and other scientific applications also make extensive use of graphics. With the rapid devel- opment of new computer-based instrumentation such CT (computer tomography), PET (positron emission tomography), and MRI (magnetic resonance imaging), medical systems have increasingly employed computer graphics technologies. Computer graphics is also a crucial ingredient in video games and other entertainment applications. Traditionally computer graphics has to deal with implementation details, using the low- level algorithms to convert primitives such as lines to pixels, to determine the surfaces hidden from the view, to calculate the color values of points on a surface, and so on. These algorithms and methods have made the subject technically difficult and complex. In this book we shall rely on the Java 2D and Java 3D packages to avoid dealing with most of the low-level details directly. This will enable us to focus on the big picture of modeling and rendering problems in graphics, rather than the tedious implementation details. 1.2 Evolution of Computer Graphics Programming Graphics programming has appeared in almost every level of computer architecture. Generally it is moving from low-level, platform-dependent methods toward abstract, high- level, and portable environments. Figure 1.2 gives examples of graphics programming environments at various levels of computer architecture. The subsections that follow discuss the characteristics of graphics programming at the different levels. interaction Platform independent (Java 2D and Java 3D) Graphics standard (GKS, PHIGS, OpenGL) OS (WIN32, X, Mac OS) Hardware (direct register/video buffer programming) FIGURE 1.2 Graphics programming at different levels. 3D viewing
📄 Page 17
4 Chapter 1 Overview of Computer Graphics 1.2.1 Hardware Level Computer graphics programs depend on output devices with graphical capabilities. The most common display devices for computer graphics are CRT (cathode ray tube) monitors and LCD (liquid crystal display) panels. These are 2D raster devices that provide a display sur- face consisting of a rectangular array of discrete dots. A display device of this kind is usually driven by a dedicated graphics board with its own processor and memory. Lower-level graphics applications often program the graphics hardware directly. In the popular environment of personal computers running MS-DOS, for example, most graphics applications directly access the display memory. Even though BIOS and DOS provide certain primitive support for graphics functions, they are considered too slow for graphics-intensive programs. Such programs are typically written in assembly language and manipulate the hardware registers and video buffers in a highly machine-dependent way. Listing 1.1 gives an assembly program that demonstrates low-level graphics programming. It uses Microsoft Macro Assembler and can be executed on any IBM PC compatible machine with a VGA graphics card. It draws a circle by directly writing to the memory locations of the video buffer. An ideal circle centered at the origin has the equation: A computer monitor only displays discrete pixels. Therefore a set of pixels approximating the curve must be computed. Only one-eighth of the circle needs to be calculated; other por- tions can be obtained through symmetry. As illustrated in Figure 1.3, the algorithm generates a series of pixels close to the curve. Consider the top right arc of the circle. Starting from the top pixel the algorithm attempts to obtain the next pixel to the right of the current one. There are only two possible directions of movement: “east” or “southeast.” Between the two pixels, the one closer to the curve is selected. The two cases can be decided by testing the midpoint If the midpoint is inside the circle, the “east” pixel should be chosen; if it is outside, the “southeast” pixel is the closer one. The equation of the circle is used to perform the test. By using certain difference variables, the actual operations of the test can be further simplified to only integer additions. Listing 1.1 Circle.asm 1 .model small,stdcall 2 .stack 100h 3 .386 4 5 .data 6 saveMode BYTE ? ; saved video mode 7 xc WORD ? ; center x 8 yc WORD ? ; center y 9 x SWORD ? ; x coordinate 10 y SWORD ? ; y coordinate 11 dE SWORD ? ; east delta 12 dSE SWORD ? ; southeast delta 13 w WORD 320 ; screen width 14 15 .code 16 main PROC 17 mov ax,@data 18 mov ds,ax 19 20 ;Set Video Mode 320X200 21 mov ah,0Fh ; get current video mode 22 int 10h 23 mov saveMode,al ; save mode 24 1x + 1, y - 0.52. 1x = 0, y = R2, x2 + y2 = R2
📄 Page 18
1.2 Evolution of Computer Graphics Programming 5 25 mov ah,0 ; set new video mode 26 mov al,13h ; mode 13h 27 int 10h 28 29 push 0A000h ; video segment address 30 pop es ; ES = A000h (video segment) 31 32 ;Set Background 33 mov dx,3c8h ; video palette port (3C8h) 34 mov al,0 ; set palette index 35 out dx,al 36 37 ;Set screen background color to dark blue 38 mov dx,3c9h ; port address 3C9h 39 mov al,0 ; red 40 out dx,al 41 mov al,0 ; green 42 out dx,al 43 mov al,32 ; blue (32/63) 44 out dx,al 45 46 ; Draw Circle 47 ; Change color at index 1 to yellow (63,63,0) 48 mov dx,3c8h ; video palette port (3C8h) 49 mov al,1 ; set palette index 1 50 out dx,al 51 52 mov dx,3c9h ; port address 3C9h 53 mov al,63 ; red 54 out dx,al 55 mov al,63 ; green 56 out dx,al 57 mov al,0 ; blue 58 out dx,al 59 60 mov xc,160 ; center of screen 61 mov yc,100 62 63 ; Calculate coordinates 64 mov x, 0 65 mov y, 50 ; radius 50 66 mov bx, -49 ; 1-radius 67 mov dE, 3 68 mov dSE, -95 69 70 DRAW: 71 call Draw_Pixels ; Draw 8 pixels 72 73 cmp bx, 0 ; decide E or SE 74 jns MVSE 75 76 add bx, dE ; move east 77 add dE, 2 78 add dSE, 2 79 inc x 80 jmp NXT 81 MVSE: 82 add bx, dSE ; move southeast 83 add dE, 2 84 add dSE, 4 85 inc x
📄 Page 19
6 Chapter 1 Overview of Computer Graphics 86 dec y 87 NXT: 88 mov cx, x ; continue if x < y 89 cmp cx, y 90 jb DRAW 91 92 ; Restore Video Mode 93 mov ah,10h ; wait for keystroke 94 int 16h 95 mov ah,0 ; reset video mode 96 mov al,saveMode ; to saved mode 97 int 10h 98 99 .EXIT 100 main ENDP 101 102 ; Draw 8 pixels symmetrical about the center 103 Draw_Pixels PROC 104 ; Calculate the video buffer offset of the pixel. 105 mov ax, yc 106 add ax, y 107 mul w 108 add ax, xc 109 add ax, x 110 mov di, ax 111 mov BYTE PTR es:[di],1; store color index 112 ; Horizontal symmetrical pixel 113 sub di, x 114 sub di, x 115 mov BYTE PTR es:[di],1; store color index 116 ; Vertical symmetrical pixel 117 mov ax, yc 118 sub ax, y 119 mul w 120 add ax, xc 121 add ax, x 122 mov di, ax 123 mov BYTE PTR es:[di],1; store color index 124 ; Horizontal pixel 125 sub di, x 126 sub di, x 127 mov BYTE PTR es:[di],1; store color index 128 ; Switch x, y to get other 4 pixels 129 mov ax, yc 130 add ax, x 131 mul w 132 add ax, xc 133 add ax, y 134 mov di, ax 135 mov BYTE PTR es:[di],1; store color index 136 sub di, y 137 sub di, y 138 mov BYTE PTR es:[di],1; store color index 139 mov ax, yc 140 sub ax, x 141 mul w 142 add ax, xc 143 add ax, y 144 mov di, ax 145 mov BYTE PTR es:[di],1; store color index
📄 Page 20
1.2 Evolution of Computer Graphics Programming 7 146 sub di, y 147 sub di, y 148 mov BYTE PTR es:[di],1; store color index 149 150 ret 151 Draw_Pixels ENDP 152 153 END main The program first saves the current video mode and switches to mode 13h by using the BIOS interrupt 10h (line 27). The video mode 13h is an easy-to-use graphics mode with 320 by 200 pixels and 256 colors. Each pixel color is represented by a byte value in the video buffer starting from the segment address A000h. Each byte value is a color index to represent a color given in a color table. Because the aspect ratio of this mode does not match that of a standard monitor, the display may appear to be stretched vertically and the circle may actually appear as an ellipse. The background color of the screen is set to a dark blue by writing to the register at port address 3c9h (lines 38–44). The color to the circle is set to yellow (lines 48–58). The circle is centered at the middle of the screen and has a radius 50. The variables (xc, yc) define the center. A loop starting at the label DRAW (line 70) is set to calculate and draw the pixels approximating the circle. The variables (x, y) represent current pixel coordinates. The variables dE and dSE represent differences used for deciding the next move. The loop calls the procedure Draw_Pixels to draw the current pixel and seven other pixels in the sym- metrical positions. It decides the movement for the next pixel and updates the variables. The loop terminates when the calculation of one-eighth of the circle is completed. The Draw_Pixels procedure (line 103) draws eight pixels corresponding to the current calculation. It writes a color index to the memory locations corresponding to the pixels. A cal- culation of the proper offset in the video buffer is necessary because the memory addresses of the pixels are organized linearly: offset = 320*x + y E SE FIGURE 1.3 Determination of the pixels on a circle. From the current pixel, the next pixel will be either to the “east” or to the “southeast.”
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List