visual studio2022绘制正弦函数图形使用c#语言
时间: 2023-11-02 19:50:32 浏览: 237
C语言设置正弦函数图形
您可以使用以下代码绘制正弦函数图形:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SinFunctionGraph
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 2);
Pen redPen = new Pen(Color.Red, 2);
int width = this.ClientSize.Width;
int height = this.ClientSize.Height;
float xScale = width / (float)Math.PI;
float yScale = height / 2;
e.Graphics.DrawLine(blackPen, 0, height / 2, width, height / 2);
e.Graphics.DrawLine(blackPen, width / 2, 0, width / 2, height);
float x1, y1, x2, y2;
for (float x = -width / 2; x < width / 2; x += 0.01f)
{
x1 = (x + width / 2) / xScale;
y1 = (float)Math.Sin(x) * yScale + height / 2;
x2 = (x + width / 2 + 0.01f) / xScale;
y2 = (float)Math.Sin(x + 0.01f) * yScale + height / 2;
e.Graphics.DrawLine(redPen, x1, y1, x2, y2);
}
}
}
}
```
在窗体的构造方法中添加以下代码:
```csharp
this.Paint += new PaintEventHandler(Form1_Paint);
```
这样就可以在窗体上绘制正弦函数图形了。
阅读全文