Select 列表中的特定坐标
Select Specific Coordinates From List
我有一个从左到右、从上到下排列的坐标 (x, y) 列表:
我的目标是以不同的顺序只抓取其中的一些。我已经对我想要抓住的唯一点进行了编号,并按照我想要抓住它们的顺序进行了编号。类似于蛇。每向下移动一个方向就反转:
我已经尝试写出如何 select 每个点的逻辑,但我似乎无法弄清楚如何在 for 循环中得到它。或者嵌套的 for 循环,我想我需要。
length = 10
(我抓取的坐标平方的大小,即10x10。)
Increments * +
1) length * 0 + 0 // +0 +0
2) length * 1 + 0 // +1 +0
3) length * 2 + 0 // +1 +0
4) length * 1 + 1 // -1 +1
5) length * 2 + 1 // +1 +0
6) length * 3 + 0 // +1 -1
7) length * 4 + 0 // +1 +0
8) length * 3 + 1 // -1 +1
9) length * 4 + 1 // +1 +0
10) length * 5 + 0 // +1 -1
所以如果我要为我想要的每个坐标硬编码数组,我会:
1) 0
2) 10
3) 20
4) 11
5) 21
6) 30
7) 40
8) 31
9) 41
10) 50
我认为这不需要嵌套循环。
您可以使用 flip
变量来更改特定行是否“翻转”。
- 每行取两个索引
flip
在 0 和 1 之间交替,每两个索引(即每一行)
- 在偶数行上,第一个索引
length - 1
小于第二个索引
- 在奇数行,该关系被翻转。
public static void Main(string[] args)
{
int length = 11;
for (int i = 0; i < 10; ++ i) {
int flip = (i / 2) % 2;
int index = (i / 2) * length + ((i + flip) % 2) * (length - 1);
Console.WriteLine(index);
}
}
输出:
0
10
11
21
22
32
33
43
44
54
我有一个从左到右、从上到下排列的坐标 (x, y) 列表:
我的目标是以不同的顺序只抓取其中的一些。我已经对我想要抓住的唯一点进行了编号,并按照我想要抓住它们的顺序进行了编号。类似于蛇。每向下移动一个方向就反转:
我已经尝试写出如何 select 每个点的逻辑,但我似乎无法弄清楚如何在 for 循环中得到它。或者嵌套的 for 循环,我想我需要。
length = 10
(我抓取的坐标平方的大小,即10x10。)
Increments * +
1) length * 0 + 0 // +0 +0
2) length * 1 + 0 // +1 +0
3) length * 2 + 0 // +1 +0
4) length * 1 + 1 // -1 +1
5) length * 2 + 1 // +1 +0
6) length * 3 + 0 // +1 -1
7) length * 4 + 0 // +1 +0
8) length * 3 + 1 // -1 +1
9) length * 4 + 1 // +1 +0
10) length * 5 + 0 // +1 -1
所以如果我要为我想要的每个坐标硬编码数组,我会:
1) 0
2) 10
3) 20
4) 11
5) 21
6) 30
7) 40
8) 31
9) 41
10) 50
我认为这不需要嵌套循环。
您可以使用 flip
变量来更改特定行是否“翻转”。
- 每行取两个索引
flip
在 0 和 1 之间交替,每两个索引(即每一行)- 在偶数行上,第一个索引
length - 1
小于第二个索引 - 在奇数行,该关系被翻转。
public static void Main(string[] args)
{
int length = 11;
for (int i = 0; i < 10; ++ i) {
int flip = (i / 2) % 2;
int index = (i / 2) * length + ((i + flip) % 2) * (length - 1);
Console.WriteLine(index);
}
}
输出:
0
10
11
21
22
32
33
43
44
54