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

Last change on this file since 7448 was 6890, checked in by Don-vip, 10 years ago

fix some Sonar issues (Constructor Calls Overridable Method)

  • Property svn:eol-style set to native
File size: 2.7 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 javax.swing.Icon;
9
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.tools.ImageProvider;
14
15/**
16 * RotateCommand rotates a number of objects around their centre.
17 *
18 * @author Frederik Ramm
19 */
20public class RotateCommand extends TransformNodesCommand {
21
22 /**
23 * Pivot point
24 */
25 private EastNorth pivot;
26
27 /**
28 * angle of rotation starting click to pivot
29 */
30 private double startAngle = 0.0;
31
32 /**
33 * computed rotation angle between starting click and current mouse pos
34 */
35 private double rotationAngle = 0.0;
36
37 /**
38 * Creates a RotateCommand.
39 * Assign the initial object set, compute pivot point and inital rotation angle.
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 give points.
53 **/
54 protected final double getAngle(EastNorth currentEN) {
55 if ( pivot == null )
56 return 0.0; // should never happen by contract
57 return Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north());
58 }
59
60 /**
61 * Compute new rotation angle and transform nodes accordingly.
62 */
63 @Override
64 public final void handleEvent(EastNorth currentEN) {
65 double currentAngle = getAngle(currentEN);
66 rotationAngle = currentAngle - startAngle;
67 transformNodes();
68 }
69
70 /**
71 * Rotate nodes.
72 */
73 @Override
74 protected void transformNodes() {
75 for (Node n : nodes) {
76 double cosPhi = Math.cos(rotationAngle);
77 double sinPhi = Math.sin(rotationAngle);
78 EastNorth oldEastNorth = oldStates.get(n).eastNorth;
79 double x = oldEastNorth.east() - pivot.east();
80 double y = oldEastNorth.north() - pivot.north();
81 double nx = cosPhi * x + sinPhi * y + pivot.east();
82 double ny = -sinPhi * x + cosPhi * y + pivot.north();
83 n.setEastNorth(new EastNorth(nx, ny));
84 }
85 }
86
87 @Override
88 public String getDescriptionText() {
89 return trn("Rotate {0} node", "Rotate {0} nodes", nodes.size(), nodes.size());
90 }
91
92 @Override
93 public Icon getDescriptionIcon() {
94 return ImageProvider.get("data", "node");
95 }
96}
Note: See TracBrowser for help on using the repository browser.