Skip to content

Vec2D ​

com.jsmacrosce.jsmacros.api.math.Vec2D

Since1.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() 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 ​

new Vec2D(double x1, double y1, double x2, double y2)Since1.2.6#
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
new Vec2D(Pos2D start, Pos2D end)Since1.2.6#
Constructs a segment from two positions, copying their coordinates.

Parameters:

  • start: the position the segment starts at
  • end: the position the segment ends at
new Vec2D(Vec2D vec)Since1.2.6#
Copies the endpoint coordinates of another segment.

Parameters:

  • vec: the vector to copy

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 ​

add(Vec2D vec): Vec2DSince1.2.6#
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
add(double x1, double y1, double x2, double y2): Vec2DSince1.6.3#
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

compareTo ​

compareTo(Vec2D other): intSince1.2.6#
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 other

dotProduct ​

dotProduct(Vec2D vec): doubleSince1.2.6#
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 ​

equals(Object o): boolean#
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 otherwise

getDeltaX ​

getDeltaX(): doubleSince1.2.6#
Returns the x displacement from the segment's start to its end (x2 - x1).
Returns: the x distance covered by this segment

getDeltaY ​

getDeltaY(): doubleSince1.2.6#
Returns the y displacement from the segment's start to its end (y2 - y1).
Returns: the y distance covered by this segment

getEnd ​

getEnd(): Pos2DSince1.2.6#
Returns a copy of the segment's end position; modifying it does not change this segment.
Returns: a new Pos2D at the end of this segment

getMagnitude ​

getMagnitude(): doubleSince1.2.6#
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 ​

getMagnitudeSq(): doubleSince1.6.5#
Returns the squared Euclidean length of the segment. Vec3D overrides this to include the z displacement.
Returns: the squared length of this segment

getStart ​

getStart(): Pos2DSince1.2.6#
Returns a copy of the segment's start position; modifying it does not change this segment.
Returns: a new Pos2D at the start of this segment

getX1 ​

getX1(): doubleSince1.2.6#
Returns the x coordinate of the segment's start.
Returns: the x coordinate of the start

getX2 ​

getX2(): doubleSince1.2.6#
Returns the x coordinate of the segment's end.
Returns: the x coordinate of the end

getY1 ​

getY1(): doubleSince1.2.6#
Returns the y coordinate of the segment's start.
Returns: the y coordinate of the start

getY2 ​

getY2(): doubleSince1.2.6#
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 ​

multiply(Vec2D vec): Vec2DSince1.2.6#
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
multiply(double x1, double y1, double x2, double y2): Vec2DSince1.6.3#
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

normalize ​

normalize(): Vec2DSince1.6.5#
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 ​

reverse(): Vec2DSince1.2.6#
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

scale ​

scale(double scale): Vec2DSince1.6.3#
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

to3D ​

to3D(): Vec3DSince1.2.6#
Returns a Vec3D through the same x-y endpoints with both z coordinates set to 0. Vec3D overrides this to preserve its z coordinates.
Returns: a new Vec3D through the same two points, flat on the z 0 plane

toString ​

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