• 大小: 16KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: C#
  • 标签: 排序  C#  

资源简介

二维坐标点按行排序,通过Y值判断坐标点是否属于同一行,再对同一行的坐标点按X值从小到大进行排序

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TwoDimensionalCoordinatesSort
{
    class Program
    {
        struct Point
        {
            public float X;
            public float Y;
        }

        const int LineSpacing = 15;       //行间距,用来判断坐标点是否属于同一行
        
        static void Main(string[] args)
        {
            List RandomPointList = new List();
            List SortedPointList = new List();
            List YSortedPointList = new List();
            List RowPointList = new List();
            Random r = new Random();
            //第一行坐标
            for (int i = 0; i < 7; i++)
            {
                Point p = new Point();
                p.X = r.Next(100);
                p.Y = r.Next(10 20);
                RandomPointList.Add(p);
            }

            //第二行坐标
            for (int i = 0; i < 6; i++)
            {
                Point p = new Point();
                p.X = r.Next(100);
                p.Y = r.Next(40 50);
                RandomPointList.Add(p);
            }

            //第三行坐标
            for (int i = 0; i < 6; i++)
            {
                Point p = new Point();
                p.X = r.Next(100);
                p.Y = r.Next(70 80);
                RandomPointList.Add(p);
            }

            //第四行坐标
            for (int i = 0; i < 6; i++)
            {
                Point p = new Point();
                p.X = r.Next(100);
                p.Y = r.Next(100 110);
                RandomPointList.Add(p);
            }

            //第五行坐标
            for (int i = 0; i < 7; i++)
            {
                Point p = new Point();
                p.X = r.Next(100);
                p.Y = r.Next(130 140);
                RandomPointList.Add(p);
            }

            Console.WriteLine(“排序前的坐标:“);
            foreach (var point in RandomPointList)
            {
                Console.WriteLine(“(“ + point.X.ToString() + ““ + point.Y.ToString() + “)“);
            }

            YSortedPointList = RandomPointList.OrderBy(o => o.Y).ToList();        //坐标点按Y值升序排序(Y值从小到大的排序)

            //二维平面坐标点排序
            for (int i = 0; i < YSortedPointList.Count - 1; i++)
            {
                //通过Y值之间的差值大小来判断坐标点是否属于同一行
                if (Math.Abs(YSortedPointList[i].Y - YSortedPointList[i + 1].Y) < LineSpacing)
                {
                    RowPointList.Add(YSortedPointList[i]);

                    //如果最后一个点不是单独一行的情况
                    if (YSortedPointList.Count - 2 == i)
                    {
                        RowPointList.Add(YSortedPointList[i + 1]);                  //将最后一个坐标元素添加进来
                        RowPointList = RowPointList.OrderBy(o => o.X).ToList();
                        SortedPointList = SortedPointList.Concat(RowPointList).ToList();
                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\
     目录           0  2018-12-12 17:33  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\
     文件         977  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort.sln
     文件       11264  2018-12-12 17:33  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort.suo
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\bin\
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\bin\Debug\
     文件       14328  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\bin\Debug\TwoDimensionalCoordinatesSort.vshost.exe
     文件         490  2009-06-11 05:14  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\bin\Debug\TwoDimensionalCoordinatesSort.vshost.exe.manifest
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\obj\
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\obj\Debug\
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\obj\Debug\TempPE\
     文件        4523  2018-12-12 17:33  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\Program.cs
     目录           0  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\Properties\
     文件        1488  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\Properties\AssemblyInfo.cs
     文件        2531  2018-12-12 17:13  TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort\TwoDimensionalCoordinatesSort.csproj

评论

共有 条评论