1个回答
2017-05-02
展开全部
/// <summary>
/// 求直线和圆的关系,若相交或相切则求出交点。直线方程:ax+by+c=0
/// </summary>
/// <param name="p">圆心</param>
/// <param name="r">半径</param>
/// <param name="a">直线参数a</param>
/// <param name="b">直线参数b</param>
/// <param name="c">直线参数c</param>
/// <param name="rp1">交点</param>
/// <param name="rp2">交点</param>
/// <returns>0----相离 1----相切 2----相交 </returns>
int clpoint(PointF p, double r, double a, double b, double c, ref PointF rp1, ref PointF rp2)
{
int res = 0;
c = c + a * p.X + b * p.Y;
double tmp;
if (a == 0 && b != 0)
{
tmp = -c / b;
if (r * r < tmp * tmp)
res = 0;
else if (r * r == tmp * tmp)
{
res = 1;
rp1.Y = Convert.ToSingle(tmp);
rp1.X = 0;
}
else
{
res = 2;
rp1.Y = rp2.Y = Convert.ToSingle(tmp);
rp1.X = Convert.ToSingle(Math.Sqrt(r * r - tmp * tmp));
rp2.X = -rp1.X;
}
}
else if (a != 0 && b == 0)
{
tmp = -c / a;
if (r * r < tmp * tmp)
res = 0;
else if (r * r == tmp * tmp)
{
res = 1;
rp1.X = Convert.ToSingle(tmp);
rp1.Y = 0;
}
else
{
res = 2;
rp1.X = rp2.X = Convert.ToSingle(tmp);
rp1.Y = Convert.ToSingle(Math.Sqrt(r * r - tmp * tmp));
rp2.Y = -rp1.Y;
}
}
else if (a != 0 && b != 0)
{
double delta;
delta = b * b * c * c - (a * a + b * b) * (c * c - a * a * r * r);
if (delta < 0)
res = 0;
else if (delta == 0)
{
res = 1;
rp1.Y = Convert.ToSingle(-b * c / (a * a + b * b));
rp1.X = Convert.ToSingle((-c - b * rp1.Y) / a);
}
else
{
res = 2;
rp1.Y = Convert.ToSingle((-b * c + Math.Sqrt(delta)) / (a * a + b * b));
rp2.Y = Convert.ToSingle((-b * c - Math.Sqrt(delta)) / (a * a + b * b));
rp1.X = Convert.ToSingle((-c - b * rp1.Y) / a);
rp2.X = Convert.ToSingle((-c - b * rp2.Y) / a);
}
}
rp1.X += p.X;
rp1.Y += p.Y;
rp2.X += p.X;
rp2.Y += p.Y;
return res;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询