not good but great

プログラミング、アート、映画・本の感想について書きます。

averageを使って重心の位置を算出


赤い点が重心となります。

void ofApp::draw(){
    ofSetColor(0, 200, 0);
    for(int i = 0;i < particles.size();i++){
        particles[i].draw();
    }
    
    ofSetColor(200, 0, 0);
    
    //重心の位置を算出
    centroid.average(points, numPoints);
    
    //重心に円を描画
    ofCircle(centroid.x, centroid.y, 10);
    
    //各パーティクルから重心に線を引く
    ofSetColor(0);

    for(int i = 0;i < particles.size();i++){
        ofLine(centroid.x, centroid.y, particles[i].location.x,particles[i].location.y);
    }
}

averageを使えば指定した配列に入ったベクトルの重心を算出してくれるようです。渡す配列はvector型では無理でした?constじゃないとダメということでしょうか。静的配列を使ったらできました。


・参考
http://openframeworks.cc/documentation/math/ofVec2f.html#show_average