Pos2D
com.jsmacrosce.jsmacros.api.math.Pos2D
A mutable pair of double coordinates. Arithmetic methods return new positions without changing the receiver. Because Pos3D extends this class, calling an inherited two-argument arithmetic method on a Pos3D returns a Pos2D and drops z; use the three-argument Pos3D methods to retain all coordinates.
js
const start = PositionCommon.createPos(0, 0);
const end = PositionCommon.createPos(3, 4);
// Arithmetic returns a new position; start remains unchanged.
const offset = end.sub(start); // 3, 4
const doubled = end.scale(2); // 6, 8
const length = end.toVector(start).getMagnitude(); // 5
Chat.log(`offset ${offset}, doubled ${doubled}, length ${length}`);Constructors
new Pos2D
Copies the coordinates from a vanilla two-component vector.
Parameters:
vec: the vector to copy the coordinates out of
Copies another position into a separate object.
Parameters:
pos: the position to copy
Creates a position with the given coordinates.
Parameters:
x: the x coordinatey: the y coordinate
Fields
x
x: double#The mutable x coordinate.
y
y: double#The mutable y coordinate.
ZERO
Methods
add
Returns a new position with the corresponding coordinates added; this position is unchanged.
Parameters:
pos: the position to add to this one
Returns: the coordinate-wise sum
compareTo
Compares x first, then y using Double.compare(double, double). When a
Pos3D is typed as Pos2D, this overload compares only x and y; use the Pos3D.compareTo(Pos3D) overload with Pos3D arguments to include z.Parameters:
o: the position to compare this one against
Returns: a negative number, zero, or a positive number as this position is before, the same as, or after
odivide
Returns a new position with each coordinate divided by the corresponding coordinate of
pos; this position is unchanged. Division by zero follows Java floating-point rules.Parameters:
pos: the position to divide by, coordinate for coordinate
Returns: a new position at
x / pos.x, y / pos.y.Returns a new position with its coordinates divided by the respective divisors; this position is unchanged. Division by zero follows Java floating-point rules.
Parameters:
x: the x coordinate to divide byy: the y coordinate to divide by
Returns: a new position at
this.x / x, this.y / y.equals
Returns whether
o is a Pos2D of the exact same runtime class with matching coordinates. null and instances of other classes return false; Double.compare(double, double) treats NaNs as equal and distinguishes positive and negative zero. Thus a Pos2D does not equal a Pos3D.Parameters:
o: the object to compare
Returns:
true if the runtime classes and both coordinates match; false otherwisegetX
Returns the current x coordinate.
Returns: the current x coordinate
getY
Returns the current y coordinate.
Returns: the current y coordinate
hashCode
hashCode(): int#Computes a hash code from the current x and y coordinates. Because coordinates are mutable, the result can change when they do; avoid using a mutable position as a hash key.
Returns: the hash code for the current coordinates
multiply
Returns a new position with each coordinate multiplied by the corresponding coordinate of
pos; this position is unchanged.Parameters:
pos: the position to multiply by, coordinate for coordinate
Returns: a new position at
x * pos.x, y * pos.yReturns a new position with its x and y coordinates multiplied by the respective factors; this position is unchanged. For one factor on both coordinates, use scale(double).
Parameters:
x: the factor for the x coordinatey: the factor for the y coordinate
Returns: a new position at
this.x * x, this.y * yscale
sub
Returns a new position with the corresponding coordinates of
pos subtracted; this position is unchanged.Parameters:
pos: the position to subtract
Returns: a new position at
x - pos.x, y - pos.y.to3D
toReverseVector
Returns a vector from this position to the origin, opposite to toVector().
Returns: a new Vec2D from this position to
0, 0Returns a vector from this position to
end_pos; this is the opposite direction of toVector(Pos2D) with the same argument.Parameters:
end_pos: the position the vector ends at
Returns: a new Vec2D from this position to
end_posReturns a vector from this position to the given coordinates, opposite to toVector(double, double) with the same coordinates.
Parameters:
end_x: the x coordinate the vector ends atend_y: the y coordinate the vector ends at
Returns: a new Vec2D from this position to
end_x, end_ytoString
toVector
Returns a new vector from
(0, 0) to this position. Use toReverseVector() for the opposite direction.Returns: a new Vec2D from
0, 0 to this positionReturns a vector from
start_pos to this position.Parameters:
start_pos: the position the vector starts at
Returns: a new Vec2D from
start_pos to this position