public double GiveMePi() { int N = 1000; double pi = 0; Random randObj = new Random(); Dictionary <int, CircleCoordinate> CirclePoints = new Dictionary <int, CircleCoordinate>(); Dictionary <int, bool> CirclePointsUsed = new Dictionary <int, bool>(); CircleCoordinate circlePoint = new CircleCoordinate(1, 1); CirclePoints.Add(0, circlePoint); CirclePointsUsed.Add(0, false); circlePoint = new CircleCoordinate(1, 0); CirclePoints.Add(1, circlePoint); CirclePointsUsed.Add(1, false); for (int i = 2; i < N; i++) { double rand = randObj.NextDouble(); circlePoint = new CircleCoordinate(1, rand); CirclePoints.Add(i, circlePoint); //circlePoint.DisplayCoordinate(); CirclePointsUsed.Add(i, false); } CircleCoordinate currentCirclePoint = CirclePoints[0]; CirclePointsUsed[0] = true; double Distance; double MinDistance; //Safe upper limit double TotalDistance = 0; int MinDistancePos = 1; for (int i = 1; i < N; i++) { MinDistance = 2.0; for (int j = 1; j < N; j++) { if (CirclePointsUsed[j] == false) { Distance = currentCirclePoint.Distance(CirclePoints[j]); if (MinDistance > Distance) { MinDistance = Distance; MinDistancePos = j; } } } TotalDistance += MinDistance; CirclePoints[MinDistancePos].DisplayCoordinate(); Console.WriteLine("Total Dist = {0}", TotalDistance); CirclePointsUsed[MinDistancePos] = true; currentCirclePoint = CirclePoints[MinDistancePos]; } return(TotalDistance * 2); }
//Maths public double Distance(CircleCoordinate input) { double DistTot; double Dist1; double Dist2; Dist1 = input.pos1() - this.pos1(); Dist2 = input.pos2() - this.pos2(); DistTot = Math.Sqrt(Dist1 * Dist1 + Dist2 * Dist2); return(DistTot); }