Ticket #2781: align.diff

File align.diff, 2.2 KB (added by pov, 4 years ago)

patch

  • src/org/openstreetmap/josm/actions/AlignInCircleAction.java

     
    182182 
    183183        // Get average position of circumcircles of the triangles of all triplets of neighbour nodes 
    184184        if (center == null) { 
    185             center = new EastNorth(0, 0); 
    186             Node n0 = (Node) nodes.toArray()[nodes.size() - 1]; 
    187             Node n1 = (Node) nodes.toArray()[nodes.size() - 2]; 
    188             Node n2; 
    189             for (Node n : nodes) { 
    190                 n2 = n1; 
    191                 n1 = n0; 
    192                 n0 = n; 
    193                 EastNorth cc = circumcenter(n0.getEastNorth(), n1.getEastNorth(), n2.getEastNorth()); 
    194                 if (cc == null) 
    195                     return; 
    196                 center = new EastNorth(center.east() + cc.east(), center.north() 
    197                         + cc.north()); 
     185 
     186            // Compute the centroid of nodes 
     187 
     188            // See http://en.wikipedia.org/w/index.php?title=Centroid&oldid=294224857#Centroid_of_polygon for the equation used here 
     189            double area = 0; 
     190            double north = 0; 
     191            double east = 0; 
     192 
     193            // Integrate the area, east and north centroid, we'll compute the final value based on the result of integration 
     194            for (int i=0; i<nodes.size(); i++) { 
     195                EastNorth n0 = ((Node) nodes.toArray()[i]).getEastNorth(); 
     196                EastNorth n1 = ((Node) nodes.toArray()[(i+1) % nodes.size()]).getEastNorth(); 
     197 
     198                area  += n0.east()*n1.north()-n1.east()*n0.north(); 
     199                east  += (n0.east() +n1.east()) *(n0.east()*n1.north()-n1.east()*n0.north()); 
     200                north += (n0.north()+n1.north())*(n0.east()*n1.north()-n1.east()*n0.north()); 
    198201            } 
    199  
    200             center = new EastNorth(center.east() / nodes.size(), center.north() 
    201                     / nodes.size()); 
     202            area  /= 2; 
     203            north /= 6*area; 
     204            east  /= 6*area; 
     205            center = new EastNorth(east, north); 
    202206        } 
    203207 
    204208        // Node "center" now is central to all selected nodes.