]> git.basschouten.com Git - openhab-addons.git/blob
8d3049af36f6a0cb90fb6a4717ae15c634f51b65
[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 package org.openhab.binding.nanoleaf.internal.layout.shape;
14
15 import java.awt.Graphics2D;
16 import java.awt.geom.Rectangle2D;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.nanoleaf.internal.layout.ImagePoint2D;
22 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
23 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
24
25 /**
26  * A triangular shape.
27  *
28  * @author Jørgen Austvik - Initial contribution
29  */
30 @NonNullByDefault
31 public class Triangle extends Shape {
32
33     public Triangle(ShapeType shapeType, int panelId, Point2D position, int orientation) {
34         super(shapeType, panelId, position, orientation);
35     }
36
37     @Override
38     public List<Point2D> generateOutline() {
39         int height = (int) (getShapeType().getSideLength() * Math.sqrt(3) / 2);
40         Point2D v1;
41         if (pointsUp()) {
42             v1 = new Point2D(0, height * 2 / 3);
43         } else {
44             v1 = new Point2D(0, -height * 2 / 3);
45         }
46
47         Point2D v2 = v1.rotate((2.0 / 3.0) * Math.PI);
48         Point2D v3 = v1.rotate((-2.0 / 3.0) * Math.PI);
49         return Arrays.asList(v1.move(getPosition()), v2.move(getPosition()), v3.move(getPosition()));
50     }
51
52     @Override
53     protected ImagePoint2D labelPosition(Graphics2D graphics, List<ImagePoint2D> outline) {
54         Point2D centroid = new Point2D((outline.get(0).getX() + outline.get(1).getX() + outline.get(2).getX()) / 3,
55                 (outline.get(0).getY() + outline.get(1).getY() + outline.get(2).getY()) / 3);
56
57         Rectangle2D rect = graphics.getFontMetrics().getStringBounds(Integer.toString(getPanelId()), graphics);
58         return new ImagePoint2D(centroid.getX() - (int) (rect.getWidth() / 2),
59                 centroid.getY() + (int) (rect.getHeight() / 2));
60     }
61
62     private boolean pointsUp() {
63         // Upward: even multiple of 60 degrees rotation
64         return ((getOrientation() / 60) % 2) == 0;
65     }
66 }