Changeset 5125 in josm for trunk/src/org
- Timestamp:
- 2012-03-26T21:26:51+02:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java
r4982 r5125 23 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 24 24 import org.openstreetmap.josm.data.osm.Way; 25 import org.openstreetmap.josm.tools.Geometry; 25 26 import org.openstreetmap.josm.tools.Shortcut; 26 27 … … 184 185 if (center == null) { 185 186 // 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); 216 188 } 217 189 // Node "center" now is central to all selected nodes. -
trunk/src/org/openstreetmap/josm/tools/Geometry.java
r4846 r5125 3 3 4 4 import java.awt.geom.Line2D; 5 import java.math.BigDecimal; 6 import java.math.MathContext; 5 7 import java.util.ArrayList; 6 8 import java.util.Comparator; … … 583 585 return result; 584 586 } 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 } 585 619 586 620 /**
Note:
See TracChangeset
for help on using the changeset viewer.