如何在圆内随机点坐标

#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;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值