Superformula
March 3rd, 2005
The Superformula developed by Johan Gielis is a simple formula that can generate a vast diversity of natural shapes.
It produces everything from simple triangles and pentagons, to stars, spirals and petals. More information on the Superformula: Paul Bourke.
Based on the first Flash implementations of Flashforum members, Especially based on e2e4 and i++ contributions.
I’ve put together a little example to draw SuperShapes, copy the code into frame 1 of a new flash movie.
function getSuperPoint (m:Number, n1:Number, n2:Number, n3:Number, phi:Number)
{
var r:Number;
var t1:Number;
var t2:Number;
var a:Number = 1;
var b:Number = 1;
var x:Number = 0;
var y:Number = 0;
t1 = (1 / a) * Math.cos ((m / 4) * phi);
t1 = Math.abs (t1);
t1 = Math.pow (t1, n2);
t2 = (1 / b) * Math.sin ((m / 4) * phi);
t2 = Math.abs (t2);
t2 = Math.pow (t2, n3);
r = Math.pow (t1 + t2, 1 / n1);
if (r != 0)
{
r = 1 / r;
x = r * Math.cos (phi);
y = r * Math.sin (phi);
}
var p:Object = {x:x, y:y};
return p;
}
function drawSuperShape (m, n1, n2, n3)
{
var maxAngle:Number = 12 * Math.PI;
var x = 300;
var y = 200;
_root.lineStyle (1, 0x0000AA);
for (var i = 0; i < = maxAngle; i += 0.01)
{
var p:Object = getSuperPoint (m, n1, n2, n3, i);
if (i == 0)
{
_root.moveTo (x + (p.x * 100), y + (p.y * 100));
}
else
{
_root.lineTo (x + (p.x * 100), y + (p.y * 100));
}
}
}
// Then create an animated runner...
var a = 70, b = 10;
var m:Number = a / b;
var n1:Number = .5;
var n2:Number = 3;
var n3:Number = .5;
_root.createEmptyMovieClip ("control", 100);
control.onEnterFrame = function ()
{
b -= 3;
a += 1.5;
_root.m = a / b;
_root.clear ();
_root.beginFill (0x000000, 10);
_root.drawSuperShape (_root.m, _root.n1, _root.n2, _root.n3);
_root.endFill ();
};
can you Digg it?
is it del.icio.us? 
Category: General



Leave a Comment
You must be logged in to post a comment .
Trackback this post | Subscribe to the comments via RSS Feed