gpt4 book ai didi

c++ - 如何创建递归方法来比较两个值?

转载 作者:太空狗 更新时间:2023-10-29 23:18:56 26 4
gpt4 key购买 nike

我在 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; 并不完美,因为新计算的点没有考虑之前创建的点附近,所以我认为递归方法可以帮助我解决问题。

最佳答案

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

关于c++ - 如何创建递归方法来比较两个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11564834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com