draw a 2d sine wave in csharp

To draw a 2D sine wave in C#, you can use the chart control provided by Windows Forms. Here's a code snippet that demonstrates how to do it:

main.cs
// Create a new chart control
Chart chart = new Chart();
chart.Size = new Size(400, 300);
this.Controls.Add(chart);

// Add a chart area to the chart
ChartArea chartArea = new ChartArea();
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = 2 * Math.PI;
chartArea.AxisY.Minimum = -1;
chartArea.AxisY.Maximum = 1;
chart.ChartAreas.Add(chartArea);

// Add a series to the chart
Series series = new Series();
series.ChartType = SeriesChartType.Line;
series.Color = Color.Blue;

// Generate the data for the series
for (double x = 0; x <= 2 * Math.PI; x += 0.1)
{
    double y = Math.Sin(x);
    series.Points.AddXY(x, y);
}

chart.Series.Add(series);
651 chars
27 lines

This code creates a new chart control, adds a chart area and a series to it, and generates the data for the series by computing the sine of x values between 0 and 2π in steps of 0.1. Finally, the chart is displayed on the form. The resulting chart should show a smooth sine wave in blue.

gistlibby LogSnag