need help with mousePressed conflict (basic)
in
Programming Questions
•
2 years ago
hey I'm kind of a newbie at processing, and I'm trying to modify the mousepressed function for a code I borrowed off openprocessing.org.
basically what I want to happen is when I press right mouse button, the color of the water changes hue or color within HSB color mode.
here is the code. I have highlighted where I have been playing around in red.
final int W = 200;
final int H = 200;
final int zoom = 3;
final int Speed = 13;
final float A = 8;
float[][][] u = new float[W][H][3];
void setup() {
size(400, 400, P3D);
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
for(int k=0; k<3; k++) {
u[i][j][k] = 0;
}
}
}
colorMode(HSB, 255);
}
void draw() {
background(0,0,0);
noStroke();
translate(width/2-W, height/2-H);
scale(zoom*0.9);
for(int i=0; i<W-1; i++) {
for(int j=0; j<H-1; j++) {
fill(160,200,map(u[i][j][0],-255,255,-55,200) + 55);
beginShape();
vertex(i,j, u[i][j][0]*0.1);
vertex(i+1,j, u[i+1][j][0]*0.1);
vertex(i+1,j+1, u[i+1][j+1][0]*0.1);
vertex(i,j+1, u[i][j+1][0]*0.1);
endShape();
}
}
for(int i=0; i<Speed; i++) {
calc();
}
}
void calc() {
float B = 1/A;
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
if(i==W-1 || j==H-1) {
u[i][j][0] = 0;
}
else if(i==0 || j==0) {
u[i][j][0] = 0;
}
else {
u[i][j][0] = B * ( -A*u[i][j][2] + 2*(A-2)*u[i][j][1] + u[i-1][j][1] + u[i+1][j][1] + u[i][j-1][1] + u[i][j+1][1]) * 0.99;
}
}
}
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
u[i][j][2] = u[i][j][1];
u[i][j][1] = u[i][j][0];
}
}
}
void mousePressed() {
u[constrain(mouseX/zoom,0,W-1)][constrain(mouseY/zoom,0,H-1)][2] = 255;
if (mousePressed = RIGHT){
fill(random(0,255));
}
}
void mouseDragged() {
u[constrain(mouseX/zoom,0,W-1)][constrain(mouseY/zoom,0,H-1)][2] = 100;
}
basically what I want to happen is when I press right mouse button, the color of the water changes hue or color within HSB color mode.
here is the code. I have highlighted where I have been playing around in red.
final int W = 200;
final int H = 200;
final int zoom = 3;
final int Speed = 13;
final float A = 8;
float[][][] u = new float[W][H][3];
void setup() {
size(400, 400, P3D);
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
for(int k=0; k<3; k++) {
u[i][j][k] = 0;
}
}
}
colorMode(HSB, 255);
}
void draw() {
background(0,0,0);
noStroke();
translate(width/2-W, height/2-H);
scale(zoom*0.9);
for(int i=0; i<W-1; i++) {
for(int j=0; j<H-1; j++) {
fill(160,200,map(u[i][j][0],-255,255,-55,200) + 55);
beginShape();
vertex(i,j, u[i][j][0]*0.1);
vertex(i+1,j, u[i+1][j][0]*0.1);
vertex(i+1,j+1, u[i+1][j+1][0]*0.1);
vertex(i,j+1, u[i][j+1][0]*0.1);
endShape();
}
}
for(int i=0; i<Speed; i++) {
calc();
}
}
void calc() {
float B = 1/A;
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
if(i==W-1 || j==H-1) {
u[i][j][0] = 0;
}
else if(i==0 || j==0) {
u[i][j][0] = 0;
}
else {
u[i][j][0] = B * ( -A*u[i][j][2] + 2*(A-2)*u[i][j][1] + u[i-1][j][1] + u[i+1][j][1] + u[i][j-1][1] + u[i][j+1][1]) * 0.99;
}
}
}
for(int i=0; i<W; i++) {
for(int j=0; j<H; j++) {
u[i][j][2] = u[i][j][1];
u[i][j][1] = u[i][j][0];
}
}
}
void mousePressed() {
u[constrain(mouseX/zoom,0,W-1)][constrain(mouseY/zoom,0,H-1)][2] = 255;
if (mousePressed = RIGHT){
fill(random(0,255));
}
}
void mouseDragged() {
u[constrain(mouseX/zoom,0,W-1)][constrain(mouseY/zoom,0,H-1)][2] = 100;
}
1