EPicLURcher
EPicLURcher

Reputation: 21

Drawing a quadratic curve

Im trying to draw a parabala on a chart, which i have switched to a line graph.

     private void calculate_Click(object sender, EventArgs e)
    {
        double numberA = Convert.ToDouble(valueA.Text);
        double numberB = Convert.ToDouble(valueB.Text);
        double numberC = Convert.ToDouble(valueC.Text);
        displayFormula();
        double answer1 = quadCalculator1(numberA, numberB, numberC);
        double answer2 = quadCalcualtor2(numberA, numberB, numberC);
        quadOutput.Text += answer1 + " OR " + answer2;
        this.chart1.Series["quadGraph"].Points.AddXY(answer1, 0);
        this.chart1.Series["quadGraph"].Points.AddXY(answer2, 0);
        this.chart1.Series["quadGraph"].Points.AddXY(0, numberC);
    }

However the output is a straight line, i take it i need more points?

Upvotes: 1

Views: 1110

Answers (2)

EPicLURcher
EPicLURcher

Reputation: 21

Thanks. I added a for loop that calculates 20 values for the coordinates.

for (int i = -10; i < 10; i++)
{
    double pointX = i;
    double pointY = anyQuad(answer1, answer2, numberA, numberB, numberC, pointX);
    this.chart1.Series["quadGraph"].Points.AddXY(pointX, pointY);
}

Works nicely for anyone who needs it!

Upvotes: 0

T S Taylor
T S Taylor

Reputation: 1571

In short, yes you do need more data points.

What you have are the points where the plot intercepts the x and y axes, and plotting these 3 is a good start but the charting engine has no way of interpreting what sort of data set these three points come from (sin, cos and sawtooth plots for example could all be found which include the same intercepts as any quadratic).

If you want to plot an approximation of the curve itself, a quick and dirty solution would be to take the difference between answer 1 and answer 2 (being careful of the situations where they're equal or imaginary) and calculate the y values for a set of points starting some proportion below the lowest answer and the same proportion above the highest answer. You could then simply spin through the results and add them one at a time.

    private void calculate_Click(object sender, EventArgs e)
{
    double numberA = Convert.ToDouble(valueA.Text);
    double numberB = Convert.ToDouble(valueB.Text);
    double numberC = Convert.ToDouble(valueC.Text);
    displayFormula();
    double answer1 = quadCalculator1(numberA, numberB, numberC);
    double answer2 = quadCalcualtor2(numberA, numberB, numberC);
    quadOutput.Text += answer1 + " OR " + answer2;

    //this.chart1.Series["quadGraph"].Points.AddXY(answer1, 0);
    //this.chart1.Series["quadGraph"].Points.AddXY(answer2, 0);
    //this.chart1.Series["quadGraph"].Points.AddXY(0, numberC);

    // Do error checking here to determine validity of answers
    // and which is the highest and lowest of the pair

    int count = 20;
    double[,] data = GetPoints(numberA, numberB, numberC, answer1, answer2, count);
    for(int i = 0; i < count; i++)
    {
        this.chart1.Series["quadGraph"].Points.AddXY(data[i, 0], data[i, 1]);
    }
}

private double[,] GetPoints(double a, double b, double c, double xInterceptLow, double xInterceptHigh, int pointCount)
{
    double[,] output = new double[pointCount,2];

    double subRange = xInterceptLow - xInterceptHigh;
    double delta = (2* subRange) / pointCount;

    double xMin = xInterceptLow - (subRange/2);
    double xMax = xInterceptHigh + (subRange/2);

    for(int i = 0; i < pointCount; i++)
    {
        double x = xMin + ( i * delta);
        double ans = GetY(a, b, c, x);
        output[i, 0] = x;
        output[i, 1] = ans;
    }
    return output;
}

private double GetY(double a, double b, double c, double x)
{
    double answer = (a * a * x) + (b * x) + c;
    return answer;
}

Upvotes: 1

Related Questions