您的位置:首页 > 其它

生成螺旋线形状的随机点

2016-06-12 12:26 337 查看
#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);
}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: