]> git.basschouten.com Git - openhab-addons.git/blob
43dd347b99bf9737e25a2a24aaa9c417aac7c921
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13
14 package org.openhab.binding.nanoleaf.internal.layout;
15
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17
18 /**
19  * Coordinate in 2D space.
20  *
21  * @author Jørgen Austvik - Initial contribution
22  */
23 @NonNullByDefault
24 public class Point2D {
25     private final int x;
26     private final int y;
27
28     public Point2D(int x, int y) {
29         this.x = x;
30         this.y = y;
31     }
32
33     public int getX() {
34         return x;
35     }
36
37     public int getY() {
38         return y;
39     }
40
41     /**
42      * Rotates the point a given amount of radians.
43      * 
44      * @param radians The amount to rotate the point
45      * @return A new point which is rotated
46      */
47     public Point2D rotate(double radians) {
48         double sinAngle = Math.sin(radians);
49         double cosAngle = Math.cos(radians);
50
51         int newX = (int) (cosAngle * x - sinAngle * y);
52         int newY = (int) (sinAngle * x + cosAngle * y);
53         return new Point2D(newX, newY);
54     }
55
56     /**
57      * Move the point in x and y direction.
58      *
59      * @param moveX Amount to move in x direction
60      * @param moveY Amount to move in y direction
61      * @return
62      */
63     public Point2D move(int moveX, int moveY) {
64         return new Point2D(getX() + moveX, getY() + moveY);
65     }
66
67     /**
68      * Move the point in x and y direction,.
69      *
70      * @param offset Offset to move
71      * @return
72      */
73     public Point2D move(Point2D offset) {
74         return move(offset.getX(), offset.getY());
75     }
76
77     @Override
78     public String toString() {
79         return String.format("x:%d, y:%d", x, y);
80     }
81 }