Class Point2D

java.lang.Object
eu.svjatoslav.sixth.e3d.geometry.Point2D
All Implemented Interfaces:
Cloneable

public class Point2D extends Object implements 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 return this
  • 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
    Modifier and Type
    Field
    Description
    double
    X coordinate (horizontal axis).
    double
    Y coordinate (vertical axis, positive = down in screen space).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a point at the origin (0, 0).
    Point2D(double x, double y)
    Creates a point with the specified coordinates.
    Point2D(Point2D parent)
    Creates a point by copying coordinates from another point.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Point2D otherPoint)
    Adds another point to this point in place.
    Creates a new point by copying this point's coordinates.
    void
    clone(Point2D otherPoint)
    Copies coordinates from another point into this point.
    divide(double factor)
    Divides both coordinates by a factor.
    double
    getAngleXY(Point2D anotherPoint)
    Computes the angle on the X-Y plane between this point and another point.
    double
    getDistanceTo(Point2D anotherPoint)
    Computes the Euclidean distance from this point to another point.
    double
    Computes the length of this vector (magnitude).
    boolean
    Checks if both coordinates are zero.
    multiply(double factor)
    Multiplies both coordinates by a factor.
    Negates this point's coordinates in place.
    void
    Rounds this point's coordinates to integer values.
    Sets this point to the midpoint between two other points.
    subtract(Point2D otherPoint)
    Subtracts another point from this point in place.
    Converts this 2D point to a 3D point with z = 0.
     
    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.
    Returns a new point that is this point minus another.
    Resets this point's coordinates to (0, 0).

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      public double x
      X coordinate (horizontal axis).
    • y

      public double y
      Y 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 coordinate
      y - the Y coordinate
    • Point2D

      public Point2D(Point2D parent)
      Creates a point by copying coordinates from another point.
      Parameters:
      parent - the point to copy from
  • Method Details

    • add

      public Point2D add(Point2D otherPoint)
      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:
      true if current point coordinates are equal to zero
    • clone

      public Point2D clone()
      Creates a new point by copying this point's coordinates.
      Overrides:
      clone in class Object
      Returns:
      a new point with the same coordinates
    • clone

      public void clone(Point2D otherPoint)
      Copies coordinates from another point into this point.
      Parameters:
      otherPoint - the point to copy coordinates from
    • setToMiddle

      public Point2D setToMiddle(Point2D p1, Point2D p2)
      Sets this point to the midpoint between two other points.
      Parameters:
      p1 - the first point
      p2 - the second point
      Returns:
      this point (for chaining)
    • getAngleXY

      public double getAngleXY(Point2D anotherPoint)
      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

      public double getDistanceTo(Point2D anotherPoint)
      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

      public Point2D 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

      public Point2D subtract(Point2D otherPoint)
      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

      public Point2D multiply(double factor)
      Multiplies both coordinates by a factor. This point is modified.
      Parameters:
      factor - the multiplier
      Returns:
      this point (for chaining)
      See Also:
    • divide

      public Point2D divide(double factor)
      Divides both coordinates by a factor. This point is modified.
      Parameters:
      factor - the divisor
      Returns:
      this point (for chaining)
      See Also:
    • to3D

      public Point3D 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

      public Point2D zero()
      Resets this point's coordinates to (0, 0).
      Returns:
      this point (for chaining)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • withAdded

      public Point2D withAdded(Point2D other)
      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

      public Point2D withSubtracted(Point2D other)
      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

      public Point2D withNegated()
      Returns a new point with negated coordinates. This point is not modified.
      Returns:
      a new Point2D with negated coordinates
      See Also:
    • withMultiplied

      public Point2D withMultiplied(double factor)
      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

      public Point2D withDivided(double factor)
      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: