Note that there are some explanatory texts on larger screens.

plurals
  1. POSierpinski triangle AS3 problem
    text
    copied!<p>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?</p> <hr> <pre><code>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 &lt; num; i++) { drawTriangle(i); } } private function drawTriangle(i:Number):void { //CREATE P1 &amp; 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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload