编程语言
首页 > 编程语言> > C# 判断圆与矩形的冲突

C# 判断圆与矩形的冲突

作者:互联网

业务需要,我们把圆描述为孔,矩形描述为槽,在一个平面内判断是否冲突,所谓冲突即是孔与槽相交或孔在槽内。想了个简单的近似不严格判断方法,代码如下:

        /// <summary>
        /// 判断孔槽冲突
        /// </summary>
        /// <param name="kcList_kong">孔列表</param>
        /// <param name="kcList_cao">槽列表</param>
        /// <returns></returns>
        private static string CheckXMLKongCao_KongCao(List<Tools.KongCao> kcList_kong, List<Tools.KongCao> kcList_cao)
        {
            string sErrMsg = "";
            //定义槽的四个点,从原点开始,顺时针方向P1-P4
            Tools.Point p1_cao = new Tools.Point();
            Tools.Point p2_cao = new Tools.Point();
            Tools.Point p3_cao = new Tools.Point();
            Tools.Point p4_cao = new Tools.Point();

            //定义孔的四个点,从圆心左边开始,顺时针方向,分别为P1-P4
            Tools.Point p1_kong = new Tools.Point();
            Tools.Point p2_kong = new Tools.Point();
            Tools.Point p3_kong = new Tools.Point();
            Tools.Point p4_kong = new Tools.Point();

            foreach (Tools.KongCao kc_kong in kcList_kong)
            {
                //取孔的四个点
                GetPointFromKong(kc_kong, ref p1_kong, ref p2_kong, ref p3_kong, ref p4_kong);

                foreach (Tools.KongCao kc_cao in kcList_cao)
                {
                    // 取槽的四个点
                    GetPointFromCao(kc_cao, ref p1_cao, ref p2_cao, ref p3_cao, ref p4_cao);

                    //检查孔外围点P1,是否与槽冲突
                    if (CheckPointInRect(p1_kong, p1_cao, p3_cao))
                    {
                        sErrMsg = kc_kong.Name + " 与 " + kc_cao.Name + "位置冲突,请检查!";
                        break;
                    }

                    //检查孔外围点P2,是否与槽冲突
                    if (CheckPointInRect(p2_kong, p1_cao, p3_cao))
                    {
                        sErrMsg = kc_kong.Name + " 与 " + kc_cao.Name + "位置冲突,请检查!";
                        break;
                    }

                    //检查孔外围点P3,是否与槽冲突
                    if (CheckPointInRect(p3_kong, p1_cao, p3_cao))
                    {
                        sErrMsg = kc_kong.Name + " 与 " + kc_cao.Name + "位置冲突,请检查!";
                        break;
                    }

                    //检查孔外围点P4,是否与槽冲突
                    if (CheckPointInRect(p4_kong, p1_cao, p3_cao))
                    {
                        sErrMsg = kc_kong.Name + " 与 " + kc_cao.Name + "位置冲突,请检查!";
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(sErrMsg)) //出错返回
                {
                    break;
                }
            }

            return sErrMsg;
        }// end of CheckXMLKongCao_KongCao


        /// <summary>
        /// 检查孔外围点1,是否与槽冲突
        /// </summary>
        /// <param name="p1_kong"></param>
        /// <param name="p1_cao"></param>
        /// <param name="p3_cao"></param>
        private static bool CheckPointInRect(Tools.Point p1_kong, Tools.Point p1_cao, Tools.Point p3_cao)
        {
            if (((Convert.ToDouble(p1_kong.X) >= Convert.ToDouble(p1_cao.X)) &&
                  (Convert.ToDouble(p1_kong.X) <= Convert.ToDouble(p3_cao.X))) &&
                 ((Convert.ToDouble(p1_kong.Y) >= Convert.ToDouble(p1_cao.Y)) &&
                  (Convert.ToDouble(p1_kong.Y) <= Convert.ToDouble(p3_cao.Y))))
            {
                return true;
            }
            else
            {
                return false;
            }

        }// end of CheckPointInRect

        /// <summary>
        /// 取孔的四个外围点
        /// </summary>
        /// <param name="kc_kong"></param>
        /// <param name="p1_kong"></param>
        /// <param name="p2_kong"></param>
        /// <param name="p3_kong"></param>
        /// <param name="p4_kong"></param>
        private static void GetPointFromKong(Tools.KongCao kc_kong, ref Tools.Point p1, ref Tools.Point p2,
                                             ref Tools.Point p3, ref Tools.Point p4)
        {
            p1.X = Convert.ToString(Convert.ToDouble(kc_kong.Center.X) - kc_kong.Radius / 2);
            p1.Y = kc_kong.Center.Y;

            p2.X = kc_kong.Center.X;
            p2.Y = Convert.ToString(Convert.ToDouble(kc_kong.Center.Y) - kc_kong.Radius / 2);

            p3.X = Convert.ToString(Convert.ToDouble(kc_kong.Center.X) + kc_kong.Radius / 2);
            p3.Y = p1.Y;

            p4.X = p2.X;
            p4.Y = Convert.ToString(Convert.ToDouble(kc_kong.Center.Y) + kc_kong.Radius / 2);

        }// end of GetPointFromKong


        /// <summary>
        /// 取槽的四个点
        /// </summary>
        /// <param name="kc_cao"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <param name="p4"></param>
        private static void GetPointFromCao(Tools.KongCao kc_cao, ref Tools.Point p1, ref Tools.Point p2, ref Tools.Point p3,
                                            ref Tools.Point p4)
        {
            if (kc_cao.Angel == "0" || kc_cao.Angel == "180")
            {
                p1.X = Convert.ToString(Convert.ToDouble(kc_cao.Center.X) - kc_cao.Length / 2);
                p1.Y = Convert.ToString(Convert.ToDouble(kc_cao.Center.Y) - kc_cao.Width / 2);

                p2.X = p1.X;
                p2.Y = Convert.ToString(Convert.ToDouble(kc_cao.Center.Y) + kc_cao.Width / 2);

                p3.X = Convert.ToString(Convert.ToDouble(kc_cao.Center.X) + kc_cao.Length / 2);
                p3.Y = p2.Y;

                p4.X = p3.X;
                p4.Y = p1.Y;

            }
            else
            {
                p1.X = Convert.ToString(Convert.ToDouble(kc_cao.Center.X) - kc_cao.Width / 2);
                p1.Y = Convert.ToString(Convert.ToDouble(kc_cao.Center.Y) - kc_cao.Length / 2);

                p2.X = p1.X;
                p2.Y = Convert.ToString(Convert.ToDouble(kc_cao.Center.Y) + kc_cao.Length / 2);

                p3.X = Convert.ToString(Convert.ToDouble(kc_cao.Center.X) + kc_cao.Width / 2);
                p3.Y = p2.Y;

                p4.X = p3.X;
                p4.Y = p1.Y;
            }
        }// end of GetPointFromCao

 

标签:kc,Convert,p1,C#,kong,cao,冲突,矩形,Tools
来源: https://blog.csdn.net/wren2004/article/details/100692105