Ticket #691: intersection.patch

File intersection.patch, 3.0 KB (added by zorkos@…, 15 years ago)
  • DrawAction.java

     
    313313                case 0:
    314314                        return;
    315315                case 2:
    316                         // algorithm used here is a bit clumsy, anyone's welcome to replace
    317                         // it by something else. All it does it compute the intersection between
    318                         // the two segments and adjust the node position. The code doesnt
     316                        // This computes the intersection between
     317                        // the two segments and adjusts the node position.
    319318                        Iterator<Pair<Node,Node>> i = segs.iterator();
    320319                        Pair<Node,Node> seg = i.next();
    321320                        EastNorth A = seg.a.eastNorth;
     
    324323                        EastNorth C = seg.a.eastNorth;
    325324                        EastNorth D = seg.b.eastNorth;
    326325
     326                        double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
     327                       
     328                        // Check for parallel segments and do nothing if they are
     329                        // In practice this will probably only happen when a way has been duplicated
     330                       
     331                        if (u == 0) return;
     332                       
     333                        // q is a number between 0 and 1
     334                        // It is the point in the segment where the intersection occurs
     335                        // if the segment is scaled to lenght 1
     336                       
     337                        double q=det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east())
     338                                 / u;
    327339                        EastNorth intersection = new EastNorth(
    328                                 det(det(A.east(), A.north(), B.east(), B.north()), A.east() - B.east(),
    329                                         det(C.east(), C.north(), D.east(), D.north()), C.east() - D.east())/
    330                                         det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north()),
    331                                 det(det(A.east(), A.north(), B.east(), B.north()), A.north() - B.north(),
    332                                         det(C.east(), C.north(), D.east(), D.north()), C.north() - D.north())/
    333                                         det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north())
    334                         );
     340                                        B.east() + q * (A.east() - B.east()),
     341                                        B.north() + q * (A.north() - B.north()));
     342
     343                        int snapToIntersectionThreshold=0;
     344                        try { snapToIntersectionThreshold = Integer.parseInt(Main.pref.get("edit.snap-intersection-threshold","10")); } catch (NumberFormatException x) {}
    335345                       
    336                         // only adjust to intersection if within 10 pixel of mouse click; otherwise
     346                        // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
    337347                        // fall through to default action.
    338348                        // (for semi-parallel lines, intersection might be miles away!)
    339                         if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < 10) {
     349                        if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < snapToIntersectionThreshold) {
    340350                                n.eastNorth = intersection;
    341351                                return;
    342352                        }
     
    349359                        double a = P.distance(B);
    350360                        double b = P.distance(A);
    351361                        double c = A.distance(B);
    352                         double q = (a - b + c) / (2*c);
     362                        q = (a - b + c) / (2*c);
    353363                        n.eastNorth = new EastNorth(
    354364                                B.east() + q * (A.east() - B.east()),
    355365                                B.north() + q * (A.north() - B.north()));