生成螺旋线形状的随机点

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

#define PI 3.1415926535

void generate_spiral();

int size_of_point;		//the total size of points
int range;			//the range of x and y of all points
int initial_phase;		//decide position of the first point
int cycles;			//the number of laps
char filename[200];

int main(int argc, char* argv[])
{
	if( argc != 6 )
	{
		printf("This program need 5 paremeter to specified by user"
				"\n\tthe first indicate the total size of point(eg. 100,300,500...)"
				"\n\tthe second indicate the range of x and y of all points(hits:10,20...)"
				"\n\tthe third indicate the initial_phase of spiral(hits:60,150,240...)"
				"\n\tthe forth indicate the number of cycles of spiral(hits:3,10,20...)"
				"\n\tthe last one indicate the output filename to save the value of point");
		exit(0);

	}
	size_of_point = atoi(argv[1]);
	range = atoi(argv[2]);
	initial_phase = atoi(argv[3]);
	cycles = atoi(argv[4]);
	strcat(filename, argv[5]);
	generate_spiral();
	return 0;
}

void generate_spiral()
{
	FILE* fwrite;
	if( NULL == (fwrite = fopen(filename, "w")))
	{
		printf("open file error");
		exit(0);
	}
	int t;
	double s;
	double x;
	double y;
	double angle;
	for( t = 1; t <= size_of_point; t++ )			
	{
		s = range * t/(double)size_of_point;			
		angle = initial_phase + t/(double)size_of_point * 360 * cycles;	
		x = s * cos(angle * PI / 180);
		y = s * sin(angle * PI / 180);
		fprintf(fwrite, "%f\t%f\n", x, y);
	}
	fclose(fwrite);
}

下图为使用改代码使用不同的参数形成的两条螺旋线


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值