动态创建点数组
Dynamically create a point array
我想使用 graphics.DrawCurve
绘制一条曲线,并且我在单独的数组(float x[]
和 float y[]
)中有 x 和 y 值。由于 DrawCurve
需要点数组作为输入,我需要从浮点数组 x 和 y 转换或动态创建点数组。有什么快速的方法吗?
我有大约 20000 个点用于绘制曲线,为此目的使用 graphics.DrawCurve
是个好主意吗?
您可以创建比数组更好的List<Point>
。
List<Point> list = new List<Point>();
Point point=new Point(10,15);
list.Add(point);
您可以从两个数组中获取所有 x 和 y 坐标并将它们作为点放在列表中,然后使用此列表绘制曲线。
鉴于两个数组中的值具有相同的索引,您可以迭代它们、创建点并将它们添加到列表中。
List<Point> points = new List<Point>();
for(int i = 0; i < x.Length; i++){
points.Add(new Point(x[i],y[i]);
}
我没有发现在此处使用 DrawCurve
有什么特别的问题。
如果您需要点数组,请使用 points.ToArray();
有几个问题需要回答。
I couldn't find out how to allocate a point array.
好吧,分配点数组与分配任何其他类型的数组没有区别:
const int size = 100;
Point[] pointArray = new Point[size];
但数组缺少一些 "convenience"。例如,它们具有固定大小,您需要在初始化(分配)时指定。如果你需要更多 space,你必须手动创建一个新的(更大的)数组并将所有值从旧的复制到新的。
这就是为什么几乎所有你会使用数组的地方,你最好使用列表:
List<Point> pointList = new List<Point>();
然后,无论您实际需要传递一个数组,您都可以通过以下方式简单地获取它:
Point[] pointArray = pointList.ToArray();
dynamically collect the x and y values in the allocated point array
当您使用列表时,就像:
pointList.Add(new Point(x, y));
我们不知道您如何填写 float x[]
和 float y[]
。如果可能的话,我不会首先使用这两个单独的数组,而只是从一开始就使用 pointList
。有一个警告:System.Drawing.Point 仅适用于 int
值,不适用于 float
值。所以我假设您打算收集坐标的 int
值。
dynamically create the point array from the float arrays x and y
如果您无法更改坐标集合并且有使用这些数组,您可以"zip"像这样将它们组合在一起:
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,则必须使用它 "by hand"。类似于:
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]);
}
我想使用 graphics.DrawCurve
绘制一条曲线,并且我在单独的数组(float x[]
和 float y[]
)中有 x 和 y 值。由于 DrawCurve
需要点数组作为输入,我需要从浮点数组 x 和 y 转换或动态创建点数组。有什么快速的方法吗?
我有大约 20000 个点用于绘制曲线,为此目的使用 graphics.DrawCurve
是个好主意吗?
您可以创建比数组更好的List<Point>
。
List<Point> list = new List<Point>();
Point point=new Point(10,15);
list.Add(point);
您可以从两个数组中获取所有 x 和 y 坐标并将它们作为点放在列表中,然后使用此列表绘制曲线。
鉴于两个数组中的值具有相同的索引,您可以迭代它们、创建点并将它们添加到列表中。
List<Point> points = new List<Point>();
for(int i = 0; i < x.Length; i++){
points.Add(new Point(x[i],y[i]);
}
我没有发现在此处使用 DrawCurve
有什么特别的问题。
如果您需要点数组,请使用 points.ToArray();
有几个问题需要回答。
I couldn't find out how to allocate a point array.
好吧,分配点数组与分配任何其他类型的数组没有区别:
const int size = 100;
Point[] pointArray = new Point[size];
但数组缺少一些 "convenience"。例如,它们具有固定大小,您需要在初始化(分配)时指定。如果你需要更多 space,你必须手动创建一个新的(更大的)数组并将所有值从旧的复制到新的。
这就是为什么几乎所有你会使用数组的地方,你最好使用列表:
List<Point> pointList = new List<Point>();
然后,无论您实际需要传递一个数组,您都可以通过以下方式简单地获取它:
Point[] pointArray = pointList.ToArray();
dynamically collect the x and y values in the allocated point array
当您使用列表时,就像:
pointList.Add(new Point(x, y));
我们不知道您如何填写 float x[]
和 float y[]
。如果可能的话,我不会首先使用这两个单独的数组,而只是从一开始就使用 pointList
。有一个警告:System.Drawing.Point 仅适用于 int
值,不适用于 float
值。所以我假设您打算收集坐标的 int
值。
dynamically create the point array from the float arrays x and y
如果您无法更改坐标集合并且有使用这些数组,您可以"zip"像这样将它们组合在一起:
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,则必须使用它 "by hand"。类似于:
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]);
}