Digital Bauhaus Vorkurs/Projekte/Form und Raum/Code11: Difference between revisions
From Medien Wiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== Form und Raum == | == Form und Raum == | ||
<source lang="Java" | <source lang="Java" highlight="27,122,127,154,162,172">Segmente[] objekt = new Segmente[9]; | ||
void setup() { | void setup() { | ||
Line 189: | Line 189: | ||
== Segmente == | == Segmente == | ||
<source lang="Java" | <source lang="Java" highlight="12">class Segmente { | ||
float x; // X-Position | float x; // X-Position |
Revision as of 22:56, 31 January 2011
Form und Raum
Segmente[] objekt = new Segmente[9];
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;
objekt[i].move = false;
objekt[i].sx = 5;
objekt[i].sy = 5;
}
}
void draw() {
background(0);
for (int i=0; i < objekt.length; i++) {
if (objekt[i].visible) {
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) {
objekt[i].x = mouseX;
objekt[i].y = mouseY;
break;
}
}
}
void keyPressed() {
int k = (int)key-48-1; //Zahlenverschiebung: '1' entspricht objekt[0]
if ((k >= 0) && (k < objekt.length)) {
if (objekt[k].visible == false) {
objekt[k].visible = true;
}else if (objekt[k].visible) {
objekt[k].visible = false;
}
}
if (key == CODED) {
for (int i=0; i < objekt.length; i++) {
if (objekt[i].mouseOver) {
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) {
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 == '/') {
if (objekt[i].g <= 360) {
objekt[i].g += 1;
}
}
if (key == '*') {
if (objekt[i].g >= 1) {
objekt[i].g -= 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].g = random(360);
objekt[i].h = (int)random(360);
objekt[i].a = (int)random(50,100);
}
}
for (int i=0; i < objekt.length; i++) {
if ((key == '0') && (objekt[i].move == false)) {
objekt[i].move = true;
}else if ((key == '0') && (objekt[i].move)) {
objekt[i].move = false;
}
}
}
void mouseMoved() {
for (int i=0; i < objekt.length; i++) {
if ((objekt[i].move == true) && (objekt[i].mouseOver == false)) {
objekt[i].x = objekt[i].x + objekt[i].sx;
objekt[i].y = objekt[i].y + objekt[i].sy;
if (objekt[i].x >= width || objekt[i].x <= 0){
objekt[i].sx = -objekt[i].sx;
}
if (objekt[i].y >= height || objekt[i].y <= 0){
objekt[i].sy = -objekt[i].sy;
}
}
}
}
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
boolean move; //Objekt bewegt sich
int h,s,b,a; // Farbe HSB und Alpha
float sx,sy; //Geschwindigeit
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();
}
}