其他分享
首页 > 其他分享> > 判断直线和圆的交点

判断直线和圆的交点

作者:互联网

/// <summary>
/// 判断直线和圆的交点
/// </summary>
public bool GetIntersectionCircle(double startx, double starty, double endx, double endy, double centerx, double centery, double Radius,ref double[] ptInter1, ref double[] ptInter2)
{
double EPS = 0.01;  //误差允许范围
double Radius2 = Radius * Radius;

ptInter1[0] = ptInter2[0] = 65536.0;
ptInter2[1] = ptInter2[1] = 65536.0;

//求线段的长度
double fDis = Math.Sqrt((endx - startx) * (endx - startx) + (endy - starty) * (endy - starty));
double[] d = new double[2];
d[0] = (endx - startx) / fDis;
d[1] = (endy - starty) / fDis;
double[] E = new double[2];
E[0] = centerx - startx;
E[1] = centery - starty;
double a = E[0] * d[0] + E[1] * d[1];
double a2 = a * a;
double e2 = E[0] * E[0] + E[1] * E[1];
if ((Radius2 - e2 + a2) < 0)
{
return false;
}
else
{
double f = System.Math.Sqrt(Radius2 - e2 + a2);
double t = a - f;
if (((t - 0.0) > -EPS) && (t - fDis) < EPS)
{
ptInter1[0] = Math.Round(startx + t * d[0],2);
ptInter1[1] = Math.Round(starty + t * d[1],2);
}
t = a + f;
if (((t - 0.0) > -EPS) && (t - fDis) < EPS)
{
ptInter2[0] = Math.Round(startx + t * d[0],2);
ptInter2[1] = Math.Round(starty + t * d[1],2);
}
return true;
}
}

标签:直线,startx,判断,double,ptInter2,starty,fDis,交点,Math
来源: https://www.cnblogs.com/tangpyODA/p/14435723.html