///
<summary>
/// p点到(a,b)点两所在直线的垂点坐标
/// </summary>
/// <param name="a"> 直线上a点 </param>
/// <param name="b"> 直线上b点 </param>
/// <param name="p"> 点 </param>
/// <returns> 垂点坐标 </returns>
public static Vector2D GetVerticalPosition(Vector2D a, Vector2D b, Vector2D p)
{
double fa = b.Y - a.Y;
double fb = a.X - b.X;
double fc = a.Y * b.X - a.X * b.Y;
Vector2D vpt = new Vector2D(); // 垂足
vpt.X = (fb * fb * p.X - fa * fb * p.Y - fa * fc) / (fa * fa + fb * fb);
vpt.Y = (fa * fa * p.Y - fa * fb * p.X - fb * fc) / (fa * fa + fb * fb);
return vpt;
}
/// <summary>
/// p点是否在(a,b)两点所在直线上
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="foot"></param>
/// <returns></returns>
public static bool DotIsOnLine(Vector2D a, Vector2D b, Vector2D foot)
{
return Math.Min(a.X, b.X) <= foot.X && foot.X <= Math.Max(a.X, b.X) && Math.Min(a.Y, b.Y) <= foot.Y && foot.Y <= Math.Max(a.Y, b.Y);
}
/// p点到(a,b)点两所在直线的垂点坐标
/// </summary>
/// <param name="a"> 直线上a点 </param>
/// <param name="b"> 直线上b点 </param>
/// <param name="p"> 点 </param>
/// <returns> 垂点坐标 </returns>
public static Vector2D GetVerticalPosition(Vector2D a, Vector2D b, Vector2D p)
{
double fa = b.Y - a.Y;
double fb = a.X - b.X;
double fc = a.Y * b.X - a.X * b.Y;
Vector2D vpt = new Vector2D(); // 垂足
vpt.X = (fb * fb * p.X - fa * fb * p.Y - fa * fc) / (fa * fa + fb * fb);
vpt.Y = (fa * fa * p.Y - fa * fb * p.X - fb * fc) / (fa * fa + fb * fb);
return vpt;
}
/// <summary>
/// p点是否在(a,b)两点所在直线上
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="foot"></param>
/// <returns></returns>
public static bool DotIsOnLine(Vector2D a, Vector2D b, Vector2D foot)
{
return Math.Min(a.X, b.X) <= foot.X && foot.X <= Math.Max(a.X, b.X) && Math.Min(a.Y, b.Y) <= foot.Y && foot.Y <= Math.Max(a.Y, b.Y);
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/p/4654287.html,如需转载请自行联系原作者