Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Point2D.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.geometry;
6
7 import static java.lang.Math.sqrt;
8
9 /**
10  * Used to represent point in a 2D space or vector.
11  *
12  * @see Point3D
13  */
14 public class Point2D implements Cloneable {
15
16     public double x, y;
17
18     public Point2D() {
19     }
20
21     public Point2D(final double x, final double y) {
22         this.x = x;
23         this.y = y;
24     }
25
26     public Point2D(final Point2D parent) {
27         x = parent.x;
28         y = parent.y;
29     }
30
31
32     /**
33      * Add other point to current point. Value of other point will not be changed.
34      *
35      * @return current point.
36      */
37     public Point2D add(final Point2D otherPoint) {
38         x += otherPoint.x;
39         y += otherPoint.y;
40         return this;
41     }
42
43     /**
44      * @return true if current point coordinates are equal to zero.
45      */
46     public boolean isZero() {
47         return (x == 0) && (y == 0);
48     }
49
50     @Override
51     public Point2D clone() {
52         return new Point2D(this);
53     }
54
55     /**
56      * Copy coordinates from other point to current point. Value of other point will not be changed.
57      */
58     public void clone(final Point2D otherPoint) {
59         x = otherPoint.x;
60         y = otherPoint.y;
61     }
62
63     /**
64      * Set current point to middle of two other points.
65      *
66      * @param p1 first point.
67      * @param p2 second point.
68      * @return current point.
69      */
70     public Point2D setToMiddle(final Point2D p1, final Point2D p2) {
71         x = (p1.x + p2.x) / 2d;
72         y = (p1.y + p2.y) / 2d;
73         return this;
74     }
75
76     public double getAngleXY(final Point2D anotherPoint) {
77         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
78     }
79
80     /**
81      * Compute distance to another point.
82      *
83      * @param anotherPoint point to compute distance to.
84      * @return distance from current point to another point.
85      */
86     public double getDistanceTo(final Point2D anotherPoint) {
87         final double xDiff = x - anotherPoint.x;
88         final double yDiff = y - anotherPoint.y;
89
90         return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
91     }
92
93     /**
94      * Calculate length of vector.
95      *
96      * @return length of vector.
97      */
98     public double getVectorLength() {
99         return sqrt(((x * x) + (y * y)));
100     }
101
102     /**
103      * Invert current point.
104      *
105      * @return current point.
106      */
107     public Point2D invert() {
108         x = -x;
109         y = -y;
110         return this;
111     }
112
113     /**
114      * Round current point coordinates to integer.
115      */
116     public void roundToInteger() {
117         x = (int) x;
118         y = (int) y;
119     }
120
121     /**
122      * Subtract other point from current point. Value of other point will not be changed.
123      *
124      * @return current point.
125      */
126     public Point2D subtract(final Point2D otherPoint) {
127         x -= otherPoint.x;
128         y -= otherPoint.y;
129         return this;
130     }
131
132     /**
133      * Convert current point to 3D point.
134      * Value of the z coordinate will be set to zero.
135      *
136      * @return 3D point.
137      */
138     public Point3D to3D() {
139         return new Point3D(x, y, 0);
140     }
141
142     /**
143      * Set current point to zero.
144      *
145      * @return current point.
146      */
147     public Point2D zero() {
148         x = 0;
149         y = 0;
150         return this;
151     }
152
153     @Override
154     public String toString() {
155         return "Point2D{" +
156                 "x=" + x +
157                 ", y=" + y +
158                 '}';
159     }
160 }