Пример #1
0
        // Draw function
        public void DrawFunction(Canvas graphFunctions, string function, Color functionColor)
        {
            // creates syntax tree
            SyntaxTree calculator = new SyntaxTree(function);

            GeometryGroup g = new GeometryGroup();

            Point currentPoint    = new Point();
            Point previousPoint   = new Point();
            bool  isCurrentPoint  = false;
            bool  isPreviousPoint = false;

            Brush brush = new SolidColorBrush(functionColor);


            // for each x point calclulates y with step of range x divided by canvas width => x+=step of each point
            for (double x = minX; x < maxX; x += (maxX - minX) / graphFunctions.Width)
            {
                // calculates y value for each x point
                double y = calculator.Calculate(x, out isCurrentPoint);

                if (Math.Abs(y) > maxY * 2)
                {
                    isCurrentPoint = false;
                }
                if (isCurrentPoint)
                {
                    currentPoint.X = (x / ((maxY - minX) / graphFunctions.Width) -
                                      minX / ((maxX - minX) / graphFunctions.Width));
                    currentPoint.Y = (graphFunctions.Height -
                                      (y / ((maxY - minY) / graphFunctions.Height) -
                                       minY / ((maxY - minY) / graphFunctions.Height)));
                    if (isPreviousPoint)
                    {
                        // connect prev and current point with line
                        g.Children.Add(new LineGeometry(
                                           previousPoint,
                                           currentPoint));

                        Path functionPath = new Path();
                        functionPath.StrokeThickness = 1;
                        functionPath.Stroke          = brush;
                        functionPath.Data            = g;

                        graphFunctions.Children.Add(functionPath);
                    }
                }

                // set current point to previous till the end of canvas
                previousPoint   = currentPoint;
                isPreviousPoint = isCurrentPoint;
            }
        }