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 void 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);
174 * Round current point coordinates to integer values.
176 public void roundToInteger() {
183 * Scale down current point by factor.
184 * All coordinates will be divided by factor.
186 * @param factor factor to scale by.
187 * @return current point.
189 public Point3D scaleDown(final double factor) {
197 * Scale up current point by factor.
198 * All coordinates will be multiplied by factor.
200 * @param factor factor to scale by.
201 * @return current point.
203 public Point3D scaleUp(final double factor) {
211 * Set current point coordinates to given values.
212 * @param x X coordinate.
213 * @param y Y coordinate.
214 * @param z Z coordinate.
216 public void setValues(final double x, final double y, final double z) {
223 * Subtract other point from current point. Value of other point will not be changed.
225 * @return current point.
227 public Point3D subtract(final Point3D otherPoint) {
235 public String toString() {
236 return "x:" + x + " y:" + y + " z:" + z;
240 * Translate current point along X axis by given increment.
242 * @return current point.
244 public Point3D translateX(final double xIncrement) {
250 * Translate current point along Y axis by given increment.
252 * @return current point.
254 public Point3D translateY(final double yIncrement) {
260 * Translate current point along Z axis by given increment.
262 * @return current point.
264 public Point3D translateZ(final double zIncrement) {
270 * Here we assume that Z coordinate is distance to the viewer.
271 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
273 * @return point visibility status.
275 public boolean isVisible() {
280 * Resets point coordinates to zero along all axes.
282 * @return current point.
284 public Point3D zero() {