Avoid division by zero in simplication algorithm when two end points
[potlatch2.git] / net / systemeD / potlatch2 / tools / Simplify.as
index 009b01b17eac3a83a4f3c5cefb90c43b7521942e..468923edc349523ec36e951d3bd860f2b0c711cd 100644 (file)
@@ -12,7 +12,7 @@ package net.systemeD.potlatch2.tools {
                public static function simplify(way:Way, map:Map, keepOffscreen:Boolean):void {
                        if (way.length<3 || way.isArea()) { return; }
 
-                       var action:CompositeUndoableAction = new CompositeUndoableAction("Straighten");
+                       var action:CompositeUndoableAction = new CompositeUndoableAction("Simplify");
                        
                        var xa:Number, xb:Number;
                        var ya:Number, yb:Number;
@@ -65,7 +65,12 @@ package net.systemeD.potlatch2.tools {
                private static function getDistance(ax:Number,ay:Number,bx:Number,by:Number,l:Number,cx:Number,cy:Number):Number {
                        // l=length of line
                        // r=proportion along AB line (0-1) of nearest point
-                       var r:Number=((cx-ax)*(bx-ax)+(cy-ay)*(by-ay))/(l*l);
+                       var r:Number;
+                        if (l > 0) {
+                                r=((cx-ax)*(bx-ax)+(cy-ay)*(by-ay))/(l*l);
+                        } else {
+                                r=0;
+                        }
                        // now find the length from cx,cy to ax+r*(bx-ax),ay+r*(by-ay)
                        var px:Number=(ax+r*(bx-ax)-cx);
                        var py:Number=(ay+r*(by-ay)-cy);