Ignore:
Timestamp:
2011-08-26T22:26:37+02:00 (13 years ago)
Author:
stoecker
Message:

fix #5257 - patch by simon04 - fix way order validator checks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r4134 r4344  
    1515import org.openstreetmap.josm.command.Command;
    1616import org.openstreetmap.josm.data.coor.EastNorth;
     17import org.openstreetmap.josm.data.coor.LatLon;
    1718import org.openstreetmap.josm.data.osm.BBox;
    1819import org.openstreetmap.josm.data.osm.Node;
     
    221222     */
    222223    public static boolean angleIsClockwise(Node commonNode, Node firstNode, Node secondNode) {
    223         double dy1 = (firstNode.getEastNorth().getY() - commonNode.getEastNorth().getY());
    224         double dy2 = (secondNode.getEastNorth().getY() - commonNode.getEastNorth().getY());
    225         double dx1 = (firstNode.getEastNorth().getX() - commonNode.getEastNorth().getX());
    226         double dx2 = (secondNode.getEastNorth().getX() - commonNode.getEastNorth().getX());
    227 
    228         return dy1 * dx2 - dx1 * dy2 > 0;
     224        return angleIsClockwise(commonNode.getEastNorth(), firstNode.getEastNorth(), secondNode.getEastNorth());
    229225    }
    230226
     
    527523    }
    528524
    529 
     525    /**
     526     * Determines whether a way is oriented clockwise.
     527     *
     528     * Internals: Assuming a closed non-looping way, compute twice the area
     529     * of the polygon using the formula {@code 2 * area = sum (X[n] * Y[n+1] - X[n+1] * Y[n])}.
     530     * If the area is negative the way is ordered in a clockwise direction.
     531     *
     532     * @param w the way to be checked.
     533     * @return true if and only if way is oriented clockwise.
     534     * @throws IllegalArgumentException if way is not closed (see {@see Way#isClosed}).
     535     * @see http://paulbourke.net/geometry/polyarea/
     536     */
     537    public static boolean isClockwise(Way w) {
     538        if (!w.isClosed()) {
     539            throw new IllegalArgumentException("Way must be closed to check orientation.");
     540        }
     541
     542        double area2 = 0.;
     543        int nodesCount = w.getNodesCount();
     544
     545        for (int node = 1; node <= /*sic! consider last-first as well*/ nodesCount; node++) {
     546            LatLon coorPrev = w.getNode(node - 1).getCoor();
     547            LatLon coorCurr = w.getNode(node % nodesCount).getCoor();
     548            area2 += coorPrev.lon() * coorCurr.lat();
     549            area2 -= coorCurr.lon() * coorPrev.lat();
     550        }
     551        return area2 < 0;
     552    }
    530553}
Note: See TracChangeset for help on using the changeset viewer.