?? transform.java
字號:
/*
Netwar
Copyright (C) 2002 Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
This file is part of Netwar.
Netwar is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Netwar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Netwar; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package netwar.utils.vectorgraphics;
/** A class that handles conversions between Point3D and Point2D and visa-versa.
*
* This class is stand alone, rather than included in the Point classes, so that multiple Transform classes could exist. This was done in order to show the unit in action on the netwar panel.
* @author Kyle Kakligian
*/
public class Transform {
private float SCALE = 5;
private float XX = SCALE;
private float XY = 0;
private float XZ = 0;
private float YX = 0;
private float YY = -SCALE/2;
private float YZ = -SCALE*(float)Math.sqrt(3)/2;
private float ZX = 0;
private float ZY = -SCALE/2;
private float ZZ = SCALE*(float)Math.sqrt(3)/2;
private float X0 = 600;
private float Y0 = 350;
private float Xmin = 0;
private float Xmax = (int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth();
private float Ymin = 0;
private float Ymax = (int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();
private int iteration = 0;
/** Creates a new instance of Transform with default values*/
public Transform() {
}
/** Converts the gamespace point into a 2D screenspace point using the transform matrix.
* @param pt The point to convert.
* @return Returns a new Point2D.
*/
public Point2D getPoint2D(Point3D pt) {
return new Point2D(pt.x * XX + pt.y * XY + pt.z * XZ + X0,
pt.x * YX + pt.y * YY + pt.z * YZ + Y0);
}
/** Converts the gamespace point into a 2D screenspace point using the transform matrix.
* @param p3 The point to convert.
* @param p2 The point to return.
* @return Returns the Point2D.
*/
public Point2D getPoint2D(Point3D p3, Point2D p2) {
p2.set(p3.x * XX + p3.y * XY + p3.z * XZ + X0,
p3.x * YX + p3.y * YY + p3.z * YZ + Y0);
return p2;
}
/** Converts the gamespace point into a 3D screenspace point with Z using the transform matrix.
* @param p3 The point to convert.
* @param p2 The point to return.
* @return True iff the point is within the screen grid.
*/
public boolean getPoint2DwithZ(Point3D p3, Point3D p2) {
p2.set(p3.x * XX + p3.y * XY + p3.z * XZ + X0,
p3.x * YX + p3.y * YY + p3.z * YZ + Y0,
p3.x * ZX + p3.y * ZY + p3.z * ZZ);
return (p2.x >= Xmin && p2.x <= Xmax && p2.y >= Ymin && p2.y <= Ymax);
}
/** Converts a 2D screenspace point into a 3D gamespace point using the transform matrix.
* @param pt A Point2D in screen space.
* @return Returns a new Point3D.
*/
public Point3D getPoint3D(Point2D pt) {
return new Point3D(
(pt.x - X0 - XY / YY * (pt.y - Y0))/(XX - XY * YX / YY),
(pt.y - Y0 -YX / XX * (pt.x - X0))/(YY - YX * XY / XX),
0);
}
/** Converts a 2D screenspace point into a 3D gamespace point using the transform matrix.
* @param p2 A Point2D in screen space.
* @param p3 A Point3D to return.
* @return Returns the Point3D.
*/
public Point3D getPoint3D(Point2D p2, Point3D p3) {
p3.set(
(p2.x - X0 - XY / YY * (p2.y - Y0))/(XX - XY * YX / YY),
(p2.y - Y0 -YX / XX * (p2.x - X0))/(YY - YX * XY / XX),
0);
return p3;
}
/** Converts a 2D screenspace point into a 3D gamespace point using the transform matrix.
* @param x The x-axis value in screen space.
* @param y The x-axis value in screen space.
* @return Returns a new Point3D.
*/
public Point3D getPoint3D(int x, int y) {
return new Point3D(
(x - X0 - XY / YY * (y - Y0))/(XX - XY * YX / YY),
(y - Y0 -YX / XX * (x - X0))/(YY - YX * XY / XX),
0);
}
/** Adjusts the transform matrix in the (x,y,0) direction. Use this method for the effect of scrolling.
* @param x Change in the x direction.
* @param y Change in the y direction.
*/
public void translate(int x, int y) {
X0 += x;
Y0 += y;
iteration++;
}
/** Returns the maxX variable.
* @return the MaxX variable.
*/
public int maxX() { return (int)Xmax; }
/** Returns the maxY variable.
* @return the maxY variable.
*/
public int maxY() { return (int)Ymax; }
/** Adjust the transformation matrix due to a resize of the displayer.
* This is necessary now, because the Transform will allow a GraphicThing to know it is out of bounds.
* Includes re-centering.
* @param deltaWidth the change in width.
* @param deltaHeight the change in height.
*/
public void resize(float deltaWidth, float deltaHeight) {
translate((int)(deltaWidth / 2), (int)(deltaHeight / 2));
Xmax += deltaWidth;
Ymax += deltaHeight;
}
/** Adjusts the transform matrix in the (0,0,z) direction. Use this method for the effect of zooming in and out.
* @param factor The magnification factor.
* @param halfwidth Half the width of the screen, in screenspace
* @param halfheight Half the height of the screen, in screenspace.
*/
public void zoom(float factor, int halfwidth, int halfheight) {
if(factor == 0 || factor == 1) return;
SCALE *= factor;
XX *= factor;
XY *= factor;
XZ *= factor;
YX *= factor;
YY *= factor;
YZ *= factor;
// ZX *= factor;
// ZY *= factor;
// ZZ *= factor;
X0 = X0 * factor + halfwidth * (1 - factor);
Y0 = Y0 * factor + halfheight * (1 - factor);
iteration++;
}
/** Returns the current scaling value. This is constant until zoom() is called.
* @return Scale.
*/
public float getScale() { return SCALE; }
/** Returns the current iteration number, incremented every time the scaling factors change.
* Thus, if the same point3D is translated according to the same Transform with the same
* iteration number and the Point3D hasn't changed in between,
* it knows the outcome will be the same.
* @return the current iteration number
*/
public int getIteration() { return iteration; }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -