我试图在AS3上写sierpinski三角形,但看起来我错过了一些东西。我陷入了这行代码并试图解决问题。我做了计算,但似乎有些东西失踪。你可以帮忙吗?
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();
}
}
}
答案 0 :(得分:3)
这可以在没有很好的数学技能的情况下完成,因为你只需要在一开始就做复杂的计算,然后简单地使用中间点(在我的例子中:p12 =&gt; p1和p2之间的中点等等。)三角形的两侧。它是一个递归函数,每回合调用三次,直到达到最大值。我在下面列举了我的例子。小心max的值,因为如果设置得太高,它可以轻松冻结你的计算机......
这是一个非常有趣的问题!
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);
}
}
}