Package com.smartfoxserver.entities.data
Class SFSVector2
java.lang.Object
com.smartfoxserver.entities.data.SFSVector2
- All Implemented Interfaces:
Serializable
A class representing a Vector in 2D space
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns a new vector with all components in absolute values.floatangle()Returns the angle of this vector in radians, measured from the positive x-axis.floataspect()Returns the aspect ratio of this vector, the ratio of x to y.ceil()Returns a new vector with all components rounded up to the nearest integer.floatcross(SFSVector2 vec) Returns the 2D cross product (also known as the perpendicular dot product or wedge product) of this vector and the specified vector.floatReturns the squared distance between this vector and the specified vector.floatdistanceTo(SFSVector2 vec) Returns the Euclidean distance between this vector and the specified vector.divide(SFSVector2 vec) Returns a new vector that is the component-wise division of this vector by the specified vector.floatdot(SFSVector2 vec) Returns the dot product of this vector and the specified vector.static SFSVector2empty()booleanfloor()Returns a new vector with all components rounded down to the nearest integer.inthashCode()booleanReturns true if the vector is normalized, i.e., its length is approximately equal to 1.floatlength()Returns the length (magnitude) of this vector.lerp(SFSVector2 vec, float weight) Returns the result of the linear interpolation between this vector and the specified vector by the specified weight.max(SFSVector2 vec) Returns a new vector with the component-wise maximum of this vector and the specified vector.min(SFSVector2 vec) Returns a new vector with the component-wise minimum of this vector and the specified vector.mult(SFSVector2 vec) Returns a new vector that is the component-wise multiplication of this vector and the specified vector.Returns a new vector that is the normalized (unit length) version of this vector.rotate(float angle) Returns a new vector that is the result of rotating this vector by the specified angle.round()Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.sub(SFSVector2 vec) Returns a new vector that is the result of subtracting the specified vector from this vector.sum(SFSVector2 vec) Returns a new vector that is the sum of this vector and the specified vector.toString()
-
Field Details
-
x
public float x -
y
public float y
-
-
Constructor Details
-
SFSVector2
public SFSVector2(float x, float y) -
SFSVector2
-
-
Method Details
-
empty
-
length
public float length()Returns the length (magnitude) of this vector.- Returns:
- the length of the vector
-
isNormalized
public boolean isNormalized()Returns true if the vector is normalized, i.e., its length is approximately equal to 1.- Returns:
- true if the vector is normalized, false otherwise
-
sum
Returns a new vector that is the sum of this vector and the specified vector.- Parameters:
vec- the vector to add- Returns:
- a new vector representing the sum
- Throws:
IllegalArgumentException- if vec is null
-
sub
Returns a new vector that is the result of subtracting the specified vector from this vector.- Parameters:
vec- the vector to subtract- Returns:
- a new vector representing the difference
- Throws:
IllegalArgumentException- if vec is null
-
mult
Returns a new vector that is the component-wise multiplication of this vector and the specified vector.- Parameters:
vec- the vector to multiply with- Returns:
- a new vector representing the component-wise product
- Throws:
IllegalArgumentException- if vec is null
-
divide
Returns a new vector that is the component-wise division of this vector by the specified vector. If any component of the divisor is zero, the result will be Infinity or NaN.- Parameters:
vec- the vector to divide by- Returns:
- a new vector representing the component-wise quotient
- Throws:
IllegalArgumentException- if vec is null
-
normalize
Returns a new vector that is the normalized (unit length) version of this vector. If this vector has zero length, returns a zero vector.- Returns:
- a new normalized vector
-
angle
public float angle()Returns the angle of this vector in radians, measured from the positive x-axis. The returned angle is in the range [-PI, PI].- Returns:
- the angle in radians
-
rotate
Returns a new vector that is the result of rotating this vector by the specified angle.- Parameters:
angle- the angle to rotate by, in radians- Returns:
- a new rotated vector
-
dot
Returns the dot product of this vector and the specified vector. The dot product will be 0 for a right angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees. When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned.- Parameters:
vec- the vector to compute the dot product with- Returns:
- the dot product
- Throws:
IllegalArgumentException- if vec is null
-
cross
Returns the 2D cross product (also known as the perpendicular dot product or wedge product) of this vector and the specified vector. This returns a scalar representing the z-component of the 3D cross product when treating the 2D vectors as 3D vectors with z=0. The magnitude represents the area of the parallelogram formed by the two vectors. The sign indicates orientation: positive if vec is counterclockwise from this vector, negative if clockwise.- Parameters:
vec- the vector to compute the cross product with- Returns:
- the scalar cross product
- Throws:
IllegalArgumentException- if vec is null
-
distanceTo
Returns the Euclidean distance between this vector and the specified vector.- Parameters:
vec- the vector to measure distance to- Returns:
- the distance between the two vectors
- Throws:
IllegalArgumentException- if vec is null
-
distanceSquaredTo
Returns the squared distance between this vector and the specified vector. This is more efficient than distanceTo() when comparing distances.- Parameters:
vec- the vector to measure squared distance to- Returns:
- the squared distance between the two vectors
- Throws:
IllegalArgumentException- if vec is null
-
abs
Returns a new vector with all components in absolute values.- Returns:
- a new vector with absolute values
-
min
Returns a new vector with the component-wise minimum of this vector and the specified vector.- Parameters:
vec- the vector to compare with- Returns:
- a new vector with the minimum components
- Throws:
IllegalArgumentException- if vec is null
-
max
Returns a new vector with the component-wise maximum of this vector and the specified vector.- Parameters:
vec- the vector to compare with- Returns:
- a new vector with the maximum components
- Throws:
IllegalArgumentException- if vec is null
-
floor
Returns a new vector with all components rounded down to the nearest integer.- Returns:
- a new vector with floored components
-
ceil
Returns a new vector with all components rounded up to the nearest integer.- Returns:
- a new vector with ceiling components
-
round
Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.- Returns:
- a new vector with rounded components
-
lerp
Returns the result of the linear interpolation between this vector and the specified vector by the specified weight. The weight range is 0.0 to 1.0, representing the amount of interpolation. Values outside this range will extrapolate.- Parameters:
vec- the target vector to interpolate towardsweight- the interpolation weight (0.0 = this vector, 1.0 = target vector)- Returns:
- a new interpolated vector
- Throws:
IllegalArgumentException- if vec is null
-
aspect
public float aspect()Returns the aspect ratio of this vector, the ratio of x to y. If y is zero, the result will be Infinity, -Infinity, or NaN.- Returns:
- the aspect ratio (x / y)
-
equals
-
hashCode
public int hashCode() -
toString
-