Index: trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java	(revision 6048)
+++ trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java	(revision 6049)
@@ -315,4 +315,5 @@
      * Checks whether the selected objects are suitable to join and joins them if so
      */
+    @Override
     public void actionPerformed(ActionEvent e) {
         LinkedList<Way> ways = new LinkedList<Way>(Main.main.getCurrentDataSet().getSelectedWays());
@@ -1295,5 +1296,5 @@
      */
     private RelationRole addOwnMultigonRelation(Collection<Way> inner, Way outer) {
-        if (inner.size() == 0) return null;
+        if (inner.isEmpty()) return null;
         // Create new multipolygon relation and add all inner ways to it
         Relation newRel = new Relation();
@@ -1371,5 +1372,5 @@
         }
 
-        Relation newRel = null;
+        Relation newRel;
         switch (multiouters.size()) {
         case 0:
Index: trunk/src/org/openstreetmap/josm/tools/Geometry.java
===================================================================
--- trunk/src/org/openstreetmap/josm/tools/Geometry.java	(revision 6048)
+++ trunk/src/org/openstreetmap/josm/tools/Geometry.java	(revision 6049)
@@ -51,12 +51,13 @@
         //stupid java, cannot instantiate array of generic classes..
         @SuppressWarnings("unchecked")
-        ArrayList<Node>[] newNodes = new ArrayList[ways.size()];
-        BBox[] wayBounds = new BBox[ways.size()];
-        boolean[] changedWays = new boolean[ways.size()];
+        int n = ways.size();
+        ArrayList<Node>[] newNodes = new ArrayList[n];
+        BBox[] wayBounds = new BBox[n];
+        boolean[] changedWays = new boolean[n];
 
         Set<Node> intersectionNodes = new LinkedHashSet<Node>();
 
         //copy node arrays for local usage.
-        for (int pos = 0; pos < ways.size(); pos ++) {
+        for (int pos = 0; pos < n; pos ++) {
             newNodes[pos] = new ArrayList<Node>(ways.get(pos).getNodes());
             wayBounds[pos] = getNodesBounds(newNodes[pos]);
@@ -66,7 +67,6 @@
         //iterate over all way pairs and introduce the intersections
         Comparator<Node> coordsComparator = new NodePositionComparator();
-
-        WayLoop: for (int seg1Way = 0; seg1Way < ways.size(); seg1Way ++) {
-            for (int seg2Way = seg1Way; seg2Way < ways.size(); seg2Way ++) {
+        WayLoop: for (int seg1Way = 0; seg1Way < n; seg1Way ++) {
+            for (int seg2Way = seg1Way; seg2Way < n; seg2Way ++) {
 
                 //do not waste time on bounds that do not intersect
@@ -128,5 +128,4 @@
                                 boolean insertInSeg1 = false;
                                 boolean insertInSeg2 = false;
-
                                 //find if the intersection point is at end point of one of the segments, if so use that point
 
@@ -264,23 +263,40 @@
 
         //TODO: do this locally.
+        //TODO: remove this check after careful testing
         if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
 
-        // Convert line from (point, point) form to ax+by=c
-        double a1 = y2 - y1;
-        double b1 = x1 - x2;
-        double c1 = x2*y1 - x1*y2;
-
-        double a2 = y4 - y3;
-        double b2 = x3 - x4;
-        double c2 = x4*y3 - x3*y4;
+        // solve line-line intersection in parametric form:
+        // (x1,y1) + (x2-x1,y2-y1)* u  = (x3,y3) + (x4-x3,y4-y3)* v
+        // (x2-x1,y2-y1)*u - (x4-x3,y4-y3)*v = (x3-x1,y3-y1)
+        // if 0<= u,v <=1, intersection exists at ( x1+ (x2-x1)*u, y1 + (y2-y1)*u )
+        
+        double a1 = x2 - x1;
+        double b1 = x3 - x4;
+        double c1 = x3 - x1;
+
+        double a2 = y2 - y1;
+        double b2 = y3 - y4;
+        double c2 = y3 - y1;
 
         // Solve the equations
         double det = a1*b2 - a2*b1;
-        if (det == 0) return null; // Lines are parallel
-
-        double x = (b1*c2 - b2*c1)/det;
-        double y = (a2*c1 -a1*c2)/det;
-
-        return new EastNorth(x, y);
+        
+        double uu = b2*c1 - b1*c2 ;
+        double vv = a1*c2 - a2*c1;
+        double mag = Math.abs(uu)+Math.abs(vv);
+                
+        if (Math.abs(det) > 1e-12 * mag) {
+            double u = uu/det, v = vv/det;
+            if (u>-1e-8 && u < 1+1e-8 && v>-1e-8 && v < 1+1e-8 ) {
+                if (u<0) u=0;
+                if (u>1) u=1.0;
+                return new EastNorth(x1+a1*u, y1+a2*u);
+            } else {
+                return null;
+            }
+        } else {
+            // parallel lines
+            return null;
+        } 
     }
 
