Vec2D
com.jsmacrosce.jsmacros.api.math.Vec2D
A 2D line segment with mutable start and end coordinates. Measurements use the displacement from start to end; arithmetic methods return new segments. 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
Constructs a segment from its start and end coordinates.
Parameters:
x1: the x coordinate of the starty1: the y coordinate of the startx2: the x coordinate of the endy2: the y coordinate of the end
Constructs a segment from two positions, copying their coordinates.
Parameters:
start: the position the segment starts atend: the position the segment ends at
Fields
x1
x1: double#The x coordinate of the segment's start.
x2
x2: double#The x coordinate of the segment's end.
y1
y1: double#The y coordinate of the segment's start.
y2
y2: double#The y coordinate of the segment's end.
Methods
add
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
compareTo
Compares 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
otherdotProduct
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 ordered endpoint coordinates. Coordinates use Double.compare(double, double): 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 otherwisegetDeltaX
Returns the x displacement from the segment's start to its end (
x2 - x1).Returns: the x distance covered by this segment
getDeltaY
Returns the y displacement from the segment's start to its end (
y2 - y1).Returns: the y distance covered by this segment
getEnd
getMagnitude
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
getMagnitudeSq
getStart
getX1
Returns the x coordinate of the segment's start.
Returns: the x coordinate of the start
getX2
Returns the x coordinate of the segment's end.
Returns: the x coordinate of the end
getY1
Returns the y coordinate of the segment's start.
Returns: the y coordinate of the start
getY2
Returns the y coordinate of the segment's end.
Returns: the y coordinate of the end
hashCode
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
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 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 overrides this to include z coordinates.Returns: a new segment with the same direction but a length of 1
reverse
scale
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