Package eu.svjatoslav.sixth.e3d.geometry
Class Point2D
java.lang.Object
eu.svjatoslav.sixth.e3d.geometry.Point2D
- All Implemented Interfaces:
Cloneable
A mutable 2D point or vector with double-precision coordinates.
Point2D represents either a position in 2D space or a directional vector,
with public x and y fields for direct access. It is commonly used
for screen-space coordinates after 3D-to-2D projection.
All mutation methods return this for fluent chaining:
Point2D p = new Point2D(10, 20)
.multiply(2.0)
.add(new Point2D(5, 5))
.negate();
// p is now (-25, -45)
Mutability convention:
- Imperative verbs (
add,subtract,negate,multiply,divide) mutate this point and returnthis with-prefixed methods (withAdded,withSubtracted,withNegated,withMultiplied,withDivided) return a new point without modifying this one
Warning: This class is mutable with public fields. Clone before storing references that should not be shared:
Point2D safeCopy = original.clone();
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds another point to this point in place.clone()Creates a new point by copying this point's coordinates.voidCopies coordinates from another point into this point.divide(double factor) Divides both coordinates by a factor.doublegetAngleXY(Point2D anotherPoint) Computes the angle on the X-Y plane between this point and another point.doublegetDistanceTo(Point2D anotherPoint) Computes the Euclidean distance from this point to another point.doubleComputes the length of this vector (magnitude).booleanisZero()Checks if both coordinates are zero.multiply(double factor) Multiplies both coordinates by a factor.negate()Negates this point's coordinates in place.voidRounds this point's coordinates to integer values.setToMiddle(Point2D p1, Point2D p2) Sets this point to the midpoint between two other points.Subtracts another point from this point in place.to3D()Converts this 2D point to a 3D point with z = 0.toString()Returns a new point that is the sum of this point and another.withDivided(double factor) Returns a new point with coordinates divided by a factor.withMultiplied(double factor) Returns a new point with coordinates multiplied by a factor.Returns a new point with negated coordinates.withSubtracted(Point2D other) Returns a new point that is this point minus another.zero()Resets this point's coordinates to (0, 0).
-
Field Details
-
x
public double xX coordinate (horizontal axis). -
y
public double yY coordinate (vertical axis, positive = down in screen space).
-
-
Constructor Details
-
Point2D
public Point2D()Creates a point at the origin (0, 0). -
Point2D
public Point2D(double x, double y) Creates a point with the specified coordinates.- Parameters:
x- the X coordinatey- the Y coordinate
-
Point2D
Creates a point by copying coordinates from another point.- Parameters:
parent- the point to copy from
-
-
Method Details
-
add
Adds another point to this point in place. This point is modified, the other point is not.- Parameters:
otherPoint- the point to add- Returns:
- this point (for chaining)
- See Also:
-
isZero
public boolean isZero()Checks if both coordinates are zero.- Returns:
trueif current point coordinates are equal to zero
-
clone
Creates a new point by copying this point's coordinates. -
clone
Copies coordinates from another point into this point.- Parameters:
otherPoint- the point to copy coordinates from
-
setToMiddle
Sets this point to the midpoint between two other points.- Parameters:
p1- the first pointp2- the second point- Returns:
- this point (for chaining)
-
getAngleXY
Computes the angle on the X-Y plane between this point and another point.- Parameters:
anotherPoint- the other point- Returns:
- the angle in radians
-
getDistanceTo
Computes the Euclidean distance from this point to another point.- Parameters:
anotherPoint- the point to compute distance to- Returns:
- the distance between the two points
-
getVectorLength
public double getVectorLength()Computes the length of this vector (magnitude).- Returns:
- the vector length
-
negate
Negates this point's coordinates in place. This point is modified.- Returns:
- this point (for chaining)
- See Also:
-
roundToInteger
public void roundToInteger()Rounds this point's coordinates to integer values. -
subtract
Subtracts another point from this point in place. This point is modified, the other point is not.- Parameters:
otherPoint- the point to subtract- Returns:
- this point (for chaining)
- See Also:
-
multiply
Multiplies both coordinates by a factor. This point is modified.- Parameters:
factor- the multiplier- Returns:
- this point (for chaining)
- See Also:
-
divide
Divides both coordinates by a factor. This point is modified.- Parameters:
factor- the divisor- Returns:
- this point (for chaining)
- See Also:
-
to3D
Converts this 2D point to a 3D point with z = 0.- Returns:
- a new 3D point with the same x, y and z = 0
-
zero
Resets this point's coordinates to (0, 0).- Returns:
- this point (for chaining)
-
toString
-
withAdded
Returns a new point that is the sum of this point and another. This point is not modified.- Parameters:
other- the point to add- Returns:
- a new Point2D representing the sum
- See Also:
-
withSubtracted
Returns a new point that is this point minus another. This point is not modified.- Parameters:
other- the point to subtract- Returns:
- a new Point2D representing the difference
- See Also:
-
withNegated
Returns a new point with negated coordinates. This point is not modified.- Returns:
- a new Point2D with negated coordinates
- See Also:
-
withMultiplied
Returns a new point with coordinates multiplied by a factor. This point is not modified.- Parameters:
factor- the multiplier- Returns:
- a new Point2D with multiplied coordinates
- See Also:
-
withDivided
Returns a new point with coordinates divided by a factor. This point is not modified.- Parameters:
factor- the divisor- Returns:
- a new Point2D with divided coordinates
- See Also:
-