Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用x和y坐标定位2d单元_C#_Algorithm - Fatal编程技术网

C# 使用x和y坐标定位2d单元

C# 使用x和y坐标定位2d单元,c#,algorithm,C#,Algorithm,我想创建一个名为point的类,它可以在给定坐标和数据表大小的情况下计算位置。如果电子表格的大小为3X3,则以下是GetPosition方法的预期结果: 坐标0,0表示0位置 坐标0,1表示1个位置 坐标0,2表示2位置 坐标1,0表示3位置 坐标1,1表示4位置 坐标1,2表示位置5 坐标2,0表示6位置 坐标2,1表示7位置 坐标2,2表示8位置 请尝试以下操作: using System; using System.Collections.Generic; using System.Lin

我想创建一个名为point的类,它可以在给定坐标和数据表大小的情况下计算位置。如果电子表格的大小为3X3,则以下是GetPosition方法的预期结果:

坐标0,0表示0位置

坐标0,1表示1个位置

坐标0,2表示2位置

坐标1,0表示3位置

坐标1,1表示4位置

坐标1,2表示位置5

坐标2,0表示6位置

坐标2,1表示7位置

坐标2,2表示8位置

请尝试以下操作:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<Point>> points = new List<List<Point>>() {
                new List<Point>() { new Point(0,0),new Point(0,1),new Point(0,2),},
                new List<Point>() { new Point(1,0),new Point(1,1),new Point(1,2),},
                new List<Point>() { new Point(2,0),new Point(2,1),new Point(2,2),},
                new List<Point>() { new Point(3,0),new Point(3,1),new Point(3,2),}
            };

        }
    }
}

第一个问题是示例屏幕截图中的X坐标有4个可能值0,1,2,3,因为有4行,而Y坐标只有3个可能值0,1,2,因为只有3列,所以它应该是4x3,而不是3x4。我建议使用行和列而不是x和y,这样您就不会感到困惑。 修正后,对于给定为行、列的任何单元格,可以使用以下公式获得位置:行*TotalNoOfColumns+col,其中行和列是基于零的索引。

公式将是x*列+y

或者,如果观察图案,则这些坐标与其各自的编号之间存在关系:

您正在有效地将数字从以3为基数的列转换为以10为基数的列:

例如,坐标1,1可以读取为11,但以3为底。如果你把它转换成10进制,你会得到4。类似地,当你将基数3中的21转换为基数10,得到7。。。等等


然而,将数字转换为不同的基数并不是C语言内置的东西。因此,除非您想经历将数字转换为不同基数的麻烦,否则您最好使用上面的公式。

?X*Size_Y+Y?@tkausl谢谢让我检查一下@tkausl它只适用于行-列相等的正方形意义的电子表格,对吗?不,@tkausl的forumula适用于任何一个矩形区域。是的。谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<Point>> points = new List<List<Point>>() {
                new List<Point>() { new Point(0,0),new Point(0,1),new Point(0,2),},
                new List<Point>() { new Point(1,0),new Point(1,1),new Point(1,2),},
                new List<Point>() { new Point(2,0),new Point(2,1),new Point(2,2),},
                new List<Point>() { new Point(3,0),new Point(3,1),new Point(3,2),}
            };

        }
    }
}
(0,0) = (0 * 3) + 0 = 0
(0,1) = (0 * 3) + 1 = 1
(0,2) = (0 * 3) + 2 = 2
(1,0) = (1 * 3) + 0 = 3
(1,1) = (1 * 3) + 1 = 4
(1,2) = (1 * 3) + 2 = 5
(2,0) = (2 * 3) + 0 = 6
(2,1) = (2 * 3) + 1 = 7
(2,2) = (2 * 3) + 2 = 8