c# 画一个正弦函数

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.运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值