如何创建一个递归方法来比较两个值?

时间:2012-07-19 16:04:30

标签: c++ algorithm recursion openframeworks

我在for循环中生成随机值,我想测试下一个即将生成的值是否接近任何其他值,如果是,我想将它定位在公差半径之外,这个就是我到目前为止:

int spacing = 30;
// create a bunch of random points
for ( int i = 0; i < MAX_NUMBER_OF_DOTS; i++ )
{
    ofVec2f point( ofRandom(spacing, ofGetWidth() - (spacing + spacing)), ofRandom( spacing, ofGetHeight() - (spacing + spacing)) );

    // Loop trough previously create points and check its distance
    for ( int j = 0; j < dots.size(); j++ )
    {
        ofVec2f testPoint = dots[j];
        float minDistance = 20.0f;
        // If the point is too close move it to a random point around it
        if ( point.distance( testPoint ) < minDistance )
        {
            point.x += cos( ofRandom( TWO_PI ) ) + minDistance;
            point.y += sin( ofRandom( TWO_PI ) ) + minDistance;
        }
    }
    dots.push_back( point );
}

dots vector<ofVec2f> dots; 的位置 不完美,因为新的计算点没有考虑到先前创建的点附近,所以我认为递归方法可以帮助我解决问题。

1 个答案:

答案 0 :(得分:0)

您可以使用geometric hashing算法检查新创建的点是否比“minDistance”更接近任何其他先前创建的点。 当应用于蛋白质构象时,可以找到描述该方法的好文章here