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

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

javadoc fixes

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