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) {
62 * Add coordinates of current point to other point. Value of current point will not be changed.
64 * @return current point.
66 public Point3D addTo(final Point3D... otherPoints) {
67 for (final Point3D otherPoint : otherPoints) otherPoint.add(this);
72 * Create new point by cloning position of current point.
74 * @return newly created clone.
76 public Point3D clone() {
77 return new Point3D(this);
81 * Copy coordinates from other point to current point. Value of other point will not be changed.
83 public Point3D clone(final Point3D otherPoint) {
91 * Set current point coordinates to the middle point between two other points.
93 * @param p1 first point.
94 * @param p2 second point.
95 * @return current point.
97 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
98 x = (p1.x + p2.x) / 2d;
99 y = (p1.y + p2.y) / 2d;
100 z = (p1.z + p2.z) / 2d;
105 * @return true if current point coordinates are equal to zero.
107 public boolean isZero() {
108 return (x == 0) && (y == 0) && (z == 0);
111 public double getAngleXZ(final Point3D anotherPoint) {
112 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
115 public double getAngleYZ(final Point3D anotherPoint) {
116 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
119 public double getAngleXY(final Point3D anotherPoint) {
120 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
124 * Compute distance to another point.
126 * @param anotherPoint point to compute distance to.
127 * @return distance to another point.
129 public double getDistanceTo(final Point3D anotherPoint) {
130 final double xDelta = x - anotherPoint.x;
131 final double yDelta = y - anotherPoint.y;
132 final double zDelta = z - anotherPoint.z;
134 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
138 * @return length of current vector.
140 public double getVectorLength() {
141 return sqrt(((x * x) + (y * y) + (z * z)));
145 * Invert current point coordinates.
147 * @return current point.
149 public Point3D invert() {
157 * Rotate current point around center point by angleXZ and angleYZ.
159 * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
161 * @param center center point.
162 * @param angleXZ angle around XZ axis.
163 * @param angleYZ angle around YZ axis.
165 public Point3D rotate(final Point3D center, final double angleXZ,
166 final double angleYZ) {
167 final double s1 = sin(angleXZ);
168 final double c1 = cos(angleXZ);
170 final double s2 = sin(angleYZ);
171 final double c2 = cos(angleYZ);
177 final double y1 = (z * s2) + (y * c2);
178 final double z1 = (z * c2) - (y * s2);
180 final double x1 = (z1 * s1) + (x * c1);
181 final double z2 = (z1 * c1) - (x * s1);
190 public Point3D rotate(final double angleXZ, final double angleYZ) {
191 return rotate(new Point3D(0, 0, 0), angleXZ, angleYZ);
195 * Round current point coordinates to integer values.
197 public void roundToInteger() {
204 * Scale down current point by factor.
205 * All coordinates will be divided by factor.
207 * @param factor factor to scale by.
208 * @return current point.
210 public Point3D scaleDown(final double factor) {
218 * Scale up current point by factor.
219 * All coordinates will be multiplied by factor.
221 * @param factor factor to scale by.
222 * @return current point.
224 public Point3D scaleUp(final double factor) {
232 * Set current point coordinates to given values.
234 * @param x X coordinate.
235 * @param y Y coordinate.
236 * @param z Z coordinate.
238 public void setValues(final double x, final double y, final double z) {
245 * Subtract other point from current point. Value of other point will not be changed.
247 * @return current point.
249 public Point3D subtract(final Point3D otherPoint) {
257 public String toString() {
258 return "x:" + x + " y:" + y + " z:" + z;
262 * Translate current point along X axis by given increment.
264 * @return current point.
266 public Point3D translateX(final double xIncrement) {
272 * Translate current point along Y axis by given increment.
274 * @return current point.
276 public Point3D translateY(final double yIncrement) {
282 * Translate current point along Z axis by given increment.
284 * @return current point.
286 public Point3D translateZ(final double zIncrement) {
292 * Here we assume that Z coordinate is distance to the viewer.
293 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
295 * @return point visibility status.
297 public boolean isVisible() {
302 * Resets point coordinates to zero along all axes.
304 * @return current point.
306 public Point3D zero() {