public partial class Form2 : Form { private const int MaxLevel = 3; public Form2() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); //DrawTriangle(this.pictureBox1.CreateGraphics()); } private void DrawTriangle(Graphics g) { Point pt1 = new Point(this.ClientRectangle.Width / 2, 10); int h = this.ClientRectangle.Height - 20; Point pt2 = pt1; Point pt3 = pt1; pt2.Offset((int)(-h * Math.Cos(Math.PI / 6.0) / 2), h); pt3.Offset((int)(h * Math.Cos(Math.PI / 6.0) / 2), h); g.FillPolygon(Brushes.Black , new Point[] { pt1, pt2, pt3}); Triangle(g, pt1, pt2, pt3, 1); } private void Triangle(Graphics g, Point p1, Point p2, Point p3, int level) { Point c1 = new Point(Math.Abs(p3.X + p2.X) / 2, Math.Abs(p3.Y + p2.Y) / 2); Point c2 = new Point(Math.Abs(p3.X + p1.X) / 2, Math.Abs(p3.Y + p1.Y) / 2); Point c3 = new Point(Math.Abs(p2.X + p1.X) / 2, Math.Abs(p2.Y + p1.Y) / 2); g.FillPolygon(Brushes.White, new Point[] { c1, c2, c3 }); if (level < MaxLevel) { Triangle(g, p1, c3, c2, level + 1); Triangle(g, c3, p2, c1, level + 1); Triangle(g, c2, c1, p3, level + 1); } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { DrawTriangle(e.Graphics); } |