Home c# Build graph c #

Build graph c #

Author

Date

Category

Good afternoon, please tell me how to plot a polynomial:
the polynomial itself p (x) = An * X ^ n + An-1 * X ^ n-1 + … + A1X + A0

int [] array = new int [10];
int i, num, power;
float x;
num = 4;
x = 1;
array [0] = 3;
array [1] = -5;
array [2] = 6;
array [3] = 8;
array [4] = -9;
power = num;
double k = 0;
for (i = 0; i & lt; = num; i ++)
{
  k + = Math.Pow (x, power--) * array [i];
 // richTextBox1.AppendText (Convert.ToString (k) + "\ n");
}

how to build a graph right now?


Answer 1, authority 100%

For a start, the standard class Chart , although there are third-party components for building beautiful charts, it seems to me that first it is better to understand the standard control. The MSDN documentation has a fairly detailed Tutorial … I’ll show you the main points in the code, details in the official documentation.

class ChartForm: Form
{
  public ChartForm ()
  {
    // create a Chart element
    Chart myChart = new Chart ();
    // put it on the form and stretch it to the whole window.
    myChart.Parent = this;
    myChart.Dock = DockStyle.Fill;
    // add an area to the Chart for drawing charts, they can be
    // a lot, so we give it a name.
    myChart.ChartAreas.Add (new ChartArea ("Math functions"));
    // Create and configure a set of points for drawing a graph, including
    // not forgetting to specify the name of the area on which we want to display this
// set of points.
    Series mySeriesOfPoint = new Series ("Sinus");
    mySeriesOfPoint.ChartType = SeriesChartType.Line;
    mySeriesOfPoint.ChartArea = "Math functions";
    for (double x = -Math.PI; x & lt; = Math.PI; x + = Math.PI / 10.0)
    {
      mySeriesOfPoint.Points.AddXY (x, Math.Sin (x));
    }
    // Add the created set of points to the Chart
    myChart.Series.Add (mySeriesOfPoint);
  }
}

Here is the actual minimum of code for drawing a graph on a form. There are no settings for the axes of coordinates and the grid, nor other graphical features that this control supports, because examples of using almost all the features of this control are in the official documentation, see the link above.

Addition:

You need to move the calculation of the polynomial into a separate function. This can be done like this

double Polynom (double x, double [] coefficients)
{
  double y = 0.0;
  double currentX = 1.0;
  for (int i = 0; i & lt; coefficients.Length; i ++)
  {
    y + = currentx * coefficients [i];
    currentx * = x;
  }
  return y;
}

And substitute it for Math.Sin in my example.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions