示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (SimulationPaused)
        {
            if (SimulationSave)
            {
                if (SimulationTrace.Count > 0)
                {
                    GenerateFile();
                }
                SimulationSave = false;
            }
            if (SimulationReset)
            {
                Scene scene = SceneManager.GetActiveScene();
                SceneManager.LoadScene(scene.name);
            }
        }
        else
        {
            //Generate the Uniform Random Number
            for (int i = 0; i < UpdateInterval; i++)
            {
                double RandomX    = UnityEngine.Random.Range(0.0f, Radius);
                double RandomY    = UnityEngine.Random.Range(0.0f, Radius);
                Color  PointColor = Color.white;

                //Calculate the Result
                if (Math.Sqrt((RandomX * RandomX) + (RandomY * RandomY)) <= Radius)
                {
                    Interior_N += 1;
                    PointColor  = Color.blue;
                }
                else
                {
                    PointColor = Color.green;
                }
                PointColor.a = 0.4f;
                Total_N     += 1;


                Estimate = 4 * ((float)Interior_N / Total_N);

                //Generate the Interval Record for the Runtime Trace
                RuntimeData TraceLineItem;
                TraceLineItem.GeneratedX    = RandomX;
                TraceLineItem.GeneratedY    = RandomY;
                TraceLineItem.EstimatedPi   = Estimate;
                TraceLineItem.InteriorCount = Interior_N;
                TraceLineItem.TotalCount    = Total_N;
                SimulationTrace.Add(TraceLineItem);

                //Generate Visual
                UILineRenderer Point = UILineRenderer.Instantiate(Boundaries);
                Point.rectTransform.SetParent(Boundaries.rectTransform.parent);
                Point.rectTransform.anchoredPosition = Boundaries.rectTransform.anchoredPosition;
                Point.rectTransform.localScale       = new Vector3(1f, 1f, 1f);
                Point.rectTransform.localPosition    = new Vector3(Point.rectTransform.localPosition.x, Point.rectTransform.localPosition.y, 0f);
                Point.points.Clear();
                Point.color = PointColor;
                Point.points.Add(new Vector2((float)RandomX - 0.05f, (float)RandomY - 0.05f));
                Point.points.Add(new Vector2((float)RandomX + 0.05f, (float)RandomY + 0.05f));
            }

            //Set the Description Labels
            TotalNumberOfPoints.text    = Total_N.ToString();
            NumberOfInteriorPoints.text = Interior_N.ToString();
            EstimationOfPi.text         = Estimate.ToString();
        }
    }