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) {
38 public Point3D(final Point3D parentPoint) {
44 public Point3D add(final Point3D direction) {
51 public Point3D clone() {
52 return new Point3D(this);
55 public Point3D clone(final Point3D source) {
62 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
63 x = (p1.x + p2.x) / 2d;
64 y = (p1.y + p2.y) / 2d;
65 z = (p1.z + p2.z) / 2d;
69 public boolean isZero() {
70 return (x == 0) && (y == 0) && (z == 0);
73 public double getAngleXZ(final Point3D anotherPoint) {
74 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
77 public double getAngleYZ(final Point3D anotherPoint) {
78 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
81 public double getAngleXY(final Point3D anotherPoint) {
82 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
86 * Compute distance to another point.
88 public double getDistanceTo(final Point3D anotherPoint) {
89 final double xDelta = x - anotherPoint.x;
90 final double yDelta = y - anotherPoint.y;
91 final double zDelta = z - anotherPoint.z;
93 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
96 public double getVectorLength() {
97 return sqrt(((x * x) + (y * y) + (z * z)));
100 public Point3D invert() {
107 public void rotate(final Point3D center, final double angleXZ,
108 final double angleYZ) {
109 final double s1 = sin(angleXZ);
110 final double c1 = cos(angleXZ);
112 final double s2 = sin(angleYZ);
113 final double c2 = cos(angleYZ);
119 final double y1 = (z * s2) + (y * c2);
120 final double z1 = (z * c2) - (y * s2);
122 final double x1 = (z1 * s1) + (x * c1);
123 final double z2 = (z1 * c1) - (x * s1);
130 public void roundToInteger() {
136 public Point3D scaleDown(final double factor) {
143 public Point3D scaleUp(final double factor) {
150 public void setValues(final double x, final double y, final double z) {
156 public Point3D subtract(final Point3D direction) {
164 public String toString() {
165 return "x:" + x + " y:" + y + " z:" + z;
168 public Point3D translateX(final double xIncrement) {
173 public Point3D translateY(final double yIncrement) {
178 public Point3D translateZ(final double zIncrement) {
183 public boolean isVisible() {
188 // if ((z > 0) && (x > -1000) && (y > -1000) && (x < 2000) && (y <
195 public Point3D zero() {