2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.geometry;
10 import static java.lang.Math.*;
13 * Used to represent point in a 3D space or vector.
16 public class Point3D implements Cloneable {
18 public double x, y, z;
23 public Point3D(final double x, final double y, final double z) {
29 public Point3D(final float x, final float y, final float z) {
35 public Point3D(final int x, final int y, final int z) {
41 public Point3D(final Point3D parentPoint) {
47 public Point3D add(final Point3D direction) {
54 public Point3D clone() {
55 return new Point3D(this);
58 public Point3D clone(final Point3D source) {
65 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
66 x = (p1.x + p2.x) / 2d;
67 y = (p1.y + p2.y) / 2d;
68 z = (p1.z + p2.z) / 2d;
72 public boolean isZero() {
73 return (x == 0) && (y == 0) && (z == 0);
76 public double getAngleXZ(final Point3D anotherPoint) {
77 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
80 public double getAngleYZ(final Point3D anotherPoint) {
81 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
84 public double getAngleXY(final Point3D anotherPoint) {
85 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
89 * Compute distance to another point.
91 public double getDistanceTo(final Point3D anotherPoint) {
92 final double xDelta = x - anotherPoint.x;
93 final double yDelta = y - anotherPoint.y;
94 final double zDelta = z - anotherPoint.z;
96 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
99 public double getVectorLength() {
100 return sqrt(((x * x) + (y * y) + (z * z)));
103 public Point3D invert() {
110 public void rotate(final Point3D center, final double angleXZ,
111 final double angleYZ) {
112 final double s1 = sin(angleXZ);
113 final double c1 = cos(angleXZ);
115 final double s2 = sin(angleYZ);
116 final double c2 = cos(angleYZ);
122 final double y1 = (z * s2) + (y * c2);
123 final double z1 = (z * c2) - (y * s2);
125 final double x1 = (z1 * s1) + (x * c1);
126 final double z2 = (z1 * c1) - (x * s1);
133 public void roundToInteger() {
139 public Point3D scaleDown(final double factor) {
146 public Point3D scaleUp(final double factor) {
153 public void setValues(final double x, final double y, final double z) {
159 public Point3D subtract(final Point3D direction) {
167 public String toString() {
168 return "x:" + x + " y:" + y + " z:" + z;
171 public Point3D translateX(final double xIncrement) {
176 public Point3D translateY(final double yIncrement) {
181 public Point3D translateZ(final double zIncrement) {
186 public boolean isVisible() {
191 // if ((z > 0) && (x > -1000) && (y > -1000) && (x < 2000) && (y <
198 public Point3D zero() {