Changeset 389 in josm for trunk/src


Ignore:
Timestamp:
2007-10-15T01:34:39+02:00 (18 years ago)
Author:
framm
Message:
  • when new node inserted in exactly two segments at the same time, adjust node position to intersection if within 10px of click.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r388 r389  
    1313import java.util.HashMap;
    1414import java.util.HashSet;
     15import java.util.Iterator;
    1516import java.util.Map;
    1617import java.util.Set;
     
    224225        }
    225226
     227        /*
     228         * Adjusts the position of a node to lie on a segment (or a segment
     229         * intersection).
     230         *
     231         * If one or more than two segments are passed, the node is adjusted
     232         * to lie on the first segment that is passed.
     233         *
     234         * If two segments are passed, the node is adjusted to be at their
     235         * intersection.
     236         *
     237         * No action is taken if no segments are passed.
     238         *
     239         * @param segs the segments to use as a reference when adjusting
     240         * @param n the node to adjust
     241         */
    226242        private static void adjustNode(Collection<NodePair> segs, Node n) {
    227                 // FIXME: find intersection if more than one seg.
    228                 if (segs.size() >= 1) {
    229                         EastNorth P = n.eastNorth;
    230                         NodePair seg = segs.iterator().next();
     243               
     244                switch (segs.size()) {
     245                case 0:
     246                        return;
     247                case 2:
     248                        // algorithm used here is a bit clumsy, anyone's welcome to replace
     249                        // it by something else. All it does it compute the intersection between
     250                        // the two segments and adjust the node position. The code doesnt
     251                        Iterator<NodePair> i = segs.iterator();
     252                        NodePair seg = i.next();
    231253                        EastNorth A = seg.a.eastNorth;
    232254                        EastNorth B = seg.b.eastNorth;
     255                        seg = i.next();
     256                        EastNorth C = seg.a.eastNorth;
     257                        EastNorth D = seg.b.eastNorth;
     258
     259                        EastNorth intersection = new EastNorth(
     260                                det(det(A.east(), A.north(), B.east(), B.north()), A.east() - B.east(),
     261                                        det(C.east(), C.north(), D.east(), D.north()), C.east() - D.east())/
     262                                        det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north()),
     263                                det(det(A.east(), A.north(), B.east(), B.north()), A.north() - B.north(),
     264                                        det(C.east(), C.north(), D.east(), D.north()), C.north() - D.north())/
     265                                        det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north())
     266                        );
     267                       
     268                        // only adjust to intersection if within 10 pixel of mouse click; otherwise
     269                        // fall through to default action.
     270                        // (for semi-parallel lines, intersection might be miles away!)
     271                        if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < 10) {
     272                                n.eastNorth = intersection;
     273                                return;
     274                        }
     275               
     276                default:
     277                        EastNorth P = n.eastNorth;
     278                        seg = segs.iterator().next();
     279                        A = seg.a.eastNorth;
     280                        B = seg.b.eastNorth;
    233281                        double a = P.distance(B);
    234282                        double b = P.distance(A);
     
    241289        }
    242290       
     291        // helper for adjustNode
     292        static double det(double a, double b, double c, double d)
     293        {
     294                return a * d - b * c;
     295        }
     296
     297       
    243298        @Override public String getModeHelpText() {
    244299                return "Click to add a new node. Ctrl: no node re-use/auto-insert. Shift: no auto-connect. Alt: new way";
Note: See TracChangeset for help on using the changeset viewer.