what i want to do is that when i click and mouse inside the quadrilateral,it will be divided to four pieces. there will be four mouse position for each piece .the four mouse position will move to four different position.
why Epos doesnt move, i run function alone on other sketch ,it worked but when i combine with all stuffs it doesntwork. pvector pos = max random(speed). plz tell me why.
its my first long code and i decide to finish it no matter how .
PVector A, B, C, D;
float s = 200;
int binary = 0;
int d;
PVector speedE1 = new PVector (random(50, 10), random(50, 10));
PVector speedE2 = new PVector (random(50, 10), random(50, 10));
PVector speedE3 = new PVector (random(50, 10), random(50, 10));
PVector speedE4 = new PVector (random(500, 100), random(500, 100));
Boom c;
void setup() {
size(900, 900);
background(255);
drawrect();
c = new Boom(A, B, C, D);
}
void draw() {
if (mousePressed) {
c.testmousepos();
}
c.changepos();
c.drawpices();
}
void drawrect() {
A = new PVector (random(width/2-s, width/2+s), random(height/2-s, height/2+s));
B = new PVector (A.x+random(s*2), random(height/2-s, height/2+s));
if (A.y>B.y) {
C = new PVector (random(width/2-s, width/2+s), A.y+random(100, s));
D = new PVector (C.x-random(100, s), A.y+random(100, s));
} else {
C = new PVector (random(width/2-s, width/2+s), B.y+random(100, s));
D = new PVector (C.x-random(100, s), B.y+random(100, s));
}
testintersect(A, D, B, C);
if (d == 1) {
drawrect() ;
}
//println(A, B, C, D);
beginShape();
vertex(A.x, A.y);
vertex(B.x, B.y);
vertex(C.x, C.y);
vertex(D.x, D.y);
endShape(CLOSE);
}
PVector poschange(PVector pos,PVector speed) {
//PVector pos_;
pos = new PVector();
if (speed.x >= -0.1 && speed.x <= 0.1 ) {
return pos;
}
if (speed.y >= -0.1 && speed.y <= 0.1 ) {
return pos;
}
if (pos.x < 0 || pos.x > width) {
speed.x *= -0.99;
}
if (pos.y < 0 || pos.y > height) {
speed.y *= -0.99;
}
pos.add(speed);
println(pos);
return pos;
}
void testintersect(PVector L1, PVector L2, PVector pos, PVector pos2) {
d = 0;
float x1 = L1.x;
float y1 = L1.y;
float x2 = L2.x;
float y2 = L2.y;
float x3 = pos.x;
float y3 = pos.y;
float x4 = pos2.x;
float y4 = pos2.y;
float den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
if (den == 0) {
return;
}
float t = ((x1-x3) * (y3-y4) - (y1-y3) * (x3-x4)) / den;
float u = - ((x1-x2) * (y1-y3) - (y1-y2) * (x1-x3)) / den;
if (t > 0 && t < 1 && u > 0 ) {
d = 1;
//println(binary);
}
}
class Boom {
PVector La1, La2, Lb1, Lb2;
boolean mouseinside = false;
PVector pzero = new PVector(0, 0);
PVector E ;
PVector E1 ;// won't work here
PVector E2 ;
PVector E3 ;
PVector E4 ;
Boom(PVector tempLa1, PVector tempLa2, PVector tempLb1, PVector tempLb2 ) {
La1=tempLa1.copy();
La2=tempLa2.copy();
Lb1=tempLb1.copy();
Lb2=tempLb2.copy();
}
void testmousepos() {
E = new PVector(mouseX, mouseY);
testintersect( La1, La2, E, pzero);
binary+=d;
testintersect( La2, Lb1, E, pzero);
binary+=d;
testintersect( Lb1, Lb2, E, pzero);
binary+=d;
testintersect( Lb2, La1, E, pzero);
binary+=d;
if (binary == 1 || binary == 3) {
mouseinside = true;
} else {
mouseinside = false;
}
binary = 0;
}
void changepos() {
E1 = poschange(E1,speedE1);
E2 = poschange(E2,speedE2);
E3 = poschange(E3,speedE3);
E4 = poschange(E4,speedE4);
}
void drawpices() {
if (mouseinside) {
// if (mousePressed) {
// E1 = E;
// E2 = E1;
// E3 = E1;
// E4 = E1;
// }
//println("Here 1");
fill(255);
beginShape();
vertex(Lb2.x, Lb2.y);
vertex(La1.x, La1.y);
vertex(E1.x, E1.y);
endShape(CLOSE);
//rect(E2.x, E2.y,10,10);
//println(E2);
fill(255);
beginShape();
vertex(La1.x, La1.y);
vertex(La2.x, La2.y);
vertex(E2.x, E2.y);
endShape(CLOSE);
fill(255);
beginShape();
vertex(Lb1.x, Lb1.y);
vertex(La2.x, La2.y);
vertex(E3.x, E3.y);
endShape(CLOSE);
beginShape();
fill(255);
vertex(Lb1.x, Lb1.y);
vertex(Lb2.x, Lb2.y);
vertex(E4.x, E4.y);
endShape(CLOSE);
//rect(E2.x, E2.y,10,10);
//println(E);
}
}
}