Class SFSVector2

java.lang.Object
com.smartfoxserver.entities.data.SFSVector2
All Implemented Interfaces:
Serializable

public class SFSVector2 extends Object implements Serializable
A class representing a Vector in 2D space
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
     
    float
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    SFSVector2(float x, float y)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abs()
    Returns a new vector with all components in absolute values.
    float
    Returns the angle of this vector in radians, measured from the positive x-axis.
    float
    Returns the aspect ratio of this vector, the ratio of x to y.
    Returns a new vector with all components rounded up to the nearest integer.
    float
    Returns the 2D cross product (also known as the perpendicular dot product or wedge product) of this vector and the specified vector.
    float
    Returns the squared distance between this vector and the specified vector.
    float
    Returns the Euclidean distance between this vector and the specified vector.
    Returns a new vector that is the component-wise division of this vector by the specified vector.
    float
    Returns the dot product of this vector and the specified vector.
    static SFSVector2
     
    boolean
    equals(Object other)
     
    Returns a new vector with all components rounded down to the nearest integer.
    int
     
    boolean
    Returns true if the vector is normalized, i.e., its length is approximately equal to 1.
    float
    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.
    Returns a new vector with the component-wise maximum of this vector and the specified vector.
    Returns a new vector with the component-wise minimum of this vector and the specified vector.
    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.
    Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
    Returns a new vector that is the result of subtracting the specified vector from this vector.
    Returns a new vector that is the sum of this vector and the specified vector.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      public float x
    • y

      public float y
  • Constructor Details

    • SFSVector2

      public SFSVector2(float x, float y)
    • SFSVector2

      public SFSVector2(SFSVector2 vec2)
  • Method Details

    • empty

      public static SFSVector2 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

      public SFSVector2 sum(SFSVector2 vec)
      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

      public SFSVector2 sub(SFSVector2 vec)
      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

      public SFSVector2 mult(SFSVector2 vec)
      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

      public SFSVector2 divide(SFSVector2 vec)
      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

      public SFSVector2 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

      public SFSVector2 rotate(float angle)
      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

      public float dot(SFSVector2 vec)
      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

      public float cross(SFSVector2 vec)
      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

      public float distanceTo(SFSVector2 vec)
      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

      public float distanceSquaredTo(SFSVector2 vec)
      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

      public SFSVector2 abs()
      Returns a new vector with all components in absolute values.
      Returns:
      a new vector with absolute values
    • min

      public SFSVector2 min(SFSVector2 vec)
      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

      public SFSVector2 max(SFSVector2 vec)
      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

      public SFSVector2 floor()
      Returns a new vector with all components rounded down to the nearest integer.
      Returns:
      a new vector with floored components
    • ceil

      public SFSVector2 ceil()
      Returns a new vector with all components rounded up to the nearest integer.
      Returns:
      a new vector with ceiling components
    • round

      public SFSVector2 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

      public SFSVector2 lerp(SFSVector2 vec, float weight)
      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 towards
      weight - 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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object