2014-01-27 197 views
1

我試圖在圖表上繪製一個parabala,我已經切換到一個線形圖。繪製二次曲線

 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); 
    } 

然而,輸出是一條直線,我認爲我需要更多的點?

回答

1

總之,是的,你需要更多的數據點。

你所擁有的是繪圖截取x軸和y軸的點,並且繪製這3個圖是一個好的開始,但繪圖引擎無法解釋這三個點來自哪種數據集(sin,例如cos和鋸齒圖都可以找到,其中包括與任何二次方程相同的截距)。

如果你想繪製曲線本身的近似值,一個快速和骯髒的解決方案將採取答案1和答案2(小心他們是相等或虛構的情況下)之間的差異,並計算出對於一組點,y值低於最低迴答開始某個比例,高於最高回答的比例相同。然後,您可以簡單地旋轉結果並逐個添加它們。

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; 
} 
0

謝謝。我添加了一個for循環來計算座標的20個值。

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); 
} 

適合任何需要它的人!