# Vec3D

`com.jsmacrosce.jsmacros.api.math.Vec3D`

**Since:** 1.2.6

A 3D line segment with mutable endpoints. Length and angle measurements include z.  
 Inherited [Vec2D](Vec2D.md) overloads still operate in 2D: `add(Vec2D)` and
 `multiply(Vec2D)` return a plain `Vec2D`, while `dotProduct(Vec2D)` and
 `compareTo(Vec2D)` ignore z.  
 The 3D arithmetic overloads return new segments; [normalize()](#com.jsmacrosce.jsmacros.api.math.Vec3D_normalize-) divides both endpoints by
 the magnitude, moving the start unless it is at the origin.  
 ```js
// Player.getPlayer() is null outside a world, so there is nothing to aim there
const player = Player.getPlayer();  
if (player !== null) {
  // a unit length segment from the origin along the direction the player looks in
  const look = PositionCommon.createLookingVector(player.getYaw(), player.getPitch());  
  // the same angles again, read back off the segment itself
  Chat.log(`looking yaw ${look.getYaw()}, pitch ${look.getPitch()}`);  
  // push the end of it five blocks out to get a point in the world
  const far = look.getEnd().scale(5);  
  Chat.log(`which lands on ${far}`);  
}
 ```

## Constructors

### new Vec3D {#com.jsmacrosce.jsmacros.api.math.Vec3D_constructor-double-double-double-double-double-double-}

```js
new Vec3D(double x1, double y1, double z1, double x2, double y2, double z2)
```

makes a segment from the coordinates of its two ends.

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

**Since:** 1.2.6

### new Vec3D {#com.jsmacrosce.jsmacros.api.math.Vec3D_constructor-Pos3D-Pos3D-}

```js
new Vec3D(Pos3D start, Pos3D end)
```

makes a segment running from one position to another.

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

**Since:** 1.2.6

### new Vec3D {#com.jsmacrosce.jsmacros.api.math.Vec3D_constructor-Vec3D-}

```js
new Vec3D(Vec3D vec)
```

copies another vector. The result is a separate object, so writing to it afterwards does not
 touch `vec`.

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

**Since:** 2.1.0

## Fields

### z1 {#com.jsmacrosce.jsmacros.api.math.Vec3D_z1}

```js
z1: double
```

The z coordinate of the segment's start.

### z2 {#com.jsmacrosce.jsmacros.api.math.Vec3D_z2}

```js
z2: double
```

The z coordinate of the segment's end.

## Methods

### add {#com.jsmacrosce.jsmacros.api.math.Vec3D_add-Vec3D-}

```js
add(Vec3D vec): Vec3D
```

Returns a new segment with each endpoint coordinate added to the corresponding coordinate of
 `vec`; this segment is unchanged. A [Vec2D](Vec2D.md) argument instead selects the inherited
 2D overload, which returns a `Vec2D` and ignores z.

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

**Returns:** a new segment with every endpoint coordinate added to its matching one

**Since:** 1.2.6

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

```js
add(double x1, double y1, double z1, double x2, double y2, double z2): Vec3D
```

Returns a new segment with its start offset by the first three amounts and its end by the last
 three. Equal offsets translate the segment; 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
- `z1`: the amount to add to the start's z coordinate
- `x2`: the amount to add to the end's x coordinate
- `y2`: the amount to add to the end's y coordinate
- `z2`: the amount to add to the end's z coordinate

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

**Since:** 1.6.3

### add {#com.jsmacrosce.jsmacros.api.math.Vec3D_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.Vec3D_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

### addEnd {#com.jsmacrosce.jsmacros.api.math.Vec3D_addEnd-Pos3D-}

```js
addEnd(Pos3D pos): Vec3D
```

Returns a new segment with its end offset by `pos` and its start unchanged. This segment
 is not modified.

**Parameters:**
- `pos`: the position to move the end by

**Returns:** a new segment ending at the moved point and starting where this one did

**Since:** 1.6.4

### addEnd {#com.jsmacrosce.jsmacros.api.math.Vec3D_addEnd-double-double-double-}

```js
addEnd(double x, double y, double z): Vec3D
```

Returns a new segment with its end offset by the given amounts and its start unchanged; this
 segment is not modified.

**Parameters:**
- `x`: the amount to add to the end's x coordinate
- `y`: the amount to add to the end's y coordinate
- `z`: the amount to add to the end's z coordinate

**Returns:** a new segment ending at the moved point and starting where this one did

**Since:** 1.6.4

### addStart {#com.jsmacrosce.jsmacros.api.math.Vec3D_addStart-Pos3D-}

```js
addStart(Pos3D pos): Vec3D
```

Returns a new segment with its start offset by `pos` and its end unchanged. This segment
 is not modified.

**Parameters:**
- `pos`: the position to move the start by

**Returns:** a new segment starting at the moved point and ending where this one did

**Since:** 1.6.4

### addStart {#com.jsmacrosce.jsmacros.api.math.Vec3D_addStart-double-double-double-}

```js
addStart(double x, double y, double z): Vec3D
```

Returns a new segment with its start offset by the given amounts and its end unchanged; this
 segment is not modified.

**Parameters:**
- `x`: the amount to add to the start's x coordinate
- `y`: the amount to add to the start's y coordinate
- `z`: the amount to add to the start's z coordinate

**Returns:** a new segment starting at the moved point and ending where this one did

**Since:** 1.6.4

### compareTo {#com.jsmacrosce.jsmacros.api.math.Vec3D_compareTo-Vec3D-}

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

Compares segments lexicographically by start position, then end position; each position is
 compared by x, y, then z using [Pos3D.compareTo(Pos3D)](Pos3D.md#com.jsmacrosce.jsmacros.api.math.Pos3D_compareTo-Pos3D-). This coordinate ordering does
 not compare segment lengths or whether they intersect.

**Parameters:**
- `o`: 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 `o`

**Since:** 1.2.6

### compareTo {#com.jsmacrosce.jsmacros.api.math.Vec3D_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

### crossProduct {#com.jsmacrosce.jsmacros.api.math.Vec3D_crossProduct-Vec3D-}

```js
crossProduct(Vec3D vec): Vec3D
```

Returns the cross product of the endpoint differences as a segment from the origin. Its
 direction follows the right-hand rule from this segment toward `vec`; swapping the
 arguments negates it. Parallel directions produce a zero vector. The result is not
 normalized.

**Parameters:**
- `vec`: the other segment to take the perpendicular to

**Returns:** a new vector from the origin to the vector perpendicular to both segments

**Since:** 1.2.6

### dotProduct {#com.jsmacrosce.jsmacros.api.math.Vec3D_dotProduct-Vec3D-}

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

Returns the dot product of the segments' endpoint differences. Its sign indicates whether
 their directions are less than, equal to, or greater than a right angle apart; it is zero if
 either segment has zero length. The result is not normalized, so its magnitude depends on
 both segment lengths. A [Vec2D](Vec2D.md) argument selects the inherited 2D overload, which
 ignores z.

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

**Returns:** the dot product of the two direction differences

**Since:** 1.2.6

### dotProduct {#com.jsmacrosce.jsmacros.api.math.Vec3D_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](#), 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.Vec3D_equals-Object-}

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

Compares this segment with another object by exact runtime class and all six 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. This compares the segment's
 endpoints, not just its displacement or length.

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

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

### getDeltaZ {#com.jsmacrosce.jsmacros.api.math.Vec3D_getDeltaZ-}

```js
getDeltaZ(): double
```

how far the segment runs along z, that is its end's z minus its start's z. It is positive
 when the end is to the south of the start and negative when it is to the north, in the same
 sense that a positive x difference points east.

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

**Since:** 1.2.6

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

```js
getEnd(): Pos3D
```

Returns a copy of the segment's end position, including its z coordinate. Modifying the
 returned position does not change this segment.

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

**Since:** 1.2.6

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

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

Returns the Euclidean length across x, y, and z. Unlike [Vec2D.getMagnitude()](Vec2D.md#com.jsmacrosce.jsmacros.api.math.Vec2D_getMagnitude-), this
 includes vertical displacement.

**Returns:** the length of this segment

**Since:** 1.2.6

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

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

Returns the squared Euclidean length across x, y, and z.

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

**Since:** 1.6.5

### getPitch {#com.jsmacrosce.jsmacros.api.math.Vec3D_getPitch-}

```js
getPitch(): float
```

Returns the segment's pitch in degrees: `0` is level, negative is up, and positive is
 down. It uses the y difference and the combined horizontal distance across x and z; a
 zero-length segment returns `0`.

**Returns:** the pitch of this segment's direction in degrees

**Since:** 1.2.6

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

```js
getStart(): Pos3D
```

a new position at the start of the segment, with the z coordinate from `z1` as well as
 the flat ones. This is a copy, so writing to it does not move the segment.

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

**Since:** 1.2.6

### getYaw {#com.jsmacrosce.jsmacros.api.math.Vec3D_getYaw-}

```js
getYaw(): float
```

Returns the segment's yaw in degrees, using the view convention: `0` is south,  
 `90` is west, and `-90` is east. Only x and z differences matter. The result is
 in `(-180, 180]`; exactly north returns `180`. With no horizontal displacement,  
 including a zero-length segment, the result is `0`.

**Returns:** the yaw of this segment's direction in degrees, in `(-180, 180]`

**Since:** 1.2.6

### getZ1 {#com.jsmacrosce.jsmacros.api.math.Vec3D_getZ1-}

```js
getZ1(): double
```

the z coordinate of the start of the segment, the same value as the `z1` field.

**Returns:** the z coordinate of the start

**Since:** 1.2.6

### getZ2 {#com.jsmacrosce.jsmacros.api.math.Vec3D_getZ2-}

```js
getZ2(): double
```

the z coordinate of the end of the segment, the same value as the `z2` field.

**Returns:** the z coordinate of the end

**Since:** 1.2.6

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

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

Computes a hash code from the current ordered endpoint coordinates, including both z
 coordinates. Because this segment is mutable, its hash may 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.Vec3D_multiply-Vec3D-}

```js
multiply(Vec3D vec): Vec3D
```

Returns a new segment whose endpoint coordinates are multiplied componentwise by those of
 `vec`; this is not a uniform scale, and this segment is unchanged. A [Vec2D](Vec2D.md)
 argument instead selects the inherited 2D overload, which returns a `Vec2D` and ignores
 z.

**Parameters:**
- `vec`: the segment whose endpoint coordinates are the factors

**Returns:** a new segment with every endpoint coordinate multiplied by its matching one

**Since:** 1.2.6

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

```js
multiply(double x1, double y1, double z1, double x2, double y2, double z2): Vec3D
```

Returns a new segment with each endpoint coordinate multiplied by its corresponding factor;  
 the start and end can therefore be scaled independently. This segment is unchanged. For one
 factor applied to all coordinates, use [scale(double)](#com.jsmacrosce.jsmacros.api.math.Vec3D_scale-double-).

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

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

**Since:** 1.6.3

### multiply {#com.jsmacrosce.jsmacros.api.math.Vec3D_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.Vec3D_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.Vec3D_normalize-}

```js
normalize(): Vec3D
```

Returns a new segment with both endpoints divided by this segment's length. The resulting
 segment has length one, but its start is also scaled and is preserved only if it is the
 origin. If the segment has zero length, division produces `NaN` coordinates.

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

**Since:** 1.6.5

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

```js
reverse(): Vec3D
```

Returns a new segment with its 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.Vec3D_scale-double-}

```js
scale(double scale): Vec3D
```

Returns a new segment with all six endpoint coordinates multiplied by `scale`. This
 scales both endpoints about the origin; this segment is unchanged.

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

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

**Since:** 1.6.3

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

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

Returns a copy of this segment, including both z coordinates.

**Returns:** a new Vec3D containing all six endpoint coordinates

### toMojangFloatVector {#com.jsmacrosce.jsmacros.api.math.Vec3D_toMojangFloatVector-}

```js
toMojangFloatVector(): Vector3f
```

Returns the endpoint difference (`x2 - x1, y2 - y1, z2 - z1`) as a float vector, not
 a position. Each component is narrowed from `double` to `float` and may lose
 precision.

**Returns:** a new `Vector3f` holding the coordinate differences of this segment

**Since:** 1.6.5

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

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

Formats the ordered start and end coordinates in x, y, z order. Finite coordinates have six
 fractional digits; formatting uses the default locale, so this text is not a stable
 serialization.

**Returns:** the formatted endpoint coordinates
