Changed license to Creative Commons Zero (CC0).
[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 *
6  */
7
8 package eu.svjatoslav.sixth.e3d.geometry;
9
10 import static java.lang.Math.sqrt;
11
12 public class Point2D implements Cloneable {
13
14     public double x, y;
15
16     public Point2D() {
17     }
18
19     public Point2D(final double x, final double y) {
20         this.x = x;
21         this.y = y;
22     }
23
24     public Point2D(final Point2D source) {
25         x = source.x;
26         y = source.y;
27     }
28
29     public Point2D add(final Point2D direction) {
30         x += direction.x;
31         y += direction.y;
32         return this;
33     }
34
35     public boolean isZero() {
36         return (x == 0) && (y == 0);
37     }
38
39     @Override
40     public Point2D clone() {
41         return new Point2D(this);
42     }
43
44     public void clone(final Point2D source) {
45         x = source.x;
46         y = source.y;
47     }
48
49     public Point2D getMiddle(final Point2D p1, final Point2D p2) {
50         x = (p1.x + p2.x) / 2d;
51         y = (p1.y + p2.y) / 2d;
52         return this;
53     }
54
55     public double getAngleXY(final Point2D anotherPoint) {
56         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
57     }
58
59     /**
60      * Compute distance to another point.
61      */
62     public double getDistanceTo(final Point2D anotherPoint) {
63         final double xDiff = x - anotherPoint.x;
64         final double yDiff = y - anotherPoint.y;
65
66         return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
67     }
68
69     public double getVectorLength() {
70         return sqrt(((x * x) + (y * y)));
71     }
72
73     public Point2D invert() {
74         x = -x;
75         y = -y;
76         return this;
77     }
78
79     public void roundToInteger() {
80         x = (int) x;
81         y = (int) y;
82     }
83
84     public Point2D subtract(final Point2D point) {
85         x -= point.x;
86         y -= point.y;
87         return this;
88     }
89
90     public Point3D to3D() {
91         return new Point3D(x, y, 0);
92     }
93
94     public Point2D zero() {
95         x = 0;
96         y = 0;
97         return this;
98     }
99
100     @Override
101     public String toString() {
102         return "Point2D{" +
103                 "x=" + x +
104                 ", y=" + y +
105                 '}';
106     }
107 }