Changeset 5125 in josm


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

Make polygon centroid computation a public method in Geometry class

Location:
trunk/src/org/openstreetmap/josm
Files:
2 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.
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r4846 r5125  
    33
    44import java.awt.geom.Line2D;
     5import java.math.BigDecimal;
     6import java.math.MathContext;
    57import java.util.ArrayList;
    68import java.util.Comparator;
     
    583585        return result;
    584586    }
     587   
     588    public static EastNorth getCentroid(List<Node> nodes) {
     589        // Compute the centroid of nodes
     590
     591        BigDecimal area = new BigDecimal(0);
     592        BigDecimal north = new BigDecimal(0);
     593        BigDecimal east = new BigDecimal(0);
     594
     595        // See http://en.wikipedia.org/w/index.php?title=Centroid&oldid=294224857#Centroid_of_polygon for the equation used here
     596        for (int i = 0; i < nodes.size(); i++) {
     597            EastNorth n0 = nodes.get(i).getEastNorth();
     598            EastNorth n1 = nodes.get((i+1) % nodes.size()).getEastNorth();
     599
     600            BigDecimal x0 = new BigDecimal(n0.east());
     601            BigDecimal y0 = new BigDecimal(n0.north());
     602            BigDecimal x1 = new BigDecimal(n1.east());
     603            BigDecimal y1 = new BigDecimal(n1.north());
     604
     605            BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128));
     606
     607            area = area.add(k, MathContext.DECIMAL128);
     608            east = east.add(k.multiply(x0.add(x1, MathContext.DECIMAL128), MathContext.DECIMAL128));
     609            north = north.add(k.multiply(y0.add(y1, MathContext.DECIMAL128), MathContext.DECIMAL128));
     610        }
     611
     612        BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3
     613        area  = area.multiply(d, MathContext.DECIMAL128);
     614        north = north.divide(area, MathContext.DECIMAL128);
     615        east = east.divide(area, MathContext.DECIMAL128);
     616
     617        return new EastNorth(east.doubleValue(), north.doubleValue());
     618    }
    585619
    586620    /**
Note: See TracChangeset for help on using the changeset viewer.