Digital Bauhaus Vorkurs/Projekte/Form und Raum/Code9: Difference between revisions

From Medien Wiki
Line 153: Line 153:
== Segmente ==
== Segmente ==


<source lang="Java">class Segmente {
<source lang="Java" highlight="15,33,34">class Segmente {
    
    
   float x; // X-Position
   float x; // X-Position

Revision as of 17:52, 23 January 2011

Springende Formen

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 = width/2; // Mitte
    objekt[i].y = height/2;
    
    objekt[i].seg = 0; // Dreieck
    objekt[i].r = 100;
    objekt[i].g = 90; // Richtung nach oben
    
    objekt[i].h = 0; // Rot
    objekt[i].s = 100;
    objekt[i].b = 100;
    objekt[i].a = 100;
    
    objekt[i].mouseOver = false;
    objekt[i].visible = false;
    
    objekt[i].vx = 5;
    objekt[i].vy = 5;
  }
}

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() {
  if (objekt[0].mouseOver == true) {
    objekt[0].x = mouseX;
    objekt[0].y = mouseY;
  }else if (objekt[1].mouseOver == true) {
    objekt[1].x = mouseX;
    objekt[1].y = mouseY;
  }else if (objekt[2].mouseOver == true) {
    objekt[2].x = mouseX;
    objekt[2].y = mouseY;
  }else if (objekt[3].mouseOver == true) {
    objekt[3].x = mouseX;
    objekt[3].y = mouseY;
  }else if (objekt[4].mouseOver == true) {
    objekt[4].x = mouseX;
    objekt[4].y = mouseY;
  }else if (objekt[5].mouseOver == true) {
    objekt[5].x = mouseX;
    objekt[5].y = mouseY;
  }
}

void keyPressed() {
  if (key == '0') {
    objekt[0].visible = true;
  }
  if (key == '1') {
    objekt[1].visible = true;
  }
  if (key == '2') {
    objekt[2].visible = true;
  }
  if (key == '3') {
    objekt[3].visible = true;
  }
  if (key == '4') {
    objekt[4].visible = true;
  }
  if (key == '5') {
    objekt[5].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;
          }
        }
      }
    }
  }
    
  if (key == ENTER || key == RETURN) {
    for (int i=0; i < objekt.length; i++) {
      objekt[i].x = random(width);
      objekt[i].y = random(height);
      
      objekt[i].seg = (int)random(37);
      objekt[i].r = (int)random(50,100);
      
      objekt[i].h = (int)random(360);
      objekt[i].a = (int)random(50,100);
    }
  }
}

void mouseMoved() {
  for (int i=0; i < objekt.length; i++) {
    if (objekt[i].mouseOver == false) {
      objekt[i].x = objekt[i].x + objekt[i].vx;
      objekt[i].y = objekt[i].y + objekt[i].vy;
    
      if (objekt[i].x >= width || objekt[i].x <= 0){
        objekt[i].vx = -objekt[i].vx;
      }
      if (objekt[i].y >= height || objekt[i].y <= 0){
        objekt[i].vy = -objekt[i].vy;
      }
    }
  }
}

Segmente

class Segmente {
  
  float x; // X-Position
  float y; // Y-Position
  
  int seg; // Segmentzähler
  int r; // Radius
  float g; // Richtung
  
  boolean mouseOver; //Maus über dem Objekt
  boolean visible; //Objekt sichtbar
  
  int h,s,b,a; // Farbe HSB und Alpha

  float vx,vy; //Geschwindigkeit
  
  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();
  }
}