C# 画身盛开的玫瑰花
时间: 2023-07-12 22:04:55 浏览: 56
以下是使用 C# 画身盛开的玫瑰花的示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Rose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Red);
for (int i = 0; i < 360; i++)
{
double x = Math.Sin(i * Math.PI / 180) * Math.Sin(i * Math.PI / 180) * Math.Sin(i * Math.PI / 180);
double y = Math.Cos(i * Math.PI / 180) * Math.Cos(i * Math.PI / 180) * Math.Cos(i * Math.PI / 180);
int px = (int)(120 + 80 * x);
int py = (int)(120 + 80 * y);
g.FillRectangle(brush, px, py, 1, 1);
}
}
}
}
```
这里使用了 `System.Drawing` 命名空间中的 `Graphics` 和 `Brush` 类,以及 `System.Windows.Forms` 命名空间中的 `Form` 类和 `PaintEventArgs` 类。
在 `Paint` 事件中,首先创建了一个画笔 `brush`,颜色为红色。然后通过一个循环遍历了 0 到 359 度之间的所有角度,计算出每个角度上的 x 坐标和 y 坐标,最后将坐标转换为像素坐标,并使用画笔 `brush` 在相应的位置上绘制一个像素点。
最终效果是绘制出了一个身披盛开玫瑰花的图案。