#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;
}
如何在圆内随机点坐标
最新推荐文章于 2022-06-05 23:26:03 发布