Koch curve variant in C# using delegates and userPaintBox
Why? Because I can. And because delegates are cute.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Setup
turtleBox.Reset();
KochDelegate kochFunction = null;
kochFunction = delegate(int angle, int step, int depth)
{
if (depth-- < 0) return;
turtleBox.Turn(angle);
kochFunction(-angle, step, depth);
turtleBox.Move(step);
turtleBox.Turn(-angle);
kochFunction(angle, step, depth);
turtleBox.Move(step);
kochFunction(angle, step, depth);
turtleBox.Turn(-angle);
turtleBox.Move(step);
kochFunction(-angle, step, depth);
turtleBox.Turn(angle);
};
kochFunction(90, 4, 8);
}
}
public class TurtleBox : PictureBox
{
private Pen pen = new Pen(Color.Black);
private Graphics graphics;
public void Reset()
{
Image = new Bitmap(Size.Width, Size.Height);
graphics = Graphics.FromImage(Image);
graphics.Clear(Color.White);
graphics.ResetTransform();
graphics.ScaleTransform(1, -1);
graphics.TranslateTransform(0, -Size.Height + 1);
Turn(-90);
}
public void Turn(float angle)
{
graphics.RotateTransform(angle);
}
public new void Move(int step)
{
graphics.DrawLine(pen, 0, 0, 0, step);
graphics.TranslateTransform(0, step);
}
}
delegate void KochDelegate(int angle, int step, int depth);
Tuddelum

January 8th, 2010 at 04:44
Wow. That looks pretty cool. And the code isn’t even that big.
January 8th, 2010 at 06:06
Nej nemlig! Det er sjovt at prøve at tænke anstrakt engang imellem og så finde en løsning der er ultrakort!