偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C# GDI+ 實(shí)現(xiàn)物體橢圓運(yùn)動(dòng)詳解

開發(fā) 前端
注意事項(xiàng)1. 確保啟用雙緩沖以防止閃爍2. 注意釋放GDI+資源(使用using語(yǔ)句)3. 合理設(shè)置定時(shí)器間隔,避免過于頻繁的刷新4. 考慮性能優(yōu)化,避免在繪圖時(shí)進(jìn)行復(fù)雜計(jì)算。

一、原理介紹

橢圓運(yùn)動(dòng)是一種常見的周期性運(yùn)動(dòng),其軌跡形成一個(gè)橢圓。物體在橢圓上運(yùn)動(dòng)的坐標(biāo)可以用以下參數(shù)方程表示:

x = a * cos(t)
y = b * sin(t)

其中:

  • a 為橢圓的長(zhǎng)半軸
  • b 為橢圓的短半軸
  • t 為參數(shù)角度(0-360度)

二、完整代碼實(shí)現(xiàn)

圖片圖片

public partial class Form1 : Form
{
    private Timer timer;
    private double angle = 0;
    private const double STEP = 1;  // 角度步進(jìn)值  
    private const int A = 150;      // 長(zhǎng)半軸  
    private const int B = 100;      // 短半軸  
    private Point center;           // 橢圓中心點(diǎn)  
    private Point currentPos;       // 運(yùn)動(dòng)物體當(dāng)前位置  


    public Form1()
    {
        InitializeComponent();


        // 啟用雙緩沖,防止閃爍  
        this.DoubleBuffered = true;
        // 設(shè)置控件樣式為全部在WM_PAINT中繪制  
        // 這樣可以防止控件擦除背景時(shí)閃爍,提高繪制效率  
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);


        // 計(jì)算中心點(diǎn)  
        center = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);


        // 初始化定時(shí)器  
        timer = new Timer();
        timer.Interval = 20;  // 20ms更新一次  
        timer.Tick += Timer_Tick;
        timer.Start();
    }


    private void Timer_Tick(object sender, EventArgs e)
    {
        // 計(jì)算新位置  
        angle = (angle + STEP) % 360;
        double radian = angle * Math.PI / 180;


        currentPos = new Point(
            (int)(center.X + A * Math.Cos(radian)),
            (int)(center.Y + B * Math.Sin(radian))
        );


        this.Invalidate();  // 觸發(fā)重繪  
    }


    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;


        // 設(shè)置高質(zhì)量繪圖  
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


        // 繪制橢圓軌跡  
        using (Pen pen = new Pen(Color.LightGray, 1))
        {
            g.DrawEllipse(pen,
                center.X - A, center.Y - B,
                2 * A, 2 * B);
        }


        // 繪制運(yùn)動(dòng)物體  
        using (SolidBrush brush = new SolidBrush(Color.Blue))
        {
            const int BALL_SIZE = 20;
            g.FillEllipse(brush,
                currentPos.X - BALL_SIZE / 2,
                currentPos.Y - BALL_SIZE / 2,
                BALL_SIZE, BALL_SIZE);
        }


        // 繪制中心點(diǎn)  
        using (SolidBrush brush = new SolidBrush(Color.Red))
        {
            g.FillEllipse(brush,
                center.X - 3, center.Y - 3,
                6, 6);
        }


        // 顯示當(dāng)前角度  
        using (Font font = new Font("Arial", 12))
        using (SolidBrush brush = new SolidBrush(Color.Black))
        {
            g.DrawString($"Angle: {angle:F1}°",
                font, brush, new PointF(10, 10));
        }
    }
}

三、注意事項(xiàng)

  1. 確保啟用雙緩沖以防止閃爍
  2. 注意釋放GDI+資源(使用using語(yǔ)句)
  3. 合理設(shè)置定時(shí)器間隔,避免過于頻繁的刷新
  4. 考慮性能優(yōu)化,避免在繪圖時(shí)進(jìn)行復(fù)雜計(jì)算

這個(gè)示例展示了如何使用C# GDI+實(shí)現(xiàn)基本的橢圓運(yùn)動(dòng)效果,代碼簡(jiǎn)潔且易于理解,可以作為更復(fù)雜動(dòng)畫效果的基礎(chǔ)。

責(zé)任編輯:武曉燕 來(lái)源: 技術(shù)老小子
相關(guān)推薦

2024-11-08 14:06:26

2009-08-21 09:23:11

C# GDI+

2009-08-19 17:45:26

C#使用GDI+

2025-01-14 09:10:34

C#機(jī)器人代碼

2009-08-31 17:35:19

C#使用GDI+實(shí)現(xiàn)餅

2009-08-27 17:11:50

C#模擬

2009-08-25 18:04:30

C#實(shí)現(xiàn)Singlet

2009-09-09 18:50:23

C# 加密RSA

2009-08-31 16:23:13

C#接口

2009-08-25 17:43:17

C#串口監(jiān)聽

2009-09-09 18:57:26

C# 加密TripleDES

2009-08-25 10:44:50

C#實(shí)現(xiàn)多語(yǔ)言

2009-08-21 10:13:02

C#異步初步

2009-08-26 12:59:08

C#打印設(shè)置

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:32:37

C#打印文檔

2009-08-26 11:07:36

C#打印窗體

2009-08-20 16:33:44

Socket異步通訊

2009-09-09 12:55:59

C# TextBox事

2009-09-03 14:55:56

C#實(shí)現(xiàn)DataGri
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)