How to make a struct component a variable?

Not sure I am asking this right or this even possible. I feel to explain my question it is best to ask right in the code at the relevant places so please see my comments in the snippet below.

I wonder how to achieve this without building a new list of values for each I iteration. I feel this should not be necessary.

The bigger picture of this loop is to plot individual dimensions of 3D points to three new 2D plots of these. Hope that makes sense.

for (int i = 0; i < 3; i++) // 3 iterations (X,Y;Z)
{           
    // what here? how to make the data component of Vector3D a variable
    for (int k = 0; k <= Points.Count - 1; k++)
    {
        Vector2D TL = new Vector2D();

        TL.x = ((1 / (float)FrameCount.Sum()) * k);
        TL.y = Points[k].x;      // on i = 0 want Points[k].x
                                 // on i = 1 want Points[k].y
                                 // on i = 2 want Points[k].z

        TimelinePoints.Add(TL); // just collect to a flat list for now
    }
}
Jon Skeet
people
quotationmark

One option would be to have an array of extraction functions that you could apply to points. You can then use the LINQ Select overload that accepts a Func<TInput, int, TOutput> to generate a sequence of the values you want to add, and add it to TimeLinePoints that way.

// Possibly store this in a static variable somewhere
var extractors = new Func<Point, float>[] { p => p.x, p => p.y, p => p.z };

// Just do this once; store the result as a float for simplicity when dividing later.
float frameSum = FrameCount.Sum();
foreach (var extractor in extractors)
{
    TimeLinePoints.AddRange(Points.Select((point, index) =>
        new Vector2D(index / frameSum, extractor(point));
}

(You could go even further using SelectMany potentially, but that's where I'd start...)

people

See more on this question at Stackoverflow