source: josm/trunk/src/org/openstreetmap/josm/command/RotateCommand.java@ 10250

Last change on this file since 10250 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • 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.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.Objects;
8
9import org.openstreetmap.josm.data.coor.EastNorth;
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12
13/**
14 * RotateCommand rotates a number of objects around their centre.
15 *
16 * @author Frederik Ramm
17 */
18public class RotateCommand extends TransformNodesCommand {
19
20 /**
21 * Pivot point
22 */
23 private final EastNorth pivot;
24
25 /**
26 * angle of rotation starting click to pivot
27 */
28 private final double startAngle;
29
30 /**
31 * computed rotation angle between starting click and current mouse pos
32 */
33 private double rotationAngle;
34
35 /**
36 * Creates a RotateCommand.
37 * Assign the initial object set, compute pivot point and inital rotation angle.
38 * @param objects objects to fetch nodes from
39 * @param currentEN cuurent eats/north
40 */
41 public RotateCommand(Collection<OsmPrimitive> objects, EastNorth currentEN) {
42 super(objects);
43
44 pivot = getNodesCenter();
45 startAngle = getAngle(currentEN);
46 rotationAngle = 0.0;
47
48 handleEvent(currentEN);
49 }
50
51 /**
52 * Get angle between the horizontal axis and the line formed by the pivot and given point.
53 * @param currentEN cuurent eats/north
54 * @return angle between the horizontal axis and the line formed by the pivot and given point
55 **/
56 protected final double getAngle(EastNorth currentEN) {
57 if (pivot == null)
58 return 0.0; // should never happen by contract
59 return Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north());
60 }
61
62 /**
63 * Compute new rotation angle and transform nodes accordingly.
64 */
65 @Override
66 public final void handleEvent(EastNorth currentEN) {
67 double currentAngle = getAngle(currentEN);
68 rotationAngle = currentAngle - startAngle;
69 transformNodes();
70 }
71
72 /**
73 * Rotate nodes.
74 */
75 @Override
76 protected void transformNodes() {
77 for (Node n : nodes) {
78 double cosPhi = Math.cos(rotationAngle);
79 double sinPhi = Math.sin(rotationAngle);
80 EastNorth oldEastNorth = oldStates.get(n).getEastNorth();
81 double x = oldEastNorth.east() - pivot.east();
82 double y = oldEastNorth.north() - pivot.north();
83 double nx = cosPhi * x + sinPhi * y + pivot.east();
84 double ny = -sinPhi * x + cosPhi * y + pivot.north();
85 n.setEastNorth(new EastNorth(nx, ny));
86 }
87 }
88
89 @Override
90 public String getDescriptionText() {
91 return trn("Rotate {0} node", "Rotate {0} nodes", nodes.size(), nodes.size());
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hash(super.hashCode(), pivot, startAngle, rotationAngle);
97 }
98
99 @Override
100 public boolean equals(Object obj) {
101 if (this == obj) return true;
102 if (obj == null || getClass() != obj.getClass()) return false;
103 if (!super.equals(obj)) return false;
104 RotateCommand that = (RotateCommand) obj;
105 return Double.compare(that.startAngle, startAngle) == 0 &&
106 Double.compare(that.rotationAngle, rotationAngle) == 0 &&
107 Objects.equals(pivot, that.pivot);
108 }
109}
Note: See TracBrowser for help on using the repository browser.