Categories
Generative Gestaltung

Rotation of a regular polygon sphere

/**
 * Rotation of a regular polygon sphere
 * Author: Matthias Jäger
 * Date: 2020-06-26
 */

/** VARIABLES */
// ---------------------------------------------------------------------- //

// Diameter of the tracing sphere 
const diameter = 100.0;

// Speed of rotation
const step = 0.01;

// Radius of rotation 
const radius = 200.0;

// Holding the current angle of rotation
let angle = 0.0;


/** MAIN */
// ---------------------------------------------------------------------- //

/** Called first, once */
function setup() {

  // Create the a square canvas in 3d mode 
  createCanvas(800, 800, WEBGL);

  // Clearing the background only once
  background(255);

  // Overwrite the default color with transparent white
  fill(255, 250);
}


/** Contiously called each frame */
function draw() {

  // Check if the rotation is a full turn
  if (angle < TWO_PI) {

    // Calculate the current position
    let x = cos(angle) * radius;
    let y = sin(angle) * radius;
    let z = -cos(angle * sin(angle)) * radius;

    // Draw the polygon sphere 
    push();
    translate(x, y, z);
    sphere(diameter);
    pop();

    // Done, icrement the angle
    angle += step;
  
  } else {
    
    // Stop rendering into the canvas
    noLoop();
    
    // Export current canvas.
    save("Rotation-of-a-regular-polygon-phere.jpg");
  }
}