Index: /trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java	(revision 1028)
+++ /trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java	(revision 1029)
@@ -511,7 +511,6 @@
 			return;
 		case 2:
-			// algorithm used here is a bit clumsy, anyone's welcome to replace
-			// it by something else. All it does it compute the intersection between
-			// the two segments and adjust the node position. The code doesnt
+			// This computes the intersection between
+			// the two segments and adjusts the node position. 
 			Iterator<Pair<Node,Node>> i = segs.iterator();
 			Pair<Node,Node> seg = i.next();
@@ -522,17 +521,27 @@
 			EastNorth D = seg.b.eastNorth;
 
+			double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
+			
+			// Check for parallel segments and do nothing if they are
+			// In practice this will probably only happen when a way has been duplicated
+			
+			if (u == 0) return;
+			
+			// q is a number between 0 and 1
+			// It is the point in the segment where the intersection occurs
+			// if the segment is scaled to lenght 1
+			
+			double q = det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east()) / u;
 			EastNorth intersection = new EastNorth(
-				det(det(A.east(), A.north(), B.east(), B.north()), A.east() - B.east(),
-					det(C.east(), C.north(), D.east(), D.north()), C.east() - D.east())/
-					det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north()),
-				det(det(A.east(), A.north(), B.east(), B.north()), A.north() - B.north(),
-					det(C.east(), C.north(), D.east(), D.north()), C.north() - D.north())/
-					det(A.east() - B.east(), A.north() - B.north(), C.east() - D.east(), C.north() - D.north())
-			);
-
-			// only adjust to intersection if within 10 pixel of mouse click; otherwise
+					B.east() + q * (A.east() - B.east()),
+					B.north() + q * (A.north() - B.north()));
+
+			int snapToIntersectionThreshold=0;
+			try { snapToIntersectionThreshold = Integer.parseInt(Main.pref.get("edit.snap-intersection-threshold","10")); } catch (NumberFormatException x) {}
+
+			// only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
 			// fall through to default action.
 			// (for semi-parallel lines, intersection might be miles away!)
-			if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < 10) {
+			if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < snapToIntersectionThreshold) {
 				n.eastNorth = intersection;
 				return;
@@ -547,5 +556,5 @@
 			double b = P.distanceSq(A);
 			double c = A.distanceSq(B);
-			double q = (a - b + c) / (2*c);
+            q = (a - b + c) / (2*c);
 			n.eastNorth = new EastNorth(
 				B.east() + q * (A.east() - B.east()),
