Digital Bauhaus Vorkurs/Projekte/Form und Raum/Code10

From Medien Wiki

Veränderbare Komposition

Segmente[] objekt = new Segmente[6];

void setup() {
  size(800, 800);
  smooth();
  noStroke();
  frameRate(5);
  colorMode(HSB,360,100,100,100);
  
  for (int i=0; i < objekt.length; i++) {
    objekt[i] = new Segmente();
    
    objekt[i].x = random(100,width-100);
    objekt[i].y = random(100,height-100);
    
    objekt[i].seg = (int)random(6);
    objekt[i].r = 100;
    objekt[i].g = 90; // Richtung nach oben
    
    objekt[i].h = (int)random(360);
    objekt[i].s = 100;
    objekt[i].b = 100;
    objekt[i].a = 100;
    
    objekt[i].mouseOver = false;
    objekt[i].visible = false;
  }
}

void draw() {
  background(0);
  
  for (int i=0; i < objekt.length; i++) {
    if (objekt[i].visible == true) {
      objekt[i].maleSegmente();
    }
  }
  
  for (int i=0; i < objekt.length; i++) {
    if ((mouseX >= objekt[i].x-objekt[i].r) && (mouseX <= objekt[i].x+objekt[i].r) && (mouseY >= objekt[i].y-objekt[i].r) && (mouseY <= objekt[i].y+objekt[i].r)) {
      objekt[i].mouseOver = true;
    }else {
      objekt[i].mouseOver = false;
    }
  }
}

void mouseDragged() {
  for (int i=objekt.length-1; i >= 0 ; i--) {
    if (objekt[i].mouseOver == true) {
      objekt[i].x = mouseX;
      objekt[i].y = mouseY;
      break;
    }
  }
}

void keyPressed() {
  int k = (int)key-48-1;
  if ((k >= 0) && (k < objekt.length)) {
    objekt[k].visible = true;
  }
  
  if (key == CODED) {
    for (int i=0; i < objekt.length; i++) {
      if (objekt[i].mouseOver == true) {
        if (keyCode == UP) {
          if (objekt[i].seg <= 100) {
            objekt[i].seg += 1;
          }
        }
        if (keyCode == DOWN) {
          if (objekt[i].seg >= 1) {
            objekt[i].seg -= 1;
          }
        } 
        if (keyCode == RIGHT) {
          if (objekt[i].h <= 360) {
            objekt[i].h += 1;
          }
        }
        if (keyCode == LEFT) {
          if (objekt[i].h >= 1) {
            objekt[i].h -= 1;
          }
        }
      }
    }
  }
  
  for (int i=0; i < objekt.length; i++) {
    if (objekt[i].mouseOver == true) {
      if (key == ',') {
        if (objekt[i].r <= 200) {
          objekt[i].r += 1;
        }
      }
      if (key == '.') {
        if (objekt[i].r >= 50) {
          objekt[i].r -= 1;
        }
      } 
      if (key == '+') {
        if (objekt[i].a <= 100) {
          objekt[i].a += 1;
        }
      }
      if (key == '-') {
        if (objekt[i].a >= 10) {
          objekt[i].a -= 1;
        }
      }
    }
  }
    
  if (key == ENTER || key == RETURN) {
    for (int i=0; i < objekt.length; i++) {
      objekt[i].x = random(width);
      objekt[i].y = random(height);
      
      switch((int)random(4)) {
        case 0:
          objekt[i].seg = (int)random(6);
          break;
        case 1:
          objekt[i].seg = (int)random(6,12); 
         break; 
        case 3:
          objekt[i].seg = (int)random(12,100);
          break;
        default:
          break;
      }
      objekt[i].r = (int)random(50,100);
      
      objekt[i].h = (int)random(360);
      objekt[i].a = (int)random(50,100);
    }
  }
}

Segmente

class Segmente {
  
  float x; // X-Position
  float y; // Y-Position
  
  int seg; // Segmentzähler
  int r; // Radius
  float g; // Richtung bzw. Grad
  
  boolean mouseOver; //Maus über dem Objekt
  boolean visible; //Objekt sichtbar
  
  int h,s,b,a; // Farbe HSB und Alpha
  
  Segmente() {
    println("Segmente erstellt!");
  }
  
  void maleSegmente() {
    fill(h,s,b,a);
    maleSegmente(seg,r,g,x,y);
  }
  
  void maleSegmente(int seg,int r,float g,float x,float y) {
    float segmentzahl = 3 + (float)seg;
    float angleStep = 360/segmentzahl;
    
    beginShape();
    vertex(x, y); 
    for (float angle=0; angle<=360; angle+=angleStep) {
      float vx = x + cos(radians(angle-g))*r;
      float vy = y + sin(radians(angle-g))*r;
      vertex(vx, vy);
    }
    vertex(x + cos(radians(360-g))*r, y + sin(radians(360-g))*r);
    endShape();
  }
}