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 {
15 public double x, y, z;
20 public Point3D(final double x, final double y, final double z) {
26 public Point3D(final float x, final float y, final float z) {
32 public Point3D(final int x, final int y, final int z) {
39 * Creates new current point by cloning coordinates from parent point.
41 public Point3D(final Point3D parent) {
48 * Add other point to current point.
50 * @return current point.
52 public Point3D add(final Point3D otherPoint) {
59 public Point3D clone() {
60 return new Point3D(this);
63 public Point3D clone(final Point3D parent) {
70 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
71 x = (p1.x + p2.x) / 2d;
72 y = (p1.y + p2.y) / 2d;
73 z = (p1.z + p2.z) / 2d;
77 public boolean isZero() {
78 return (x == 0) && (y == 0) && (z == 0);
81 public double getAngleXZ(final Point3D anotherPoint) {
82 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
85 public double getAngleYZ(final Point3D anotherPoint) {
86 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
89 public double getAngleXY(final Point3D anotherPoint) {
90 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
94 * Compute distance to another point.
96 public double getDistanceTo(final Point3D anotherPoint) {
97 final double xDelta = x - anotherPoint.x;
98 final double yDelta = y - anotherPoint.y;
99 final double zDelta = z - anotherPoint.z;
101 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
104 public double getVectorLength() {
105 return sqrt(((x * x) + (y * y) + (z * z)));
108 public Point3D invert() {
115 public void rotate(final Point3D center, final double angleXZ,
116 final double angleYZ) {
117 final double s1 = sin(angleXZ);
118 final double c1 = cos(angleXZ);
120 final double s2 = sin(angleYZ);
121 final double c2 = cos(angleYZ);
127 final double y1 = (z * s2) + (y * c2);
128 final double z1 = (z * c2) - (y * s2);
130 final double x1 = (z1 * s1) + (x * c1);
131 final double z2 = (z1 * c1) - (x * s1);
138 public void roundToInteger() {
144 public Point3D scaleDown(final double factor) {
151 public Point3D scaleUp(final double factor) {
158 public void setValues(final double x, final double y, final double z) {
165 * Subtract other point from current point.
167 * @return current point.
169 public Point3D subtract(final Point3D otherPoint) {
177 public String toString() {
178 return "x:" + x + " y:" + y + " z:" + z;
182 * @return current point.
184 public Point3D translateX(final double xIncrement) {
190 * @return current point.
192 public Point3D translateY(final double yIncrement) {
198 * @return current point.
200 public Point3D translateZ(final double zIncrement) {
206 * Here we assume that Z coordinate is distance to the viewer.
207 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
209 * @return point visibility status.
211 public boolean isVisible() {
216 * Resets point to 0 coordinate in X, Y and Z axis.
218 * @return current point.
220 public Point3D zero() {