Ignore:
Timestamp:
2012-03-26T21:26:51+02:00 (14 years ago)
Author:
Don-vip
Message:

Make polygon centroid computation a public method in Geometry class

File:
1 edited

Legend:

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

    r4982 r5125  
    2323import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2424import org.openstreetmap.josm.data.osm.Way;
     25import org.openstreetmap.josm.tools.Geometry;
    2526import org.openstreetmap.josm.tools.Shortcut;
    2627
     
    184185        if (center == null) {
    185186            // Compute the centroid of nodes
    186 
    187             BigDecimal area = new BigDecimal(0);
    188             BigDecimal north = new BigDecimal(0);
    189             BigDecimal east = new BigDecimal(0);
    190 
    191             // See http://en.wikipedia.org/w/index.php?title=Centroid&oldid=294224857#Centroid_of_polygon for the equation used here
    192             for (int i = 0; i < nodes.size(); i++) {
    193                 EastNorth n0 = nodes.get(i).getEastNorth();
    194                 EastNorth n1 = nodes.get((i+1) % nodes.size()).getEastNorth();
    195 
    196                 BigDecimal x0 = new BigDecimal(n0.east());
    197                 BigDecimal y0 = new BigDecimal(n0.north());
    198                 BigDecimal x1 = new BigDecimal(n1.east());
    199                 BigDecimal y1 = new BigDecimal(n1.north());
    200 
    201                 BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
    202 
    203                 area = area.add(k, MathContext.DECIMAL128);
    204                 east = east.add(k.multiply(x0.add(x1, MathContext.DECIMAL128), MathContext.DECIMAL128));
    205                 north = north.add(k.multiply(y0.add(y1, MathContext.DECIMAL128), MathContext.DECIMAL128));
    206 
    207             }
    208 
    209             BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3
    210             area  = area.multiply(d, MathContext.DECIMAL128);
    211             north = north.divide(area, MathContext.DECIMAL128);
    212             east = east.divide(area, MathContext.DECIMAL128);
    213 
    214             center = new EastNorth(east.doubleValue(), north.doubleValue());
    215 
     187            center = Geometry.getCentroid(nodes);
    216188        }
    217189        // Node "center" now is central to all selected nodes.
Note: See TracChangeset for help on using the changeset viewer.