?? point3d.java
字號(hào):
/*
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;
import netwar.utils.Trig;
/** This class is used to represent a location or vector in gamespace. A Point3D can be translated into a Point2D using the internal transform matrix, and that matrix can be manipulated for zooming and changes in angle.<br>
* <br>
* Note that the get_ functions return a new Point2D/3D while the do_ funtions affect <B>this</B> Point3D.
* @author Daniel Grund
* @author Kyle Kakligian
*/
public class Point3D {
/** X-axis value or length.
*/
public float x;
/** Y-axis value or length.
*/
public float y;
/** Z-axis value or length.
*/
public float z;
/** Constructor for the orgin.
*/
public Point3D() {}
/** Constructs a Point3D
* @param X X-axis value or length.
* @param Y Y-axis value or length.
* @param Z Z-axis value or length.
*/
public Point3D(float X, float Y, float Z) {x=X; y=Y; z=Z;}
/** Constructs a Point3D, converting the doubles to floats.
* @param X X-axis value or length.
* @param Y Y-axis value or length.
* @param Z Z-axis value or length.
*/
public Point3D(double X, double Y, double Z) {x=(float)X; y=(float)Y; z=(float)Z;}
/** Copy constructor
* @param v Copy.
*/
public Point3D(Point3D v) {x = v.x; y = v.y; z = v.z;}
/** Returns a new Point3D that is the scalar product of <B>this</B> one.
* @param scalar Multiplier
* @return Returns a new Point3D
*/
public Point3D getProduct(float scalar) {
return new Point3D(x * scalar, y * scalar, z * scalar); }
/** Evalutates the dot (scalar) product of two vectors.
* @param v Secound vector in the dot product operation.
* @return Scalar.
*/
public float getDotProduct(Point3D v) {
return x*v.x + y*v.y + z*v.z; }
/** Evalutates the cross (vector) product of two vectors.
* @param v Secound vector in the cross product operation.
* @return Returns a new Point3D
*/
public Point3D getCrossProduct(Point3D v) {
return new Point3D(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x); }
/** Returns the sum of <B>this</B> Point3D and the given vector.
* @param v The vector to add to <B>this</B>.
* @return Returns a new Point3D.
*/
public Point3D getSum(Point3D v) {
return new Point3D(x + v.x, y + v.y, z + v.z); }
/** Returns the vector difference of <B>this</B> Point3D and the given Point3D.
* @param v The secound point in this operation that the returned vector points to. (<B>this</B> + returned = <I>v</I>)
* @return Returns a new Point3D
*/
public Point3D getDifference(Point3D v) {
return new Point3D(x - v.x, y - v.y, z - v.z); }
/** Returns a new Point3D that is the scalar product of <B>this</B> one.
* @param scalar Multiplier.
* @return Returns a reference to <B>this</B> where the result is stored.
*/
public Point3D doProduct(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return this;
}
/** Evalutates the cross (vector) product of two vectors.
* @param v Secound vector in the cross product operation.
* @return Returns a reference to <B>this</B> where the result is stored.
*/
public Point3D doCrossProduct(Point3D v) {
float X = x; float Y = y; float Z = z;
x = Y * v.z - Z * v.y;
y = Z * v.x - X * v.z;
z = X * v.y - Y * v.x;
return this;
}
/** Returns the sum of <B>this</B> Point3D and the given vector.
* @param v The vector to add to <B>this</B>.
* @return Returns a reference to <B>this</B> where the result is stored.
*/
public Point3D doSum(Point3D v) {
x += v.x;
y += v.y;
z += v.z;
return this;
}
/** Returns the vector difference of <B>this</B> Point3D and the given Point3D.
* @param v The secound point in this operation that the returned vector points to. (pre<B>this</B> + post<B>this</B> = <I>v</I>)
* @return Returns a reference to <B>this</B> where the result is stored.
*/
public Point3D doDifference(Point3D v) {
x -= v.x;
y -= v.y;
z -= v.z;
return this;
}
/** Changes the <B>length</B> of this vector to 1. The direction is kept constant.
*/
public void Normalize() {
float r = getLength();
x /= r;
y /= r;
z /= r;
}
/** Returns the distance to the orgin, or, returns the length of the vector. (Whether <B>this</B> is a vector or a point, both values are the same but have different meanings)
* @return Length.
* @see #getLengthSquared getLengthSquared()
*/
public float getLength() {
return (float)Math.sqrt(x*x+y*y+z*z);
}
//Faster version of get length.
//Don't need to squareroot if you compare squares instead.
/** Same as getLength() without the square root. Since all distances are positive and reletive, we can compare their square distances. This is an optimization.
* @return x*x + y*y + z*z
* @see #getLength getLength()
*/
public float getLengthSquared() {
return (x*x+y*y+z*z);
}
/** Rotates point around the line containing linePt which extends in the direction indicated by vector by an angle theta.
* Returns a point, after it is transformed.
* @return point, after it is transformed.
* @param linePt The point which is on the line.
* @param vec The direction which the line extends from linePt.
* @param theta The angle to rotate by. */
public Point3D doRotate(Point3D linePt, Point3D vec, int theta) {
double c = Trig.cos(theta);
double s = Trig.sin(theta);
double X = vec.x;
double Y = vec.y;
double Z = vec.z;
double D2 = (float)vec.getLengthSquared();
double D = (float)Math.sqrt(D2);
if(this == linePt) return this;
doDifference(linePt);
double newX = (x) * ( c + (1 - c) * X * X / D2 )
+ (y) * ( ( (1 - c) * (X * Y / D ) - (Z * s ) ) / D)
+ (z) * ( ( (1 - c) * (X * Z / D ) + (Y * s ) ) / D )
+ linePt.x;
double newY = (x) * ( ( (1 - c) * (Y * X / D ) + (Z * s ) ) / D )
+ (y) * ( c + (1 - c) * Y * Y / D2 )
+ (z) * ( ( (1 - c) * (Y * Z / D ) - (X * s ) ) / D )
+ linePt.y;
double newZ = (x) * ( ( (1 - c) * (X * Z / D ) - (Y * s ) ) / D )
+ (y) * ( ( (1 - c) * (Y * Z / D ) + (X * s ) ) / D )
+ (z) * ( c + (1 - c) * Z * Z / D2 )
+ linePt.z;
x = (float)newX;
y = (float)newY;
z = (float)newZ;
return this;
}
/** Rotates point around the line containing linePt which extends in the direction indicated by vector by an angle theta.
* Returns a new point, which is the transformed point: the original point is left intact.
* @return A new Point, which is the rotation of point.
* @param linePt The point which is on the line.
* @param vec The direction which the line extends from linePt.
* @param theta The angle to rotate by. */
public Point3D getRotate(Point3D linePt, Point3D vec, int theta) {
double c = Trig.cos(theta);
double s = Trig.sin(theta);
double X = vec.x;
double Y = vec.y;
double Z = vec.z;
double D2 = (float)vec.getLengthSquared();
double D = (float)Math.sqrt(D2);
Point3D newPt = getDifference(linePt);
double newX = (x) * ( c + (1 - c) * X * X / D2 )
+ (y) * ( ( (1 - c) * (X * Y / D ) - (Z * s ) ) / D)
+ (z) * ( ( (1 - c) * (X * Z / D ) + (Y * s ) ) / D )
+ linePt.x;
double newY = (x) * ( ( (1 - c) * (Y * X / D ) + (Z * s ) ) / D )
+ (y) * ( c + (1 - c) * Y * Y / D2 )
+ (z) * ( ( (1 - c) * (Y * Z / D ) - (X * s ) ) / D )
+ linePt.y;
double newZ = (x) * ( ( (1 - c) * (X * Z / D ) - (Y * s ) ) / D )
+ (y) * ( ( (1 - c) * (Y * Z / D ) + (X * s ) ) / D )
+ (z) * ( c + (1 - c) * Z * Z / D2 )
+ linePt.z;
newPt.x = (float)newX;
newPt.y = (float)newY;
newPt.z = (float)newZ;
return newPt;
}
/** Implementation of Object.equals().
* Compares two Point3Ds for equality. This operation is reflexive, symmetric, and transitive.
* For any reference value x, x.equals(null) returns false.
*
* @param p The reference Point3D with which to compare.
* @return true if <B>this</B> Point3D is the same as the <I>p</I> argument; false otherwise.
*/
public boolean equals(Point3D p)
{ if(p==null) return false; return (x == p.x && y == p.y && z == p.z); }
/** Implementation of Object.equals().
* Compares two Point3Ds for equality. This operation is reflexive, symmetric, and transitive.
* For any reference value x, x.equals(null) returns false.
*
* @param X The x-axis value with which to compare.
* @param Y The y-axis value with which to compare.
* @param Z The z-axis value with which to compare.
* @return true if <B>this</B> Point3D is the same as described in the arguments; false otherwise.
*/
public boolean equals(float X, float Y, float Z)
{ return (x == X && y == Y && z == Z); }
/** Sets <B>this</B> point as a copy of the given one.
* @param p The Point3D to copy.
* @return Returns a reference to itself.
*/
public Point3D set(Point3D p)
{ x = p.x; y = p.y; z = p.z; return this; }
/** Sets <B>this</B> point from the given information.
* @param X The new x-axis value.
* @param Y The new y-axis value.
* @param Z The new z-axis value.
* @return Returns a reference to itself.
*/
public Point3D set(float X, float Y, float Z)
{ x = X; y = Y; z = Z; return this; }
/** (0,0,0)
*/
public static Point3D origin = new Point3D(0,0,0);
/** +east direction
*/
public static Point3D unitEast = new Point3D(1,0,0);
/** +north direction
*/
public static Point3D unitNorth = new Point3D(0,1,0);
/** +up direction
*/
public static Point3D unitUp = new Point3D(0,0,1);
/** +west direction
*/
public static Point3D unitWest = new Point3D(-1,0,0);
/** +south direction
*/
public static Point3D unitSouth = new Point3D(0,-1,0);
/** +down direction
*/
public static Point3D unitDown = new Point3D(0,0,-1);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -