b171c7d2ceb79cae591e7741b1f90631e25ad599
[potlatch2.git] / net / systemeD / potlatch2 / tools / Straighten.as
1 package net.systemeD.potlatch2.tools {
2         import net.systemeD.halcyon.Map;
3         import net.systemeD.halcyon.connection.Way;
4         import net.systemeD.halcyon.connection.Node;
5
6         public class Straighten {
7
8                 public static function straighten(way:Way,map:Map):void {
9                         if (way.length<3) { return; }
10
11                         var a:Node=way.getNode(0);
12                         var b:Node=way.getNode(way.length-1);
13                         if (a==b) { return; }
14                         
15                         // ** could potentially do the 'too bendy?' check here as per Potlatch 1
16                         
17                         var todelete:Array=[];
18                         var n:Node;
19                 
20                         for (var i:uint=1; i<way.length-1; i++) {
21                                 n=way.getNode(i);
22                                 if (n.parentWays.length>1 || n.hasTags() ||
23                                     n.lon<map.edge_l || n.lon>map.edge_r ||
24                                     n.lat<map.edge_b || n.lat>map.edge_t) {
25
26                                         // junction node, tagged, or off-screen - so retain and move
27                                         var u:Number=((n.lon -a.lon )*(b.lon -a.lon )+
28                                                       (n.latp-a.latp)*(b.latp-a.latp))/
29                                                      (Math.pow(b.lon-a.lon,2)+Math.pow(b.latp-a.latp,2));
30                                         n.setLonLatp(a.lon +u*(b.lon -a.lon ),
31                                                      a.latp+u*(b.latp-a.latp));
32                                         
33                                 } else {
34                                         // safe to delete
35                                         if (todelete.indexOf(n)==-1) { todelete.push(n); }
36                                 }
37                         }
38                         for each (n in todelete) { n.remove(); }
39                 }
40         }
41 }