JAVA三维图形编程入门(1)

来源:互联网 发布:淘宝微淘怎么晒买家秀 编辑:程序博客网 时间:2024/06/14 13:25

626Writing Java 3-D Applets and Applications

626编写Java3-D 小应用程序和应用程序

Currently, 2-D graphics and animationdominate the pictorial content of most Web sites. However, many softwarecompanies are working hard to bring 3-D to the Web. For example, VRML (VirtualReality Modeling Language) lets browsers display interactive 3-D Web pages.Many programmers feel that VRML is to 3-D graphics what HTML is to 2-Dgraphics. As you will learn, Java is another way to bring 3-D images to yourWeb pages. Due to Javas performance, however, you have to keep your imagessimple.

目前, 2-D图形和2-D动画是统治极大多数WEB网站的画面内容. 但是, 许多软件公司为了要把3-D带进WEB正在艰苦地工作着. 例如,VRML(Virtual Reality Modeling Language)可让浏览器显示交互的3-D WEB 页面. 许多程序员以为VRML是专为3-D作图的,而HTML是为2-D作图的. 你以下将会看到, Java也是一种把3-D图象带到你的WEB页面的方法. 但由于受Java功能的限制, 你必须使用简单的图形.

As you have learned, Java is an interpretive language. Ingeneral, interpretive languages are much slower that compiled languages. Asmore just-in-time compilers become available, you will see Javas performanceimprove greatly. A just-in-time compiler converts Java byte code into machinecode just after a browser downloads the code.

你已知道, Java是一种解释性语言. 一般地, 解释性语言运行时要远比编译语言为慢.当即将来到的更多的编译程序变为可用时,你将看到Java的性能会大大改进. 这些编译器在浏览器卸载Java的字节代码后就会把代码转换成机器码.

 

Despite Javas slow performance, you can still create simple 3-Dapplets and applications. For example, you can write an applet that displays differentviews of a 3-D object or allows a user to traverse a simple 3-D world. Javaspeed limits the complexity and interactively of such 3-D scenes. However, ifyou stick to effects that do not require intense computation, you can still dointeresting things in 3-D with Java.

尽管Java运行很慢, 你仍可以用来创建简单的3-D小应用程序和应用程序. 例如, 你可以编写一个小应用程序来显示一个3-D对象的不同侧面, 或让用户在一个3-D的地球上旅游. Java速度限制了所画3-D对象的复杂性和交互式作图. 但是, 如果你一定要看效果而不要求快速的计算, 你仍可用Java作出一些有趣的3-D东西.

 

627 Understanding a 3-D Coordinate System

627 了解3-D座标系

To create a 3-D application, you must first understand the 3-Dcoordinate system. In Tip xxx, you learned about 2-D screen coordinate systemswhich are based on the x- and y-axes. A 3-D coordinate system is similar exceptthat it extends into a third dimension along a z-axis. Figure 627.1 shows acube defined within a 3-D coordinate system.

为了创建一个3-D应用程序, 你必须首先了解3-D座标系. 在TIP 427 中, 你已知道了基于x和y轴的2-D屏幕座标系统. 3-D座标系是类似的, 只是它沿着一个z轴扩展成了三维. 图627.1展示了定义在3-D座标系中的一个立方体.

 

Phil find it-

Figure 627.1 A 3-D coordinate system.

图627.1 一个3-D座标系

 

There are many orientations that x-, y-, and z-axes can have. Mostpeople use only two of these orientations extensively. One is the right-handedcoordinate system and the other is the left-handed coordinates system. Within aright-handed coordinate system, you put your thumb in the direction of thez-axis and your fingers in the direction of the x-axis. Then, when you curlyour fingers, they point in the direction of the y-axis. You can determine aleft-handed coordinate system in the same manner, except you use your lefthand. Figure 627.2 demonstrates left-handed and right-handed coordinatesystems.

这里,x-, y-, 和 z- 轴可以有许多可能的方向. 大部分人广泛使用的仅仅是其中的两种方向. 一种是右手座标系,而另一种是左手座标系. 在右手座标系中, 你把姆指朝向z-轴,而把其它手指指向x-轴,然后再这些手指弯曲起来, 则它们就指向y-轴的方向.你可用同样的方式来定义一个左手座标系, 只是你要改用左手. 图片627.2画出了左手座标系和右手座标系. 

 

Figure 627.2 Left-handed and right-handed coordinate systems.

图627.2 左手座标系和右手座标系

 

The advantage of 3-D graphics over 2-D graphics is that you onlydefine objects in a 3-D coordinate system once. Then, you can display theobjects from different points of view, or manipulate the objects in 3-D space mathematically. The following tips will give you the basics you need to writeJava programs that work in 3-D.

3-D图形比2-D图形的优点在于在3-D坐标系统中对象只需定义一次, 然后,你就可以从不同的视点来显示对象,或利用数学工具在3-D空间中操纵对象.以下一系列的TIP将向你提供用Java编写3-D程序的基本知识.

 

 

628 Understanding Vectors

628 了解3-D座标系

In 3-D graphics programs, you use vectors in many ways. Forexample, you use a vector to define the direction of an observer relative tothe 3-D scene. You also use vectors to calculate the angle that light reflectsoff a surface. Also, vectors play an important role in the way objects areconverted from 3-D space to the screen, a process known as rendering.A vectorrepresents a direction and magnitude and is not fixed at any particularlocation. You can think of a vector as a line with an arrow head

at one end of it. The magnitude of a vector defines the linessize. The vectors direction defines where the vector points.

在编写3-D图形程序时, 你常要利用向量工具。例如, 用一向量来定义一个观察者观察3-D景色的方向. 你也要用向量来计算光线从物体表面反射出来的角度. 还有, 当你把物体从3-D空间变换成屏幕上,要进行所谓的着色(rendering)时,向量也起重要作用.一个向量表示一个方向和大小,但不固定在任何特殊的位置上. 你可以把一个向量想象成为一根直线段,在线段的一端带着一个箭头; 向量的大小确定了线段的长短, 而向量的方向确定了箭头指向哪里。

 

Two vectors may point in the same direction, but have differentmagnitudes. Physicists use vectors to denote velocity. Velocity defines boththe speed and direction of an object. You know that cars on a highway cantravel in the same direction, but not necessarily at the same speed. Conversely,two vectors can have the same magnitude but point in different directions. Asyou know, you can drive at the same speed but in many directions. Each time youturn, you are changing your velocity vector.

两个向量可以指向同一个方向而具有不同的大小. 物理学家用向量来表示物体运动的速度(velocity). 速度既定义了运动的快慢速度(speed),也定义了运动的方向. 你知道汽车在高速公路上可以在同一方向上行驶,但不一定需要用同一个速度. 反过来,两个向量也可以有相同的大小而有不同的方向. 正如你可以用同一种快慢速度但朝着各种不同的方向驾驶一样. 你每一次打弯,你就改变了你的速度向量.

 

629 Calculating a Vectors Magnitude

629 计算向量的大小

Mathematicians define a 2-D vector by its length along the x-and y-axes. To calculate the magnitude of a 3-D vector, you calculate itslength along the x-, y-, and z-axes. With a 2-D vector, as shown in Figure629.1, the x- and y-lengths make up the sides of a right triangle.

数学家在定义2-D向量时,是利用它在x-和y-轴上的分量(投影)的大小. 同样,为了计算一个3-D向量的大小, 你可计算它在x-,y-,z-三个轴上的分量的大小. 图629.1说明了2-D向量的表示法, x-,y-分量的长度组成直角三角形的两个边.

 

Figure 629.1 Calculating a 2-D vectors magnitude.

图629.1 2-D向量的计算

 

Because the sides make up a right triangle, the length of a vectoris the square root of the sum of the squares. In Java, you can calculate themagnitude of a 2-D vector with the following code:

由于向量的x-,y-分量组成直角三角形的两个边, 向量本省的长度就等于这两个分量的平方的平均的平方根. 在Java中,你可用以下代码来计算一个2-D向量的大小:

mag = Math.sqrt(x*x + y*y);

 

You can define a 3-D vector by its length along the x-, y-, andz-axes. Again, to calculate the magnitude of a vector, you calculate itslength. To visualize how to calculate the magnitude of a 3-D vector, thinkabout solving it in steps. First, you must calculate the length of the shadowof the vector in the x-y plane. As you have learned, for 2-D vectors this isthe square root of the sum of the x and y squares. Then, you have a righttriangle with this value, the shadow of the vector in the x-y plane, and thethird dimension z. To find the length, you find the sum of the squares

again. Figure 629.2 shows a vector in 3-D space.

你可以利用3-D向量在x-,y-,z-三个轴上的分量来定义3-D向量. 同样,为了计算3-D向量计算大小,你要算出它的长度. 为了形象地说明3-D向量大小如何计算,我们分几步来加以说明: 首先, 你必须计算向量在x-y 平面的投影的长度. 你已知道, 对于2-D向量, 这个长度就等于x,y这两个分量的平方的平均的平方根. 然后, 利用这一个值,即向量在在x-y平面上的影子,以及向量z分量的大小,你就得到了一个直角三角形. 为了计算向量的长度,你再一次计算和的平方根. 图629.2说明了3-D空间的一个向量.

 

Figure 629.2 Calculating a 3-D vectors magnitude.

图629.2 3-D向量大小的计算

 

The following code demonstrates how you calculate the magnitudeof a 3-D vector using Java:

以下的代码说明了怎样利用Java语言计算一个3-D向量的大小.

mag = Math.sqrt(x*x + y*y +z*z);

 

630 Adding Vectors

630 向量相加

You can graphically add vectors by placing the head to tail, andthen drawing a vector from the first vectors tail to the last vectors head.Figure 630 shows two vectors that have been added together graphically.

你可以用作图法来实现向量的加法. 为此, 把两个向量头尾相接, 然后作一个向量,从前一个向量的尾部连到后一个向量的头部. 图630说明了用作图法加在一起的一个向量.

 

Figure 630 Adding vectors graphically.

图630 用作图法来加向量

 

You can also add vectors mathematically. To do this, you need tounderstand components. Vector components are the vectors length along eachaxis. Consider the following notation for a 2-D vector:

你也可以通过数学计算来做向量加法. 为此, 你必须理解向量的分量. 向量的分量就是向量在座标系统的各个轴上的投影. 考虑以下2-D向量的记法:

A = 2i + 2j

 

In this case, i represents a univector along the x-axis. Aunivector is a vector with a magnitude of one. The j represents a univectoralong the y-axis.

在这里, i代表沿x-轴的单位向量. 单位向量就是大小为1的一个向量. j代表沿y-轴的单位向量.

*** This is confusing rework

***本TIP以下解释明显错,现按书上译出

{The syntax says if you add 3i and 4j, you get vector A.Mathematicians refer to 3i as the x component of vector A and to 4j as the ycomponent of vector A. You can add two vectors by adding their components, asthe following line demonstrates.}

这种记法的意思是, 向量 A 沿x-轴方向有3个单位的长度, 而沿y-轴方向有4个单位的长度. 为了把向量数学地加起来,你可对向量的分量按如以方式进行相加: 

A = 2i + 2j

B = 1i + 2j

C = A + B = (2i + 2j) + (1i+ 2j) = 3i + 4j

 

{For 3-D vectors, there is a third component k along the z-axis,as shown.}

对于3-D向量, 除了x-,y-分量外, 你还要用第三个分量k来代表沿z-轴的分量,如:

A = 1i + 2j + 3k

 

631 Calculating a Dot Product

631 计算向量的点积

***以下有错(书上也错),已部分修改了

One of the calculations that you can perform on two vectors isto compute a dot product. A dot product determines if two vectors are in thesame direction. For example, you can use a dot product to determine if asurface is facing an observer. A normal vector is a vector that isperpendicular to a surface. If a surface faces an observer, then the dotproduct of the surfaces normal vector and a vector from the center of thesurface to the observer will be positive. If the dot product is negative, theback of the surface faces the observer.

在两个向量上可以实行的另一种计算是点积. 两个向量的点积是一个标量(实数),它的大小和两个向量的长度有关,还和它们的方向有关. 当两个向量的方向一致时点积的值最大. 利用这一事实,可用来判别两个向量是否有相同的方向. 也可用来判别一个面是否朝向观察者. 一个平面的法向量是平面的一根垂直线. 如果平面朝向观察者,则平面法向量,与从平面中心到观察者所构成的向量, 的点积为正. 如果点积为负, 则表示平面的背面朝向了观察者.

To calculate a dot product, you multiply the components of thevector, then add them up. You get a positive result if the vectors are in thesame direction. You get a negative result if the vectors are in oppositedirections. You will get zero if the vectors are ninety degrees apart. Thefollowing code defines a Vector3d class and a method that calculates the dotproduct of two vectors: 

为了计算点积,你把两个向量的对应分量分别相乘,然后将它们加在一起. 如果向量的方向相同, 你就得到正的结果.  如果向量的方向相反, 你就得到负的结果. 如果两个向量之间夹着90度的角, 则你得到的结果为0. 以下的代码定义了一个Vector3d 类, 并提供了一个计算两个向量点积的方法:

public class Vector3d {

 

   float x = 0;

   float y = 0;

   float z = 0;

  

   public Vector3d(float i, float j, float k)

     {

        x = i;

        y = j;

        z = k;

     }

 

   public float dot(Vector3d A)

     {

        float result = A.x * x + A.y * x + A.z* z;

        return (result);

     }

 

   public float mag()

     {

       float result = (float)Math.sqrt(x*x +y*y + z*z);

       return (result);

     }

 

   public String toString()

     {

       return(new String("(" + x +"," + y + "," + z + ")"));

     }

}

 

632 Calculating a Cross Product

632 计算向量的叉积

One of the most useful vector calculations is the cross product.The result of the cross product of two vectors is a third vector that isperpendicular to the two vectors. For example, if you perform the cross productof the x-axis and y-axis of a right-handed coordinate system, the result is thez-axis. The z-axis is perpendicular to the x-y plane.

向量的最有用的计算之一是叉积. 两个向量的叉积生成和两个向量都垂直的第三个向量.例如,你可以对x-轴和y-轴上的两个正的向量进行叉积, 则在右手座标系中,其生成的叉积向量是沿z-轴的一个正向量.z轴是垂直于x-y平面的.

When you calculate a cross product, the result is orderdependent. For example, A X B (pronounced A cross B), where A and B arevectors, is not the same as B X A. In fact, the results are two vectors thatare in opposite directions. To calculate a cross product, you cross multiplythe vector components. The following code demonstrates how to calculate thecross product of two vectors. The code builds upon the Vector3d class presentedin the previous tip.

叉积的结果是和次序有关的. 也就是说, A X B(读成 A 叉乘 B)一般不等于 B X A.这里A,B都是向量. 实际上, 这两个结果正好是两个方向相反的向量. 为了计算叉积,你把向量的分量交错相乘.以下的代码举例说明了怎样计算两个向量的叉积. 代码是建立在上一TIP所提供的Vector3d 类之上的.

 

public Vector3dcross(Vector3d A)

  {

    float cx = (y*A.z) - (A.y*z);

    float cy = (A.x*z) - (x*A.z);

    float cz = (x*A.y) - (A.x*y);

    return (new Vector3d(cx, cy, cz));

 }

 

633 Understanding the Viewport

633 了解视区

Within a graphics program, a viewport describes a 3-D scene from an observers point of view. As you know, a computer screen is a 2-D surface.You can use a viewport to render a 3-D object to a flat 2-D computer screenbased on a particular point of view.

在图形程序中, 视区(viewport)画下了从观察者眼中所看到的一幅3-D情景. 你知道, 电脑的屏幕是一个2-D的面. 你能利用视区把一个3-D对象画在一个扁平的电脑屏幕上是建立在一个特殊视点基础上的.

Think of the viewport as a porthole that sits between theobservers eyes and the viewable objects. You define a viewport by a point thatdefines the observers location, a vector that determines the observers line ofsight, and a vector that defines the observers up direction. Another importantvalue is how far the viewport is from the observer.

可把视区(`viewport)设想为一个方孔,它位于观察者的眼睛和被观察对象之间. 你可以通过观察者的眼睛位置,以及观察者的视线向量,和向上垂直于视线的向量,来确定一个视区,另一重要参数是视距,即被观察物体离开观察者眼睛的距离有多少远。远。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Actually, a viewport is really a second 3-D coordinate system.The viewports x-axis is horizontal to the observers point of view. Theviewports y-axis is vertical to the observers point of view. Finally, thez-axis is the observers line of sight into the computer screen. Figure 633shows an observer and a viewport.

实际上, 视区是第二个3-D坐标系统. 视区的x-轴从观察者来看是水平的; 视区的y-轴从观察者来看是垂直的. 最后, 视区的z-轴就是观察者向电脑屏幕中看过去的视线. 图633画出了一个观察者和一个视区.

 

Figure 633An observer and a viewport.

图633 观察者和视区

 

Before you can convert a 3-D object to the viewport coordinatesystem, you must understand matrixes. Matrixes are a convenient way to perform3-D transformations, as the following tips will demonstrate.

在你能把一个3-D对象转换成视区坐标系统之前, 你必须先要了解矩阵. 矩阵是实现3-D变换的方便工具, 这在以下的TIP中将会得到证明.

 

634 Understanding Matrixes

634 了解矩阵

A coordinate is a point in space. You can transform (move orchange) a point using a matrix. Matrixeslet you translate (move), scale, and rotate objects. The advantage of matrixesis that you can multiply a series of matrixes together to create a singlematrix. You can then use the single matrix to perform complex transformations.As you have learned, converting a 3-D point to screen coordinates is reallyjust converting a point from one 3-D coordinate system to another. Actually,you can perform this conversion using matrixes. A series of rotations,translations, and scaling can convert any 3-D coordinate system into another soyou can build a single matrix with all the steps necessary to translate a pointfrom the 3-D world to the viewport-coordinate system.

一个坐标对应于空间的一个点. 你可以利用矩阵对点的位置实行变换. 利用矩阵来移动或转动一个对象,或改变对象的大小. 矩阵的好处就是可以把一系列的矩阵乘在一起, 构成一个单一的矩阵, 然后利用这一单个的矩阵来作变换.  正如你所了解的,把一个3-D点变换到屏幕坐标,实际就是一个坐标系统到另一个坐标系统的转换. 这种转换就可利用矩阵来实现. 一系列的旋转,平移,和变比就可把各种坐标系统转换成另一种坐标系. 这样, 你可为所有需要的步骤创建一个单一的矩阵,以便把一个点从3-D世界转换到屏幕上的视区座标系统.

 

Matrixes are also useful when you want to perform an animation.Imagine the hands of a clock that must rotate around in a circle. You candefine a matrix that performs the rotation. Then, you can combine that matrixwith one that transforms a coordinate from a 3-D world to viewport coordinates.This way, you will avoid performing many of the intermediate calculations.Matrixes allow for easy rotation, translation, scaling animation, and also efficient3-D to 2-D calculations of a large list of coordinates.

当你要实现一个动画时矩阵也有用处. 设想钟表的针要绕圆周转动一圈. 你就可以定义一个实现转动的矩阵. 然后, 你可把这个矩阵与实行3-D世界到视区坐标转换的矩阵结合起来. 这样, 你就可以免去大量的中间的计算.矩阵允许你进行简易的动画, 如转动, 平动, 放大缩小, 它也可有效地对大量坐标清单实行3-D到2-D的计算.

 

If you wish to learn more about the math behind 3-D graphics,many good books are available. The following tips, however, try to give a clearexplanation with an emphasis on use, not theory. These tips should give you ageneral understanding of how 3-D graphics work. In the following tips, you willlearn how to implement and use a 3-D matrix class.

如果你愿意学习有关3-D图形学的更多的基本数学知识, 你可以找到许多好的参考书.以下的TIP打算对此作一些的解释,但强调使用,而不是理论. 这些TIP将给你3-D图形是怎样工作的一个一般性的了解. 你将学习怎样实现和使用3-D矩阵类.

 

635 Implementing a 3-D Matrix

635 3-D矩阵的实现

In the previous tip, you learned about matrixes. You can usematrixes to transform 3-D objects. Since there is no standard matrix class inJava, you must write your own. The following code defines the basis of a 3-Dmatrix class.

在上一TIP中你已知道了矩阵的意义. 你可用矩阵来变换一个3-D对象. 由于在Java中没有标准的矩阵类, 你必须自己来编写. 以下的代码定义了一个3-D矩阵类:

public class Matrix3d{       // Define a 3-D Matrix class

 

   float e00,e01,e02,e03;

   float e10,e11,e12,e13;

   float e20,e21,e22,e23;

   float e30,e31,e32,e33;

 

   static final double toRad = Math.PI/180.0;

   static int REPLACE = 0;

   static int PRECONCAT = 1;

   static int POSTCONCAT = 2;

 

   public Matrix3d()           // Create an identity matrix 

     {      

       identity ();

     }

 

   public void identity()      // Make an identity matrix

     {

       e01 = e02 = e03 = 0.0f;

       e10 = e12 = e13 = 0.0f;

       e20 = e21 = e23 = 0.0f;

       e30 = e32 = e32 = 0.0f;

       e00 = 1.0f;

       e11 = 1.0f;

       e22 = 1.0f;

     }

 

   public Point3d transform(Point3d p)  // Transform a point

     {                                  // using a matrix  

       float x = p.x * e00 + p.y * e01 + p.z *e02 + e03;

       float y = p.x * e10 + p.y * e11 + p.z *e12 + e13;

       float z = p.x * e20 + p.y * e21 + p.z *e22 + e23;

       return(new Point3d(x,y,z));

     }

 }

Notice that the Matrix3d class constructor makes an identitymatrix. By definition, a point that is transformed by an identity matrix isleft unchanged. Therefore, the matrix initially does nothing.

注意 Matrix3d 类构造函数产生一个单位矩阵. 按定义, 一个点的坐标数据经过单位矩阵变换后,它们的值不改变. 因此, 矩阵初始时不做任何事情.

 

636 Implementing a 3-D Point

636 3-D点的实现

In the previous tips Matrix3d class, the transform method takesa 3-D point and transforms it using the matrix. The following code demonstratesthe Point3d class that is an argument to the transform method:

在上一TIP的Matrix3d类中, transform方法以一3-D点为参数,并利用矩阵来对其实行变换. 以下的代码说明了Point3d类是transform方法的一个自变量:

public class Point3d {

 

    float x = 0f;  // 3-D world coordinates

    float y = 0f;

    float z = 0f;

 

    public Point(float x0, float y0, float z0)

      {

        x = x0;

        y = y0;

        z = z0;

      }

 

    public String toString()

      {

         return(new String("(" + x +"," + y + "," + z + ")"));

      }

 }

As you can see, the Point3d class constructor assigns threeparameters to the member variables x, y, and z which define the pointscoordinates. The toString function simply displays each coordinates value.

你能看出, Point3d 类构造函数把三个参数赋给数值变量x,y和z, 由它们来定义点的坐标. 函数toString 只是用来显示每个坐标的值.

 

637 Creating a Translate Matrix

637 平移矩阵的实现

When you translate a point, you are really just moving it in 3-Dspace. Within the Matrix3d class presented in Tip 635, you can add thefollowing translate method which modifies the matrix, by adding x, y, and zoffsets:

当你平移一个点时, 你实际就是让它在3-D空间中改变位置. 你可以在TIP 635 中提出的Matrix3d类中加入以下的translate方法,它通过增加x,y,z的偏移量来修改矩阵:

// Translate the matrix

public void translate(floatx, float y, float z)

  {

    e03 += x;

    e13 += y;

    e23 += z;

  }

 

638 Using the Translate Matrix

638 平移矩阵的使用

In the previous tip, you created a translate method. Thefollowing program demonstrates how to use the Matrix3d class to perform atranslate operation. The Matrix3d and Point3d class is not shown in the programcode; rather, assume the classes reside in their own file:

在以上TIP中你创建了平移方法 translate. 以下的程序说明怎样利用Matrix3d类来执行平移操作. 在程序的代码中没有表示出 Matrix3d类和 Point3d类;我们假设这些类都是在它们各自的文件中:

public class testTranslate{

 

   public static void main(String args)

     {

        Matrix3d matrix = new Matrix3d();

        Point3d ptA = new Point3d(5.0f,5.0f,5.0f);

 

        matrix.translate(3.0f,4.0f,5.0f);      // define translate

        System.out.println("Before A"+ ptA);

        ptA = matrix.transform(ptA);           // move point A

        System.out.println("After A"+ ptA);

     }

 }

 

639 Creating a Scale Matrix

639 创建一个变比矩阵

Within a graphics program, there will be times when you need toscale an object, either increasing or decreasing the objects size. To scale a3-D point, you multiply the points x, y, and z components by a scale factor.The scale factor may be different or the same for each component. The followingscale method modifies the Matrix3d object so that it will scale a point whenyou call the transform method. If you make multiple calls to the scale method,the scales are multiplied together.

在一图形程序中你有时需要改变一对象的大小比例, 或者是放大它, 或者是缩小它.为了改变一个3-D点(的位置)的大小比例, 你可将点的x, y, 和 z 分量都乘以一个比例因子. 比例因子对每一个分量可以是不同的也可以是相同的. 以下的变比方法修改了Matrix3d 对象, 这样当你调用它时就能改变点的位置. 如果你重复调用这一变比方法,则点的位置也会不断改变.

 

// Scale a matrix by x,y,z

public void scale(float x,float y, float z)

  {

    e00 *= x;

    e11 *= y;

    e22 *= z;

  }

  

// Scale a matrix by asingle value

public void scale(float s)

  {

    e00 *= s;

    e11 *= s;

    e22 *= s;

  }

 

640 Using the ScaleMatrix

640 变比矩阵的使用

In the previous tip, you learned how to create a scale methodwith which you can increase or decrease the components of a matrix. Thefollowing program, testScale.java, demonstrates how to use the Matrix3d classto perform a scale. The Matrix3d class is not shown in the program code. Aswith the previous tips, Matrix3d and Point3d classes are assumed to reside intheir own files.

在以上TIP中你已知道怎样来创建一个变比方法, 用它来加大或减小矩阵的图形.以下程序,testScale.java,说明了怎样利用Matrix3d类来实现变比. Matrix3d 类和 Point3d  类假定都已保留在它们各自的文件中.

public class testScale {

 

   public static void main(String args)

     {

        Matrix3d matrixA = new Matrix3d();

        Matrix3d matrixB = new Matrix3d();

 

        Point3d ptA = new Point3d(100.0f,100.0f, 100.0f);

        Point3d ptB = new Point3d(100.0f,100.0f, 100.0f);

 

        matrixA.scale(0.30f,0.40f,0.50f); //define scale A

        matrixB.scale(0.50f);             // define scale B

        System.out.println("Before A"+ ptA + ", B" + ptB);

 

        ptA = matrixA.transform(ptA);     // scale point A

        ptB = matrixB.transform(ptB);     // scale point B

        System.out.println("After A"+ ptA + ", B" + ptB);

     }

}

As you can see in the above example, there are two scalemethods. One method takes three arguments and the other takes one argument. Theprogram displays the value of the points before and after the transform calls.

从以上例子可以看出这里有两种不同的变比方法. 一种方法用了三个变元,而另一种方法只需要一个变元. 程序显示了在变换调用之前和之后的点的值.

 

【java三维编程(一)完】


 


原创粉丝点击