Program jest napisany w C#.
Program do rysowania trojkata Sierpinskiego z tej strony: http://csharphelper.com/blog/2015/01/draw-sierpinski-triangle-c/
Zmodyfikowany kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace howto_sierpinski_triangle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int Level = 3;
private int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
DrawGasket();
}
private void btnDraw_Click(object sender, EventArgs e)
{
DrawGasket();
}
private void Form1_Resize(object sender, EventArgs e)
{
DrawGasket();
}
// Draw the triangle.
private void DrawGasket()
{
Level = int.Parse(txtLevel.Text);
i = 1;
Bitmap bm = new Bitmap(
picGasket.ClientSize.Width,
picGasket.ClientSize.Height);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.Clear(Color.White);
gr.SmoothingMode = SmoothingMode.AntiAlias;
// Draw the top-level triangle.
const float margin = 10;
PointF top_point = new PointF(
picGasket.ClientSize.Width / 2f,
margin);
PointF left_point = new PointF(
margin,
picGasket.ClientSize.Height - margin);
PointF right_point = new PointF(
picGasket.ClientRectangle.Right - margin,
picGasket.ClientRectangle.Bottom - margin);
DrawTriangle(gr, Level, top_point, left_point, right_point);
}
// Display the result.
picGasket.Image = bm;
// Save the bitmap into a file.
bm.Save("Triangle " + Level + ".bmp");
}
// Draw a triangle between the points.
private void DrawTriangle(Graphics gr, int level,
PointF top_point, PointF left_point, PointF right_point)
{
// See if we should stop.
if (level == 0)
{
// Fill the triangle.
PointF[] points =
{
top_point, right_point, left_point
};
gr.FillPolygon(Brushes.Red, points);
Font drawFont = new Font("Arial", 12);
SolidBrush drawBrush = new SolidBrush(Color.Black);
SizeF stringSize = new SizeF();
stringSize = gr.MeasureString(i.ToString(), drawFont);
PointF drawPoint = new PointF(top_point.X - (stringSize.Width / 2), (top_point.Y + right_point.Y) / 2);
gr.DrawString(i.ToString(),drawFont,drawBrush,drawPoint);
i++;
}
else
{
// Find the edge midpoints.
PointF left_mid = new PointF(
(top_point.X + left_point.X) / 2f,
(top_point.Y + left_point.Y) / 2f);
PointF right_mid = new PointF(
(top_point.X + right_point.X) / 2f,
(top_point.Y + right_point.Y) / 2f);
PointF bottom_mid = new PointF(
(left_point.X + right_point.X) / 2f,
(left_point.Y + right_point.Y) / 2f);
// Recursively draw smaller triangles.
DrawTriangle(gr, level - 1, top_point, left_mid, right_mid);
DrawTriangle(gr, level - 1, left_mid, left_point, bottom_mid);
DrawTriangle(gr, level - 1, right_mid, bottom_mid, right_point);
}
}
}
}
efekt na dołączonym obrazku ;)