Package eu.svjatoslav.sixth.e3d.geometry
Class Point3D
java.lang.Object
eu.svjatoslav.sixth.e3d.geometry.Point3D
- All Implemented Interfaces:
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 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:
Point3D safeCopy = original.clone();
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionPoint3D()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).Creates a new point by cloning coordinates from the parent point.Point3D(IntegerPoint point) Creates a point from anIntegerPoint(used by octree voxel coordinates). -
Method Summary
Modifier and TypeMethodDescriptionAdds another point to this point in place.Adds coordinates of current point to one or more other points.clone()Create new point by cloning position of current point.Copies coordinates from another point into this point.computeMiddlePoint(Point3D p1, Point3D p2) Set current point coordinates to the middle point between two other points.Computes the cross-product of this vector with another.divide(double factor) Divides all coordinates by a factor.doubleComputes the dot product of this vector with another.doublegetAngleXY(Point3D anotherPoint) Computes the angle on the X-Y plane between this point and another point.doublegetAngleXZ(Point3D anotherPoint) Computes the angle on the X-Z plane between this point and another point.doublegetAngleYZ(Point3D anotherPoint) Computes the angle on the Y-Z plane between this point and another point.doublegetDistanceTo(Point3D anotherPoint) Compute distance to another point.doubleComputes the length (magnitude) of this vector.booleanHere we assume that Z coordinate is distance to the viewer.booleanisZero()Checks if all coordinates are zero.Returns a new point that is a linear interpolation between this point and another.multiply(double factor) Multiplies all coordinates by a factor.negate()Negates this point's coordinates in place.static Point3Dorigin()Returns a new point at the origin (0, 0, 0).static Point3Dpoint(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.Rotates this point around a center point by the given XZ and YZ angles.voidRound current point coordinates to integer values.voidsetValues(double x, double y, double z) Set current point coordinates to given values.Subtracts another point from this point in place.toString()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.unit()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.withSubtracted(Point3D other) Returns a new point that is this point minus another.zero()Resets point coordinates to zero along all axes.
-
Field Details
-
x
public double xX coordinate (horizontal axis). -
y
public double yY coordinate (vertical axis, positive = down in screen space). -
z
public double zZ 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 coordinatey- the Y coordinatez- 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 coordinatey- the Y coordinatez- 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 coordinatey- the Y coordinatez- the Z coordinate
-
Point3D
Creates a point from anIntegerPoint(used by octree voxel coordinates).- Parameters:
point- the integer point to convert
-
Point3D
Creates a new point by cloning coordinates from the parent point.- Parameters:
parent- the point to copy coordinates from
-
-
Method Details
-
origin
Returns a new point at the origin (0, 0, 0).- Returns:
- a new Point3D at the origin
-
point
Returns a new point with the specified coordinates.- Parameters:
x- the X coordinatey- the Y coordinatez- the Z coordinate- Returns:
- a new Point3D with the given coordinates
-
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:
-
addTo
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
Create new point by cloning position of current point. -
clone
Copies coordinates from another point into this point.- Parameters:
otherPoint- the point to copy coordinates from- Returns:
- this point (for chaining)
-
computeMiddlePoint
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:
trueif current point coordinates are equal to zero
-
getAngleXZ
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
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
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
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
Negates this point's coordinates in place. This point is modified.- Returns:
- this point (for chaining)
- See Also:
-
rotate
Rotates this point around a center point by the given XZ and YZ angles.- Parameters:
center- the center point to rotate aroundangleXZ- the angle in the XZ plane (yaw) in radiansangleYZ- the angle in the YZ plane (pitch) in radians- Returns:
- this point (for chaining)
-
rotate
Rotate current point around the origin by the given angles.- Parameters:
angleXZ- angle around the XZ plane (yaw), in radiansangleYZ- angle around the YZ plane (pitch), in radians- Returns:
- this point (mutated)
-
roundToInteger
public void roundToInteger()Round current point coordinates to integer values. -
divide
Divides all coordinates by a factor. This point is modified.- Parameters:
factor- the divisor- Returns:
- this point (for chaining)
- See Also:
-
multiply
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
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
-
translateX
Translates this point along the X axis.- Parameters:
xIncrement- the amount to add to the X coordinate- Returns:
- this point (for chaining)
-
translateY
Translates this point along the Y axis.- Parameters:
yIncrement- the amount to add to the Y coordinate- Returns:
- this point (for chaining)
-
translateZ
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
Resets point coordinates to zero along all axes.- Returns:
- current point.
-
dot
Computes the dot product of this vector with another.- Parameters:
other- the other vector- Returns:
- the dot product (scalar)
-
cross
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
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
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
Returns a new point with negated coordinates. This point is not modified.- Returns:
- a new Point3D with negated coordinates
- See Also:
-
unit
Returns a new unit vector (normalized) in the same direction. This point is not modified.- Returns:
- a new Point3D with unit length
-
lerp
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 pointt- the interpolation parameter (0 to 1)- Returns:
- a new Point3D representing the interpolated position
-
withMultiplied
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
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:
-