java - How to compare PVectors with cosine rule -


this simple thing, stumped. part of larger thing, code snippet. each of green circles arranged around particle gate. when mouse moves around particle closest gate should turn red indicate no longer free. closest gate found comparing pvector heading of gate against pvector(mousex,mousey) using cosine rule.

particle p;  void setup() {   size(1000, 1000);   p = new particle(25);   p.setloc(width/2, height/2); }  void draw() {   background(0);   p.update();   pvector mouse = new pvector(mousex, mousey);   p.gm.getgate(mouse).notfree();   p.display();  }  class particle {    gatemanager gm;    pvector loc;    float size;   float n_size;   float r;//radius;   float topspeed;    boolean clicked;    color main = color(200, 200, 0);   color highlight = color(255, 0, 0);   color colour = color(200, 200, 0);    string name;    particle(int size_) {       loc = new pvector(0,0);     this.n_size = size_;     this.size = n_size*30000;     r = sqrt(size/pi);     topspeed = 2000/size;      gm = new gatemanager(this);    }    void setloc(float x, float y) {     loc.set(x, y);   }    void display()   {     gm.display();   }    gate getgate(pvector pv)   {     pvector pv_ = pvector.sub(pv, loc);     return gm.getgate(pv_);   }    void update() {     gm.update();   } }///particle     class gate {    float size = 5;   pvector loc;   pvector locp;   float angle;   boolean free;   //edge edge;   pvector heading; ///which way pointing?   gatemanager parent;   float x, y;    color colour;   color free = color(0, 255, 0);   color notfree = color(255, 0, 0);     gate(gatemanager gm, float angle_) {      this.parent = gm;     this.angle = angle_;     this.x = parent.parent.r * .5 * cos(angle);     this.y = parent.parent.r * .5 * sin(angle);     locp = new pvector(x, y);     loc = new pvector(x, y);     free = true;   }    boolean isfree() {     return free;   }    void notfree()   {     free = false;     colour = notfree;   }    void free()   {     colour = free;     free = true;   }    void update()   {     free();     heading = pvector.sub(loc, parent.parent.loc);     heading.normalize();     loc.set(parent.parent.loc.x +locp.x, parent.parent.loc.y+locp.y);   }    void display()   {     nostroke();     fill(colour);     ellipse(loc.x, loc.y, size, size);   } }  class gatemanager {    arraylist<gate> gates;   int capacity;   particle parent;   float angle;    gatemanager(particle p)   {     capacity = int(p.n_size*8);     this.parent = p;     gates = new arraylist<gate>();     angle = two_pi/capacity;      (int = 0; i<capacity; i++)     {       gates.add(new gate(this, angle*i));     }   }    gate getgate(pvector pv)   {      float tolerance = -1;     int i_val=-1;     boolean found = false;      (int = 0; i<capacity; i++)     {       if (gates.get(i).isfree()) {         float temp = cosinesimilarity(pv, gates.get(i).heading);         if ( (temp>tolerance))         {           tolerance = temp;           i_val = i;           found = true;         }       }     }       i_val = constrain(i_val, 0, capacity);      text(i_val, 100, height-100);     return gates.get(i_val);   }    void free()   {     (gate g : gates)     {       g.free();     }   }    void update() {       (gate g : gates)     {       g.update();     }   }    void display() {     (gate g : gates)     {       g.display();     }   } }//gatemanager  float cosinesimilarity(pvector va, pvector vb) {   float dotproduct = 0.0;   float norma = 0.0;   float normb = 0.0;    dotproduct += va.x * vb.x;   dotproduct += va.y * vb.y;   norma += math.pow(va.x, 2);   norma += math.pow(va.y, 2);   normb += math.pow(vb.x, 2);   normb += math.pow(vb.y, 2);    return dotproduct / (sqrt(norma) * sqrt(normb)); } 

you haven't told code does, or how differs expected. it's hard debug, since don't know think wrong it.

but might able simplify approach using dist() function instead:

void setup() {   size(500, 500);   ellipsemode(radius); }  void draw() {   background(0);    float circleonex = 100;   float circleoney = 200;   float circleoneradius = 50;    float circletwox = 400;   float circletwoy = 300;   float circletworadius = 50;    float distancefromcircleone = dist(mousex, mousey, circleonex, circleoney);   float distancefromcircletwo = dist(mousex, mousey, circletwox, circletwoy);    if (distancefromcircleone < distancefromcircletwo) {     //circle 1 closer     fill(255, 0, 0);     ellipse(circleonex, circleoney, circleoneradius, circleoneradius);     fill(0, 0, 255);     ellipse(circletwox, circletwoy, circletworadius, circletworadius);   } else {     //circle 2 closer     fill(0, 0, 255);     ellipse(circleonex, circleoney, circleoneradius, circleoneradius);     fill(255, 0, 0);     ellipse(circletwox, circletwoy, circletworadius, circletworadius);   } } 

if still can't working, please post mcve instead of whole sketch, , try more specific what's not working. try narrow down few lines possible, , try figure out line of code differs expect do.


Comments