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.sqrt;
10 * Used to represent point in a 2D space or vector.
14 public class Point2D implements Cloneable {
21 public Point2D(final double x, final double y) {
26 public Point2D(final Point2D parent) {
33 * Add other point to current point. Value of other point will not be changed.
35 * @return current point.
37 public Point2D add(final Point2D otherPoint) {
44 * @return true if current point coordinates are equal to zero.
46 public boolean isZero() {
47 return (x == 0) && (y == 0);
51 public Point2D clone() {
52 return new Point2D(this);
56 * Copy coordinates from other point to current point. Value of other point will not be changed.
58 public void clone(final Point2D otherPoint) {
64 * Set current point to middle of two other points.
66 * @param p1 first point.
67 * @param p2 second point.
68 * @return current point.
70 public Point2D setToMiddle(final Point2D p1, final Point2D p2) {
71 x = (p1.x + p2.x) / 2d;
72 y = (p1.y + p2.y) / 2d;
76 public double getAngleXY(final Point2D anotherPoint) {
77 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
81 * Compute distance to another point.
83 * @param anotherPoint point to compute distance to.
84 * @return distance from current point to another point.
86 public double getDistanceTo(final Point2D anotherPoint) {
87 final double xDiff = x - anotherPoint.x;
88 final double yDiff = y - anotherPoint.y;
90 return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
94 * Calculate length of vector.
96 * @return length of vector.
98 public double getVectorLength() {
99 return sqrt(((x * x) + (y * y)));
103 * Invert current point.
105 * @return current point.
107 public Point2D invert() {
114 * Round current point coordinates to integer.
116 public void roundToInteger() {
122 * Subtract other point from current point. Value of other point will not be changed.
124 * @return current point.
126 public Point2D subtract(final Point2D otherPoint) {
133 * Convert current point to 3D point.
134 * Value of the z coordinate will be set to zero.
138 public Point3D to3D() {
139 return new Point3D(x, y, 0);
143 * Set current point to zero.
145 * @return current point.
147 public Point2D zero() {
154 public String toString() {