Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApplicationGraphics
- {
- public partial class MainWindow : Window
- {
- ManualResetEvent MRE = new ManualResetEvent(true);
- double x0 = 250;
- double y0 = 150;
- double r = 100;
- double a = 0;
- Point pt = new Point();
- int numPoints = 300;
- Polyline pline = new Polyline();
- BackgroundWorker Worker = new BackgroundWorker() { WorkerReportsProgress = true };
- public MainWindow()
- {
- InitializeComponent();
- StopButton.Click += (o, e) => MRE.Reset();
- StartButton.Click += (o, e) => MRE.Set();
- Worker.DoWork += (o, e) => DrawSpiroGraph(0);
- Worker.ProgressChanged += (o, e) => Progressbar1.Value = e.ProgressPercentage;
- }
- public void SetLineFeatures()
- {
- pline = new Polyline();
- pline.Stroke = Brushes.Blue;
- }
- static Random rand = new Random((int)DateTime.Now.Ticks);
- double aaFactor = (rand.Next(10, 96)) * (-0.01);
- void DrawSpiroGraph(int iteration) {
- Thread.Sleep(30);
- MRE.WaitOne();
- pt = new Point();
- pt.X = x0 + r * Math.Cos(a);
- pt.Y = y0 + r * Math.Sin(a);
- double rr = 0.5 * r;
- double aa = aaFactor * a;
- Point pnt = new Point();
- pnt.X = pt.X + rr * Math.Cos(aa);
- pnt.Y = pt.Y + rr * Math.Sin(aa);
- a += 0.5;
- pline.Dispatcher.BeginInvoke((Action)(() => pline.Points.Add(pnt)));
- Worker.ReportProgress((int)(((float)iteration / numPoints) * 100));
- if (iteration < numPoints) DrawSpiroGraph(iteration+1);
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- if (Worker.IsBusy) return;
- if (canvas.Children.Contains(pline))
- canvas.Children.Remove(pline);
- SetLineFeatures();
- Worker.RunWorkerAsync();
- canvas.Children.Add(pline);
- }
- private void button2_Click(object sender, RoutedEventArgs e) { this.Close(); }
- }
- }
Add Comment
Please, Sign In to add comment