コッホ曲線(コッホきょくせん、英語: Koch curve)はフラクタル図形の一つ。スウェーデンの数学者ヘルゲ・フォン・コッホ (Helge von Koch) が考案した。線分を3等分し、分割した2点を頂点とする正三角形の作図を無限に繰り返すことによって得られる図形である。1回の操作で線分の長さが 4/3 倍になるので、操作を無限に繰り返して得られるコッホ曲線の長さは無限大である。完全なものは作図することができない。  ※Wikipediaより

はい作ってみました
なんかこういうの好き
美しいよね

実際に無限回繰り返すことなんて不可能なので
繰り返し回数は6回までにしました
延々と続けてもいいんだけどまた時限式ブラクラって言われるから・・・w

※クリックでstart

Flash Player 10 にしてください

右クリック→拡大 で拡大してみると楽しいよ
もっと大きく見たい人はこちらでどうぞ
コッホ曲線拡大版

Line.as


package {
	import flash.display.Sprite;
	import flash.geom.Point;
	public class Line extends Sprite{
		public var startP:Point;
		public var endP:Point;
		public var distance:Number;
		public var angle:Number;
		function Line(startP:Point, endP:Point) {
			this.startP = startP;
			this.endP = endP;
			distance = Point.distance(startP, endP);
			angle = Math.atan2(endP.y-startP.y, endP.x-startP.x);

			graphics.lineStyle(0,0x000000);
			graphics.moveTo(startP.x, startP.y);
			graphics.lineTo(endP.x, endP.y);
			graphics.endFill();
		}
	}
}

KochCurve.as


package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Point;
	public class KochCurve extends Sprite {
		private var list:Array = [];
		private var endPoint:Point;
		private var count:int=0;
		private const N:int=6;
		private const W:int = 300;
		private const H:int = 300;
		function KochCurve() {
			var line:Line=new Line(new Point(0,H/2),new Point(W, H/2));
			endPoint=line.endP;
			list.push(line);
			stage.addChild(line);

			stage.addEventListener(MouseEvent.CLICK,onClick);
			function onClick(event:MouseEvent):void {
				stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
				stage.removeEventListener(MouseEvent.CLICK,onClick);
			}

		}
		private function onEnterFrameHandler(event:Event):void {
			var line:Line=list[0];
			stage.removeChild(line);
			list.splice(0, 1);

			var p1:Point=new Point();
			p1.x=Math.cos(line.angle) * line.distance / 3 + line.startP.x;
			p1.y=Math.sin(line.angle) * line.distance / 3 + line.startP.y;
			var p2:Point=new Point();
			p2.x=Math.cos(line.angle - Math.PI / 3) * line.distance / 3 + p1.x;
			p2.y=Math.sin(line.angle - Math.PI / 3) * line.distance / 3 + p1.y;
			var p3:Point=new Point();
			p3.x=Math.cos(line.angle + Math.PI / 3) * line.distance / 3 + p2.x;
			p3.y=Math.sin(line.angle + Math.PI / 3) * line.distance / 3 + p2.y;

			var newLine:Line=new Line(line.startP,p1);
			stage.addChild(newLine);
			list.push(newLine);
			newLine=new Line(p1,p2);
			stage.addChild(newLine);
			list.push(newLine);
			newLine=new Line(p2,p3);
			stage.addChild(newLine);
			list.push(newLine);
			newLine=new Line(p3,line.endP);
			stage.addChild(newLine);
			list.push(newLine);

			if (line.endP == endPoint) {
				count++;
				if (count == N) {
					stage.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
				}
			}
		}
	}
}
このエントリーを含むはてなブックマークはてなブックマーク - フラクタル~コッホ曲線~ この記事をクリップ!Livedoorクリップ - フラクタル~コッホ曲線~ BuzzurlにブックマークBuzzurlにブックマーク FC2ブックマークへ追加 Bookmark this on Delicious Digg This