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