Refactoring.
[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 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     @Override
36     public Point2D clone() {
37         return new Point2D(this);
38     }
39
40     public void clone(final Point2D source) {
41         x = source.x;
42         y = source.y;
43     }
44
45     public Point2D computeMiddlePoint(final Point2D p1, final Point2D p2) {
46         x = (p1.x + p2.x) / 2d;
47         y = (p1.y + p2.y) / 2d;
48         return this;
49     }
50
51     public double getAngleXY(final Point2D anotherPoint) {
52         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
53     }
54
55     public double getDistanceTo(final Point2D anotherPoint) {
56         final double xDiff = x - anotherPoint.x;
57         final double yDiff = y - anotherPoint.y;
58
59         return Math.sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
60     }
61
62     public Point2D invert() {
63         x = -x;
64         y = -y;
65         return this;
66     }
67
68     public void roundToInteger() {
69         x = (int) x;
70         y = (int) y;
71     }
72
73     public Point2D subtract(final Point2D direction) {
74         x -= direction.x;
75         y -= direction.y;
76         return this;
77     }
78
79     public Point3D to3D() {
80         return new Point3D(x, y, 0);
81     }
82
83     public Point2D zero() {
84         x = 0;
85         y = 0;
86         return this;
87     }
88
89 }