source: josm/trunk/src/org/openstreetmap/josm/actions/AlignInLineAction.java@ 1750

Last change on this file since 1750 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9import java.util.LinkedList;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.command.Command;
15import org.openstreetmap.josm.command.MoveCommand;
16import org.openstreetmap.josm.command.SequenceCommand;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * Aligns all selected nodes into a straight line (useful for
24 * roads that should be straight, but have side roads and
25 * therefore need multiple nodes)
26 *
27 * @author Matthew Newton
28 */
29public final class AlignInLineAction extends JosmAction {
30
31 public AlignInLineAction() {
32 super(tr("Align Nodes in Line"), "alignline", tr("Move the selected nodes in to a line."),
33 Shortcut.registerShortcut("tools:alignline", tr("Tool: {0}", tr("Align Nodes in Line")), KeyEvent.VK_L, Shortcut.GROUP_EDIT), true);
34 }
35
36 /**
37 * The general algorithm here is to find the two selected nodes
38 * that are furthest apart, and then to align all other selected
39 * nodes onto the straight line between these nodes.
40 */
41 public void actionPerformed(ActionEvent e) {
42 Collection<OsmPrimitive> sel = Main.ds.getSelected();
43 Collection<Node> nodes = new LinkedList<Node>();
44 Collection<Node> itnodes = new LinkedList<Node>();
45 for (OsmPrimitive osm : sel)
46 if (osm instanceof Node) {
47 nodes.add((Node)osm);
48 itnodes.add((Node)osm);
49 }
50 // special case if no single nodes are selected and exactly one way is:
51 // then use the way's nodes
52 if ((nodes.size() == 0) && (sel.size() == 1))
53 for (OsmPrimitive osm : sel)
54 if (osm instanceof Way) {
55 nodes.addAll(((Way)osm).nodes);
56 itnodes.addAll(((Way)osm).nodes);
57 }
58 if (nodes.size() < 3) {
59 JOptionPane.showMessageDialog(Main.parent, tr("Please select at least three nodes."));
60 return;
61 }
62
63 // Find from the selected nodes two that are the furthest apart.
64 // Let's call them A and B.
65 double distance = 0;
66
67 Node nodea = null;
68 Node nodeb = null;
69
70 for (Node n : nodes) {
71 itnodes.remove(n);
72 for (Node m : itnodes) {
73 double dist = Math.sqrt(n.getEastNorth().distance(m.getEastNorth()));
74 if (dist > distance) {
75 nodea = n;
76 nodeb = m;
77 distance = dist;
78 }
79 }
80 }
81
82 // Remove the nodes A and B from the list of nodes to move
83 nodes.remove(nodea);
84 nodes.remove(nodeb);
85
86 // Find out co-ords of A and B
87 double ax = nodea.getEastNorth().east();
88 double ay = nodea.getEastNorth().north();
89 double bx = nodeb.getEastNorth().east();
90 double by = nodeb.getEastNorth().north();
91
92 // A list of commands to do
93 Collection<Command> cmds = new LinkedList<Command>();
94
95 // OK, for each node to move, work out where to move it!
96 for (Node n : nodes) {
97 // Get existing co-ords of node to move
98 double nx = n.getEastNorth().east();
99 double ny = n.getEastNorth().north();
100
101 if (ax == bx) {
102 // Special case if AB is vertical...
103 nx = ax;
104 } else if (ay == by) {
105 // ...or horizontal
106 ny = ay;
107 } else {
108 // Otherwise calculate position by solving y=mx+c
109 double m1 = (by - ay) / (bx - ax);
110 double c1 = ay - (ax * m1);
111 double m2 = (-1) / m1;
112 double c2 = n.getEastNorth().north() - (n.getEastNorth().east() * m2);
113
114 nx = (c2 - c1) / (m1 - m2);
115 ny = (m1 * nx) + c1;
116 }
117
118 // Add the command to move the node to its new position.
119 cmds.add(new MoveCommand(n, nx - n.getEastNorth().east(), ny - n.getEastNorth().north() ));
120 }
121
122 // Do it!
123 Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Line"), cmds));
124 Main.map.repaint();
125 }
126}
Note: See TracBrowser for help on using the repository browser.