1.概要
c# 画一个正弦函数
2.代码
using System;
using System.Drawing;
using System.Windows.Forms;public class SineWaveForm : Form
{private const int Width = 800;private const int Height = 600;private const double Amplitude = 100.0;private const double Period = 200.0;protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);Graphics g = e.Graphics;Pen pen = new Pen(Color.Blue, 2);for (double x = 0; x < Width; x += 1){double y = Height / 2 - Amplitude * Math.Sin((2 * Math.PI / Period) * x);int yInt = (int)y;if (yInt >= 0 && yInt < Height){// 绘制点来模拟连续的线(对于更平滑的线,使用g.DrawCurve或g.DrawBezier) g.DrawEllipse(pen, (float)x, (float)yInt, 2, 2);}}pen.Dispose();}public SineWaveForm(){this.DoubleBuffered = true; // 减少绘图时的闪烁 this.ClientSize = new Size(Width, Height);}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new SineWaveForm());}
}
3.运行结果