cscの作法 その51


概要

cscの作法、調べてみた。
コッホ曲線やってみた。

参考にしたページ

写真

サンプルコード

using System;
using System.Windows.Forms;
using System.Drawing;

class form1: Form {
	private const double PAI = 3.141592653589393238462643383279;
	private const double thetaOf60Degree = 60 * PAI / 180;
	private int maxLevel = 4;
	form1() {
		BackColor = Color.Black;
		this.Text = "コッホ曲線";
		ClientSize = new Size(700, 500);
	}
	protected override void OnPaint(PaintEventArgs e) {
		DrawKochCurve(e.Graphics, 0, 100, 639, 100, 0);
		base.OnPaint(e);
	}
	private void DrawKochCurve(Graphics g, int x1, int y1, int x2, int y2, int level) {
		if (level == maxLevel)
		{
			g.DrawLine(Pens.Yellow, x1, 479 - y1, x2, 479 - y2);
		}
		else
		{
			int vx = (x2 - x1) / 3;
			int vy = (y2 - y1) / 3;
			int xx1 = x1 + vx;
			int yy1 = y1 + vy;
			int[] v1 = rotate(thetaOf60Degree, vx, vy);
			int xx2 = xx1 + v1[0];
			int yy2 = yy1 + v1[1];
			int[] v2 = rotate(-thetaOf60Degree, vx, vy);
			int xx3 = xx2 + v2[0];
			int yy3 = yy2 + v2[1];
			level++;
			DrawKochCurve(g, x1, y1, xx1, yy1, level);
			DrawKochCurve(g, xx1, yy1, xx2, yy2, level);
			DrawKochCurve(g, xx2, yy2, xx3, yy3, level);
			DrawKochCurve(g, xx3, yy3, x2, y2, level);
		}
	}
	private int[] rotate(double theta, int x, int y) {
		double sinTheta = Math.Sin(theta);
		double cosTheta = Math.Cos(theta);
		int x2 = (int)(cosTheta * x - sinTheta * y);
		int y2 = (int)(sinTheta * x + cosTheta * y);
		return new int[] { x2, y2 };
	}
	[STAThread]
	public static void Main() {
		Application.Run(new form1());
	}
}






以上。