src/com/wherehoo/WHGeo.java
/**
     * Calculates the distance between an <tt>origin</tt> point and a line segment described by <tt>end_line</tt> 
     *@param origin a starting point
     *@param end_line a line segment 
     *@return Geographical distance from <tt>origin</tt> to <tt>end_line</tt>, given with double precision
     */
Calculates the distance between an origin point and a line segment described by end_line
@param origin a starting point
@param end_line a line segment
@return Geographical distance from origin to end_line, given with double precision
Calculates the distance between an origin point and a line segment described by end_line @param origin a starting point @param end_line a line segment @return Geographical distance from origin to end_line given with double precision
public static double distance(Point2D.Double origin, Line2D.Double line_end){
	Point2D.Double p1;
	Point2D.Double p2;
	double headingP1P2;
	double headingP1O;
	double headingP2P1;
	double headingP2O;
	double angleOP1P2;
	double angleOP2P1;
	p1= new Point2D.Double(line_end.getX1(),line_end.getY1());
	p2= new Point2D.Double(line_end.getX2(),line_end.getY2());
	headingP1P2= WHGeo.heading(p1,p2);
	headingP1O = WHGeo.heading(p1,origin);
	headingP2P1= WHGeo.heading(p2,p1);
	headingP2O = WHGeo.heading(p2,origin);
	angleOP1P2 = Math.abs((new BigDecimal(headingP1P2).subtract(new BigDecimal(headingP1O))).doubleValue());
	if (angleOP1P2>180)
	    angleOP1P2 = 360-angleOP1P2;
	angleOP2P1 = Math.abs(new BigDecimal(headingP2P1-headingP2O).doubleValue());
	if (angleOP2P1>180)
	    angleOP2P1 = 360-angleOP2P1;
	if((angleOP1P2>90)||(angleOP2P1>90)){
	    return Math.min(WHGeo.distance(origin,p1),WHGeo.distance(origin,p2));
	}
	else {
	    BigDecimal temp_dist = new BigDecimal(WHGeo.distance(origin,p1));
	    BigDecimal sin = new BigDecimal(Math.sin(Math.toRadians(angleOP1P2)));
	    return ((temp_dist.multiply(sin)).doubleValue());
	}
    }
public static double distance ( Point2D . Double origin , Line2D . Double line_end ) { Point2D . Double p1 ; Point2D . Double p2 ; double headingP1P2 ; double headingP1O ; double headingP2P1 ; double headingP2O ; double angleOP1P2 ; double angleOP2P1 ; p1 = new Point2D . Double ( line_end . getX1 ( ) , line_end . getY1 ( ) ) ; p2 = new Point2D . Double ( line_end . getX2 ( ) , line_end . getY2 ( ) ) ; headingP1P2 = WHGeo . heading ( p1 , p2 ) ; headingP1O = WHGeo . heading ( p1 , origin ) ; headingP2P1 = WHGeo . heading ( p2 , p1 ) ; headingP2O = WHGeo . heading ( p2 , origin ) ; angleOP1P2 = Math . abs ( ( new BigDecimal ( headingP1P2 ) . subtract ( new BigDecimal ( headingP1O ) ) ) . doubleValue ( ) ) ; if ( angleOP1P2 > 180 ) angleOP1P2 = 360 - angleOP1P2 ; angleOP2P1 = Math . abs ( new BigDecimal ( headingP2P1 - headingP2O ) . doubleValue ( ) ) ; if ( angleOP2P1 > 180 ) angleOP2P1 = 360 - angleOP2P1 ; if ( ( angleOP1P2 > 90 ) || ( angleOP2P1 > 90 ) ) { return Math . min ( WHGeo . distance ( origin , p1 ) , WHGeo . distance ( origin , p2 ) ) ; } else { BigDecimal temp_dist = new BigDecimal ( WHGeo . distance ( origin , p1 ) ) ; BigDecimal sin = new BigDecimal ( Math . sin ( Math . toRadians ( angleOP1P2 ) ) ) ; return ( ( temp_dist . multiply ( sin ) ) . doubleValue ( ) ) ; } }
Calculates the distance between an <tt>origin</tt> point and a line segment described by <tt>end_line</tt>
@param origin a starting point
@param end_line a line segment
@return Geographical distance from <tt>origin</tt> to <tt>end_line</tt>, given with double precision
Calculates the distance between an <tt > origin< / tt > point and a line segment described by <tt > end_line< / tt > @param origin a starting point @param end_line a line segment @return Geographical distance from <tt > origin< / tt > to <tt > end_line< / tt > given with double precision
//some helper variables
 //calculate them
 //System.out.println("p1: "+p1.getX()+","+p1.getY());
 //System.out.println("p2: "+p2.getX()+","+p2.getY());
 //System.out.println("origin: "+origin.getX()+","+origin.getY());
 //System.out.println("hp1p2: "+headingP1P2);
 //System.out.println("hp1o: "+headingP1O);
 //System.out.println("hp2p1: "+headingP2P1);
 //System.out.println("hp2o: "+headingP2O);
 //System.out.println("angles: "+angleOP1P2+","+angleOP2P1);
 //check if either of these angles is obtuse
 //System.out.println("obtuse");
 //return the distance of the closer point
 //System.out.println("not obtuse");
 //return the distance to P1P2 line
 //System.out.println("distance: "+WHGeo.distance(origin,p1));
 //System.out.println("sin : "+Math.sin(Math.toRadians(angleOP1P2)));
