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