倒叙的著名文章:点导直线的距离

来源:百度文库 编辑:神马品牌网 时间:2024/03/29 02:13:34
已知三个点的坐标,求一点到另外两点确定的直线的距离。

// 已知三点的坐标:
float Ax,Bx,Cx,Ay,By,Cy;
求C 到AB直线的距离.

L = sqrt( (Bx-Ax)^2 + (By-Ay)^2 );

s = ((Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)) / (L * L);

距离:
distance= fabs(s) * L;

若A,B点重合,两点迭在一起,L=0, 距离:
distance= sqrt( (Cx-Ax)^2 + (Cy-Ay)^2 );

public class Distance
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please input the coordinates of the first point");
double aX = input.nextDouble();
double aY = input.nextDouble();
System.out.println("Please input the coordinates of the second point");
double bX = input.nextDouble();
double bY = input.nextDouble();
System.out.println("Please input the coordinates of the third point");
double cX = input.nextDouble();
double cY = input.nextDouble();

double ac = Math.sqrt((aY - cY) * (aY - cY) + (aX - cX) * ( aX - cX));
double bc = Math.sqrt((bY - cY) * (bY - cY) + (bX - cX) * ( bX - cX));
double ab = Math.sqrt((aY - bY) * (aY - bY) + (aX - bX) * ( aX - bX));

double cosC = (ac * ac + bc * bc) / ( 2 * ac * bc);
double sinC = Math.sqrt( 1 - cosC * cosC);
double distance = ac * bc * sinC / ab;
System.out.println("The distance is " + distance);
}
}