📄 vector2f.java
字号:
/*
* Copyright (c) 2003-2009 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme.math;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.logging.Logger;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
/**
* <code>Vector2f</code> defines a Vector for a two float value vector.
*
* @author Mark Powell
* @author Joshua Slack
*/
public class Vector2f implements Externalizable, Savable, Cloneable {
private static final Logger logger = Logger.getLogger(Vector2f.class.getName());
private static final long serialVersionUID = 1L;
/**
* the x value of the vector.
*/
public float x;
/**
* the y value of the vector.
*/
public float y;
/**
* Creates a Vector2f with the given initial x and y values.
*
* @param x
* The x value of this Vector2f.
* @param y
* The y value of this Vector2f.
*/
public Vector2f(float x, float y) {
this.x = x;
this.y = y;
}
/**
* Creates a Vector2f with x and y set to 0. Equivalent to Vector2f(0,0).
*/
public Vector2f() {
x = y = 0;
}
/**
* Creates a new Vector2f that contains the passed vector's information
*
* @param vector2f
* The vector to copy
*/
public Vector2f(Vector2f vector2f) {
this.x = vector2f.x;
this.y = vector2f.y;
}
/**
* set the x and y values of the vector
*
* @param x
* the x value of the vector.
* @param y
* the y value of the vector.
* @return this vector
*/
public Vector2f set(float x, float y) {
this.x = x;
this.y = y;
return this;
}
/**
* set the x and y values of the vector from another vector
*
* @param vec
* the vector to copy from
* @return this vector
*/
public Vector2f set(Vector2f vec) {
this.x = vec.x;
this.y = vec.y;
return this;
}
/**
* <code>add</code> adds a provided vector to this vector creating a
* resultant vector which is returned. If the provided vector is null, null
* is returned.
*
* @param vec
* the vector to add to this.
* @return the resultant vector.
*/
public Vector2f add(Vector2f vec) {
if (null == vec) {
logger.warning("Provided vector is null, null returned.");
return null;
}
return new Vector2f(x + vec.x, y + vec.y);
}
/**
* <code>addLocal</code> adds a provided vector to this vector internally,
* and returns a handle to this vector for easy chaining of calls. If the
* provided vector is null, null is returned.
*
* @param vec
* the vector to add to this vector.
* @return this
*/
public Vector2f addLocal(Vector2f vec) {
if (null == vec) {
logger.warning("Provided vector is null, null returned.");
return null;
}
x += vec.x;
y += vec.y;
return this;
}
/**
* <code>addLocal</code> adds the provided values to this vector
* internally, and returns a handle to this vector for easy chaining of
* calls.
*
* @param addX
* value to add to x
* @param addY
* value to add to y
* @return this
*/
public Vector2f addLocal(float addX, float addY) {
x += addX;
y += addY;
return this;
}
/**
* <code>add</code> adds this vector by <code>vec</code> and stores the
* result in <code>result</code>.
*
* @param vec
* The vector to add.
* @param result
* The vector to store the result in.
* @return The result vector, after adding.
*/
public Vector2f add(Vector2f vec, Vector2f result) {
if (null == vec) {
logger.warning("Provided vector is null, null returned.");
return null;
}
if (result == null)
result = new Vector2f();
result.x = x + vec.x;
result.y = y + vec.y;
return result;
}
/**
* <code>dot</code> calculates the dot product of this vector with a
* provided vector. If the provided vector is null, 0 is returned.
*
* @param vec
* the vector to dot with this vector.
* @return the resultant dot product of this vector and a given vector.
*/
public float dot(Vector2f vec) {
if (null == vec) {
logger.warning("Provided vector is null, 0 returned.");
return 0;
}
return x * vec.x + y * vec.y;
}
/**
* <code>cross</code> calculates the cross product of this vector with a
* parameter vector v.
*
* @param v
* the vector to take the cross product of with this.
* @return the cross product vector.
*/
public Vector3f cross(Vector2f v) {
return new Vector3f(0, 0, determinant(v));
}
public float determinant(Vector2f v) {
return (x * v.y) - (y * v.x);
}
/**
* Sets this vector to the interpolation by changeAmnt from this to the
* finalVec this=(1-changeAmnt)*this + changeAmnt * finalVec
*
* @param finalVec
* The final vector to interpolate towards
* @param changeAmnt
* An amount between 0.0 - 1.0 representing a percentage change
* from this towards finalVec
*/
public void interpolate(Vector2f finalVec, float changeAmnt) {
this.x = (1 - changeAmnt) * this.x + changeAmnt * finalVec.x;
this.y = (1 - changeAmnt) * this.y + changeAmnt * finalVec.y;
}
/**
* Sets this vector to the interpolation by changeAmnt from beginVec to
* finalVec this=(1-changeAmnt)*beginVec + changeAmnt * finalVec
*
* @param beginVec
* The begining vector (delta=0)
* @param finalVec
* The final vector to interpolate towards (delta=1)
* @param changeAmnt
* An amount between 0.0 - 1.0 representing a precentage change
* from beginVec towards finalVec
*/
public void interpolate(Vector2f beginVec, Vector2f finalVec,
float changeAmnt) {
this.x = (1 - changeAmnt) * beginVec.x + changeAmnt * finalVec.x;
this.y = (1 - changeAmnt) * beginVec.y + changeAmnt * finalVec.y;
}
/**
* Check a vector... if it is null or its floats are NaN or infinite, return
* false. Else return true.
*
* @param vector
* the vector to check
* @return true or false as stated above.
*/
public static boolean isValidVector(Vector2f vector) {
if (vector == null) return false;
if (Float.isNaN(vector.x) ||
Float.isNaN(vector.y)) return false;
if (Float.isInfinite(vector.x) ||
Float.isInfinite(vector.y)) return false;
return true;
}
/**
* <code>length</code> calculates the magnitude of this vector.
*
* @return the length or magnitude of the vector.
*/
public float length() {
return FastMath.sqrt(lengthSquared());
}
/**
* <code>lengthSquared</code> calculates the squared value of the
* magnitude of the vector.
*
* @return the magnitude squared of the vector.
*/
public float lengthSquared() {
return x * x + y * y;
}
/**
* <code>distanceSquared</code> calculates the distance squared between
* this vector and vector v.
*
* @param v the second vector to determine the distance squared.
* @return the distance squared between the two vectors.
*/
public float distanceSquared(Vector2f v) {
double dx = x - v.x;
double dy = y - v.y;
return (float) (dx * dx + dy * dy);
}
/**
* <code>distanceSquared</code> calculates the distance squared between
* this vector and vector v.
*
* @param v the second vector to determine the distance squared.
* @return the distance squared between the two vectors.
*/
public float distanceSquared(float otherX, float otherY) {
double dx = x - otherX;
double dy = y - otherY;
return (float) (dx * dx + dy * dy);
}
/**
* <code>distance</code> calculates the distance between this vector and
* vector v.
*
* @param v the second vector to determine the distance.
* @return the distance between the two vectors.
*/
public float distance(Vector2f v) {
return FastMath.sqrt(distanceSquared(v));
}
/**
* <code>mult</code> multiplies this vector by a scalar. The resultant
* vector is returned.
*
* @param scalar
* the value to multiply this vector by.
* @return the new vector.
*/
public Vector2f mult(float scalar) {
return new Vector2f(x * scalar, y * scalar);
}
/**
* <code>multLocal</code> multiplies this vector by a scalar internally,
* and returns a handle to this vector for easy chaining of calls.
*
* @param scalar
* the value to multiply this vector by.
* @return this
*/
public Vector2f multLocal(float scalar) {
x *= scalar;
y *= scalar;
return this;
}
/**
* <code>multLocal</code> multiplies a provided vector to this vector
* internally, and returns a handle to this vector for easy chaining of
* calls. If the provided vector is null, null is returned.
*
* @param vec
* the vector to mult to this vector.
* @return this
*/
public Vector2f multLocal(Vector2f vec) {
if (null == vec) {
logger.warning("Provided vector is null, null returned.");
return null;
}
x *= vec.x;
y *= vec.y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -