# Pos2D

`com.jsmacrosce.jsmacros.api.math.Pos2D`

**Since:** 1.2.6

A mutable pair of `double` coordinates. Arithmetic methods return new positions without
changing the receiver. Because [Pos3D](Pos3D.md) extends this class, calling an inherited two-argument
arithmetic method on a `Pos3D` returns a `Pos2D` and drops z; use the three-argument
[Pos3D](Pos3D.md) 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 {#com.jsmacrosce.jsmacros.api.math.Pos2D_constructor-Vec2-}

```js
new Pos2D(Vec2 vec)
```

Copies the coordinates from a vanilla two-component vector.

**Parameters:**
- `vec`: the vector to copy the coordinates out of

**Since:** 2.1.0

### new Pos2D {#com.jsmacrosce.jsmacros.api.math.Pos2D_constructor-Pos2D-}

```js
new Pos2D(Pos2D pos)
```

Copies another position into a separate object.

**Parameters:**
- `pos`: the position to copy

**Since:** 2.1.0

### new Pos2D {#com.jsmacrosce.jsmacros.api.math.Pos2D_constructor-double-double-}

```js
new Pos2D(double x, double y)
```

Creates a position with the given coordinates.

**Parameters:**
- `x`: the x coordinate
- `y`: the y coordinate

**Since:** 1.2.6

## Fields

### x {#com.jsmacrosce.jsmacros.api.math.Pos2D_x}

```js
x: double
```

The mutable x coordinate.

### y {#com.jsmacrosce.jsmacros.api.math.Pos2D_y}

```js
y: double
```

The mutable y coordinate.

### ZERO {#com.jsmacrosce.jsmacros.api.math.Pos2D_ZERO}

```js
static ZERO: Pos2D
```

Mutable origin used by the no-argument vector conversions; changing it affects later conversions.

## Methods

### add {#com.jsmacrosce.jsmacros.api.math.Pos2D_add-Pos2D-}

```js
add(Pos2D pos): Pos2D
```

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

**Since:** 1.2.6

### add {#com.jsmacrosce.jsmacros.api.math.Pos2D_add-double-double-}

```js
add(double x, double y): Pos2D
```

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`

**Since:** 1.6.3

### compareTo {#com.jsmacrosce.jsmacros.api.math.Pos2D_compareTo-Pos2D-}

```js
compareTo(Pos2D o): int
```

Compares x first, then y using [Double.compare(double, double)](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html). When a `Pos3D`
is typed as `Pos2D`, this overload compares only x and y; use the
[Pos3D.compareTo(Pos3D)](Pos3D.md#com.jsmacrosce.jsmacros.api.math.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`

**Since:** 1.2.6

### divide {#com.jsmacrosce.jsmacros.api.math.Pos2D_divide-Pos2D-}

```js
divide(Pos2D pos): Pos2D
```

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`.

**Since:** 1.8.4

### divide {#com.jsmacrosce.jsmacros.api.math.Pos2D_divide-double-double-}

```js
divide(double x, double y): Pos2D
```

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`.

**Since:** 1.8.4

### equals {#com.jsmacrosce.jsmacros.api.math.Pos2D_equals-Object-}

```js
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)](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html)
treats NaNs as equal and distinguishes positive and negative zero. Thus a `Pos2D` does not equal a [Pos3D](Pos3D.md).

**Parameters:**
- `o`: the object to compare

**Returns:** `true` if the runtime classes and both coordinates match; `false` otherwise

### getX {#com.jsmacrosce.jsmacros.api.math.Pos2D_getX-}

```js
getX(): double
```

Returns the current x coordinate.

**Returns:** the current x coordinate

**Since:** 1.2.6

### getY {#com.jsmacrosce.jsmacros.api.math.Pos2D_getY-}

```js
getY(): double
```

Returns the current y coordinate.

**Returns:** the current y coordinate

**Since:** 1.2.6

### hashCode {#com.jsmacrosce.jsmacros.api.math.Pos2D_hashCode-}

```js
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 {#com.jsmacrosce.jsmacros.api.math.Pos2D_multiply-Pos2D-}

```js
multiply(Pos2D pos): Pos2D
```

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`

**Since:** 1.2.6

### multiply {#com.jsmacrosce.jsmacros.api.math.Pos2D_multiply-double-double-}

```js
multiply(double x, double y): Pos2D
```

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)](#com.jsmacrosce.jsmacros.api.math.Pos2D_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`

**Since:** 1.6.3

### scale {#com.jsmacrosce.jsmacros.api.math.Pos2D_scale-double-}

```js
scale(double scale): Pos2D
```

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`

**Since:** 1.6.3

### sub {#com.jsmacrosce.jsmacros.api.math.Pos2D_sub-Pos2D-}

```js
sub(Pos2D pos): Pos2D
```

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`.

**Since:** 1.8.4

### sub {#com.jsmacrosce.jsmacros.api.math.Pos2D_sub-double-double-}

```js
sub(double x, double y): Pos2D
```

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`.

**Since:** 1.8.4

### to3D {#com.jsmacrosce.jsmacros.api.math.Pos2D_to3D-}

```js
to3D(): Pos3D
```

Returns a new [Pos3D](Pos3D.md) with the same x and y coordinates and z set to `0`.

**Returns:** a new [Pos3D](Pos3D.md) at `x, y, 0`

**Since:** 1.2.6

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos2D_toReverseVector-}

```js
toReverseVector(): Vec2D
```

Returns a vector from this position to the origin, opposite to [toVector()](#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-).

**Returns:** a new [Vec2D](Vec2D.md) from this position to `0, 0`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos2D_toReverseVector-Pos2D-}

```js
toReverseVector(Pos2D end_pos): Vec2D
```

Returns a vector from this position to `end_pos`; this is the opposite direction of
[toVector(Pos2D)](#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-Pos2D-) with the same argument.

**Parameters:**
- `end_pos`: the position the vector ends at

**Returns:** a new [Vec2D](Vec2D.md) from this position to `end_pos`

**Since:** 1.6.4

### toReverseVector {#com.jsmacrosce.jsmacros.api.math.Pos2D_toReverseVector-double-double-}

```js
toReverseVector(double end_x, double end_y): Vec2D
```

Returns a vector from this position to the given coordinates, opposite to
[toVector(double, double)](#com.jsmacrosce.jsmacros.api.math.Pos2D_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](Vec2D.md) from this position to `end_x, end_y`

**Since:** 1.6.4

### toString {#com.jsmacrosce.jsmacros.api.math.Pos2D_toString-}

```js
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 {#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-}

```js
toVector(): Vec2D
```

Returns a new vector from `(0, 0)` to this position. Use [toReverseVector()](#com.jsmacrosce.jsmacros.api.math.Pos2D_toReverseVector-) for the opposite direction.

**Returns:** a new [Vec2D](Vec2D.md) from `0, 0` to this position

**Since:** 1.2.6

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-Pos2D-}

```js
toVector(Pos2D start_pos): Vec2D
```

Returns a vector from `start_pos` to this position.

**Parameters:**
- `start_pos`: the position the vector starts at

**Returns:** a new [Vec2D](Vec2D.md) from `start_pos` to this position

**Since:** 1.6.4

### toVector {#com.jsmacrosce.jsmacros.api.math.Pos2D_toVector-double-double-}

```js
toVector(double start_x, double start_y): Vec2D
```

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](Vec2D.md) from `start_x, start_y` to this position

**Since:** 1.6.4
