C # move polyline

I am new to graphical programming with C #. Yesterday I started a new project (WPF). There is a Polyline object that I have to move around the screen with the coordinates that I calculate. I do not know how to move an object and do something like animation. When I click the mouse, I want to run this Move() method after that to go into the while loop and when the condition is complete (end == true) I want to end the loop and complete the animation. And while I'm in a loop, my idea is to move my polyline with slow movements. I tried putting Move() on a thread and using Thread.Sleep(...); , but I could only see the final position in the whole Polyline way. I tried putting it in new Thread(new ThreadStart(Move)); ... and this.Dispatcher.BeginInvoke , the effect was the same. Could you please tell me how can I make this movement?

  public void Move() { bool end = false; while (!end) { double x = lastPosX; double y = lastPosY; double a = y1 - y; double b = x - x1; double c = -x * y1 + x1 * y; double u, v; GetC(out u, out v); if (y1 < lastPosY) { GetCoordinates(ref u, ref v); } if (u > width || v > height) { gameEnd = true; } lastPosX = u; lastPosY = v; p.Points.Remove(p.Points.First()); p.Points.Add(new Point(u, v)); } } 
+4
source share
1 answer

I was not able to understand how your Move method works, but here is an example of how you can move a polyline from left to right in MouseDown. Hope you can adapt it to your needs.

Xaml

 <Canvas Name="myCanvas"> <Polyline Name="myPolyline" MouseDown="Polyline_MouseDown" Canvas.Left="75" Canvas.Top="50" Points="25,25 0,50 25,75 50,50 25,25 25,0" Stroke="Blue" StrokeThickness="10"/> </Canvas> 

Code for

 private void Polyline_MouseDown(object sender, MouseButtonEventArgs e) { double left = Canvas.GetLeft(myPolyline); var animationThread = new Thread(new ThreadStart(() => { while (left < 300) { left += 10; // SetLeft is done in the UI thread Dispatcher.Invoke(new Action(() => { Canvas.SetLeft(myPolyline, left); })); Thread.Sleep(50); } })); animationThread.Start(); } 
+1
source

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


All Articles