有几个问题需要回答。
我找不到如何分配一个点数组。
嗯,分配一个点数组和为任何其他类型的数组分配没有区别:
const int size = 100;
Point[] pointArray = new Point[size];
但是数组缺少一些“便利”。例如,它们具有在初始化(分配)点需要指定的固定大小。如果您需要更多空间,则必须手动创建一个新的(更大的)数组,并将所有值从旧数组复制到新数组。
这就是为什么几乎无论何时您使用数组,您可能最好使用列表:
List<Point> pointList = new List<Point>();
随后,你需要传递一个数组的地方,你可以通过以下方式简单获取:
Point[] pointArray = pointList.ToArray();
动态收集分配点数组中的x和y值。
当你使用列表时,这样做很容易:
pointList.Add(new Point(x, y));
我们不知道您如何填写
float x[]
和
float y[]
。如果可能的话,我一开始就不会有这两个单独的数组,而是直接使用
pointList
。但有一个例外:
System.Drawing.Point只能使用
int
值,而不能使用
float
值。因此,我假设您想收集坐标的
int
值。
从浮点数数组x和y动态创建点数组
如果您无法更改坐标的收集方式,并且必须使用这些数组进行工作,您可以像这样将它们“压缩”在一起:
IEnumerable<Point> points = x.Zip(y, (xCoord, yCoord) =>
(new Point((int)xCoord, (int)yCoord));
或者,如果您知道您需要一个数组:
Point[] pointArray = x.Zip(y, (xCoord, yCoord) =>
(new Point((int)xCoord, (int)yCoord)).ToArray();
为此,您需要能够使用System.Linq
(换句话说,高于.Net 2.0)。
如果您无法使用Linq,则必须手动完成。类似于:
int size = Math.Min(x.Length, y.Length);
Point[] pointArray = new Point[size];
for (int index = 0; index < size; index++)
{
pointArray[index] = new Point((int)x[index], (int)y[index]);
}
xArray.Zip(yArray, (x, y) => (new Point(x,y))).ToArray()
- Corak