Vec3D
com.jsmacrosce.jsmacros.api.math.Vec3D
A 3D line segment with mutable endpoints. Length and angle measurements include z.
Inherited Vec2D 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() 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
makes a segment from the coordinates of its two ends.
Parameters:
x1: the x coordinate of the starty1: the y coordinate of the startz1: the z coordinate of the startx2: the x coordinate of the endy2: the y coordinate of the endz2: the z coordinate of the end
makes a segment running from one position to another.
Parameters:
start: the position the segment starts atend: the position the segment ends at
Fields
z1
z1: double#The z coordinate of the segment's start.
z2
z2: double#The z coordinate of the segment's end.
Methods
add
Returns a new segment with each endpoint coordinate added to the corresponding coordinate of
vec; this segment is unchanged. A Vec2D 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
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 coordinatey1: the amount to add to the start's y coordinatez1: the amount to add to the start's z coordinatex2: the amount to add to the end's x coordinatey2: the amount to add to the end's y coordinatez2: the amount to add to the end's z coordinate
Returns: a new segment offset endpoint for endpoint
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.y2Returns 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 coordinatey1: the amount to add to the start's y coordinatex2: the amount to add to the end's x coordinatey2: the amount to add to the end's y coordinate
Returns: a new segment offset endpoint for endpoint
addEnd
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
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 coordinatey: the amount to add to the end's y coordinatez: 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
addStart
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
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 coordinatey: the amount to add to the start's y coordinatez: 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
compareTo
Compares segments lexicographically by start position, then end position; each position is compared by x, y, then z using 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
oCompares segments by start position, then by end position when their starts compare equal; each position is ordered by 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
othercrossProduct
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
dotProduct
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 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
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
equals
Compares this segment with another object by exact runtime class and all six ordered endpoint coordinates. Coordinates use Double.compare(double, double): 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 otherwisegetDeltaZ
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
getEnd
getMagnitude
Returns the Euclidean length across x, y, and z. Unlike Vec2D.getMagnitude(), this includes vertical displacement.
Returns: the length of this segment
getMagnitudeSq
Returns the squared Euclidean length across x, y, and z.
Returns: the squared length of this segment
getPitch
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
getStart
getYaw
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]getZ1
the z coordinate of the start of the segment, the same value as the
z1 field.Returns: the z coordinate of the start
getZ2
the z coordinate of the end of the segment, the same value as the
z2 field.Returns: the z coordinate of the end
hashCode
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
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 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
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).
Parameters:
x1: the factor for the start's x coordinatey1: the factor for the start's y coordinatez1: the factor for the start's z coordinatex2: the factor for the end's x coordinatey2: the factor for the end's y coordinatez2: the factor for the end's z coordinate
Returns: a new segment with each endpoint coordinate multiplied by its factor
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.y2Returns 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 coordinatey1: the factor for the start's y coordinatex2: the factor for the end's x coordinatey2: the factor for the end's y coordinate
Returns: a new segment with each endpoint coordinate multiplied by its factor
normalize
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
reverse
scale
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
to3D
toMojangFloatVector
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