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