Advertisement
SuperMonkey

Sierpinsky.hx

Jul 14th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.17 KB | None | 0 0
  1. import flash.display.Graphics;
  2. import flash.display.Sprite;
  3.  
  4. class Point extends flash.geom.Point {
  5.     public function new(x,y) {
  6.         super(x,y);
  7.     }
  8.  
  9.     public function mid(pt2:Point) {
  10.         return new Point((pt2.x+x)/2, (pt2.y+y)/2);
  11.     }
  12. }
  13.  
  14. class Sierpinsky extends Sprite {
  15.     public var w:Int;
  16.     public var h:Int;
  17.  
  18.     public function new() {
  19.         super();
  20.  
  21.         flash.Lib.current.addChild(this);
  22.  
  23.         w = flash.Lib.current.stage.stageWidth;
  24.         h = flash.Lib.current.stage.stageHeight;
  25.     }
  26.  
  27.     public function triangle(a:Point, b:Point, c:Point) {
  28.         graphics.moveTo(a.x, a.y);
  29.         graphics.lineStyle(1);
  30.        
  31.         graphics.lineTo(b.x, b.y);
  32.         graphics.lineTo(c.x, c.y);
  33.         graphics.lineTo(a.x, a.y);
  34.        
  35.     }
  36.  
  37.     public function sier(a:Point, b:Point, c:Point, iter:Int) {
  38.         if(iter == 0) {
  39.             return ;
  40.         }
  41.  
  42.         triangle(a.mid(b),b.mid(c),c.mid(a));
  43.  
  44.         sier(a.mid(b), b, b.mid(c), iter-1);
  45.         sier(a.mid(b), a, a.mid(c), iter-1);
  46.         sier(b.mid(c), a.mid(c), c, iter-1);
  47.     }
  48.  
  49.     public static function main() {
  50.         var s = new Sierpinsky();
  51.  
  52.         s.triangle(new Point(s.w / 2, 0), new Point(s.w,s.h), new Point(0,s.h));
  53.         s.sier(new Point(s.w / 2, 0), new Point(s.w,s.h), new Point(0,s.h), 7);
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement