Ejemplo n.º 1
0
        private float SolveTree(float x, float y, BinTreeNode <string> tree)
        {
            switch (tree.GetInfo())
            {
            case "x":
                return(x);

            case "y":
                return(y);

            case "+":
                return(SolveTree(x, y, tree.GetLeft()) + SolveTree(x, y, tree.GetRight()));

            case "-":
                return(SolveTree(x, y, tree.GetLeft()) - SolveTree(x, y, tree.GetRight()));

            case "*":
                return(SolveTree(x, y, tree.GetLeft()) * SolveTree(x, y, tree.GetRight()));

            case "/":
                return(SolveTree(x, y, tree.GetLeft()) / SolveTree(x, y, tree.GetRight()));

            case "^":
                return((float)Math.Pow(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "sin":
                return((float)Math.Sin(SolveTree(x, y, tree.GetLeft())));

            case "cos":
                return((float)Math.Cos(SolveTree(x, y, tree.GetLeft())));

            case "tan":
                return((float)Math.Tan(SolveTree(x, y, tree.GetLeft())));

            case "abs":
                return((float)Math.Abs(SolveTree(x, y, tree.GetLeft())));

            case "pow":
                return((float)Math.Pow(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "max":
                return((float)Math.Max(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "min":
                return((float)Math.Min(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "sqrt":
                return((float)Math.Sqrt(SolveTree(x, y, tree.GetLeft())));



            default:
                return(float.Parse(tree.GetInfo()));
            }
        }
Ejemplo n.º 2
0
        public Mesh GetMesh()
        {
            //func = GetValidFunc(inputCOBO.Text);
            BinTreeNode <string> tree = new BinTreeNode <string>(func);

            MakeTree(tree);


            int   range = 100;
            float range_Offset = range / 2;
            float jumpSize = 0.1f;
            float xx, yy;

            Mesh ret = new Mesh(func);

            int[,] vert_index = new int[range, range];

            for (int y = 0; y < range; y++)
            {
                for (int x = 0; x < range; x++)
                {
                    xx = (x - range_Offset) * jumpSize;
                    yy = (y - range_Offset) * jumpSize;

                    ret.Vertices.Add(new Vertex(xx, SolveTree(xx, yy, tree), yy));
                    ret.Vertices[ret.Vertices.Count - 1].Texel = new Vector3(x, y, 0);
                    vert_index[x, y] = ret.Vertices.Count - 1;
                }
            }

            for (int y = 0; y < range - 1; y++)
            {
                for (int x = 0; x < range - 1; x++)
                {
                    ret.Faces.Add(new Face(ret.Vertices[vert_index[x, y]], ret.Vertices[vert_index[x, y + 1]], ret.Vertices[vert_index[x + 1, y]]));
                    ret.Faces[ret.Faces.Count - 1].Set_Parent(ret);
                    ret.Faces.Add(new Face(ret.Vertices[vert_index[x + 1, y + 1]], ret.Vertices[vert_index[x + 1, y]], ret.Vertices[vert_index[x, y + 1]]));
                    ret.Faces[ret.Faces.Count - 1].Set_Parent(ret);
                }
            }


            foreach (Vertex vertex in ret.Vertices)
            {
                vertex.Normal_Calculate();
            }


            return(ret);
        }
Ejemplo n.º 3
0
 public void SetRight(BinTreeNode <T> tree)
 {
     this.right = tree;
 }
Ejemplo n.º 4
0
 public void SetLeft(BinTreeNode <T> tree)
 {
     this.left = tree;
 }
Ejemplo n.º 5
0
 public BinTreeNode(BinTreeNode <T> left, T x, BinTreeNode <T> right)
 {
     this.left  = left;
     this.info  = x;
     this.right = right;
 }
Ejemplo n.º 6
0
 public BinTreeNode(T x)
 {
     this.left  = null;
     this.info  = x;
     this.right = null;
 }
Ejemplo n.º 7
0
        private void MakeTree(BinTreeNode <string> tree)
        {
            short  bracketsCounter = 0;
            string s;
            string tempS = tree.GetInfo();

            if (IsWrappedWithBrackets(tempS))
            {
                tempS = tempS.Substring(1, tempS.Length - 2);
                tree.SetInfo(tempS);
            }

            // +, -
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    if (s == "+" || s == "-")
                    {
                        if (i == 0)
                        {
                            tempS = '0' + tempS;
                            i++;
                        }

                        tree.SetLeft(new BinTreeNode <string>(tempS.Substring(0, i)));
                        MakeTree(tree.GetLeft());
                        tree.SetRight(new BinTreeNode <string>(tempS.Substring(i + 1)));
                        MakeTree(tree.GetRight());

                        tree.SetInfo(s);
                        i     = tempS.Length;
                        tempS = s;
                    }
                }
            }

            // *, /, ^
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    if (s == "*" || s == "/" || s == "^")
                    {
                        if (i == 0)
                        {
                            tempS = '0' + tempS;
                            i++;
                        }

                        tree.SetLeft(new BinTreeNode <string>(tempS.Substring(0, i)));
                        MakeTree(tree.GetLeft());
                        tree.SetRight(new BinTreeNode <string>(tempS.Substring(i + 1)));
                        MakeTree(tree.GetRight());

                        tree.SetInfo(s);
                        i     = tempS.Length;
                        tempS = s;
                    }
                }
            }

            //
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    //if (s == "*" || s == "/" || s == "^")
                    //{
                    //    tree.SetLeft(new BinTreeNode<string>(tempS.Substring(0, i)));
                    //    MakeTree(tree.GetLeft());
                    //    tree.SetRight(new BinTreeNode<string>(tempS.Substring(i + 1)));
                    //    MakeTree(tree.GetRight());

                    //    tree.SetInfo(s);
                    //    i = tempS.Length;
                    //}


                    //else
                    if (tempS.Length > i + 3)
                    {
                        s = tempS.Substring(i, 3);

                        if (s == "sin" || s == "cos" || s == "tan" || s == "abs" || s == "log")
                        {
                            tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 3)));
                            MakeTree(tree.GetLeft());

                            tree.SetInfo(s);
                            i = tempS.Length;
                        }
                        else if (s == "max" || s == "min" || s == "pow")
                        {
                            int g = i + 3;
                            for (; g < tempS.Length && tempS[g] != ','; g++)
                            {
                                ;
                            }

                            tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 4, g - i - 4)));
                            MakeTree(tree.GetLeft());
                            tree.SetRight(new BinTreeNode <string>(tempS.Substring(g + 1)));
                            MakeTree(tree.GetRight());

                            tree.SetInfo(s);
                            i = tempS.Length;
                        }


                        else if (tempS.Length > i + 4)
                        {
                            s = tempS.Substring(i, 4);

                            if (s == "sqrt")
                            {
                                tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 4)));
                                MakeTree(tree.GetLeft());

                                tree.SetInfo(s);
                                i = tempS.Length;
                            }
                        }
                    }
                }
            }
        }