根据三点创建圆弧

private Point3d GetArcCenter(Point3d pt1,Point3d pt2,Point3d pt3,ref double radius)
        {
            double xysm, xyse, xy;
            double[] m_ArcCenter = new double[3];
            xy = Math.Pow(pt1.X, 2) + Math.Pow(pt1.Y, 2);
            xyse = xy - Math.Pow(pt3.X, 2) - Math.Pow(pt3.Y, 2);
            xysm = xy - Math.Pow(pt2.X, 2) - Math.Pow(pt2.Y, 2);
            xy = (pt1.X - pt2.X) * (pt1.Y - pt3.Y) - (pt1.X - pt3.X) * (pt1.Y - pt2.Y);
            // 判断参数有效性
            if (Math.Abs(xy)<0.000001)
            {
                MessageBox.Show("所输入的参数无法创建圆形!");
                return Point3d.Origin;
            }

            // 获得圆心和半径
            m_ArcCenter[0] = ((xysm * (pt1.Y - pt3.Y)) - (xyse * (pt1.Y - pt2.Y))) / (2 * xy);
            m_ArcCenter[1] = ((xyse * (pt1.X - pt2.X)) - (xysm * (pt1.X - pt3.X))) / (2 * xy);
            m_ArcCenter[2] = 0;
            radius = Math.Sqrt((pt1.X - m_ArcCenter[0]) * (pt1.X - m_ArcCenter[0]) + (pt1.Y - m_ArcCenter[1]) * (pt1.Y - m_ArcCenter[1]));
            if (radius<0.000001)
            {
                MessageBox.Show("半径过小");
                return Point3d.Origin;
            }

            // 函数返回圆心的位置,而半径在参数中通过引用方式返回
            Point3d arcCenter = new Point3d(m_ArcCenter[0], m_ArcCenter[1], m_ArcCenter[2]);
            return arcCenter;
        }

        public Arc AddArcFromPt(Point3d ptSt,Point3d ptSc,Point3d ptEn,ObjectId objid,string featurename)
        {
            // 三点法创建圆弧
            Point3d ptCenter;
            double radius = 0.0;
            Arc objArc;
            ptCenter = GetArcCenter(ptSt, ptSc, ptEn, ref radius);
            if (isClockWise(ptCenter, ptSt, ptSc, ptEn))
                objArc = AddArcCSEP(ptCenter, ptSt, ptEn, radius,objid,featurename);
            else
                objArc = AddArcCSEP(ptCenter, ptEn, ptSt, radius,objid,featurename);
            return objArc;
        }

        private Arc AddArcCSEP(Point3d ptCenter,Point3d ptSt,Point3d ptEn,double radius,ObjectId objid,string featurename)
        {
            double radiusent;
            double stAng, enAng;
            // 计算半径
            radiusent = radius;
            // 计算起点角度和终点角度
            stAng = CalLineAngle(ptCenter, ptSt);
            enAng = CalLineAngle(ptCenter, ptEn);
            Arc objArc = new Arc(ptCenter, radius, stAng, enAng);
            objArc.LinetypeId = objid;
            objArc.Layer = featurename;
            objArc.SetDatabaseDefaults();
            return objArc;
        }

        private bool isClockWise(Point3d ptCenter,Point3d ptSt,Point3d ptSc,Point3d ptEn)
        {
            double a1, a2, a3;
            a1 = CalLineAngle(ptSt, ptCenter);
            a2 = CalLineAngle(ptSc, ptCenter);
            a3 = CalLineAngle(ptEn, ptCenter);
            bool isClock = (a1 < a2) | (a2 < a3) | (a1 < a3);
            return isClock;
        }

        private double CalLineAngle(Point3d vStartPt,Point3d vEndPt)
        {
            double X, Y;
            double CalculateLineAngle = 0.0;
            double dbJudgeAngle = 0.001;
            X = vEndPt.X - vStartPt.X;
            Y = vEndPt.Y - vStartPt.Y;
            if ((X > 0 && Y > 0)||(X > 0 && Y < 0))
            {
                CalculateLineAngle = Math.Atan(Y / X);
            }
            else if ((X < 0 && Y > 0) || (X < 0 && Y < 0))
            {
                CalculateLineAngle = Math.PI + Math.Atan(Y / X);
            }
            else if(X == 0)
            {
                if (Y < 0)
                {
                    CalculateLineAngle = -Math.PI / 2;
                }
                else if (Y > 0)
                {
                    CalculateLineAngle = Math.PI / 2;
                }
            }
            else if (Y == 0)
            {
                if (X < 0)
                {
                    CalculateLineAngle = Math.PI;
                }
                else if (X > 0)
                {
                    CalculateLineAngle = 0;
                }
            }
            else
            {
                ;
            }

            if (Math.Abs(CalculateLineAngle - 0) < dbJudgeAngle)
            {
                CalculateLineAngle = 0;
                return CalculateLineAngle;
            }
            else if (CalculateLineAngle < 0)
            {
                CalculateLineAngle = 2 * Math.PI + CalculateLineAngle;
                return CalculateLineAngle;
            }
            else
                return CalculateLineAngle;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值