Raymond Z.

asked • 09/19/20

Help with understanding a Javascript program that deals with using trigonometry and transformations

var numPlanets = 4;
var arms = 11;
var speed = 0.015;
var tick = 0;
translate(200,200);
var draw = function() {
background(0, 0, 0);
tick += speed;
for (var s = 10; s < arms * 20; s += 20) {
pushMatrix();
for (var p = 0; p < numPlanets; p++) {
fill(255, 0, 60 * p + 20);
ellipse(0, 0, 30 / p, 30 / p);
translate(sin(tick * p + s) * (8 / (p * 0.1)), cos(tick * p + s) * (8 / (p * 0.1)));
}
popMatrix();
}
};

I learned a bit about push-popMatrix() and transformation methods like translate() and rotate(), but I still can't understand how this program works. How does Line 14 allow the ellipses drawn spin relative to each other and how are the variables and trig methods used to dictate the translation?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.