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.
63 * @return current point.
65 public Point3D addTo(final Point3D ... otherPoints) {
66 for (final Point3D otherPoint : otherPoints) otherPoint.add(this);
71 * Create new point by cloning position of current point.
73 * @return newly created clone.
75 public Point3D clone() {
76 return new Point3D(this);
80 * Copy coordinates from other point to current point. Value of other point will not be changed.
82 public Point3D clone(final Point3D otherPoint) {
90 * Set current point coordinates to the middle point between two other points.
92 * @param p1 first point.
93 * @param p2 second point.
94 * @return current point.
96 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
97 x = (p1.x + p2.x) / 2d;
98 y = (p1.y + p2.y) / 2d;
99 z = (p1.z + p2.z) / 2d;
104 * @return true if current point coordinates are equal to zero.
106 public boolean isZero() {
107 return (x == 0) && (y == 0) && (z == 0);
110 public double getAngleXZ(final Point3D anotherPoint) {
111 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
114 public double getAngleYZ(final Point3D anotherPoint) {
115 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
118 public double getAngleXY(final Point3D anotherPoint) {
119 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
123 * Compute distance to another point.
125 * @param anotherPoint point to compute distance to.
126 * @return distance to another point.
128 public double getDistanceTo(final Point3D anotherPoint) {
129 final double xDelta = x - anotherPoint.x;
130 final double yDelta = y - anotherPoint.y;
131 final double zDelta = z - anotherPoint.z;
133 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
137 * @return length of current vector.
139 public double getVectorLength() {
140 return sqrt(((x * x) + (y * y) + (z * z)));
144 * Invert current point coordinates.
146 * @return current point.
148 public Point3D invert() {
156 * Rotate current point around center point by angleXZ and angleYZ.
158 * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
160 * @param center center point.
161 * @param angleXZ angle around XZ axis.
162 * @param angleYZ angle around YZ axis.
164 public Point3D rotate(final Point3D center, final double angleXZ,
165 final double angleYZ) {
166 final double s1 = sin(angleXZ);
167 final double c1 = cos(angleXZ);
169 final double s2 = sin(angleYZ);
170 final double c2 = cos(angleYZ);
176 final double y1 = (z * s2) + (y * c2);
177 final double z1 = (z * c2) - (y * s2);
179 final double x1 = (z1 * s1) + (x * c1);
180 final double z2 = (z1 * c1) - (x * s1);
189 public Point3D rotate(final double angleXZ, final double angleYZ) {
190 return rotate(new Point3D(0, 0, 0), angleXZ, angleYZ);
194 * Round current point coordinates to integer values.
196 public void roundToInteger() {
203 * Scale down current point by factor.
204 * All coordinates will be divided by factor.
206 * @param factor factor to scale by.
207 * @return current point.
209 public Point3D scaleDown(final double factor) {
217 * Scale up current point by factor.
218 * All coordinates will be multiplied by factor.
220 * @param factor factor to scale by.
221 * @return current point.
223 public Point3D scaleUp(final double factor) {
231 * Set current point coordinates to given values.
232 * @param x X coordinate.
233 * @param y Y coordinate.
234 * @param z Z coordinate.
236 public void setValues(final double x, final double y, final double z) {
243 * Subtract other point from current point. Value of other point will not be changed.
245 * @return current point.
247 public Point3D subtract(final Point3D otherPoint) {
255 public String toString() {
256 return "x:" + x + " y:" + y + " z:" + z;
260 * Translate current point along X axis by given increment.
262 * @return current point.
264 public Point3D translateX(final double xIncrement) {
270 * Translate current point along Y axis by given increment.
272 * @return current point.
274 public Point3D translateY(final double yIncrement) {
280 * Translate current point along Z axis by given increment.
282 * @return current point.
284 public Point3D translateZ(final double zIncrement) {
290 * Here we assume that Z coordinate is distance to the viewer.
291 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
293 * @return point visibility status.
295 public boolean isVisible() {
300 * Resets point coordinates to zero along all axes.
302 * @return current point.
304 public Point3D zero() {