문제

I am trying to write sierpinski triangle on AS3 ,but it looks like i am missing something.I am stuck in this lines of code and trying to solve what is the problem.I made the calculations but it seems like there is something missing.Can you help?


public class Fract extends Sprite 
{
    private var rand:Function = Math.random;
    private var num:Number = 5;
    private var w:Number = stage.stageWidth;
    private var h:Number = stage.stageHeight;
    private var spr:Sprite;
    private var small:Sprite;

    public function Fract()
    {
        init();
    }

    private function init():void
    {   
        var i:Number;
        spr = new Sprite();
        small = new Sprite();
        addChild(spr);
        addChild(small);

        for (i = 1; i < num; i++)
        {
            drawTriangle(i);
        }
    }

    private function drawTriangle(i:Number):void
    {
        //CREATE P1 & P2
        var p1:Point = new Point((w / 4), (h - h / 4));
        var p2:Point = new Point((w - w / 4), p1.y);
        //Calculate distance between first two points
        var dist:Number = p2.x - p1.x;
        var dist2:Number = dist / 2;
        //Calculate p3y-so the triangle is equilateral
        var p3y:Number = Math.sqrt((dist * dist) - (dist2 * dist2));
        var p3:Point = new Point(p1.x + dist2, p1.y - p3y);

        if (i == 1)
        {
            spr.graphics.lineStyle(1, 0, 1);
            spr.graphics.beginFill(0, 1);
            spr.graphics.moveTo(p1.x, p1.y);
            spr.graphics.lineTo(p2.x, p2.y);
            spr.graphics.lineTo(p3.x, p3.y);
            spr.graphics.lineTo(p1.x, p1.y);
            spr.graphics.endFill();
        }
        else
        {
            var p4:Point = new Point(p1.x + (dist2 / i), p1.y - (p3y / i));
            var p5:Point = new Point(p4.x + (dist2 / (i - 1)), p4.y);
            var smallDist:Number = p5.x - p4.x;
            var smallDist2:Number = smallDist / 2;
            var p6y:Number = Math.sqrt((smallDist * smallDist) - (smallDist2 * smallDist2));
            var p6:Point = new Point(p4.x + smallDist2, p4.y + p6y);

            small.graphics.lineStyle(1, 0, 1);
            small.graphics.beginFill(0xffffff, 1);
            small.graphics.moveTo(p4.x, p4.y);
            small.graphics.lineTo(p5.x, p5.y);
            small.graphics.lineTo(p6.x, p6.y);
            small.graphics.lineTo(p4.x, p4.y);
            small.graphics.endFill();
        }
    }
}
도움이 되었습니까?

해결책

This can be done without great math skills, since you only have to do the complicated calculation in the very beginning, and then simply use the mid points (in my example: p12 => mid point between p1 and p2, etc.) of the triangle's sides. It is a recursive function, which calls itself three times each turn, until a max level is reached. I've included my example below. Be careful with the value for max, because it can easily freeze your computer for a while, if set too high...

This was a really fun question!

public class Fract extends Sprite 
{
    private var w : Number = stage.stageWidth;
    private var h : Number = stage.stageHeight;
    private var max : uint = 6;

    public function Fract ()
    {
        init( );
    }

    private function init () : void
    {   
        var p1 : Point = new Point( (w / 4), (h - h / 4) );
        var p2 : Point = new Point( (w - w / 4), p1.y );
        var dist : Number = (p2.x - p1.x);
        var dist2 : Number = dist * .5;
        var p3 : Point = new Point( p1.x + dist2, p1.y - Math.sqrt( (dist * dist) - (dist2 * dist2) ) );

        drawTriangles( p1, p2, p3, 0, max );
    }

    private function drawTriangles (p1 : Point, p2 : Point, p3 : Point, level : uint, max : uint) : void
    {
        var spr : Sprite = new Sprite( );
        addChild( spr );
        var g : Graphics = spr.graphics;
        g.lineStyle( 1, 0, 1 );
        g.moveTo( p1.x, p1.y );
        g.lineTo( p2.x, p2.y );
        g.lineTo( p3.x, p3.y );
        g.lineTo( p1.x, p1.y );

        if (level < max) 
        {
            var p12 : Point = new Point( p1.x + (p2.x - p1.x) * .5, p1.y + (p2.y - p1.y) * .5 );
            var p23 : Point = new Point( p2.x + (p3.x - p2.x) * .5, p2.y + (p3.y - p2.y) * .5 );
            var p31 : Point = new Point( p3.x + (p1.x - p3.x) * .5, p3.y + (p1.y - p3.y) * .5 );
            drawTriangles( p1, p12, p31, level + 1, max);
            drawTriangles( p12, p2, p23, level + 1, max);
            drawTriangles( p31, p23, p3, level + 1, max);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top