コード例 #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);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            bool         bExit    = false;              //退出标识
            int          ptCounts = 0;                  //点计数器
            int          lnCounts = 0;                  //线计数器
            ArrayList    points   = new ArrayList();    //点对象数组
            List <CLine> lines    = new List <CLine>(); //线对象数组

            //输入一组点,组成线,输入格式为:x y,当输入字符为“;”时,表示结束一条线的输入,当输入字符为“#”时表示结束所有输入
            Console.WriteLine("Please input points' coordination one by one,the format is x y");
            do
            {
                string instr = Console.ReadLine();               //读入坐标值
                if (instr.IndexOf(";") >= 0 && points.Count > 1) //发现“;”,用已输入的坐标值产生新的线
                {
                    int   m  = points.Count;                     //已有点数
                    CLine ln = new CLine();                      //产生新的线对象
                    ln.POINTS = new CPoint[m];                   //为线对象的点数组分配空间
                    for (int n = 0; n < m; n++)
                    {
                        ln.POINTS[n] = (CPoint)points[n]; //为每个点赋值
                    }
                    ln.SetValidation();                   //设置有效性
                    ln.ID = lnCounts++;
                    lines.Add(ln);                        //把线对象加入列表
                    points.Clear();                       //清空点数组对象
                }
                else if (instr.IndexOf("#") >= 0)         //结束输入
                {
                    bExit = true;
                }
                else
                {
                    string[] ts = instr.Split(' ');    //分解输入字符串
                    double   x  = double.Parse(ts[0]); //取得x
                    double   y  = double.Parse(ts[1]); //取得y
                    CPoint   p  = new CPoint(x, y);    //产生新的点对象
                    p.ID = ptCounts++;
                    points.Add(p);                     //将点加入点数组中
                }
            } while (!bExit);
            bExit = false;
            Console.WriteLine("------Options------");
            Console.WriteLine(@"1 表示打印信息
2 表示比较两条线要素
3 表示两条线相加
4 表示移动线
5 表示插入点
6 表示删除点");
            Console.WriteLine("-------------------");
            do
            {
                Console.Write("Please choose one business: ");
                string key = Console.ReadLine();
                switch (key)
                {
                case "1":
                    for (int i = 0; i < lines.Count; i++)
                    {
                        Console.WriteLine("line {0} contains {1} points and {2} meters long",
                                          i + 1, lines[i].POINTS.Length, lines[i].GetLineLength());
                        for (int j = 0; j < lines[i].POINTS.Length; j++)
                        {
                            Console.WriteLine("({0},{1})", lines[i].POINTS[j].PX, lines[i].POINTS[j].PY);
                        }
                    }
                    break;

                case "2":
                    if (lines[0].isValid && lines[1].isValid)
                    {
                        if (lines[0] > lines[1])
                        {
                            Console.WriteLine("The first line is longer than the second one");
                        }
                        else
                        {
                            Console.WriteLine("The first line is not longer than the second one");
                        }
                    }
                    break;

                case "3":
                    Console.Write("Please choose the first line's number: ");
                    int numLn1 = int.Parse(Console.ReadLine());
                    Console.Write("Please choose the first line's number: ");
                    int numLn2 = int.Parse(Console.ReadLine());
                    if ((lines[numLn1 - 1] + lines[numLn2 - 1]) != null)
                    {
                        lines.Add(lines[numLn1 - 1] + lines[numLn2 - 1]);
                    }
                    else
                    {
                        Console.WriteLine("ERROR!");
                    }
                    break;

                case "4":
                    Console.Write("Please choose the line's number: ");
                    int numLn = int.Parse(Console.ReadLine()) - 1;
                    Console.Write(@"1 Select Center Point to Move to
2 Select one Point to Move to
Please select the choice:");
                    string myChoice = Console.ReadLine();
                    switch (myChoice)
                    {
                    case "1":
                        Console.Write("Input your destination points: ");
                        string   destinationPt1 = Console.ReadLine();
                        string[] dp1            = destinationPt1.Split(' '); //分解输入字符串
                        double   x1             = double.Parse(dp1[0]);      //取得x
                        double   y1             = double.Parse(dp1[1]);      //取得y
                        CPoint   p1             = new CPoint(x1, y1);        //产生新的点对象
                        lines[numLn].Moveto(p1);
                        break;

                    case "2":
                        Console.Write("Input the point's index you choose: ");
                        int selectedIndex = int.Parse(Console.ReadLine());
                        Console.Write("Input your destination points: ");
                        string   destinationPt2 = Console.ReadLine();
                        string[] dp2            = destinationPt2.Split(' '); //分解输入字符串
                        double   x2             = double.Parse(dp2[0]);      //取得x
                        double   y2             = double.Parse(dp2[1]);      //取得y
                        CPoint   p2             = new CPoint(x2, y2);        //产生新的点对象
                        lines[numLn].Moveto(p2, selectedIndex - 1);
                        break;

                    default:
                        Console.WriteLine("ERROR!");
                        break;
                    }
                    break;

                case "5":
                    Console.Write("Please choose the line's number: ");
                    int addLn = int.Parse(Console.ReadLine()) - 1;
                    Console.Write("Which location do you want to insert: ");
                    int addLoc = int.Parse(Console.ReadLine()) - 1;
                    Console.Write("Input your destination points");
                    string   Pt    = Console.ReadLine();
                    string[] add   = Pt.Split(' ');          //分解输入字符串
                    double   addx  = double.Parse(add[0]);   //取得x
                    double   addy  = double.Parse(add[1]);   //取得y
                    CPoint   addPt = new CPoint(addx, addy); //产生新的点对象
                    lines[addLn].AddPoint(addLoc, addPt);
                    break;

                case "6":
                    //在此处填写代码
                    break;

                default:
                    bExit = true;
                    break;;
                }
                Console.WriteLine("-----------------------------");
            } while (!bExit);
        }