示例#1
0
        /// <summary>
        /// Graph the equation.
        /// </summary>
        private void Graph()
        {
            try {
                Equation equation = Equation.ParseEquation(_equation);
                Equation xmin     = Equation.ParseEquation(_xmin);
                Equation xmax     = Equation.ParseEquation(_xmax);
                _history.Add(_equation);

                double min = xmin.GetValue();
                double max = xmax.GetValue();

                double ymin = Double.NaN;
                double ymax = Double.NaN;

                _graph = new Graph(250, 250);
                double delta = (max - min) / 250.0d;
                if (max < min)
                {
                    Debug.Log("Max Less Than Min:" + max + " < " + min);
                    return;
                }
                for (double x = min; x <= max; x += delta)
                {
                    double y = equation.GetValue(x);
                    if (Double.IsNaN(y))
                    {
                        continue;
                    }
                    if (Double.IsNaN(ymin) || y < ymin)
                    {
                        ymin = y;
                    }
                    if (Double.IsNaN(ymax) || y > ymax)
                    {
                        ymax = y;
                    }
                }
                _ymax  = ymax;
                _ymin  = ymin;
                _gxmin = min;
                _gxmax = max;
                Debug.Log("Y min:" + ymin + " max:" + ymax + " delta:" + delta);
                _graph.drawGraph(x => equation.GetValue(min + x * delta), ymax, ymin, 250);
                _geq = equation;
            } catch (ParsingException e) {
                Debug.Log(_equation + ":" + e.Message);
                _history.Add(_equation + "\n" + e.Message);
            }
        }
示例#2
0
 /// <summary>
 /// Gets the value after any evaluation and equation parsing.
 /// </summary>
 /// <returns>The value.</returns>
 /// <param name="content">Content.</param>
 private string GetValue(string content)
 {
     if (content.StartsWith("="))
     {
         string   equation = EvalCell(content);
         Equation eq       = Equation.ParseEquation(equation);
         double   value    = eq.GetValue();
         if (value > 1e12d || value < 1e-3d)
         {
             return(value.ToString("e6"));
         }
         return(value.ToString("0.####"));
     }
     return(content);
 }
示例#3
0
        /// <summary>
        /// Calculate the equation.
        /// </summary>
        private void Calculate()
        {
            double value = 0.0d;

            try {
                Equation equation = Equation.ParseEquation(_equation);

                value = equation.GetValue();
                _history.Add(_equation + "=" + value.ToString("G5"));
                _windowPos.height = 0.0f;
                _windowPos.width  = 0.0f;

                if (_history.Count > MAX_HISTORY)
                {
                    _history.RemoveRange(0, _history.Count - MAX_HISTORY);
                }
            } catch (ParsingException e) {
                Debug.Log(_equation + ":" + e.Message);
                _history.Add(_equation + "\n" + e.Message);
            }
        }
示例#4
0
        private void OnGraphWindow(int windowId)
        {
            bool draw = false;

            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            GUILayout.Label("Lines", _labelStyle);
            if (GUILayout.Button("+", _buttonStyle) && _graphLineCnt < _lineColor.Length)
            {
                _graphLineCnt++;
            }
            if (GUILayout.Button("-", _buttonStyle) && _graphLineCnt > 1)
            {
                _graphLineCnt--;
                _graphPos.height = 0.0f;
                if (_graphLines.ContainsKey(_graphLineCnt))
                {
                    _graphLines.Remove(_graphLineCnt);
                }
                draw = true;
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            bool lockX = GUILayout.Toggle(_lockX, "Lock X");

            if (lockX != _lockX)
            {
                _refreshGraph = true;
                _lockX        = lockX;
            }
            bool lockY = GUILayout.Toggle(_lockY, "Lock Y");

            if (lockY != _lockY)
            {
                _refreshGraph = true;
                _lockY        = lockY;
            }
            GUILayout.EndHorizontal();
            try {
                if (_lockX)
                {
                    GUILayout.BeginHorizontal();
                    string updated = "";
                    string minPos  = "XMin";
                    GUILayout.Label("Xmin:", _labelStyle, GUILayout.Width(40.0f));
                    GUI.SetNextControlName(minPos);
                    updated = GUILayout.TextField(
                        _graphLineEdit == -1 ? _xmin : GetValue(_xmin), _textFieldStyle,
                        GUILayout.MinWidth(40.0f));
                    if (GUI.GetNameOfFocusedControl() == minPos)
                    {
                        if (_graphLineEdit == -1 && updated != _xmin)
                        {
                            _xmin = updated;
                            draw  = true;
                        }
                        _graphLineEdit = -1;
                    }

                    GUILayout.Label("Xmax:", _labelStyle, GUILayout.Width(40.0f));
                    string maxPos = "XMax";
                    GUI.SetNextControlName(maxPos);
                    updated = GUILayout.TextField(
                        _graphLineEdit == -1 ? _xmax : GetValue(_xmax), _textFieldStyle,
                        GUILayout.MinWidth(40.0f));
                    if (GUI.GetNameOfFocusedControl() == maxPos)
                    {
                        if (_graphLineEdit == -1 && updated != _xmax)
                        {
                            _xmax = updated;
                            draw  = true;
                        }
                        _graphLineEdit = -1;
                    }
                    GUILayout.EndHorizontal();
                }
                for (int line = 0; line < _graphLineCnt; line++)
                {
                    if (!_graphLines.ContainsKey(line))
                    {
                        _graphLines.Add(line, new GraphLine());
                        _graphLines [line].LineColor = _lineColor [line];
                    }
                    GUILayout.BeginHorizontal();
                    GUIStyle  temp  = new GUIStyle(_labelStyle);
                    Texture2D color = new Texture2D(1, 1);
                    color.wrapMode = TextureWrapMode.Repeat;
                    color.SetPixel(0, 0, _graphLines [line].LineColor);
                    color.Apply();
                    temp.normal.background = color;
                    GUILayout.Label("", temp, GUILayout.Width(20.0f));
                    string cPos = "CG" + line;
                    GUI.SetNextControlName(cPos);
                    string updated = GUILayout.TextField(
                        _graphLines [line].Content, _textFieldStyle, GUILayout.MinWidth(150.0f));

                    if (GUI.GetNameOfFocusedControl() == cPos)
                    {
                        if (_graphLineEdit == line)
                        {
                            _graphLines [line].Content = updated;
                        }
                        _graphLineEdit = line;
                    }

                    if (!_lockX)
                    {
                        string minPos = "MinG" + line;
                        GUI.SetNextControlName(minPos);
                        updated = GUILayout.TextField(
                            _graphLineEdit == line ? _graphLines [line].XMin : GetValue(_graphLines [line].XMin), _textFieldStyle,
                            GUILayout.MinWidth(20.0f));
                        if (GUI.GetNameOfFocusedControl() == minPos)
                        {
                            if (_graphLineEdit == line)
                            {
                                _graphLines [line].XMin = updated;
                            }
                            _graphLineEdit = line;
                        }

                        string maxPos = "MaxG" + line;
                        GUI.SetNextControlName(maxPos);
                        updated = GUILayout.TextField(
                            _graphLineEdit == line ? _graphLines [line].XMax : GetValue(_graphLines [line].XMax), _textFieldStyle,
                            GUILayout.MinWidth(20.0f));
                        if (GUI.GetNameOfFocusedControl() == maxPos)
                        {
                            if (_graphLineEdit == line)
                            {
                                _graphLines [line].XMax = updated;
                            }
                            _graphLineEdit = line;
                        }
                    }
                    else
                    {
                        _graphLines [line].XMin = _xmin;
                        _graphLines [line].XMax = _xmax;
                    }
                    GUILayout.EndHorizontal();
                    if (_slider > 1e-2d)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("X:" + _graphLines[line].x.ToString("0.0###"), _labelStyle);
                        GUILayout.Label("Y:" + _graphLines[line].y.ToString("0.0###"), _labelStyle);
                        GUILayout.EndHorizontal();
                    }
                    draw = draw || _graphLines [line].Dirty;
                }
                float slider = GUILayout.HorizontalSlider(_slider, 0.0f, 300.0f);
                if (slider != _slider)
                {
                    _slider          = slider;
                    draw             = true;
                    _graphPos.height = 0.0f;
                }

                if (draw)
                {
                    _ymax = Double.NaN;
                    _ymin = Double.NaN;
                }
                if (draw || _refreshGraph)
                {
                    _refreshGraph = false;
                    _graph.reset();
                    for (int line = 0; line < _graphLineCnt; line++)
                    {
                        _graphLines[line].Dirty = false;

                        if (_graphLines [line].Content.Length == 0)
                        {
                            continue;
                        }
                        if (_graphLines [line].XMin.Length == 0)
                        {
                            continue;
                        }
                        if (_graphLines [line].XMax.Length == 0)
                        {
                            continue;
                        }

                        Equation equation = Equation.ParseEquation(EvalCell(_graphLines [line].Content));
                        Equation xmin     = Equation.ParseEquation(EvalCell(_graphLines [line].XMin));
                        Equation xmax     = Equation.ParseEquation(EvalCell(_graphLines [line].XMax));

                        double min = xmin.GetValue();
                        double max = xmax.GetValue();

                        double ymin = Double.NaN;
                        double ymax = Double.NaN;

                        if (max <= min || Double.IsNaN(max) || Double.IsNaN(min))
                        {
                            continue;
                        }

                        double delta = (max - min) / 300.0d;

                        int px = 0;
                        for (double x = min; x <= max; x += delta)
                        {
                            double y = equation.GetValue(x);
                            if (Double.IsNaN(y))
                            {
                                continue;
                            }
                            if (Double.IsNaN(ymin) || y < ymin)
                            {
                                ymin = y;
                            }
                            if (Double.IsNaN(ymax) || y > ymax)
                            {
                                ymax = y;
                            }
                            if (px == (int)_slider)
                            {
                                _graphLines[line].x = x;
                                _graphLines[line].y = y;
                            }
                            px++;
                        }

                        if (Double.IsNaN(_ymin) || ymin < _ymin)
                        {
                            _ymin         = ymin;
                            _refreshGraph = true;
                        }
                        if (Double.IsNaN(_ymax) || ymax > _ymax)
                        {
                            _ymax         = ymax;
                            _refreshGraph = true;
                        }
                        if (_lockY)
                        {
                            ymin = _ymin;
                            ymax = _ymax;
                        }
                        _graph.drawLineOnGraph(x => equation.GetValue(min + x * delta), ymax, ymin, 300,
                                               _graphLines [line].LineColor);
                    }
                    if (_slider > 1e-2d)
                    {
                        _graph.drawVerticalLine((int)_slider, Color.grey);
                    }
                    _graph.Apply();
                }
            } catch (Exception e) {
                Debug.Log(e.Message + "\n" + e.StackTrace);
            }
            if (_graph != null)
            {
                GUILayout.Label("Y max:" + _ymax.ToString("G10"), _labelStyle);
                GUILayout.Box(_graph.getImage());
                GUILayout.Label("Y min:" + _ymin.ToString("G10"), _labelStyle);
            }
            GUILayout.EndVertical();
            GUI.DragWindow();
        }