Geo平面内三点是否在一条直线上

判断三点(a,b,c)是否在一条直线直线,其实只需要判断直线ab 和 直线 bc 的位置即可(两条直线的关系 相交 平行 重合)  
判断两条直线位置关系只需判断两直线斜率k即可 k相同即可

计算垂足的话,如果两条直线垂直,那么他们斜率存在 k2 = (1/k1)
然后根据两相交线求相交点方程即可
public class Geo {
	public static void main(String[] args) {
		Point p0 = new Point(116.38094,39.92362);
		Point p1 = new Point(116.38545,39.921959);
		Point p2 = new Point(116.38546,39.921979);
		GetFootOfPerpendicular(p0, p1, p2);
		System.out.println(line(p0, p1, p2));
	}
	/**
	 * 判断三点是否在一条直线上
	 * @param start
	 * @param end
	 * @param point
	 * @return
	 */
	public static boolean line(Point start , Point end , Point point){
		double sx , ex , px ;
		double sy,ey,py ; 
		
		sx = start.x ;
		ex= end.x ;
		px = point.x;
		
		sy = start.y;
		ey = end.y; 
		py = point.y; 
		return  Math.abs(((sx - ex) *  (py - sy) - (px - sx ) * (sy - ey)) )<= 0.0000000001  ; 
	}
	/**
	 * 计算垂足坐标
	 * @param start
	 * @param end
	 * @param point
	 * @return
	 */
	public static Point GetFootOfPerpendicular(Point start , Point end , Point point){
		double k1  =  (start.x - end.x) / (start.y - end.y);
		double b1 = start.y - k1*start.x ; 
		double b2 = point.y - (1/k1)*point.x ; 
		double tempx =( b2 - b1 ) /( k1-(1/k1)) ; 
		double tempy =k1*tempx+ b1 ;
		return new Point(tempx , tempy) ; 
	}
}
class Point {
	public double x ; 
	public double y ; 
	public Point(double x , double y ){
		this.x = x ; 
		this.y = y ; 
	}
	@Override
	public String toString() {
		return "Point [x=" + x + ", y=" + y + "]";
	}	
}

  

posted @ 2017-06-14 16:15  王南辉  阅读(389)  评论(0编辑  收藏  举报