Code cleanup and formatting.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Point2D.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.geometry;
11
12 import static java.lang.Math.sqrt;
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 source) {
27         x = source.x;
28         y = source.y;
29     }
30
31     public Point2D add(final Point2D direction) {
32         x += direction.x;
33         y += direction.y;
34         return this;
35     }
36
37     public boolean isZero() {
38         return (x == 0) && (y == 0);
39     }
40
41     @Override
42     public Point2D clone() {
43         return new Point2D(this);
44     }
45
46     public void clone(final Point2D source) {
47         x = source.x;
48         y = source.y;
49     }
50
51     public Point2D getMiddle(final Point2D p1, final Point2D p2) {
52         x = (p1.x + p2.x) / 2d;
53         y = (p1.y + p2.y) / 2d;
54         return this;
55     }
56
57     public double getAngleXY(final Point2D anotherPoint) {
58         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
59     }
60
61     /**
62      * Compute distance to another point.
63      */
64     public double getDistanceTo(final Point2D anotherPoint) {
65         final double xDiff = x - anotherPoint.x;
66         final double yDiff = y - anotherPoint.y;
67
68         return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
69     }
70
71     public double getVectorLength() {
72         return sqrt(((x * x) + (y * y)));
73     }
74
75     public Point2D invert() {
76         x = -x;
77         y = -y;
78         return this;
79     }
80
81     public void roundToInteger() {
82         x = (int) x;
83         y = (int) y;
84     }
85
86     public Point2D subtract(final Point2D point) {
87         x -= point.x;
88         y -= point.y;
89         return this;
90     }
91
92     public Point3D to3D() {
93         return new Point3D(x, y, 0);
94     }
95
96     public Point2D zero() {
97         x = 0;
98         y = 0;
99         return this;
100     }
101
102 }