private void button2_Click(object sender, EventArgs e) { var funcs = new ChartFunction.MathFunction[] { Math.Sin, Math.Tan, Math.Ceiling, Math.Cos, Math.Atan }; var fobj = new ChartFunction(chart1, funcs[rand.Next(funcs.Length)], Color.Black); // var fobj = new ChartFunction(chart1, Math.Ceiling, Color.Black); fobj.ExtendOnExtremum = true; fobj.Color = Color.Red; chart1.AddObject(fobj); }
/// <summary> /// Plots a function defined by a lambda inside the provided interval. /// </summary> /// <param name="function">The function to plot.</param> /// <param name="minX">The left bound of the interval.</param> /// <param name="maxX">The right bound of the interval.</param> /// <param name="functionColor">The color of the plot.</param> /// <param name="step">The sample step. Default will compute f(x) on each pixel.</param> public static void PushFunction(ChartFunction function, float minX, float maxX, Color functionColor, float step = -1f) { if (minX < CurrentChart.minX) { minX = CurrentChart.minX; } if (maxX > CurrentChart.maxX) { maxX = CurrentChart.maxX; } // Computes the default step value. if (step < 0) { step = CurrentChart.userDefinedRect.width / CurrentChart.pixelSizeRect.width; } List <Vector2> samples = new List <Vector2>(); bool aboveLimit = false, belowLimit = false; Vector2 previous = Vector2.zero; Vector2 current = Vector2.zero; for (float x = minX; x < maxX; x += step) { current = new Vector2(x, function(x)); if (current.y > CurrentChart.maxY) { // Case 1: point is above Y bound if (!aboveLimit) { Vector2 intersection = GetIntersectionPointCoordinates(current, previous, new Vector2(minX, CurrentChart.maxY), new Vector2(maxX, CurrentChart.maxY), out bool found); aboveLimit = true; if (found) { samples.Add(intersection); } PushLineChart(samples.ToArray(), functionColor); samples.Clear(); } } else if (current.y < CurrentChart.minY) { // Case 2: point is below Y bound if (!belowLimit) { Vector2 intersection = GetIntersectionPointCoordinates(current, previous, new Vector2(minX, CurrentChart.minY), new Vector2(maxX, CurrentChart.minY), out bool found); belowLimit = true; if (found) { samples.Add(intersection); } PushLineChart(samples.ToArray(), functionColor); samples.Clear(); } } else { // Case 3: point is inside bounds if (aboveLimit) { Vector2 intersection = GetIntersectionPointCoordinates(current, previous, new Vector2(minX, CurrentChart.maxY), new Vector2(maxX, CurrentChart.maxY), out bool found); aboveLimit = false; if (found) { samples.Add(intersection); } } if (belowLimit) { Vector2 intersection = GetIntersectionPointCoordinates(current, previous, new Vector2(minX, CurrentChart.minY), new Vector2(maxX, CurrentChart.minY), out bool found); belowLimit = false; if (found) { samples.Add(intersection); } } samples.Add(current); } previous = current; } PushLineChart(samples.ToArray(), functionColor); }