Skip to content

Instantly share code, notes, and snippets.

@jonobr1
Created February 23, 2012 17:00
Show Gist options
  • Save jonobr1/1893752 to your computer and use it in GitHub Desktop.
Save jonobr1/1893752 to your computer and use it in GitHub Desktop.
@brysonian's Superformula in JavaScript
(function() {
var root = this;
var previousSuperformula = root.Superformula;
var NP = 360, phi = Math.PI * 2 / NP;
var Superformula = root.Superformula = function(m, n1, n2, n3) {
var points = [];
for (var i = 0; i <= NP; i++) {
points.push(new Point(m, n1, n2, n3, phi * i));
}
return points;
};
Superformula.noConflict = function() {
root.Superformula = previousSuperformula;
return this;
};
var Point = function(m, n1, n2, n3, phi) {
var r, t1, t2, a = 1, b = 2;
t1 = Math.cos(m * phi / 4) / a;
t1 = Math.abs(t1);
t1 = Math.pow(t1, n2);
t2 = Math.sin(m * phi / 4) / b;
t2 = Math.abs(t2);
t2 = Math.pow(t2, n3);
r = Math.pow(t1 + t2, 1 / n1);
if (Math.abs(r) === 0) {
this.x = 0;
this.y = 0;
} else {
r = 1 / r;
this.x = r * Math.cos(phi);
this.y = r * Math.sin(phi);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment