Ignore:
Timestamp:
2009-06-28T22:41:25+02:00 (15 years ago)
Author:
stoecker
Message:

fix #2781 - patch by pov - better calculations for align in circle

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java

    r1640 r1713  
    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());
    198             }
    199 
    200             center = new EastNorth(center.east() / nodes.size(), center.north()
    201                     / nodes.size());
     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());
     201            }
     202            area  /= 2;
     203            north /= 6*area;
     204            east  /= 6*area;
     205            center = new EastNorth(east, north);
    202206        }
    203207
Note: See TracChangeset for help on using the changeset viewer.