Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create a point array

Tags:

c#

point

I would like to draw a curve using graphics.DrawCurve and I have x and y values in separate arrays (float x[] and float y[]). As DrawCurve needs point array as input, I need to convert or dynamically create the point array from the float arrays x and y. Is there any quick way for this?

I have around 20000 points for plotting the curve, Is it good idea to use graphics.DrawCurve on this purpose?

like image 283
Lati Avatar asked Dec 03 '25 23:12

Lati


2 Answers

There are several questions to answer.

I couldn't find out how to allocate a point array.

Well, there is no difference in allocating a point array than to do so for any other kind of array:

const int size = 100;
Point[] pointArray = new Point[size];

But arrays are missing some "convenience". For example, they have a fixed size that you need to specify at the point of initialization (allocation). And if you need more space, you have to manually create a new (bigger) array and copy all the values from the old to the new.

That's why almost everywhere you would work with an array, you're probably better off using a list:

List<Point> pointList = new List<Point>();

And then, wherever you actually need to pass an array, you can simply get it via:

Point[] pointArray = pointList.ToArray();

dynamically collect the x and y values in the allocated point array

When you work with a list, that's as easy as:

pointList.Add(new Point(x, y));

We don't know how you fill your float x[] and float y[]. If possible, I'd not have those two separate arrays in the first place and simply use the pointList from the start. With one caveat: a System.Drawing.Point only works with int values, not with float values. So I assume you meant to collect int values for the coordinates.

dynamically create the point array from the float arrays x and y

If you cannot change the collection of the coordinates and have to work with those arrays, you can "zip" them together like this:

IEnumerable<Point> points = x.Zip(y, (xCoord, yCoord) => 
    (new Point((int)xCoord, (int)yCoord));

Or, if you know you need an array:

Point[] pointArray = x.Zip(y, (xCoord, yCoord) => 
    (new Point((int)xCoord, (int)yCoord)).ToArray();

For this, you need to be able to use System.Linq (in other words higher than .Net 2.0).

If you cannot use Linq, you have to do it "by hand". Something like:

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]);
}
like image 74
Corak Avatar answered Dec 06 '25 13:12

Corak


You can create List<Point> which is better than arrays.

List<Point> list = new List<Point>();
Point point=new Point(10,15);
list.Add(point);

You can get all x and y coordinates from two arrays and put together in list as points and than use this list to draw curve.

like image 37
Mairaj Ahmad Avatar answered Dec 06 '25 14:12

Mairaj Ahmad