private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            FuncRect funcRect = viewModel.FuncRect;

            RepresentationLimitsWindow representatitonLimits = new RepresentationLimitsWindow()
            {
                Xmin = funcRect.XMin,
                Xmax = funcRect.XMax,
                Ymin = funcRect.YMin,
                Ymax = funcRect.YMax
            };

            representatitonLimits.ShowDialog();
            if (false == representatitonLimits.DialogResult)
            {
                return;
            }

            funcRect.XMin = representatitonLimits.Xmin;
            funcRect.XMax = representatitonLimits.Xmax;
            funcRect.YMin = representatitonLimits.Ymin;
            funcRect.YMax = representatitonLimits.Ymax;

            viewModel.FuncRect = funcRect;
        }
        public Line[] DrawAxis(FuncRect real, FuncRect screen)
        {
            Line[] arrayOfAxis = new Line[2];
            Line   axisX       = new Line
            {
                Stroke = Brushes.Black,

                X1 = 0,
                X2 = screen.XMax,
                Y1 = ConvertYFromRealToPant(0, 0, screen, real),
                Y2 = ConvertYFromRealToPant(0, 0, screen, real)
            };

            Line axisY = new Line
            {
                Stroke = Brushes.Black,

                Y1 = 0,
                Y2 = screen.YMax,
                X1 = ConvertXFromRealToPant(0, 0, screen, real),
                X2 = ConvertXFromRealToPant(0, 0, screen, real)
            };

            arrayOfAxis[0] = axisX;
            arrayOfAxis[1] = axisY;

            return(arrayOfAxis);
        }
        private void ViewModel_GraphicSetToDraw(object sender, ViewModelEventArgs e)
        {
            PointCollection[] graphicRepresentation     = null;
            List <Polyline>   listOfLines               = null;
            List <Graphic>    listOfGraphicsToRepresent = (List <Graphic>)e.ListOfGraphics;
            FuncRect          funcR = viewModel.FuncRect;

            RepresentationCanvas.Children.Clear();
            DrawAxisAndLines();

            if (graphicRepresentationDictionary == null) // Para que no se dibuje nada en el sizeChanged
            {
                graphicRepresentationDictionary = new Dictionary <Graphic, List <Polyline> >();
            }

            graphicRepresentationDictionary.Clear();

            foreach (Graphic g in listOfGraphicsToRepresent)
            {
                if (graphicRepresentationDictionary.ContainsKey(g) && g != null)
                {
                    listOfLines = graphicRepresentationDictionary[g];
                    foreach (Polyline line in listOfLines)
                    {
                        RepresentationCanvas.Children.Add(line);
                    }
                }
                else
                {
                    listOfLines = new List <Polyline>();

                    graphicRepresentation = FunctionRepresentationVar.DrawGraphic(g, RepresentationCanvas.ActualWidth, RepresentationCanvas.ActualHeight, funcR);
                    foreach (PointCollection p in graphicRepresentation)
                    {
                        Polyline line = new Polyline()
                        {
                            Points = p,
                            Stroke = new SolidColorBrush(g.GraphicColor)
                        };
                        listOfLines.Add(line);
                    }
                    graphicRepresentationDictionary.Add(g, listOfLines);

                    foreach (Polyline line in listOfLines)
                    {
                        RepresentationCanvas.Children.Add(line);
                    }
                }
            }
        }
        public List <Line> DrawAxisLines(FuncRect screen, FuncRect real, double actualPos, double maxLimit, double distancia, Boolean axisHorizontal)
        {
            int         counter     = 3;
            List <Line> listOflines = new List <Line>();
            Line        line        = new Line {
                Stroke = Brushes.Black
            };
            double length1, length2;

            while (actualPos < maxLimit)
            {
                // Altura de la raya de -0.5 a 0.5
                if (counter == 3)
                {
                    length1 = -0.5;
                    length2 = 0.5;
                    counter = 0;
                }
                else
                {
                    length1 = -0.25;
                    length2 = 0.25;
                }

                if (axisHorizontal)
                {
                    line.X1 = ConvertXFromRealToPant(actualPos, screen.XMin, screen, real);
                    line.X2 = ConvertXFromRealToPant(actualPos, screen.XMin, screen, real);
                    line.Y1 = ConvertYFromRealToPant(length1, screen.YMin, screen, real);
                    line.Y2 = ConvertYFromRealToPant(length2, screen.YMin, screen, real);
                }
                else
                {
                    line.X1 = ConvertXFromRealToPant(length1, screen.XMin, screen, real);
                    line.X2 = ConvertXFromRealToPant(length2, screen.XMin, screen, real);
                    line.Y1 = ConvertYFromRealToPant(actualPos, screen.YMin, screen, real);
                    line.Y2 = ConvertYFromRealToPant(actualPos, screen.YMin, screen, real);
                }

                listOflines.Add(line);

                actualPos += distancia;
                line       = new Line {
                    Stroke = Brushes.Black
                };
                counter++;
            }

            return(listOflines);
        }
        private void DrawAxisAndLines()
        {
            // Valores minimos y máximo reales del Canvas
            real = FunctionRepresentationVar.DeclareFuncRect(-10, 10, -10, 10);
            // Valores minimos y máximo de pantalla del Canvas
            screen = FunctionRepresentationVar.DeclareFuncRect(0, RepresentationCanvas.ActualWidth, 0, RepresentationCanvas.ActualHeight);
            Boolean ejeHorizontal = true;
            Boolean ejeVertical   = false;
            double  distancia     = 0.333; //0.333
            int     numBlock      = -11;
            int     count         = 2;
            double  limitX        = RepresentationCanvas.ActualWidth;
            double  limitY        = RepresentationCanvas.ActualHeight;

            // Eje X e Y
            foreach (Line l in FunctionRepresentationVar.DrawAxis(real, screen))
            {
                RepresentationCanvas.Children.Add(l);
            }

            // Lineas Eje X
            foreach (Line l in FunctionRepresentationVar.DrawAxisLines(screen, real, real.XMin, real.YMax, distancia, ejeHorizontal))
            {
                RepresentationCanvas.Children.Add(l);
                ++count;
                if (count == 3)
                {
                    ++numBlock;
                }
                count = DrawNumberLines(l, numBlock, ejeHorizontal, count);
            }

            // Numero por el que empieza desde el límite del canvas
            numBlock = -11;

            // Lineas Eje Y
            foreach (Line l in FunctionRepresentationVar.DrawAxisLines(screen, real, real.XMin, real.YMax, distancia, ejeVertical))
            {
                RepresentationCanvas.Children.Add(l);
                ++count;
                if (count == 3)
                {
                    ++numBlock;
                }
                count = DrawNumberLines(l, numBlock, ejeVertical, count);
            }
        }
        private void DrawGraphicsInList(Graphic graphic)
        {
            PointCollection[] graphicRepresentation = null;
            List <Polyline>   listOfLines           = new List <Polyline>();
            FuncRect          funcR = viewModel.FuncRect;

            graphicRepresentation = FunctionRepresentationVar.DrawGraphic(graphic, RepresentationCanvas.ActualWidth, RepresentationCanvas.ActualHeight, funcR);
            foreach (PointCollection p in graphicRepresentation)
            {
                Polyline line = new Polyline()
                {
                    Points = p,
                    Stroke = new SolidColorBrush(graphic.GraphicColor)
                };
                listOfLines.Add(line);
            }
            graphicRepresentationDictionary.Add(graphic, listOfLines);

            foreach (Polyline line in listOfLines)
            {
                RepresentationCanvas.Children.Add(line);
            }
        }
 public double ConvertYFromPantToReal(double ypant, double height, FuncRect screen, FuncRect real)
 {
     return((real.YMax - real.YMin) * ((ypant - screen.YMax) / (screen.YMin - screen.YMax)) + real.YMin);
 }
 public double ConvertXFromPantToReal(double xpant, double width, FuncRect screen, FuncRect real)
 {
     return((real.XMax - real.XMin) * ((xpant - screen.XMin) / (screen.XMax - screen.XMin)) + real.XMin);
 }
 public double ConvertYFromRealToPant(double yreal, double height, FuncRect screen, FuncRect real)
 {
     return((height - screen.YMax) * ((yreal - real.YMin) / (real.YMax - real.YMin)) + screen.YMax);
 }
 public double ConvertXFromRealToPant(double xreal, double width, FuncRect screen, FuncRect real)
 {
     return((screen.XMax - width) * ((xreal - real.XMin) / (real.XMax - real.XMin)) + screen.XMin);
 }
        public PointCollection[] DrawGraphic(Graphic g, double canvasWidth, double canvasHeight, FuncRect funcRect)
        {
            PointCollection points = new PointCollection();
            Polyline        graphicPolyline = new Polyline();
            double          xReal, yReal, xScreen, yScreen;

            /* Sustituyo el FuncRect real por los limites que ha introducido el usuario (en caso de que los modifique) */
            FuncRect real   = funcRect;
            FuncRect screen = DeclareFuncRect(0, canvasWidth, 0, canvasHeight);
            int      numberOfPoints = (int)screen.XMax;

            int i     = 0;
            int j     = 0;
            int limit = 0;

            if (g.Function.Formula.Equals(ExponentialFunction.GetFormula()) && g.ParamB < 0 ||
                g.Function.Formula.Equals(FractionalFunction.GetFormula()))
            {
                PointCollection[] listOfPoints = new PointCollection[2];
                while (j < 2)
                {
                    do
                    {
                        i++;
                        xReal = real.XMin + i * (real.XMax - real.XMin) / numberOfPoints;
                        yReal = g.Function.CalculateF(xReal);

                        xScreen = ConvertXFromRealToPant(xReal, screen.XMin, screen, real);
                        yScreen = ConvertYFromRealToPant(yReal, screen.YMin, screen, real);

                        points.Add(new Point(xScreen, yScreen));
                    } while (Convert.ToInt32(xReal) < limit);

                    listOfPoints[j] = points;
                    points          = new PointCollection();
                    j++;

                    xReal += 1;
                    limit  = (int)real.XMax;
                    i      = (int)(-(real.XMin) * numberOfPoints / (real.XMax - real.XMin));
                }

                return(listOfPoints);
            }
            else
            {
                PointCollection[] listOfPoints = new PointCollection[1];

                for (i = 0; i <= numberOfPoints; i++)
                {
                    xReal = real.XMin + i * (real.XMax - real.XMin) / numberOfPoints;
                    yReal = g.Function.CalculateF(xReal);

                    xScreen = ConvertXFromRealToPant(xReal, screen.XMin, screen, real);
                    yScreen = ConvertYFromRealToPant(yReal, screen.YMin, screen, real);
                    points.Add(new Point(xScreen, yScreen));
                }

                listOfPoints[0] = points;

                return(listOfPoints);
            }
        }