# I am stuck with one small invisible error

**URL:** <https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170>\
**Category:** Coding Questions\
**Created:** [May 19, 2021, 11:48am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170 "2021-05-19T11:48:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [May 19, 2021, 11:48am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/1 "2021-05-19T11:48:36Z")

</div>

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 .

```auto
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);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [May 19, 2021, 9:40pm UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/2 "2021-05-19T21:40:51Z")

</div>

hi,  
you send pos to poschange function:  
`PVector poschange(PVector pos,PVector speed) {..}`  
then , next line ,you create a new pvector pos ( pos = new PVector(); so pos is set to x=0,y=0)  
it s a little bit strange no?

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [May 20, 2021, 4:46am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/3 "2021-05-20T04:46:40Z")

</div>

but if i delete it . it will gives me a nullpoinderexception.is there any way to both initialise it and not to set it to (0,0) .

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 20, 2021, 9:59am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/4 "2021-05-20T09:59:44Z")

</div>

The problem is caused because you are not creating the PVectors for fields E1, E2, E3 and E4 change the class Boom so you have

```auto
  PVector E1 = new PVector();// won't work here
  PVector E2 = new PVector();
  PVector E3 = new PVector();
  PVector E4 = new PVector();

```

You also have to delete the line you added i.e.  
` pos = new PVector();`

---

<div class="post-metadata">

**Author:** ![SanjaySinghRajpoot](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SanjaySinghRajpoot](https://discourse.processing.org/u/SanjaySinghRajpoot)\
**Post date:** [May 20, 2021, 10:34am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/5 "2021-05-20T10:34:50Z")

</div>

Yes I had the same issue too

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [May 20, 2021, 11:39am UTC](https://discourse.processing.org/t/i-am-stuck-with-one-small-invisible-error/30170/6 "2021-05-20T11:39:32Z")

</div>

oh my god ,thank a lot . it moved!!!, 10000\*thank
