[英]List or Dictionary or something else?
I want to save some coordinates in a dictionary, BUT the xPos should be twice or more in the dictionary. 我想在字典中保存一些坐标,但是xPos在字典中应该是两倍或更多。
The problem is that the following exception appears: 问题是出现以下异常:
ArgumentException: An element with the same key already exists in the dictionary.
How can I solve the problem ? 我该如何解决这个问题?
I allready thought that I can use a List or an Array, but I want a Key and a Value. 我已经以为可以使用列表或数组,但我想要一个键和一个值。
After I saved the coordinates in a Dict (or something else) I want to check whether a new coordinate is a certain distance of the existing ones. 将坐标保存在Dict(或其他)中后,我想检查新坐标是否与现有坐标相距一定距离。
The xPos is allways the same: xPos始终相同:
There is a "chart" where I place some blocks in a row with different yPos. 有一个“图表”,我在其中以不同的yPos放置了一些块。
1. Block: xPos = 0, yPos = random 1.区块:xPos = 0,yPos =随机
2. Block: xPos = 1, yPos = random 2. Block:xPos = 1,yPos =随机
... ...
n. 。 Block: xPos = 80, yPos = random
区块:xPos = 80,yPos =随机
n+1. n + 1。 Block: xPos = 0, yPos = 20 + random
阻止:xPos = 0,yPos = 20 +随机
I have three iterations, for each 80 Blocks are placed. 我有三个迭代,每放置80个块。
SORRY for my bad english :| 对不起,我的英语不好:|
I hope you could understand. 我希望你能理解。
You can create a class (or a struct) to keep and use your coordinates, instead of a dictionary. 您可以创建一个类(或结构)来保留和使用您的坐标,而不是字典。 The class can have Key and Value properties and also additional fields if needed.
该类可以具有键和值属性,如果需要,还可以具有其他字段。
You should save the values like List, where Position contains: 您应该保存诸如List之类的值,其中Position包含:
public int X { get; set; }
public int Y { get; set; }
Or you can use some another class/struct from C# or some 3rd libraries (2d vector, point, etc) depends on where you want to use it. 或者,您可以使用C#中的其他类/结构或某些3rd库(2d矢量,点等),具体取决于您希望在何处使用它。 :)
:)
Or you can use List
of Tuple
to store list of int
- int
pairs without creating new class and without worrying about duplicate values : 或者你可以使用
List
的Tuple
存储的名单int
- int
对不创建新类,而不必担心重复的值:
.....
List<Tuple<int, int>> blocks = new List<Tuple<int, int>>();
blocks.Add(Tuple.Create(0, random));
blocks.Add(Tuple.Create(1, random));
.....
Since you're storing points in a 2-d surface and want to do nearest-point detection, I'd recommend using a Binary Space Partitioning (BSP) tree , such as a QuadTree. 由于您要将点存储在二维曲面中,并且想要进行最近点检测,因此建议您使用Binary Space Partitioning(BSP)树 ,例如QuadTree。 Here's a link to a quadtree implementation in C#.
这是C#中四叉树实现的链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.