B
B
bulavko2018-12-26 03:02:33
C++ / C#
bulavko, 2018-12-26 03:02:33

Why does WinForms draw graphs strangely, depending on the maximum value of x?

Good day)
I wrote a program for plotting a sine and cosine graph and ran into such a delicate problem:
the Chart component behaves very strangely, if you do not multiply the variable that is responsible for the maximum value on the x-axis by 2pi
"Wool" StackOverflow - and nothing I didn't find it... I
'm attaching the code:

private void buttonPlot_Click(object sender, EventArgs e)
        {
            double xMin = 0;
            double xMax = double.Parse(textBoxTime.Text) * 6.28;
            int discretization = int.Parse(textBoxDiscretization.Text);
            double time = double.Parse(textBoxTime.Text);
            double deltaT = Math.Round((double)discretization / (double)time);
            int amplitude = int.Parse(textBoxAmplitude.Text);
            double frequency = double.Parse(textBoxFrequency.Text);
            double phase = ConvertToRadians(double.Parse(textBoxPhase.Text));
            double step = 0.5;

            int plotPointsCount = (int)(Math.Ceiling((xMax - xMin)) / step) + 1;

            double[] x = new double[plotPointsCount];
            double[] ySin = new double[plotPointsCount];
            double[] yCos = new double[plotPointsCount];

            for (int i = 0; i < plotPointsCount; i++)

            {
                x[i] = xMin + step * i;
                ySin[i] = amplitude * Math.Sin(x[i] + (2 * Math.PI * frequency * deltaT + phase));
                yCos[i] = amplitude * Math.Cos(x[i] + (2 * Math.PI * frequency * deltaT + phase));
                x[i] = x[i] / 6.25;

            }

            chart1.ChartAreas[0].AxisX.Minimum = xMin;
            chart1.ChartAreas[0].AxisX.Maximum = xMax / 6.28;
            chart1.ChartAreas[0].AxisX.MajorGrid.Interval = step;

            chart1.Series[0].Points.DataBindXY(x, ySin);
            chart1.Series[1].Points.DataBindXY(x, yCos);
        }

and photos of how the Chart component behaves:
1) the variable is not multiplied and divided by 2pi
2) the variable is multiplied and divided by 2pi
5c22c4f5d1add158910541.jpeg5c22c503dcaf0344233156.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2018-12-26
@bulavko

x[i] = x[i] / 6.25;
Well, actually, if your xMax is equal to 1, then it turns out that the graph is built up to 1/6.25 = 0.16, as it looks on the screen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question