编程c#直线与圆的位置关系的判断

编程c#直线与圆的位置关系的判断... 编程c#直线与圆的位置关系的判断 展开
 我来答
匿名用户
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;
        }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式