2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.geometry;
7 import static java.lang.Math.*;
10 * Used to represent point in a 3D space or vector.
13 public class Point3D implements Cloneable {
16 public double x, y, z;
21 public Point3D(final double x, final double y, final double z) {
27 public Point3D(final float x, final float y, final float z) {
33 public Point3D(final int x, final int y, final int z) {
40 * Creates new current point by cloning coordinates from parent point.
42 public Point3D(final Point3D parent) {
49 * Add other point to current point. Value of other point will not be changed.
51 * @param otherPoint point to add.
52 * @return current point.
54 public Point3D add(final Point3D otherPoint) {
61 public Point3D clone() {
62 return new Point3D(this);
66 * Copy coordinates from other point to current point. Value of other point will not be changed.
68 public Point3D clone(final Point3D otherPoint) {
76 * Set current point coordinates to the middle point between two other points.
78 * @param p1 first point.
79 * @param p2 second point.
80 * @return current point.
82 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
83 x = (p1.x + p2.x) / 2d;
84 y = (p1.y + p2.y) / 2d;
85 z = (p1.z + p2.z) / 2d;
90 * @return true if current point coordinates are equal to zero.
92 public boolean isZero() {
93 return (x == 0) && (y == 0) && (z == 0);
96 public double getAngleXZ(final Point3D anotherPoint) {
97 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
100 public double getAngleYZ(final Point3D anotherPoint) {
101 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
104 public double getAngleXY(final Point3D anotherPoint) {
105 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
109 * Compute distance to another point.
111 * @param anotherPoint point to compute distance to.
112 * @return distance to another point.
114 public double getDistanceTo(final Point3D anotherPoint) {
115 final double xDelta = x - anotherPoint.x;
116 final double yDelta = y - anotherPoint.y;
117 final double zDelta = z - anotherPoint.z;
119 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
123 * @return length of current vector.
125 public double getVectorLength() {
126 return sqrt(((x * x) + (y * y) + (z * z)));
130 * Invert current point coordinates.
132 * @return current point.
134 public Point3D invert() {
142 * Rotate current point around center point by angleXZ and angleYZ.
144 * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
146 * @param center center point.
147 * @param angleXZ angle around XZ axis.
148 * @param angleYZ angle around YZ axis.
150 public Point3D rotate(final Point3D center, final double angleXZ,
151 final double angleYZ) {
152 final double s1 = sin(angleXZ);
153 final double c1 = cos(angleXZ);
155 final double s2 = sin(angleYZ);
156 final double c2 = cos(angleYZ);
162 final double y1 = (z * s2) + (y * c2);
163 final double z1 = (z * c2) - (y * s2);
165 final double x1 = (z1 * s1) + (x * c1);
166 final double z2 = (z1 * c1) - (x * s1);
176 * Round current point coordinates to integer values.
178 public void roundToInteger() {
185 * Scale down current point by factor.
186 * All coordinates will be divided by factor.
188 * @param factor factor to scale by.
189 * @return current point.
191 public Point3D scaleDown(final double factor) {
199 * Scale up current point by factor.
200 * All coordinates will be multiplied by factor.
202 * @param factor factor to scale by.
203 * @return current point.
205 public Point3D scaleUp(final double factor) {
213 * Set current point coordinates to given values.
214 * @param x X coordinate.
215 * @param y Y coordinate.
216 * @param z Z coordinate.
218 public void setValues(final double x, final double y, final double z) {
225 * Subtract other point from current point. Value of other point will not be changed.
227 * @return current point.
229 public Point3D subtract(final Point3D otherPoint) {
237 public String toString() {
238 return "x:" + x + " y:" + y + " z:" + z;
242 * Translate current point along X axis by given increment.
244 * @return current point.
246 public Point3D translateX(final double xIncrement) {
252 * Translate current point along Y axis by given increment.
254 * @return current point.
256 public Point3D translateY(final double yIncrement) {
262 * Translate current point along Z axis by given increment.
264 * @return current point.
266 public Point3D translateZ(final double zIncrement) {
272 * Here we assume that Z coordinate is distance to the viewer.
273 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
275 * @return point visibility status.
277 public boolean isVisible() {
282 * Resets point coordinates to zero along all axes.
284 * @return current point.
286 public Point3D zero() {