C# 画盛开的玫瑰花
时间: 2023-07-12 19:04:55 浏览: 269
以下是使用C#绘制盛开的玫瑰花的示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace RoseDrawing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true; // 双缓冲绘图
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
int width = this.ClientSize.Width;
int height = this.ClientSize.Height;
int centerX = width / 2;
int centerY = height / 2;
double a = 0.5 * Math.Min(width, height);
double k = 5.0 / 8.0; // 调整参数以控制花瓣的数量和大小
for (double t = 0; t < 2 * Math.PI; t += 0.01)
{
double r = a * Math.Sin(k * t);
int x = (int)(centerX + r * Math.Cos(t));
int y = (int)(centerY + r * Math.Sin(t));
g.FillEllipse(Brushes.Pink, x, y, 4, 4); // 绘制花瓣
}
}
}
}
```
在窗口中创建一个 `Paint` 事件处理程序,使用 `Graphics` 对象绘制盛开的玫瑰花。关键是计算每个点的 x 和 y 坐标并绘制它们。在此示例中,使用了极坐标方程 r = a * sin(k * t) 来计算每个点的半径,其中 a 是花瓣大小的缩放因子,k 是形状参数,可以用来控制花瓣的数量和大小。最后,使用 `FillEllipse` 方法绘制每个点作为一个小圆点,这些圆点一起形成了盛开的玫瑰花形状。