# Vec2D

`com.jsmacrosce.jsmacros.api.math.Vec2D`

**Since:** 1.2.6

A 2D line segment with mutable start and end coordinates. Measurements use the displacement
 from start to end; arithmetic methods return new segments. [normalize()](#com.jsmacrosce.jsmacros.api.math.Vec2D_normalize-) divides both
 endpoints by the segment length, so the start moves unless it is at the origin.  
 ```js
const a = PositionCommon.createPos(0, 0);  
const b = PositionCommon.createPos(3, 4);  
const segment = b.toVector(a);  
// 3, 4 for the segment across the plane and 5 for its length
Chat.log(`delta ${segment.getDeltaX()}, ${segment.getDeltaY()}, length ${segment.getMagnitude()}`);  
// The displacement has unit length; both endpoints are divided by the length.  
const direction = segment.normalize();  
Chat.log(`normalized to ${direction}`);  
 ```

## Constructors

### new Vec2D {#com.jsmacrosce.jsmacros.api.math.Vec2D_constructor-double-double-double-double-}

```js
new Vec2D(double x1, double y1, double x2, double y2)
```

Constructs a segment from its start and end coordinates.

**Parameters:**
- `x1`: the x coordinate of the start
- `y1`: the y coordinate of the start
- `x2`: the x coordinate of the end
- `y2`: the y coordinate of the end

**Since:** 1.2.6

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

```js
new Vec2D(Pos2D start, Pos2D end)
```

Constructs a segment from two positions, copying their coordinates.

**Parameters:**
- `start`: the position the segment starts at
- `end`: the position the segment ends at

**Since:** 1.2.6

### new Vec2D {#com.jsmacrosce.jsmacros.api.math.Vec2D_constructor-Vec2D-}

```js
new Vec2D(Vec2D vec)
```

Copies the endpoint coordinates of another segment.

**Parameters:**
- `vec`: the vector to copy

**Since:** 2.1.0

## Fields

### x1 {#com.jsmacrosce.jsmacros.api.math.Vec2D_x1}

```js
x1: double
```

The x coordinate of the segment's start.

### x2 {#com.jsmacrosce.jsmacros.api.math.Vec2D_x2}

```js
x2: double
```

The x coordinate of the segment's end.

### y1 {#com.jsmacrosce.jsmacros.api.math.Vec2D_y1}

```js
y1: double
```

The y coordinate of the segment's start.

### y2 {#com.jsmacrosce.jsmacros.api.math.Vec2D_y2}

```js
y2: double
```

The y coordinate of the segment's end.

## Methods

### add {#com.jsmacrosce.jsmacros.api.math.Vec2D_add-Vec2D-}

```js
add(Vec2D vec): Vec2D
```

Returns a new segment with each endpoint coordinate increased by the corresponding coordinate
 of `vec`; this segment is unchanged.

**Parameters:**
- `vec`: the segment whose endpoints are added to this one's

**Returns:** a new segment with `x1 + vec.x1, y1 + vec.y1, x2 + vec.x2, y2 + vec.y2`

**Since:** 1.2.6

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

```js
add(double x1, double y1, double x2, double y2): Vec2D
```

Returns a new segment with its start and end offset independently by the corresponding amounts;  
 this segment is unchanged.

**Parameters:**
- `x1`: the amount to add to the start's x coordinate
- `y1`: the amount to add to the start's y coordinate
- `x2`: the amount to add to the end's x coordinate
- `y2`: the amount to add to the end's y coordinate

**Returns:** a new segment offset endpoint for endpoint

**Since:** 1.6.3

### compareTo {#com.jsmacrosce.jsmacros.api.math.Vec2D_compareTo-Vec2D-}

```js
compareTo(Vec2D other): int
```

Compares segments by start position, then by end position when their starts compare equal;  
 each position is ordered by [Pos2D.compareTo(Pos2D)](Pos2D.md#com.jsmacrosce.jsmacros.api.math.Pos2D_compareTo-Pos2D-) (x, then y).

**Parameters:**
- `other`: the segment to compare this one against

**Returns:** a negative number, zero, or a positive number as this segment is before, the same
         as, or after `other`

**Since:** 1.2.6

### dotProduct {#com.jsmacrosce.jsmacros.api.math.Vec2D_dotProduct-Vec2D-}

```js
dotProduct(Vec2D vec): double
```

Returns the dot product of this segment's displacement and `vec`'s displacement. The
 result is not normalized, so its magnitude depends on both segment lengths. For a
 [Vec3D](Vec3D.md), this `Vec2D` overload ignores z; its `dotProduct(Vec3D)` overload
 includes it.

**Parameters:**
- `vec`: the segment to compare this one's direction against

**Returns:** the dot product of the endpoint displacements

**Since:** 1.2.6

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

```js
equals(Object o): boolean
```

Compares this segment with another object by exact runtime class and ordered endpoint
 coordinates. Coordinates use [Double.compare(double, double)](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Double.html): NaN values compare
 equal, while positive and negative zero compare differently.

**Parameters:**
- `o`: the object to compare with this segment

**Returns:** `true` if `o` is this object or has the same runtime class and equal
         coordinates; `false` otherwise

### getDeltaX {#com.jsmacrosce.jsmacros.api.math.Vec2D_getDeltaX-}

```js
getDeltaX(): double
```

Returns the x displacement from the segment's start to its end (`x2 - x1`).

**Returns:** the x distance covered by this segment

**Since:** 1.2.6

### getDeltaY {#com.jsmacrosce.jsmacros.api.math.Vec2D_getDeltaY-}

```js
getDeltaY(): double
```

Returns the y displacement from the segment's start to its end (`y2 - y1`).

**Returns:** the y distance covered by this segment

**Since:** 1.2.6

### getEnd {#com.jsmacrosce.jsmacros.api.math.Vec2D_getEnd-}

```js
getEnd(): Pos2D
```

Returns a copy of the segment's end position; modifying it does not change this segment.

**Returns:** a new [Pos2D](Pos2D.md) at the end of this segment

**Since:** 1.2.6

### getMagnitude {#com.jsmacrosce.jsmacros.api.math.Vec2D_getMagnitude-}

```js
getMagnitude(): double
```

Returns the Euclidean length of the segment, `sqrt(dx * dx + dy * dy)` where `dx`
 and `dy` are the endpoint displacements.

**Returns:** the length of this segment

**Since:** 1.2.6

### getMagnitudeSq {#com.jsmacrosce.jsmacros.api.math.Vec2D_getMagnitudeSq-}

```js
getMagnitudeSq(): double
```

Returns the squared Euclidean length of the segment. [Vec3D](Vec3D.md) overrides this to include
 the z displacement.

**Returns:** the squared length of this segment

**Since:** 1.6.5

### getStart {#com.jsmacrosce.jsmacros.api.math.Vec2D_getStart-}

```js
getStart(): Pos2D
```

Returns a copy of the segment's start position; modifying it does not change this segment.

**Returns:** a new [Pos2D](Pos2D.md) at the start of this segment

**Since:** 1.2.6

### getX1 {#com.jsmacrosce.jsmacros.api.math.Vec2D_getX1-}

```js
getX1(): double
```

Returns the x coordinate of the segment's start.

**Returns:** the x coordinate of the start

**Since:** 1.2.6

### getX2 {#com.jsmacrosce.jsmacros.api.math.Vec2D_getX2-}

```js
getX2(): double
```

Returns the x coordinate of the segment's end.

**Returns:** the x coordinate of the end

**Since:** 1.2.6

### getY1 {#com.jsmacrosce.jsmacros.api.math.Vec2D_getY1-}

```js
getY1(): double
```

Returns the y coordinate of the segment's start.

**Returns:** the y coordinate of the start

**Since:** 1.2.6

### getY2 {#com.jsmacrosce.jsmacros.api.math.Vec2D_getY2-}

```js
getY2(): double
```

Returns the y coordinate of the segment's end.

**Returns:** the y coordinate of the end

**Since:** 1.2.6

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

```js
hashCode(): int
```

Computes a hash from the current ordered endpoint coordinates. Because this segment is
 mutable, its hash can change when its coordinates change; avoid using it as a hash key while
 mutating it.

**Returns:** the hash code for the current coordinates

### multiply {#com.jsmacrosce.jsmacros.api.math.Vec2D_multiply-Vec2D-}

```js
multiply(Vec2D vec): Vec2D
```

Returns a new segment with each endpoint coordinate multiplied by the corresponding
 coordinate of `vec`; this is componentwise multiplication, not a uniform scale.

**Parameters:**
- `vec`: supplies the four coordinate factors

**Returns:** a new segment with `x1 * vec.x1, y1 * vec.y1, x2 * vec.x2, y2 * vec.y2`

**Since:** 1.2.6

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

```js
multiply(double x1, double y1, double x2, double y2): Vec2D
```

Returns a new segment with each endpoint coordinate multiplied by its corresponding factor;  
 the four coordinates can be scaled independently.

**Parameters:**
- `x1`: the factor for the start's x coordinate
- `y1`: the factor for the start's y coordinate
- `x2`: the factor for the end's x coordinate
- `y2`: the factor for the end's y coordinate

**Returns:** a new segment with each endpoint coordinate multiplied by its factor

**Since:** 1.6.3

### normalize {#com.jsmacrosce.jsmacros.api.math.Vec2D_normalize-}

```js
normalize(): Vec2D
```

Returns a new segment with every endpoint coordinate divided by this segment's length. The
 displacement has length 1, but the start is also divided and therefore moves unless it is at
 the origin. If the segment has zero length, the resulting coordinates are `NaN`.  
 [Vec3D](Vec3D.md) overrides this to include z coordinates.

**Returns:** a new segment with the same direction but a length of 1

**Since:** 1.6.5

### reverse {#com.jsmacrosce.jsmacros.api.math.Vec2D_reverse-}

```js
reverse(): Vec2D
```

Returns a new segment with this segment's endpoints swapped; this segment is unchanged.

**Returns:** a new segment from the end of this one to its start

**Since:** 1.2.6

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

```js
scale(double scale): Vec2D
```

Returns a new segment with all four endpoint coordinates multiplied by `scale`; this
 scales the endpoints about the origin and does not preserve the start position.

**Parameters:**
- `scale`: the factor to multiply all four endpoint coordinates by

**Returns:** a new segment with all four endpoint coordinates scaled

**Since:** 1.6.3

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

```js
to3D(): Vec3D
```

Returns a [Vec3D](Vec3D.md) through the same x-y endpoints with both z coordinates set to
 `0`. [Vec3D](Vec3D.md) overrides this to preserve its z coordinates.

**Returns:** a new [Vec3D](Vec3D.md) through the same two points, flat on the z `0` plane

**Since:** 1.2.6

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

```js
toString(): String
```

Formats the start and end coordinates in x, y order. Finite coordinates have six fractional
 digits; the decimal separator follows the default formatting locale. This is display text,  
 not a stable serialization.

**Returns:** the formatted coordinates
