I dont know why it keep saying NullPointerException



PVector A, B, C, D;
float s = 100;
int binary = 0;
Boom c;


void setup() {
  size(640, 640);
  background(255);
  drawrect();
  c = new Boom(A,B,C,D);
}


void draw() {
  c.testmousepos();
  c. getEpos () ;
  
  if (mousePressed) {
    //background(255);
  }
}


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 (A.x+random(s*2), A.y+random(s*2));
    D =   new PVector (C.x-random(s*2), A.y+random(s*2));
  } else {
    C =   new PVector (A.x+random(s*2), B.y+random(s*2));
    D =   new PVector (C.x-random(s*2), B.y+random(s*2));
    //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);
}


class Boom {

  PVector La1, La2, Lb1, Lb2;
  boolean mouseinside ;
  PVector E = new PVector(mouseX, mouseY);
  PVector E2 =E;
  PVector E3 =E;
  PVector E4 =E;

  Boom(PVector tempLa1, PVector tempLa2, PVector tempLb1, PVector tempLb2 ) {
    tempLa1=La1;
    tempLa2=La2;
    tempLb1=Lb1;
    tempLb2=Lb2;
  }

  void testmousepos() {
    testintersect( La1, La2, E);
    testintersect( La2, Lb1, E);
    testintersect( Lb1, Lb2, E);
    testintersect( Lb2, La1, E);

    if (binary == 2 || binary == 4 ) {
      mouseinside = true;
    } else {
      mouseinside = false;
    }
  }

  void getEpos () {
    if (mousePressed&&mouseinside) {
      beginShape(TRIANGLE);
      vertex(La1.x, La1.y);
      vertex(La2.x, La2.y);
      vertex(E.x, E.y);

      endShape(CLOSE);
    }
  }


  void testintersect(PVector L1, PVector L2, PVector pos) {


    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 = 0;
    float y4 = 0;

    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) {
      binary +=1;
    }

  }
}

and why it shows me the value of the parameter temLa1 is not used


////

edited

I was wrong

You want La1=tempLa1;

what you want to define is left of the =

Next: You use L1; do you mean La1?

my version



PVector A, B, C, D;
float s = 100;
int binary = 0;

Boom c;

// -----------------------------------------------------------------------------------------------

void setup() {
  size(640, 640);
  background(255);
  drawrect();
  c = new Boom(A, B, C, D);
}

void draw() {
  c.testmousepos();
  c.getEpos();

  if (mousePressed) {
    //background(255);
    //getEpos() ;
  }
}

// -----------------------------------------------------------------------------------------------

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 (A.x+random(s*2), A.y+random(s*2));
    D =   new PVector (C.x-random(s*2), A.y+random(s*2));
  } else {
    C =   new PVector (A.x+random(s*2), B.y+random(s*2));
    D =   new PVector (C.x-random(s*2), B.y+random(s*2));
    //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);
}

//============================================================================================

class Boom {

  PVector La1, La2, Lb1, Lb2;
  boolean mouseinside = false;
  PVector E = new PVector(mouseX, mouseY); // won't work here
  PVector E2 = E;
  PVector E3 = E;
  PVector E4 = E;

  Boom(PVector tempLa1, PVector tempLa2, PVector tempLb1, PVector tempLb2 ) {
    La1=tempLa1.copy();
    La2=tempLa2.copy();
    Lb1=tempLb1.copy();
    Lb2=tempLb2.copy();

    //tempLa2=La2;
    //tempLb1=Lb1;
    //tempLb2=Lb2;
  }

  void testmousepos() {

    E = new PVector(mouseX, mouseY);

    testintersect( La1, La2, E);
    testintersect( La2, Lb1, E);
    testintersect( Lb1, Lb2, E);
    testintersect( Lb2, La1, E);

    if (binary == 2 || binary == 4 ) {
      mouseinside = true;
    } else {
      // mouseinside = false; // check this ?????????????????
    }
  }

  void getEpos() {
    if (mousePressed&&mouseinside) {
      println("Here 1"); 
      beginShape(TRIANGLE);
      vertex(La1.x, La1.y);
      vertex(La2.x, La2.y);
      vertex(E.x, E.y);
      endShape(CLOSE);
    }
  }

  void testintersect(PVector L1, PVector L2, PVector pos) {
    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 = 0;
    float y4 = 0;

    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) {
      binary +=1;
    }
  }
}//class
//

Hello,

Some resources that may help now or in future:

:)

i just want to make a function to check if two lines itersect . it dosent have to be La1 .

1 Like

thanks for give me the link . i will read it seriously. :blush:\