private void BtnStart_Click(object sender, RoutedEventArgs e) { Random Rnd = new Random(); if (!started) { Counter = 0; Coprimes = 0; PieGraph.DataSeries.Clear(); PieGraph.LegendPosition = Graphs.GraphData.LegendPosition.Top; PiCalc1 = new Graphs.ScatterSeries(0); PiCalc1.SeriesName = "Pie Estimate"; PiCalc1.LineColor = Brushes.Red; PiCalc1.MarkerType = Graphs.GraphData.MarkerType.None; PiValue = new Graphs.ScatterSeries(1); PiValue.SeriesName = "Pie Value"; PiValue.LineColor = Brushes.Blue; PiValue.MarkerType = Graphs.GraphData.MarkerType.None; PieGraph.DataSeries.Add(PiCalc1); PieGraph.DataSeries.Add(PiValue); PieGraph.YAxis.Minimum = 3.12; PieGraph.YAxis.Maximum = 3.16; PieGraph.XAxis.Minimum = 0; PieGraph.XAxis.Maximum = 500; PieGraph.XAxis.FixedMaximum = true; BtnStart.Content = "STOP"; started = true; } else { BtnStart.Content = "START"; started = false; } while (started) { N1 = Rnd.Next(MaxN); N2 = Rnd.Next(MaxN); if (N1 > 0 & N2 > 0) { Counter += 1; if (GGD(N1, N2) == 1) { Coprimes += 1; } if (Coprimes > 0) { if (Counter % 1000 == 0) { CalculatedPi = Math.Sqrt(6 * Counter / Coprimes); PiCalc1.AddDataPoint(new Point(Counter / 1000, CalculatedPi)); PiValue.AddDataPoint(new Point(Counter / 1000, Math.PI)); Dispatcher.Invoke(Redraw, DispatcherPriority.ApplicationIdle); } if (Counter > 500000) { PieGraph.XAxis.FixedMaximum = false; } if (Counter > 3000000) { PiCalc1.DataList.RemoveAt(0); PiValue.DataList.RemoveAt(0); continue; } } } } }
private void BtnStart_Click(object sender, RoutedEventArgs e) { bool Add = true; if (!started) { number = 1; CalculatedPie = 0.0; CalculatedE = 1.0; PieGraph.DataSeries.Clear(); PieGraph.LegendPosition = Graphs.GraphData.LegendPosition.Top; eGraph.DataSeries.Clear(); eGraph.LegendPosition = Graphs.GraphData.LegendPosition.Top; PiCalc = new Graphs.ScatterSeries(0); PiCalc.SeriesName = "Pie Estimate"; PiCalc.LineColor = Brushes.Red; PiCalc.MarkerType = Graphs.GraphData.MarkerType.None; PiValue = new Graphs.ScatterSeries(1); PiValue.SeriesName = "Pie Value"; PiValue.LineColor = Brushes.Blue; PiValue.MarkerType = Graphs.GraphData.MarkerType.None; ECalc = new Graphs.ScatterSeries(2); ECalc.SeriesName = "e Estimate"; ECalc.LineColor = Brushes.Purple; ECalc.MarkerType = Graphs.GraphData.MarkerType.None; EValue = new Graphs.ScatterSeries(3); EValue.SeriesName = "e Value"; EValue.LineColor = Brushes.Green; EValue.MarkerType = Graphs.GraphData.MarkerType.None; PieGraph.DataSeries.Add(PiValue); PieGraph.DataSeries.Add(PiCalc); eGraph.DataSeries.Add(EValue); eGraph.DataSeries.Add(ECalc); BtnStart.Content = "STOP"; started = true; } else { BtnStart.Content = "START"; started = false; if (PieGraph.DataSeries[1].DataList.Count >= 10) { double averagePi = 0; Point[] estimates = PieGraph.DataSeries[1].DataList.ToArray(); for (int I = 0; I < 10; I++) { averagePi += estimates[estimates.Length - I - 1].Y; } averagePi /= 10; TxtEstimate.Text = averagePi.ToString(); } } while (started) { if (Add) { CalculatedPie += 1.0 / number; } else { CalculatedPie -= 1.0 / number; } CalculatedE = 1.0; for (int I = 1; I <= number; I++) { CalculatedE *= (1.0 + 1.0 / number); } Add = !Add; PiCalc.AddDataPoint(new Point(number, 4.0 * CalculatedPie)); ECalc.AddDataPoint(new Point(number, CalculatedE)); PiValue.AddDataPoint(new Point(number, Math.PI)); EValue.AddDataPoint(new Point(number, Math.E)); Dispatcher.Invoke(Redraw, DispatcherPriority.ApplicationIdle); number += 2; if (number > 2000) { BtnStart.Content = "START"; started = false; continue; } } }