Skip to content

Pos2D ​

com.jsmacrosce.jsmacros.api.math.Pos2D

Since1.2.6

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 ​

new Pos2D(Vec2 vec)Since1.2.6#
Copies the coordinates from a vanilla two-component vector.

Parameters:

  • vec: the vector to copy the coordinates out of
new Pos2D(Pos2D pos)Since1.2.6#
Copies another position into a separate object.

Parameters:

  • pos: the position to copy
new Pos2D(double x, double y)Since1.2.6#
Creates a position with the given coordinates.

Parameters:

  • x: the x coordinate
  • y: the y coordinate

Fields ​

x ​

x: double#
The mutable x coordinate.

y ​

y: double#
The mutable y coordinate.

ZERO ​

static ZERO: Pos2Dread-only#
Mutable origin used by the no-argument vector conversions; changing it affects later conversions.

Methods ​

add ​

add(Pos2D pos): Pos2DSince1.2.6#
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
add(double x, double y): Pos2DSince1.6.3#
Returns a new position with the given amounts added to its coordinates; this position is unchanged.

Parameters:

  • x: the amount to add to the x coordinate
  • y: the amount to add to the y coordinate
Returns: a new position at this.x + x, this.y + y

compareTo ​

compareTo(Pos2D o): intSince1.2.6#
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 o

divide ​

divide(Pos2D pos): Pos2DSince1.8.4#
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.
divide(double x, double y): Pos2DSince1.8.4#
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 by
  • y: the y coordinate to divide by
Returns: a new position at this.x / x, this.y / y.

equals ​

equals(Object o): boolean#
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 otherwise

getX ​

getX(): doubleSince1.2.6#
Returns the current x coordinate.
Returns: the current x coordinate

getY ​

getY(): doubleSince1.2.6#
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 ​

multiply(Pos2D pos): Pos2DSince1.2.6#
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.y
multiply(double x, double y): Pos2DSince1.6.3#
Returns 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 coordinate
  • y: the factor for the y coordinate
Returns: a new position at this.x * x, this.y * y

scale ​

scale(double scale): Pos2DSince1.6.3#
Returns a new position with both coordinates multiplied by scale; this position is unchanged.

Parameters:

  • scale: the factor to multiply both coordinates by
Returns: a new position at this.x * scale, this.y * scale

sub ​

sub(Pos2D pos): Pos2DSince1.8.4#
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.
sub(double x, double y): Pos2DSince1.8.4#
Returns a new position with the given amounts subtracted from its coordinates; this position is unchanged.

Parameters:

  • x: the x coordinate to subtract
  • y: the y coordinate to subtract
Returns: a new position at this.x - x, this.y - y.

to3D ​

to3D(): Pos3DSince1.2.6#
Returns a new Pos3D with the same x and y coordinates and z set to 0.
Returns: a new Pos3D at x, y, 0

toReverseVector ​

toReverseVector(): Vec2DSince1.6.4#
Returns a vector from this position to the origin, opposite to toVector().
Returns: a new Vec2D from this position to 0, 0
toReverseVector(Pos2D end_pos): Vec2DSince1.6.4#
Returns 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_pos
toReverseVector(double end_x, double end_y): Vec2DSince1.6.4#
Returns 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 at
  • end_y: the y coordinate the vector ends at
Returns: a new Vec2D from this position to end_x, end_y

toString ​

toString(): String#
Formats the coordinates as x, y; finite coordinates have six fractional digits. Formatting uses the default format locale, so the decimal separator may vary.
Returns: the formatted coordinates

toVector ​

toVector(): Vec2DSince1.2.6#
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 position
toVector(Pos2D start_pos): Vec2DSince1.6.4#
Returns 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
toVector(double start_x, double start_y): Vec2DSince1.6.4#
Returns a vector from the given coordinates to this position.

Parameters:

  • start_x: the x coordinate the vector starts at
  • start_y: the y coordinate the vector starts at
Returns: a new Vec2D from start_x, start_y to this position