Guest User

Untitled

a guest
Jul 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 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.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.        
  25.         double x0 = 250;
  26.         double y0 = 150;
  27.         double r = 100;
  28.         double a = 0;
  29.        
  30.         Point pt = new Point();
  31.        
  32.         int numPoints = 300;
  33.        
  34.         Polyline pline = new Polyline();
  35.        
  36.         Random rand = new Random((int)DateTime.Now.Ticks);
  37.  
  38.         BackgroundWorker Worker = new BackgroundWorker();
  39.         public MainWindow()
  40.         {
  41.             InitializeComponent();
  42.  
  43.             Worker.DoWork += (o, e) => DrawSpiroGraph();          
  44.         }
  45.  
  46.         public void SetLineFeatures()
  47.         {
  48.             pline = new Polyline();
  49.             pline.Stroke = Brushes.Blue;
  50.         }
  51.  
  52.  
  53.         public void DrawSpiroGraph()
  54.         {
  55.             double aaFactor = (rand.Next(10, 96)) * (-0.01);
  56.  
  57.             for (int i = 0; i <= numPoints; i++)
  58.             {
  59.                 Thread.Sleep(25);
  60.                 pt = new Point();
  61.                 pt.X = x0 + r * Math.Cos(a);
  62.                 pt.Y = y0 + r * Math.Sin(a);
  63.  
  64.                 double rr = 0.5 * r;
  65.                 double aa = aaFactor * a;
  66.                 Point pnt = new Point();
  67.                 pnt.X = pt.X + rr * Math.Cos(aa);
  68.                 pnt.Y = pt.Y + rr * Math.Sin(aa);
  69.                 a += 0.5;
  70.                 //System.Threading.Thread.Sleep(1000);
  71.                 pline.Dispatcher.BeginInvoke((Action)(() => pline.Points.Add(pnt)));
  72.             }
  73.         }
  74.        
  75.         private void button1_Click(object sender, RoutedEventArgs e)
  76.         {
  77.             if (canvas.Children.Contains(pline))
  78.                 canvas.Children.Remove(pline);
  79.  
  80.             SetLineFeatures();
  81.             Worker.RunWorkerAsync();
  82.             canvas.Children.Add(pline);
  83.         }
  84.  
  85.         private void button2_Click(object sender, RoutedEventArgs e)
  86.         {
  87.             this.Close();
  88.         }
  89.        
  90.  
  91.     }
  92. }
Add Comment
Please, Sign In to add comment