Koch curve variant in C# using delegates and userPaintBox
Sunday, December 27th, 2009Why? 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




Bit humour is a term I coined during my university days, and refers to technology (often CS) related quips or short pieces of wit, usually intentional but often unintentional. Mostly in the form of jokes, bit humour was often applied as a means of “de-coupling” from long, boring technical lectures, or as a type of friendly satire during homework preparations!
Check out this sweet implementation of Frogger in, yes, JavaScript: