public void AddFunction(Function f) { _currentFunctions.Add(f); }
// draw a single function private void DrawFunction(Graphics g, Function f) { // a list of list of points is required to properly skip imaginary number(e.g sqrt(-1)) var pointsList = new List<List<PointF>> {new List<PointF>()}; bool lastNan = false; for (float x = -Graph.NumOfCells/2; x < Graph.NumOfCells/2; x += Graph.Step) // from -X border to X border og the graph { float y = f.Apply(x); // the y value of x is calculated thanks to reflection PointF point = TransformPoint(new PointF(x, y)); // then the point obtained is converted to "real" coordinates //when an imaginary number is discovered it creates a new separated list of points to draw //every list of points will be drawn singularly if (double.IsNaN(y)) { if (!lastNan) // it creates a new list of points only if the last number wasn't imaginary { pointsList.Add(new List<PointF>()); lastNan = true; } } // if a point exceeds the borders of the graph his Y components is set to not visible, to prevent overflow. else if (point.Y > Graph.Heigth) { pointsList[pointsList.Count - 1].Add(new PointF(point.X, Graph.Heigth + FunctionPen.Width)); } else if (point.Y < 0) { pointsList[pointsList.Count - 1].Add(new PointF(point.X, -FunctionPen.Width)); } else { lastNan = false; pointsList[pointsList.Count - 1].Add(point); // } } //draws each list of points only if the list has a number of elements > 1 foreach (var list in pointsList.Where(list => list.Count > 1)) { g.DrawLines(FunctionPen, list.ToArray()); } }