我试图在图表上绘制抛物线,我已经切换到折线图。
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);
}
然而输出是一条直线,我认为我需要更多的分数?
答案 0 :(得分: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;
}
答案 1 :(得分: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);
}
适合任何需要它的人!