2016-04-06 14 views
0

これはおそらく簡単なことですが、私は困惑しています。 これははるかに大きな部分の一部です。したがって、コードは単なるスニペットです。 Particleの周囲に配置された緑色の円のそれぞれはGateです。マウスがParticleの周りを移動するとき、最も近いGateは、もはや空いていないことを示すために赤色に変わるはずです。最も近いGateは、GatePVector headingPVector(mouseX,mouseY)とコサインルールを用いて比較することによって見出される。PVectorをコサインルールと比較する方法

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 is it 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; 

    for (int i = 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; 

    for (int i = 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() 
    { 
    for (Gate g : gates) 
    { 
     g.free(); 
    } 
    } 

    void update() { 
    for (Gate g : gates) 
    { 
     g.update(); 
    } 
    } 

    void display() { 
    for (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)); 
} 

答えて

0

あなたは、このコードが何をしているのか、またはっきりとはどのように違うのかを教えてくれません。だから、デバッグするのは難しいです。なぜなら、あなたはそれについて間違っているとは思わないからです。

しかし、あなただけの代わりにdist()機能を使って、あなたのアプローチを簡素化することができるかもしれません:あなたはまだそれが働いて得ることができない場合は

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 one is closer 
    fill(255, 0, 0); 
    ellipse(circleOneX, circleOneY, circleOneRadius, circleOneRadius); 
    fill(0, 0, 255); 
    ellipse(circleTwoX, circleTwoY, circleTwoRadius, circleTwoRadius); 
    } else { 
    //circle two is closer 
    fill(0, 0, 255); 
    ellipse(circleOneX, circleOneY, circleOneRadius, circleOneRadius); 
    fill(255, 0, 0); 
    ellipse(circleTwoX, circleTwoY, circleTwoRadius, circleTwoRadius); 
    } 
} 

、代わりにあなたの全体のスケッチのMCVEを投稿して、試してみてください具体的に何が機能していないのかを正確に特定するできるだけ少ない行に絞り込み、コードのどの行が期待通りのものかを正確に把握してください。

関連する問題