Exemple #1
0
        //重载加法"+"操作符,可以把两个线连接在一起成为一个新的线要素
        //重载操作符"+",把二个在首或尾有关联的线连接成一个新的线段
        public static CLine operator +(CLine ln1, CLine ln2)
        {
            CLine  tempLine    = null;                                                        //新的线段
            int    m           = ln1.POINTS.GetLength(0);                                     //第一条线的点数
            int    n           = ln2.POINTS.GetLength(0);                                     //第二条线的点数
            CPoint firstStart  = ln1.POINTS[0];                                               //第一条线的起点
            CPoint firstEnd    = ln1.POINTS[m - 1];                                           //第一条线的终点
            CPoint secondStart = ln2.POINTS[0];                                               //第二条线的起点
            CPoint secondEnd   = ln2.POINTS[m - 1];                                           //第二条线的终点
            double ss          = CPoint.GetDistanceBetweenTwoPoints(firstStart, secondStart); //第一条线起点与第二条线起点之间的距离
            double se          = CPoint.GetDistanceBetweenTwoPoints(firstStart, secondEnd);   //第一条线起点与第二条线终点之间的距离
            double es          = CPoint.GetDistanceBetweenTwoPoints(firstEnd, secondStart);   //第一条线终点与第二条线起点之间的距离
            double ee          = CPoint.GetDistanceBetweenTwoPoints(firstEnd, secondEnd);     //第一条线终点与第二条线终点之间的距离

            if (ss < Tolerance)                                                               //在容限范围内
            {
                CPoint[] pts = new CPoint[m + n - 1];
                for (int i = m - 1; i >= 0; i--)
                {
                    pts[m - i - 1] = ln1.POINTS[i];   //逆序拷贝第一个线要素的点
                }
                Array.Copy(ln2.POINTS, 0, pts, m, n); //顺序拷贝第二个线要素的点
                tempLine = new CLine(pts);
            }
            else if (se < Tolerance)
            {
                CPoint[] pts = new CPoint[m + n - 1];
                for (int i = m - 1; i >= 0; i--)
                {
                    pts[m - i - 1] = ln1.POINTS[i]; //逆序拷贝第一个线要素的点
                }
                for (int j = n - 1; j >= 0; j--)
                {
                    pts[m + n - j - 1] = ln2.POINTS[j]; //逆序拷贝第二个线要素的点
                }
                tempLine = new CLine(pts);
            }
            else if (es < Tolerance)
            {
                CPoint[] pts = new CPoint[m + n - 1];
                Array.Copy(ln1.POINTS, 0, pts, 0, m); //顺序拷贝第一个线要素的点
                Array.Copy(ln2.POINTS, 0, pts, m, n); //顺序拷贝第二个线要素的点
                tempLine = new CLine(pts);
            }
            else if (ee < Tolerance)
            {
                CPoint[] pts = new CPoint[m + n - 1];
                Array.Copy(ln1.POINTS, pts, m); //顺序拷贝第一个线要素的点
                for (int i = n - 1; i >= 0; i--)
                {
                    pts[m + n - i - 1] = ln2.POINTS[i]; //逆序拷贝第二个线要素的点
                }
                tempLine = new CLine(pts);
            }
            return(tempLine);
        }
Exemple #2
0
        //函数GetLineLength计算线的长度
        public double GetLineLength()
        {
            if (_Points.Length < 2)
            {
                return(0);
            }
            double totalDistance = 0;
            int    n             = _Points.GetLength(0); //取得点的数目

            for (int i = 1; i < n; i++)
            {
                CPoint p1 = _Points[i - 1];
                CPoint p2 = _Points[i];
                totalDistance += CPoint.GetDistanceBetweenTwoPoints(p1, p2); //注意计算两点之间的距离,累加
            }
            return(totalDistance);
        }