source: josm/trunk/src/org/openstreetmap/josm/gui/draw/SymbolShape.java@ 12213

Last change on this file since 12213 was 10875, checked in by Don-vip, 8 years ago

fix #13413 - Clean ImproveWayAccuracyAction, add new class MapViewPath (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.draw;
3
4import java.awt.Shape;
5import java.awt.geom.Ellipse2D;
6import java.awt.geom.GeneralPath;
7import java.awt.geom.Rectangle2D;
8import java.util.Optional;
9import java.util.stream.Stream;
10
11/**
12 * A list of possible symbol shapes.
13 * @since 10875
14 */
15public enum SymbolShape {
16 /**
17 * A square
18 */
19 SQUARE("square", 4, Math.PI / 4),
20 /**
21 * A circle
22 */
23 CIRCLE("circle", 1, 0),
24 /**
25 * A triangle with sides of equal lengh
26 */
27 TRIANGLE("triangle", 3, Math.PI / 2),
28 /**
29 * A pentagon
30 */
31 PENTAGON("pentagon", 5, Math.PI / 2),
32 /**
33 * A hexagon
34 */
35 HEXAGON("hexagon", 6, 0),
36 /**
37 * A heptagon
38 */
39 HEPTAGON("heptagon", 7, Math.PI / 2),
40 /**
41 * An octagon
42 */
43 OCTAGON("octagon", 8, Math.PI / 8),
44 /**
45 * a nonagon
46 */
47 NONAGON("nonagon", 9, Math.PI / 2),
48 /**
49 * A decagon
50 */
51 DECAGON("decagon", 10, 0);
52
53 private final String name;
54 final int sides;
55
56 final double rotation;
57
58 SymbolShape(String name, int sides, double rotation) {
59 this.name = name;
60 this.sides = sides;
61 this.rotation = rotation;
62 }
63
64 /**
65 * Create the path for this shape around the given position
66 * @param x The x position
67 * @param y The y position
68 * @param size The size (width for rect, diameter for rest)
69 * @return The symbol.
70 * @since 10875
71 */
72 public Shape shapeAround(double x, double y, double size) {
73 double radius = size / 2;
74 Shape shape;
75 switch (this) {
76 case SQUARE:
77 // optimize for performance reasons
78 shape = new Rectangle2D.Double(x - radius, y - radius, size, size);
79 break;
80 case CIRCLE:
81 shape = new Ellipse2D.Double(x - radius, y - radius, size, size);
82 break;
83 default:
84 shape = buildPolygon(x, y, radius);
85 break;
86 }
87 return shape;
88 }
89
90 private Shape buildPolygon(double cx, double cy, double radius) {
91 GeneralPath polygon = new GeneralPath();
92 for (int i = 0; i < sides; i++) {
93 double angle = ((2 * Math.PI / sides) * i) - rotation;
94 double x = cx + radius * Math.cos(angle);
95 double y = cy + radius * Math.sin(angle);
96 if (i == 0) {
97 polygon.moveTo(x, y);
98 } else {
99 polygon.lineTo(x, y);
100 }
101 }
102 polygon.closePath();
103 return polygon;
104 }
105
106 /**
107 * Gets the number of normally straight sides this symbol has. Returns 1 for a circle.
108 * @return The sides of the symbol
109 */
110 public int getSides() {
111 return sides;
112 }
113
114 /**
115 * Gets the rotation of the first point of this symbol.
116 * @return The rotation
117 */
118 public double getRotation() {
119 return rotation;
120 }
121
122 /**
123 * Get the MapCSS name for this shape
124 * @return The name
125 */
126 public String getName() {
127 return name;
128 }
129
130 /**
131 * Get the shape with the given name
132 * @param val The name to search
133 * @return The shape as optional
134 */
135 public static Optional<SymbolShape> forName(String val) {
136 return Stream.of(values()).filter(shape -> val.equals(shape.name)).findAny();
137 }
138}
Note: See TracBrowser for help on using the repository browser.