C#picturebox生成科赫曲线
时间: 2023-12-01 12:42:36 浏览: 67
生成科赫曲线程序
5星 · 资源好评率100%
```C#
private void DrawKoch(Graphics g, PointF p1, PointF p2, int n)
{
if (n == 0)
{
g.DrawLine(Pens.Black, p1, p2);
}
else
{
float dx = (p2.X - p1.X) / 3;
float dy = (p2.Y - p1.Y) / 3;
PointF pA = new PointF(p1.X + dx, p1.Y + dy);
PointF pB = new PointF(p2.X - dx, p2.Y - dy);
float angle = (float)Math.PI / 3;
float x = pB.X - pA.X;
float y = pB.Y - pA.Y;
PointF pC = new PointF(pA.X + x * (float)Math.Cos(angle) + y * (float)Math.Sin(angle), pA.Y - x * (float)Math.Sin(angle) + y * (float)Math.Cos(angle));
DrawKoch(g, p1, pA, n - 1);
DrawKoch(g, pA, pC, n - 1);
DrawKoch(g, pC, pB, n - 1);
DrawKoch(g, pB, p2, n - 1);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
PointF p1 = new PointF(50,200);
PointF p2 = new PointF(550, 200);
DrawKoch(g, p1, p2, 5);
}
// 在PictureBox上绘制科赫曲线
```
--相关问题--:
1. C#中如何实现图片的裁剪和旋转?
2. 如何在C#
阅读全文