Polygon Exploration

Some fun with polygons!

/*
 Project:   Polygons
 Author:    GLV
 Date:      2024-06-17
 Version:   1.0.0
*/

int choice, seed;
int id, idlast, sb, num, mxvs;
int counter;

PVector[] v;

boolean warning = true;

void settings()
  {
  size(400, 400, P2D);
  }

void setup() 
  { 
  background(0);
  strokeWeight(2);
  noLoop();
  }
  
void draw()
  {
  background(0);
  translate(width/2, height/2);
 
  int vs = 10;                          // 1 to max vertices
  int sp = 12;                           // speed   
 
   // Some bit manipulation just for the challenge!
  id = ((counter+vs*sp)/sp)%(2*vs)-vs;    
  sb = (id>>23)&1;
  num = (id^-sb)+sb+1;
  
  stats(); // Development and debug
  
  v = new PVector[num];  
  int r = 150;
  
  // Polygons
  if(choice == 0)
    {
    stroke(255, 100, 0);
    for(int k = 0; k < num; k++)
      {
      float angle = k*(TAU/num) - TAU/4;
      float x = r*cos(angle);
      float y = r*sin(angle);
      v[k] = new PVector(x, y);
      }
    }
    
  // Random points
  else if (choice == 1)
    {
    stroke(0, 255, 0);
    randomSeed(seed);
    for(int k = 0; k < num; k++)
      {  
      float x = random(-width/2+10, width/2-10);
      float y = random(-height/2+10, height/2-10);
      v[k] = new PVector(x, y);
      }
    } 
    
  // Random offsets on polygons
  else if (choice == 2)
    {
    stroke(255, 255, 0);  
    for(int k = 0; k < num; k++)
      {
      r = (int) random(130, 170);
      float angle = k*(TAU/num) - TAU/4;
      float x = r*cos(angle);
      float y = r*sin(angle);
      v[k] = new PVector(x, y);
      }
    }  
      
  for(int i = 0; i < num-1; i++)
    {
    for(int j = i+1; j < num; j++)
      {
      //println(i,j);  
      line(v[i].x, v[i].y, v[j].x, v[j].y);
      }
    } 
    
  counter++;
  
  if (warning)
    {
    background(0);
    
    rectMode(CENTER);
    fill(0);
    noStroke();
    rect(0, 0, width, height);
    
    textAlign(CENTER, CENTER);
    textSize(48);
    fill(255, 0, 0);
    text("WARNING", 0, -height/3);
    
    fill(255, 255, 0);
    textSize(32);
    text("This animation contains \n fast moving images.", 0, -50);
    fill(255, 255, 0);
    textSize(32);
    text("Press 5 to continue. \n Press any other key to exit.", 0, 60);  
    fill(0, 255, 255);
    textSize(24);
    text("Press mouse to change animation!", 0, 140); 
    }
  }

void stats()
  {
  if(id != idlast) 
    {
    println(str(' ')+num, id); // end line and line feed. 
    } 
  print(sb);
  idlast = id;
  }

void keyPressed()
  {
  if (key == '5')
    {
    warning = false;
    choice = 0;
    counter = 0;
    loop();  
    }
  else
    exit();
  }
  
void mousePressed()
  {
  choice++;
  choice = choice%3;
  counter = 0;
  seed = frameCount;
  println(choice);
  }

image

image

:)

1 Like