Vector2D

public final class Vector2D

Represents a 2-dimensional vector in plane

This class can be used to create a vector for any entity with x and y as x and y points in canvas plane. The canvas plane follows cartesian coordinate system but with origin at top left corner and positive x-axis along right direction and positive y-axis along downward direction.

For ex: Here representation of position vector in canvas plane could be given as - val position = Vector2D(25f, 25f)

Constructors

Vector2D
Link copied to clipboard
Vector2D Vector2D(Float x, Float y)

Types

Companion
Link copied to clipboard
public class Companion

Functions

equals
Link copied to clipboard
Boolean equals(Object other)

Equals method to check the equality between two vectors

getX
Link copied to clipboard
final Float getX()
getY
Link copied to clipboard
final Float getY()
hashCode
Link copied to clipboard
Integer hashCode()
setX
Link copied to clipboard
final Unit setX(Float x)
setY
Link copied to clipboard
final Unit setY(Float y)

Properties

x
Link copied to clipboard
private Float x
y
Link copied to clipboard
private Float y

Extensions

add
Link copied to clipboard
final Vector2D add(Vector2D other)
addWithScalarMultiply
Link copied to clipboard
final Vector2D addWithScalarMultiply(Vector2D other, Float scalar)

To add a vector to this vector and multiplies the other vector with scalar

angleBetween
Link copied to clipboard
final Float angleBetween(Vector2D other)

Returns angle between this vector and other vector

dec
Link copied to clipboard
final Vector2D dec(Float factor)

Decrements this vector by factor

distance
Link copied to clipboard
final Float distance(Vector2D other)

Calculates Euclidean distance between two vectors

div
Link copied to clipboard
final Vector2D div(Float x, Float y)

To divide this vector by x and y values

divAssign
Link copied to clipboard
final Unit divAssign(Float factor)
divide
Link copied to clipboard
final Vector2D divide(Float factor)

To divide this vector by single factor

dot
Link copied to clipboard
final Float dot(Vector2D other)

Calculates the dot product of two vectors

heading
Link copied to clipboard
final Float heading()

Calculate the angle of rotation for this vector(only 2D vectors).

inc
Link copied to clipboard
final Vector2D inc(Float factor)

Increments this vector by factor

lerp
Link copied to clipboard
final Vector2D lerp(Float x, Float y, Float amt)

Linear interpolate the vector to another vector

limit
Link copied to clipboard
final Vector2D limit(Float max)

Limit the magnitude of this vector to the value used for the max parameter. val v = Vector2D(10, 20); // v has components 10.0, 20.0 v.limit(5); // v's components are set to // 2.2271771, 4.4543543

mag
Link copied to clipboard
final Float mag()

Calculates the magnitude (length) of the vector and returns the result asa float

magSq
Link copied to clipboard
final Float magSq()

Calculates the squared magnitude of this vector and returns the result as a float

minusAssign
Link copied to clipboard
final Unit minusAssign(Vector2D other)
multiply
Link copied to clipboard
final Vector2D multiply(Float x, Float y)

To multiply this vector with x factor and y factor

final Vector2D multiply(Float factor)

To multiply this vector with single factor See also scalarMultiply

normalize
Link copied to clipboard
final Vector2D normalize()

Normalizes the vector to length 1 - making unit vector

plusAssign
Link copied to clipboard
final Unit plusAssign(Vector2D other)
reflect
Link copied to clipboard
final Vector2D reflect(Vector2D surfaceNormal)

Reflect the incoming vector about a normal to a line in 2D, or about a normal to a plane in 3D This method acts on the vector directly

rotate
Link copied to clipboard
final Vector2D rotate(Float angle)

Rotates this vector by given angle

scalarMultiply
Link copied to clipboard
final Vector2D scalarMultiply(Float scalar)
set
Link copied to clipboard
final Vector2D set(Vector2D other)

Sets this vector as other vector

setHeading
Link copied to clipboard
final Vector2D setHeading(Float angle)

Rotate the vector to a specific angle, magnitude remains the same

setMag
Link copied to clipboard
final Vector2D setMag(Float n)

Set the magnitude of this vector to the value used for the n parameter.

sub
Link copied to clipboard
final Vector2D sub(Vector2D other)

To subtract a vector from this vector

timesAssign
Link copied to clipboard
final Unit timesAssign(Float factor)
toOffSet
Link copied to clipboard
final Offset toOffSet()

Converts a this into Offset value