Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16.  
  17. namespace WpfApplicationGraphics
  18. {
  19.     public partial class MainWindow : Window
  20.     {
  21.         ManualResetEvent MRE = new ManualResetEvent(true);
  22.  
  23.         double x0 = 250;
  24.         double y0 = 150;
  25.         double r = 100;
  26.         double a = 0;
  27.        
  28.         Point pt = new Point();
  29.        
  30.         int numPoints = 300;
  31.        
  32.         Polyline pline = new Polyline();
  33.        
  34.  
  35.         BackgroundWorker Worker = new BackgroundWorker() { WorkerReportsProgress = true };
  36.  
  37.         public MainWindow()
  38.         {
  39.             InitializeComponent();
  40.  
  41.             StopButton.Click += (o, e) => MRE.Reset();
  42.             StartButton.Click += (o, e) => MRE.Set();
  43.  
  44.             Worker.DoWork += (o, e) => DrawSpiroGraph(0);
  45.             Worker.ProgressChanged += (o, e) => Progressbar1.Value = e.ProgressPercentage;
  46.         }
  47.  
  48.         public void SetLineFeatures()
  49.         {
  50.             pline = new Polyline();
  51.             pline.Stroke = Brushes.Blue;
  52.         }
  53.  
  54.  
  55.         static Random rand = new Random((int)DateTime.Now.Ticks);
  56.         double aaFactor = (rand.Next(10, 96)) * (-0.01);
  57.  
  58.         void DrawSpiroGraph(int iteration) {
  59.             Thread.Sleep(30);
  60.             MRE.WaitOne();
  61.  
  62.             pt = new Point();
  63.             pt.X = x0 + r * Math.Cos(a);
  64.             pt.Y = y0 + r * Math.Sin(a);
  65.  
  66.             double rr = 0.5 * r;
  67.             double aa = aaFactor * a;
  68.             Point pnt = new Point();
  69.             pnt.X = pt.X + rr * Math.Cos(aa);
  70.             pnt.Y = pt.Y + rr * Math.Sin(aa);
  71.             a += 0.5;
  72.             pline.Dispatcher.BeginInvoke((Action)(() => pline.Points.Add(pnt)));
  73.  
  74.             Worker.ReportProgress((int)(((float)iteration / numPoints) * 100));
  75.  
  76.             if (iteration < numPoints) DrawSpiroGraph(iteration+1);
  77.         }
  78.  
  79.        
  80.         private void button1_Click(object sender, RoutedEventArgs e)
  81.         {
  82.             if (Worker.IsBusy) return;
  83.  
  84.             if (canvas.Children.Contains(pline))
  85.                 canvas.Children.Remove(pline);
  86.  
  87.             SetLineFeatures();
  88.             Worker.RunWorkerAsync();
  89.             canvas.Children.Add(pline);
  90.         }
  91.  
  92.  
  93.         private void button2_Click(object sender, RoutedEventArgs e) { this.Close(); }
  94.        
  95.  
  96.     }
  97. }
Add Comment
Please, Sign In to add comment