如何在圓內隨機點座標
阿新 • • 發佈:2019-02-10
#include <stdio.h> #include <math.h> #include <vector> int main() { //思路就是圍繞這個座標為圓心,產生不同半徑的圓,然後在取這個圓上,不同角度或方向的點即可 typedef struct tagPoint { float x; float y; } Point; //圓心座標,根據自己需求修改 const Point CycPoint = { .0f, .0f }; //產生圓心座標向外的隨機點半徑範圍50 - 100單位,根據自己需求修改 const int RadiusArea[] = { 50, 100 }; //當前半徑 float CurrentR = .0f; //當前角度或方向 float CurrentAngle = .0f; //隨機點個數 const int count = 20; //儲存隨機點 std::vector< Point > randomPoint; //圓周率 const float pi = 3.1415926f; Point tmp; for( int i = 0; i < count; ++i ) { //隨機半徑50 - 100範圍內 CurrentR = RadiusArea[ 0 ] + ( rand() % ( RadiusArea[ 1 ] - RadiusArea[ 0 ] ) ) + 1; //隨機方向 0 - 360度 CurrentAngle = rand() % 361; tmp.x = sin( CurrentAngle * pi / 180 ) * CurrentR; tmp.y = cos( CurrentAngle * pi / 180) * CurrentR; randomPoint.push_back( tmp ); } for( int i = 0; i < randomPoint.size(); ++i ) { const Point& p = randomPoint[ i ]; printf( "x:%f y%f\n", CycPoint.x + p.x, CycPoint.y + p.y ); } return 0; }