Lati
Lati

Reputation: 341

Dynamically create a point array

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?

Upvotes: 3

Views: 10592

Answers (3)

Corak
Corak

Reputation: 2788

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]);
}

Upvotes: 7

Andre
Andre

Reputation: 1238

Given the values in both arrays have the same Index you can itereate over them, create points and add them to a list.

List<Point> points = new List<Point>();
for(int i = 0; i < x.Length; i++){
    points.Add(new Point(x[i],y[i]);
}

I don't see a particular problem in using DrawCurve here.

If you need an array of Points use points.ToArray();

Upvotes: 1

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

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.

Upvotes: 2

Related Questions