Class Vec2f


  • public final class Vec2f
    extends Object
    2D Vector based upon two float components. Implementation borrowed from [gfxbox2](https://jausoft.com/cgit/cs_class/gfxbox2.git/tree/include/pixel/pixel2f.hpp#n29) and its data layout from JOAL's Vec3f.
    • Constructor Detail

      • Vec2f

        public Vec2f()
      • Vec2f

        public Vec2f​(Vec2f o)
      • Vec2f

        public Vec2f​(Vec3f o)
        Creating new Vec2f using Vec3f, dropping z.
      • Vec2f

        public Vec2f​(float[] xy)
      • Vec2f

        public Vec2f​(float x,
                     float y)
    • Method Detail

      • from_length_angle

        public static Vec2f from_length_angle​(float magnitude,
                                              float radians)
      • copy

        public Vec2f copy()
      • set

        public void set​(Vec2f o)
        this = o, returns this.
      • set

        public void set​(Vec3f o)
        this = o while dropping z, returns this.
      • set

        public void set​(float x,
                        float y)
        this = { x, y }, returns this.
      • set

        public Vec2f set​(float[] xy)
        this = xy, returns this.
      • set

        public void set​(int i,
                        float val)
        Sets the ith component, 0 <= i < 2
      • get

        public float[] get​(float[] xy)
        xy = this, returns xy.
      • get

        public float get​(int i)
        Gets the ith component, 0 <= i < 2
      • x

        public float x()
      • y

        public float y()
      • setX

        public void setX​(float x)
      • setY

        public void setY​(float y)
      • max

        public Vec2f max​(Vec2f m)
        this = max(this, m), returns this.
      • min

        public Vec2f min​(Vec2f m)
        this = min(this, m), returns this.
      • mul

        public Vec2f mul​(float val)
        Returns this * val; creates new vector
      • mul

        public Vec2f mul​(Vec2f a,
                         Vec2f b)
        this = a * b, returns this.
      • scale

        public Vec2f scale​(float s)
        this = this * s, returns this.
      • scale

        public Vec2f scale​(float sx,
                           float sy)
        this = this * { sx, sy }, returns this.
      • plus

        public Vec2f plus​(Vec2f arg)
        Returns this + arg; creates new vector
      • plus

        public Vec2f plus​(Vec2f a,
                          Vec2f b)
        this = a + b, returns this.
      • add

        public Vec2f add​(float dx,
                         float dy)
        this = this + { dx, dy }, returns this.
      • add

        public Vec2f add​(Vec2f b)
        this = this + b, returns this.
      • minus

        public Vec2f minus​(Vec2f arg)
        Returns this - arg; creates new vector
      • minus

        public Vec2f minus​(Vec2f a,
                           Vec2f b)
        this = a - b, returns this.
      • sub

        public Vec2f sub​(Vec2f b)
        this = this - b, returns this.
      • isZero

        public boolean isZero()
        Return true if all components are zero, i.e. it's absolute value < #EPSILON.
      • rotate

        public void rotate​(float radians,
                           Vec2f ctr)
      • rotate

        public void rotate​(float sin,
                           float cos,
                           Vec2f ctr)
      • length

        public float length()
        Return the length of this vector, a.k.a the norm or magnitude
      • lengthSq

        public float lengthSq()
        Return the squared length of this vector, a.k.a the squared norm or squared magnitude
      • angle

        public float angle()
        Return the direction angle of this vector in radians
      • normalize

        public Vec2f normalize()
        Normalize this vector in place
      • distSq

        public float distSq​(Vec2f o)
        Return the squared distance between this vector and the given one.

        When comparing the relative distance between two points it is usually sufficient to compare the squared distances, thus avoiding an expensive square root operation.

      • dist

        public float dist​(Vec2f o)
        Return the distance between this vector and the given one.
      • dot

        public float dot​(Vec2f arg)
        Return the dot product of this vector and the given one
        Returns:
        the dot product as float
      • cross

        public float cross​(Vec2f o)
        Returns cross product of this vectors and the given one, i.e. *this x o. The 2D cross product is identical with the 2D perp dot product.
        Returns:
        the resulting scalar
      • cosAngle

        public float cosAngle​(Vec2f o)
        Return the cosines of the angle between two vectors
      • angle

        public float angle​(Vec2f o)
        Return the angle between two vectors in radians
      • normal_ccw

        public Vec2f normal_ccw()
        Return the counter-clock-wise (CCW) normal of this vector, i.e. perp(endicular) vector
      • isEqual

        public boolean isEqual​(Vec2f o,
                               float epsilon)
        Equals check using a given FloatUtil.EPSILON value and FloatUtil.isEqual(float, float, float).

        Implementation considers following corner cases:

        • NaN == NaN
        • +Inf == +Inf
        • -Inf == -Inf
        Parameters:
        o - comparison value
        epsilon - consider using FloatUtil.EPSILON
        Returns:
        true if all components differ less than epsilon, otherwise false.