Example usage for java.awt.geom Line2D ptSegDist

List of usage examples for java.awt.geom Line2D ptSegDist

Introduction

In this page you can find the example usage for java.awt.geom Line2D ptSegDist.

Prototype

public static double ptSegDist(double x1, double y1, double x2, double y2, double px, double py) 

Source Link

Document

Returns the distance from a point to a line segment.

Usage

From source file:org.opensha.commons.geo.LocationUtils.java

/**
 * Computes the shortest distance between a point and a line segment. Both
 * the line and point are assumed to be at the earth's surface; the depth
 * component of each <code>Location</code> is ignored. This is a fast,
 * geometric, cartesion (flat-earth approximation) solution in which
 * longitude of the line points are scaled by the cosine of latitude; it is
 * only appropriate for use over short distances (e.g. &lt;200 km). This
 * method always returns a positive result.
 * /*w w  w . ja v  a2  s  .c o m*/
 * <p><b>Note:</b> This method fails for values spanning &#177;180&#176;;
 * see {@link #distanceToLineFast(Location, Location, Location)}.</p>
 * 
 * <p>If the line should instead be treated as infinite, use
 * {@link #distanceToLineFast(Location, Location, Location)} instead.</p>
 * 
 * @param p1 the first <code>Location</code> point on the line
 * @param p2 the second <code>Location</code> point on the line
 * @param p3 the <code>Location</code> point for which distance will be
 *        calculated
 * @return the shortest distance in km between the supplied point and line
 * @see #distanceToLineSegment(Location, Location, Location)
 * @see #distanceToLineFast(Location, Location, Location)
 */
public static double distanceToLineSegmentFast(Location p1, Location p2, Location p3) {

    double lat1 = p1.getLatRad();
    double lat2 = p2.getLatRad();
    double lat3 = p3.getLatRad();
    double lon1 = p1.getLonRad();

    // use average latitude to scale longitude
    double lonScale = Math.cos(0.5 * lat3 + 0.25 * lat1 + 0.25 * lat2);

    // first point on line transformed to origin; others scaled by lon
    double x2 = (p2.getLonRad() - lon1) * lonScale;
    double y2 = lat2 - lat1;
    double x3 = (p3.getLonRad() - lon1) * lonScale;
    double y3 = lat3 - lat1;

    return Line2D.ptSegDist(0, 0, x2, y2, x3, y3) * EARTH_RADIUS_MEAN;
}