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; namespace KochCurve { public partial class Form1 : Form { public Image image; public Graphics g; // グラフィックイメージ用変数 public Pen pen = new Pen(Color.Red); // 描画ペン public float curX, curY, curTh; // 現在位置、角度 public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawImage(image, 0, 0); } private void start(float X, float Y, float Th) { curX = X; curY = Y; curTh = Th; } private void turn(float Th) { curTh += Th; } private void move(float LN) { float TH = curTh * (float)(Math.PI / 180); float X = curX + LN * (float) Math.Cos(TH); float Y = curY + LN * (float) Math.Sin(TH); g.DrawLine(pen, curX, 200F - curY, X, 200F - Y); curX = X; curY = Y; } private void Koch(int N, float LN) { if (N <= 0) { move(LN); return; } int NN = N - 1; float L = LN / 3; Koch(NN, L); turn(60); Koch(NN, L); turn(-120); Koch(NN, L); turn(60); Koch(NN, L); } private void Form1_Load(object sender, EventArgs e) { image = new Bitmap(300, 300); g = Graphics.FromImage(image); g.Clear(Color.White); } private void button1_Click(object sender, EventArgs e) { start(0, 0, 0); Koch(5, 300); this.Invalidate(); } } }