// Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            List <List <float> > ret = calculator.calculate("16/(xy)");
            pi.initialParticle();
            pi2.initialParticle();
            float hotz = 0f;
            float maxz = 10;
            int   linegap = 20;
            int   countx = 0, county = 0;
            for (int i = 0; i <= calculator.sampleFreqy; i += 1)
            {
                county = (county + 1) % linegap;
                for (int j = 0; j <= calculator.sampleFreqx; j += 1)
                {
                    countx = (countx + 1) % linegap;
                    float nowx   = calculator.xStart + calculator.gap * j;
                    float nowy   = calculator.yStart + calculator.gap * i;
                    float mingap = calculator.gap;
                    float nowz   = ret[i][j];
                    if ((/*(hotz > maxz||float.IsNaN(hotz)) && */ nowz > maxz) || (/*(hotz < -maxz || float.IsNaN(hotz)) &&*/ nowz < -maxz))
                    {
                        hotz = float.NaN; continue;
                    }
                    if (float.IsNaN(nowz))
                    {
                        hotz = float.NaN; continue;
                    }
                    //Debug.Log(Mathf.Abs(hotz - nowz)-mingap);
                    if (j > 0 && Mathf.Abs(hotz - nowz) >= mingap && !float.IsNaN(hotz))
                    {
                        //
                        float deltax = calculator.gap * mingap / (Mathf.Abs(hotz - nowz));
                        float deltaz = (nowz - hotz) * mingap / (Mathf.Abs(hotz - nowz));
                        int   count  = 1;
                        for (float k = mingap; k < Mathf.Abs(hotz - nowz); k += mingap)
                        {
                            //Debug.Log("herer" + count);


                            Vector3 posit = new Vector3(nowx - calculator.gap + count * deltax, hotz + count * deltaz, nowy);
                            if (nowz > maxz || nowz < -maxz)
                            {
                                break;
                            }

                            if (county == linegap - 1)
                            {
                                pi2.drawParticle(posit);
                            }
                            else
                            {
                                pi.drawParticle(posit);
                            }
                            count++;
                            //if (count >= 10) { break; }
                        }
                    }
                    Vector3 posi = new Vector3((nowx), nowz, (nowy));
                    if (county == linegap - 1 || countx == linegap - 1)
                    {
                        pi2.drawParticle(posi);
                    }
                    else
                    {
                        pi.drawParticle(posi);
                    }

                    hotz = nowz;
                }
            }
            pi.endDraw();
            pi2.endDraw();

            Debug.Log("calcuFinished!");
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            List <List <float> > ret = calculator.calculate(tm.text);
            float maxz = 10;
            for (int i = 0; i <= calculator.sampleFreqy; i += 1)
            {
                for (int j = 0; j <= calculator.sampleFreqx; j += 1)
                {
                    if (float.IsNaN(ret[i][j]))
                    {
                        continue;
                    }

                    Vector3 posi = new Vector3((calculator.xStart + calculator.gap * i), ret[i][j], (calculator.yStart + calculator.gap * j));
                    Instantiate(pointModel, posi, new Quaternion(0, 0, 0, 0));
                }
            }
            Debug.Log("calcuFinished!");
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            cw.calculate("z*z+2xyz-16");
        }
        List <List <List <float> > > cwret = new List <List <List <float> > >();

        if ((cwret = cw.getCalcuResult()) != null)
        {
            Debug.Log("count1" + cwret.Count + " " + cw.sampleFreqz);
            Debug.Log("count2" + cwret[0].Count + " " + cw.sampleFreqy);
            pi.initialParticle();

            for (int i = 2; i <= cw.sampleFreqz - 2; i += 1)
            {
                for (int j = 2; j <= cw.sampleFreqy - 2; j += 1)
                {
                    for (int k = 2; k < cw.sampleFreqx - 2; k++)
                    {
                        float devi = Mathf.Abs(cwret[i][j][k]);
                        if (float.IsNaN(cwret[i][j][k]))
                        {
                            continue;
                        }

                        if (devi <= cw.gap * 4)
                        {
                            if (devi <= Mathf.Abs(cwret[i][j][k - 1 >= 0 ? (k - 1) : 0]) && devi <= Mathf.Abs(cwret[i][j][k + 1 < cw.sampleFreqx ? (k + 1) : cw.sampleFreqx - 1]))
                            {
                                Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                pi.drawParticle(posi);
                            }
                            else if (devi <= Mathf.Abs(cwret[i][j - 1 >= 0 ? (j - 1) : 0][k]) && devi <= Mathf.Abs(cwret[i][j + 1 < cw.sampleFreqy ? (j + 1) : cw.sampleFreqy - 1][k]))
                            {
                                Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                pi.drawParticle(posi);
                            }
                            else if (devi <= Mathf.Abs(cwret[i - 1 >= 0 ? (i - 1) : 0][j][k]) && devi <= Mathf.Abs(cwret[i + 1 < cw.sampleFreqz ? (i + 1) : cw.sampleFreqz - 1][j][k]))
                            {
                                Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                pi.drawParticle(posi);
                            }
                        }
                    }

                    //Debug.Log(ret[i][j]);
                }
            }
            //var mainModule = system.main;
            // mainModule.maxParticles = countp;
            pi.endDraw();
            Debug.Log("calcuFinished!");
        }


        if (Input.GetKeyDown(KeyCode.Y))
        {
            List <List <float> > ret = calculatory.calculate("pow(x,3)+pow(y,3)-6xy");

            pi.initialParticle();

            for (int i = 0; i <= calculatory.sampleFreqy; i += 1)
            {
                for (int j = 0; j <= calculatory.sampleFreqx; j += 1)
                {
                    if (float.IsNaN(ret[i][j]))
                    {
                        continue;
                    }
                    float devi = Mathf.Abs(ret[i][j]);

                    if (devi <= calculatory.gap * 25)
                    {
                        if (devi <= Mathf.Abs(ret[i][j - 1 >= 0?(j - 1):0]) && devi <= Mathf.Abs(ret[i][j + 1 < calculatory.sampleFreqx ? (j + 1) : calculatory.sampleFreqx - 1]))
                        {
                            Vector3 posi = new Vector3((calculatory.xStart + calculatory.gap * i), 0, (calculatory.yStart + calculatory.gap * j));
                            pi.drawParticle(posi);
                        }
                        else if (devi <= Mathf.Abs(ret[i - 1 >= 0 ? (i - 1) : 0][j]) && devi <= Mathf.Abs(ret[i + 1 < calculatory.sampleFreqy ? (i + 1) : calculatory.sampleFreqy - 1][j]))
                        {
                            Vector3 posi = new Vector3((calculatory.xStart + calculatory.gap * i), 0, (calculatory.yStart + calculatory.gap * j));
                            pi.drawParticle(posi);
                        }
                    }
                }
            }

            pi.endDraw();

            //float start = 0 - calculator.gap * calculator.sampleFreqx / 2;
            //for (int i = 0; i < ret.Count; i++) {
            //    Debug.Log(ret[i]);
            //}
            Debug.Log("calcuFinished!");
        }
        if (Input.GetKeyDown(KeyCode.T))
        {
            List <List <List <float> > > ret = calculatort.calculate("z*z+2xyz-16");

            pi.initialParticle();

            for (int i = 2; i <= calculatort.sampleFreqz - 2; i += 1)
            {
                for (int j = 2; j <= calculatort.sampleFreqy - 2; j += 1)
                {
                    for (int k = 2; k < calculatort.sampleFreqx - 2; k++)
                    {
                        float devi = Mathf.Abs(ret[i][j][k]);
                        if (float.IsNaN(ret[i][j][k]))
                        {
                            continue;
                        }

                        if (devi <= calculatort.gap * 4)
                        {
                            if (devi <= Mathf.Abs(ret[i][j][k - 1 >= 0 ? (k - 1) : 0]) && devi <= Mathf.Abs(ret[i][j][k + 1 < calculatort.sampleFreqx ? (k + 1) : calculatort.sampleFreqx - 1]))
                            {
                                Vector3 posi = new Vector3((calculatort.xStart + calculatort.gap * i), (calculatort.zStart + calculatort.gap * k), (calculatort.yStart + calculatort.gap * j));
                                pi.drawParticle(posi);
                            }
                            else if (devi <= Mathf.Abs(ret[i][j - 1 >= 0 ? (j - 1) : 0][k]) && devi <= Mathf.Abs(ret[i][j + 1 < calculatort.sampleFreqy ? (j + 1) : calculatort.sampleFreqy - 1][k]))
                            {
                                Vector3 posi = new Vector3((calculatort.xStart + calculatort.gap * i), (calculatort.zStart + calculatort.gap * k), (calculatort.yStart + calculatort.gap * j));
                                pi.drawParticle(posi);
                            }
                            else if (devi <= Mathf.Abs(ret[i - 1 >= 0 ? (i - 1) : 0][j][k]) && devi <= Mathf.Abs(ret[i + 1 < calculatort.sampleFreqz ? (i + 1) : calculatort.sampleFreqz - 1][j][k]))
                            {
                                Vector3 posi = new Vector3((calculatort.xStart + calculatort.gap * i), (calculatort.zStart + calculatort.gap * k), (calculatort.yStart + calculatort.gap * j));
                                pi.drawParticle(posi);
                            }
                        }
                    }

                    //Debug.Log(ret[i][j]);
                }
            }
            //var mainModule = system.main;
            // mainModule.maxParticles = countp;
            pi.endDraw();



            Debug.Log("calcuFinished!");
        }
    }
        private void Update()
        {
            Vector3 oldPos = this.transform.position;

            FollowBoundingBox(true);
            Vector3 expectedPos = this.transform.position;

            if (Vector3.Distance(expectedPos, generatedMesh.transform.position)
                > generatedMesh.transform.localScale.x * 18.0f)
            {
                this.transform.position = oldPos;
            }



            switch (State)
            {
            case AppBarStateEnum.Default:
                targetBarSize = new Vector3(numDefaultButtons * buttonWidth, buttonWidth, 1f);
                break;

            case AppBarStateEnum.Hidden:
                targetBarSize = new Vector3(numHiddenButtons * buttonWidth, buttonWidth, 1f);
                break;

            case AppBarStateEnum.Manipulation:
                targetBarSize = new Vector3(numManipulationButtons * buttonWidth, buttonWidth, 1f);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            backgroundBar.transform.localScale = Vector3.Lerp(backgroundBar.transform.localScale, targetBarSize, 0.5f);

            if (Input.GetKeyDown(KeyCode.P))
            {
                Calculator4D catmp = new Calculator4D(-5, -5, -5, 5, 5, 5, 0.1f);
                //catmp.calculate("z*z+2xyz-16");
                cw.calculate("z*z+2xyz-16");
            }
            if (Input.GetKeyDown(KeyCode.O))
            {
                Calculator4D catmp = new Calculator4D(-5, -5, -5, 5, 5, 5, 0.1f);
                //catmp.calculate("z*z+2xyz-16");
                cw.calculatevec("pow(x,3)+pow(y,3)-6xy");
            }
            List <List <float> > cwvecret;

            if ((cwvecret = cw.getCalcuResultvec()) != null)
            {
                pi.initialParticle();

                for (int i = 0; i <= cw.sampleFreqy * 10 - 1; i += 1)
                {
                    for (int j = 0; j <= cw.sampleFreqx * 10 - 1; j += 1)
                    {
                        //Debug.Log("herej" + j);
                        if (float.IsNaN(cwvecret[i][j]))
                        {
                            continue;
                        }
                        float devi = Mathf.Abs(cwvecret[i][j]);

                        if (devi <= cw.gap / 10 * 25)
                        {
                            if (devi <= Mathf.Abs(cwvecret[i][j - 1 >= 0 ? (j - 1) : 0]) && devi <= Mathf.Abs(cwvecret[i][j + 1 < cw.sampleFreqx * 10 ? (j + 1) : cw.sampleFreqx * 10 - 1]))
                            {
                                Vector3 posi = new Vector3((cw.xMin + cw.gap / 10 * i), (cw.yMin + cw.gap / 10 * j), 0);
                                pi.drawParticle(posi);
                            }
                            else if (devi <= Mathf.Abs(cwvecret[i - 1 >= 0 ? (i - 1) : 0][j]) && devi <= Mathf.Abs(cwvecret[i + 1 < cw.sampleFreqy * 10 ? (i + 1) : cw.sampleFreqy * 10 - 1][j]))
                            {
                                Vector3 posi = new Vector3((cw.xMin + cw.gap / 10 * i), (cw.yMin + cw.gap / 10 * j), 0);
                                pi.drawParticle(posi);
                            }
                        }
                    }
                }

                pi.endDraw();
            }

            List <List <List <float> > > cwret;

            if ((cwret = cw.getCalcuResult()) != null)
            {
                //todo destroy the inform
                Debug.Log("here");

                pi.initialParticle();

                for (int i = 2; i <= cw.sampleFreqz - 2; i += 1)
                {
                    for (int j = 2; j <= cw.sampleFreqy - 2; j += 1)
                    {
                        for (int k = 2; k < cw.sampleFreqx - 2; k++)
                        {
                            float devi = Mathf.Abs(cwret[i][j][k]);
                            if (float.IsNaN(cwret[i][j][k]))
                            {
                                continue;
                            }

                            if (devi <= cw.gap * 4)
                            {
                                if (devi <= Mathf.Abs(cwret[i][j][k - 1 >= 0 ? (k - 1) : 0]) && devi <= Mathf.Abs(cwret[i][j][k + 1 < cw.sampleFreqx ? (k + 1) : cw.sampleFreqx - 1]))
                                {
                                    Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                    pi.drawParticle(posi);
                                }
                                else if (devi <= Mathf.Abs(cwret[i][j - 1 >= 0 ? (j - 1) : 0][k]) && devi <= Mathf.Abs(cwret[i][j + 1 < cw.sampleFreqy ? (j + 1) : cw.sampleFreqy - 1][k]))
                                {
                                    Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                    pi.drawParticle(posi);
                                }
                                else if (devi <= Mathf.Abs(cwret[i - 1 >= 0 ? (i - 1) : 0][j][k]) && devi <= Mathf.Abs(cwret[i + 1 < cw.sampleFreqz ? (i + 1) : cw.sampleFreqz - 1][j][k]))
                                {
                                    Vector3 posi = new Vector3((cw.xMin + cw.gap * i), (cw.zMin + cw.gap * k), (cw.yMin + cw.gap * j));
                                    pi.drawParticle(posi);
                                }
                            }
                        }
                    }
                }

                pi.endDraw();
            }
        }