Class Point3D

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

public class Point3D extends Object implements Cloneable
A mutable 3D point or vector with double-precision coordinates.

Point3D is the fundamental coordinate type used throughout the Sixth 3D engine. It represents either a position in 3D space or a directional vector, with public x, y, z fields for direct access.

All mutation methods return this for fluent chaining:


 Point3D p = new Point3D(10, 20, 30)
     .multiply(2.0)
     .translateX(5)
     .add(new Point3D(1, 1, 1));
 // p is now (25, 41, 61)
 

Common operations:


 // Create points
 Point3D origin = Point3D.origin();          // (0, 0, 0)
 Point3D pos = Point3D.point(100, 200, 300);
 Point3D copy = new Point3D(pos);            // clone

 // Measure distance
 double dist = pos.getDistanceTo(origin);

 // Rotation
 pos.rotate(origin, Math.PI / 4, 0);  // rotate 45 degrees on XZ plane

 // Scale
 pos.multiply(2.0);   // double all coordinates
 pos.divide(2.0);     // halve all coordinates
 

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:


 Point3D 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).
    double
    Z coordinate (depth axis, positive = into the screen / away from viewer).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a point at the origin (0, 0, 0).
    Point3D(double x, double y, double z)
    Creates a point with the specified double-precision coordinates.
    Point3D(float x, float y, float z)
    Creates a point with the specified float coordinates (widened to double).
    Point3D(int x, int y, int z)
    Creates a point with the specified integer coordinates (widened to double).
    Point3D(Point3D parent)
    Creates a new point by cloning coordinates from the parent point.
    Creates a point from an IntegerPoint (used by octree voxel coordinates).
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Point3D otherPoint)
    Adds another point to this point in place.
    addTo(Point3D... otherPoints)
    Adds coordinates of current point to one or more other points.
    Create new point by cloning position of current point.
    clone(Point3D otherPoint)
    Copies coordinates from another point into this point.
    Set current point coordinates to the middle point between two other points.
    cross(Point3D other)
    Computes the cross-product of this vector with another.
    divide(double factor)
    Divides all coordinates by a factor.
    double
    dot(Point3D other)
    Computes the dot product of this vector with another.
    double
    getAngleXY(Point3D anotherPoint)
    Computes the angle on the X-Y plane between this point and another point.
    double
    getAngleXZ(Point3D anotherPoint)
    Computes the angle on the X-Z plane between this point and another point.
    double
    getAngleYZ(Point3D anotherPoint)
    Computes the angle on the Y-Z plane between this point and another point.
    double
    getDistanceTo(Point3D anotherPoint)
    Compute distance to another point.
    double
    Computes the length (magnitude) of this vector.
    boolean
    Here we assume that Z coordinate is distance to the viewer.
    boolean
    Checks if all coordinates are zero.
    lerp(Point3D other, double t)
    Returns a new point that is a linear interpolation between this point and another.
    multiply(double factor)
    Multiplies all coordinates by a factor.
    Negates this point's coordinates in place.
    static Point3D
    Returns a new point at the origin (0, 0, 0).
    static Point3D
    point(double x, double y, double z)
    Returns a new point with the specified coordinates.
    rotate(double angleXZ, double angleYZ)
    Rotate current point around the origin by the given angles.
    rotate(Point3D center, double angleXZ, double angleYZ)
    Rotates this point around a center point by the given XZ and YZ angles.
    void
    Round current point coordinates to integer values.
    void
    setValues(double x, double y, double z)
    Set current point coordinates to given values.
    subtract(Point3D otherPoint)
    Subtracts another point from this point in place.
     
    translateX(double xIncrement)
    Translates this point along the X axis.
    translateY(double yIncrement)
    Translates this point along the Y axis.
    translateZ(double zIncrement)
    Translates this point along the Z axis.
    Returns a new unit vector (normalized) in the same direction.
    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 point coordinates to zero along all axes.

    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).
    • z

      public double z
      Z coordinate (depth axis, positive = into the screen / away from viewer).
  • Constructor Details

    • Point3D

      public Point3D()
      Creates a point at the origin (0, 0, 0).
    • Point3D

      public Point3D(double x, double y, double z)
      Creates a point with the specified double-precision coordinates.
      Parameters:
      x - the X coordinate
      y - the Y coordinate
      z - the Z coordinate
    • Point3D

      public Point3D(float x, float y, float z)
      Creates a point with the specified float coordinates (widened to double).
      Parameters:
      x - the X coordinate
      y - the Y coordinate
      z - the Z coordinate
    • Point3D

      public Point3D(int x, int y, int z)
      Creates a point with the specified integer coordinates (widened to double).
      Parameters:
      x - the X coordinate
      y - the Y coordinate
      z - the Z coordinate
    • Point3D

      public Point3D(IntegerPoint point)
      Creates a point from an IntegerPoint (used by octree voxel coordinates).
      Parameters:
      point - the integer point to convert
    • Point3D

      public Point3D(Point3D parent)
      Creates a new point by cloning coordinates from the parent point.
      Parameters:
      parent - the point to copy coordinates from
  • Method Details

    • origin

      public static Point3D origin()
      Returns a new point at the origin (0, 0, 0).
      Returns:
      a new Point3D at the origin
    • point

      public static Point3D point(double x, double y, double z)
      Returns a new point with the specified coordinates.
      Parameters:
      x - the X coordinate
      y - the Y coordinate
      z - the Z coordinate
      Returns:
      a new Point3D with the given coordinates
    • add

      public Point3D add(Point3D 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:
    • addTo

      public Point3D addTo(Point3D... otherPoints)
      Adds coordinates of current point to one or more other points. The current point's coordinates are added to each target point.
      Parameters:
      otherPoints - the points to add this point's coordinates to
      Returns:
      this point (for chaining)
    • clone

      public Point3D clone()
      Create new point by cloning position of current point.
      Overrides:
      clone in class Object
      Returns:
      newly created clone.
    • clone

      public Point3D clone(Point3D otherPoint)
      Copies coordinates from another point into this point.
      Parameters:
      otherPoint - the point to copy coordinates from
      Returns:
      this point (for chaining)
    • computeMiddlePoint

      public Point3D computeMiddlePoint(Point3D p1, Point3D p2)
      Set current point coordinates to the middle point between two other points.
      Parameters:
      p1 - first point.
      p2 - second point.
      Returns:
      current point.
    • isZero

      public boolean isZero()
      Checks if all coordinates are zero.
      Returns:
      true if current point coordinates are equal to zero
    • getAngleXZ

      public double getAngleXZ(Point3D anotherPoint)
      Computes the angle on the X-Z plane between this point and another point.
      Parameters:
      anotherPoint - the other point
      Returns:
      the angle in radians
    • getAngleYZ

      public double getAngleYZ(Point3D anotherPoint)
      Computes the angle on the Y-Z plane between this point and another point.
      Parameters:
      anotherPoint - the other point
      Returns:
      the angle in radians
    • getAngleXY

      public double getAngleXY(Point3D 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(Point3D anotherPoint)
      Compute distance to another point.
      Parameters:
      anotherPoint - point to compute distance to.
      Returns:
      distance to another point.
    • getVectorLength

      public double getVectorLength()
      Computes the length (magnitude) of this vector.
      Returns:
      the vector length
    • negate

      public Point3D negate()
      Negates this point's coordinates in place. This point is modified.
      Returns:
      this point (for chaining)
      See Also:
    • rotate

      public Point3D rotate(Point3D center, double angleXZ, double angleYZ)
      Rotates this point around a center point by the given XZ and YZ angles.

      See also: Let's remove Quaternions from every 3D Engine

      Parameters:
      center - the center point to rotate around
      angleXZ - the angle in the XZ plane (yaw) in radians
      angleYZ - the angle in the YZ plane (pitch) in radians
      Returns:
      this point (for chaining)
    • rotate

      public Point3D rotate(double angleXZ, double angleYZ)
      Rotate current point around the origin by the given angles.
      Parameters:
      angleXZ - angle around the XZ plane (yaw), in radians
      angleYZ - angle around the YZ plane (pitch), in radians
      Returns:
      this point (mutated)
    • roundToInteger

      public void roundToInteger()
      Round current point coordinates to integer values.
    • divide

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

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

      public void setValues(double x, double y, double z)
      Set current point coordinates to given values.
      Parameters:
      x - X coordinate.
      y - Y coordinate.
      z - Z coordinate.
    • subtract

      public Point3D subtract(Point3D 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:
    • toString

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

      public Point3D translateX(double xIncrement)
      Translates this point along the X axis.
      Parameters:
      xIncrement - the amount to add to the X coordinate
      Returns:
      this point (for chaining)
    • translateY

      public Point3D translateY(double yIncrement)
      Translates this point along the Y axis.
      Parameters:
      yIncrement - the amount to add to the Y coordinate
      Returns:
      this point (for chaining)
    • translateZ

      public Point3D translateZ(double zIncrement)
      Translates this point along the Z axis.
      Parameters:
      zIncrement - the amount to add to the Z coordinate
      Returns:
      this point (for chaining)
    • isVisible

      public boolean isVisible()
      Here we assume that Z coordinate is distance to the viewer. If Z is positive, then point is in front of the viewer, and therefore it is visible.
      Returns:
      point visibility status.
    • zero

      public Point3D zero()
      Resets point coordinates to zero along all axes.
      Returns:
      current point.
    • dot

      public double dot(Point3D other)
      Computes the dot product of this vector with another.
      Parameters:
      other - the other vector
      Returns:
      the dot product (scalar)
    • cross

      public Point3D cross(Point3D other)
      Computes the cross-product of this vector with another. Returns a new vector perpendicular to both input vectors.
      Parameters:
      other - the other vector
      Returns:
      a new Point3D representing the cross-product
    • withAdded

      public Point3D withAdded(Point3D 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 Point3D representing the sum
      See Also:
    • withSubtracted

      public Point3D withSubtracted(Point3D 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 Point3D representing the difference
      See Also:
    • withNegated

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

      public Point3D unit()
      Returns a new unit vector (normalized) in the same direction. This point is not modified.
      Returns:
      a new Point3D with unit length
    • lerp

      public Point3D lerp(Point3D other, double t)
      Returns a new point that is a linear interpolation between this point and another. When t=0, returns this point. When t=1, returns the other point.
      Parameters:
      other - the other point
      t - the interpolation parameter (0 to 1)
      Returns:
      a new Point3D representing the interpolated position
    • withMultiplied

      public Point3D 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 Point3D with multiplied coordinates
      See Also:
    • withDivided

      public Point3D 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 Point3D with divided coordinates
      See Also: