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.
93 * @return length of vector.
95 public double getVectorLength() {
96 return sqrt(((x * x) + (y * y)));
100 * Invert current point.
102 * @return current point.
104 public Point2D invert() {
111 * Round current point coordinates to integer.
113 public void roundToInteger() {
119 * Subtract other point from current point. Value of other point will not be changed.
121 * @return current point.
123 public Point2D subtract(final Point2D otherPoint) {
130 * Convert current point to 3D point.
131 * Value of the z coordinate will be set to zero.
135 public Point3D to3D() {
136 return new Point3D(x, y, 0);
140 * Set current point to zero.
142 * @return current point.
144 public Point2D zero() {
151 public String toString() {