2 * Sixth 3D engine. Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.geometry;
12 import static java.lang.Math.*;
15 * Used to represent point in a 3D space or vector.
18 public class Point3D implements Cloneable {
20 public double x, y, z;
25 public Point3D(final double x, final double y, final double z) {
31 public Point3D(final float x, final float y, final float z) {
37 public Point3D(final int x, final int y, final int z) {
43 public Point3D(final Point3D parentPoint) {
49 public Point3D add(final Point3D direction) {
56 public Point3D clone() {
57 return new Point3D(this);
60 public Point3D clone(final Point3D source) {
67 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
68 x = (p1.x + p2.x) / 2d;
69 y = (p1.y + p2.y) / 2d;
70 z = (p1.z + p2.z) / 2d;
74 public boolean isZero() {
75 return (x == 0) && (y == 0) && (z == 0);
78 public double getAngleXZ(final Point3D anotherPoint) {
79 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
82 public double getAngleYZ(final Point3D anotherPoint) {
83 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
86 public double getAngleXY(final Point3D anotherPoint) {
87 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
91 * Compute distance to another point.
93 public double getDistanceTo(final Point3D anotherPoint) {
94 final double xDelta = x - anotherPoint.x;
95 final double yDelta = y - anotherPoint.y;
96 final double zDelta = z - anotherPoint.z;
98 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
101 public double getVectorLength() {
102 return sqrt(((x * x) + (y * y) + (z * z)));
105 public Point3D invert() {
112 public void rotate(final Point3D center, final double angleXZ,
113 final double angleYZ) {
114 final double s1 = sin(angleXZ);
115 final double c1 = cos(angleXZ);
117 final double s2 = sin(angleYZ);
118 final double c2 = cos(angleYZ);
124 final double y1 = (z * s2) + (y * c2);
125 final double z1 = (z * c2) - (y * s2);
127 final double x1 = (z1 * s1) + (x * c1);
128 final double z2 = (z1 * c1) - (x * s1);
135 public void roundToInteger() {
141 public Point3D scaleDown(final double factor) {
148 public Point3D scaleUp(final double factor) {
155 public void setValues(final double x, final double y, final double z) {
161 public Point3D subtract(final Point3D direction) {
169 public String toString() {
170 return "x:" + x + " y:" + y + " z:" + z;
173 public Point3D translateX(final double xIncrement) {
178 public Point3D translateY(final double yIncrement) {
183 public Point3D translateZ(final double zIncrement) {
188 public boolean isVisible() {
193 // if ((z > 0) && (x > -1000) && (y > -1000) && (x < 2000) && (y <
200 public Point3D zero() {