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.
12 public class Point2D implements Cloneable {
19 public Point2D(final double x, final double y) {
24 public Point2D(final Point2D parent) {
31 * Add other point to current point. Value of other point will not be changed.
33 * @return current point.
35 public Point2D add(final Point2D otherPoint) {
42 * @return true if current point coordinates are equal to zero.
44 public boolean isZero() {
45 return (x == 0) && (y == 0);
49 public Point2D clone() {
50 return new Point2D(this);
54 * Copy coordinates from other point to current point. Value of other point will not be changed.
56 public void clone(final Point2D otherPoint) {
62 * Set current point to middle of two other points.
64 * @param p1 first point.
65 * @param p2 second point.
66 * @return current point.
68 public Point2D setToMiddle(final Point2D p1, final Point2D p2) {
69 x = (p1.x + p2.x) / 2d;
70 y = (p1.y + p2.y) / 2d;
74 public double getAngleXY(final Point2D anotherPoint) {
75 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
79 * Compute distance to another point.
81 * @param anotherPoint point to compute distance to.
82 * @return distance from current point to another point.
84 public double getDistanceTo(final Point2D anotherPoint) {
85 final double xDiff = x - anotherPoint.x;
86 final double yDiff = y - anotherPoint.y;
88 return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
92 * Calculate length of vector.
94 * @return length of vector.
96 public double getVectorLength() {
97 return sqrt(((x * x) + (y * y)));
101 * Invert current point.
103 * @return current point.
105 public Point2D invert() {
112 * Round current point coordinates to integer.
114 public void roundToInteger() {
120 * Subtract other point from current point. Value of other point will not be changed.
122 * @return current point.
124 public Point2D subtract(final Point2D otherPoint) {
131 * Convert current point to 3D point.
132 * Value of the z coordinate will be set to zero.
136 public Point3D to3D() {
137 return new Point3D(x, y, 0);
141 * Set current point to zero.
143 * @return current point.
145 public Point2D zero() {
152 public String toString() {