How to transfer all coordinates from a WPF PolyLine object?

I have a graphic image made dynamically with a polyline object. It creates something interesting, but I would like to save only the last 10 coordinates, and as soon as we reach the 10th position, each coordinate will move to the left by an X-pixel, and a new value will be added at the end.

In the "Add" function of my drawing class, I tried this code:

        if (points.Count > 10)
        {
            myPolyline.Points.RemoveAt(0);

            foreach(Point p in myPolyline.Points)
            {
                p.X = p.X - 50;//Move all coord back to have a place for the new one
            }   
        }

This does not work because we cannot change the collection variable in a ForEach loop. What is the best way to do this in WPF / C #?

More details

I can do this by doing this:

            for (int i = 0; i < this.myPolyline.Points.Count; i++)
            {
                this.myPolyline.Points[i] = new Point(this.myPolyline.Points[i].X - 50, this.myPolyline.Points[i].Y);
            }

But I would like to make a cleaner way to do this without creating an instant object.

+3
2

, Point - , . ...

Point p = this.myPolyline.Points[i];
p.X -= 50;
this.myPolyline.Points[i] = p;

... , , .

for myPolyline.Points[i], :

  • Point X.
  • foreach, for.
  • myPolyline.Points[i].X -= 50 - , Point , .

PolyLine, LayoutTransform RenderTransform, Point, .

: , PointCollection for :

static public void ChangePoints( this PointCollection pc, Vector v ) {
    for (int i = 0; i < pc.Count; i++ ) {
        pc[i] += v;
        // the above works on the indexer because you're doing an operation on the
        // Point rather than on one of the Point members.
    }
}

:

myPolyline.Points.ChangePoints( new Vector( -50, 0 ) );

Point , , . Vector .

+4

.

, ( ).

+3

Source: https://habr.com/ru/post/1714999/


All Articles