Ticket #10821: iwa.patch

File iwa.patch, 3.1 KB (added by akks, 7 years ago)
  • src/org/openstreetmap/josm/tools/Geometry.java

     
    413413    public static EastNorth closestPointToLine(EastNorth lineP1, EastNorth lineP2, EastNorth point) {
    414414        return closestPointTo(lineP1, lineP2, point, false);
    415415    }
    416 
     416   
    417417    /**
    418418     * This method tests if secondNode is clockwise to first node.
    419419     *
     
    826826    }
    827827
    828828    /**
    829      * Returns the coordinate of intersection of segment sp1-sp2 and an altitude
    830      * to it starting at point ap. If the line defined with sp1-sp2 intersects
    831      * its altitude out of sp1-sp2, null is returned.
     829     * Returns the coordinate of intersection of segment p1-p2 and an altitude
     830     * to it starting at point p. If the line defined with p1-p2 intersects
     831     * its altitude out of p1-p2, null is returned.
    832832     *
    833      * @param sp1
    834      * @param sp2
    835      * @param ap
     833     * @param p1
     834     * @param p2
     835     * @param point
    836836     * @return Intersection coordinate or null
    837837     */
    838     public static EastNorth getSegmentAltituteIntersection(EastNorth sp1, EastNorth sp2, EastNorth ap) {
     838    public static EastNorth getSegmentAltituteIntersection(EastNorth p1, EastNorth p2, EastNorth point) {
    839839
    840         CheckParameterUtil.ensureValidCoordinates(sp1, "sp1");
    841         CheckParameterUtil.ensureValidCoordinates(sp2, "sp2");
    842         CheckParameterUtil.ensureValidCoordinates(ap, "ap");
     840        CheckParameterUtil.ensureParameterNotNull(p1, "p1");
     841        CheckParameterUtil.ensureParameterNotNull(p2, "p2");
     842        CheckParameterUtil.ensureParameterNotNull(point, "point");
    843843
    844         Double segmentLenght = sp1.distance(sp2);
    845         Double altitudeAngle = getSegmentAngle(sp1, sp2) + Math.PI / 2;
     844        double ldx = p2.getX() - p1.getX();
     845        double ldy = p2.getY() - p1.getY();
    846846
    847         // Taking a random point on the altitude line (angle is known).
    848         EastNorth ap2 = new EastNorth(ap.east() + 1000
    849                 * Math.cos(altitudeAngle), ap.north() + 1000
    850                 * Math.sin(altitudeAngle));
     847        if (ldx == 0 && ldy == 0) //segment zero length
     848            return p1;
    851849
    852         // Finding the intersection of two lines
    853         EastNorth resultCandidate = Geometry.getLineLineIntersection(sp1, sp2,
    854                 ap, ap2);
     850        double pdx = point.getX() - p1.getX();
     851        double pdy = point.getY() - p1.getY();
    855852
    856         // Filtering result
    857         if (resultCandidate != null
    858                 && resultCandidate.distance(sp1) * .999 < segmentLenght
    859                 && resultCandidate.distance(sp2) * .999 < segmentLenght) {
    860             return resultCandidate;
     853        double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
     854
     855        if (offset < -1e-8 || offset > 1e-8) {
     856            return null;
    861857        } else {
    862             return null;
     858            return new EastNorth(p1.getX() + ldx * offset, p1.getY() + ldy * offset);
    863859        }
    864860    }
    865861