|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请!大神帮忙,小弟的需求是,随机产生圆(x,y,r),横纵坐标和半径,让每个圆相互不相交。 谢谢啦!急急急!
#include <stdio.h>
#include <stdlib.h> //生成随机数用
#include <time.h> //利用时间生成种子
#include <math.h>
struct Parameter
{
double x;
double y;
double r;
struct Parameter *next;
};
Parameter *head,*p,*p1;
#define LEN sizeof(Parameter)
int n=1;
int Cross(double ,double ,double );
int Distance(double ,double ,double);
void main()
{
double a[3],sum=0.00;
int num,i;
FILE *fp;
printf("input the num:");
scanf("%d",&num);
if(!(fp=fopen("data.txt","w+")))
{
printf("error!\n");
exit(0);
}
srand(time(NULL)); //生成种子
for(;n<=num;n++)
{
for(i=0;i<3;i++)
{
a[i]=(rand()%100)/100.00;
}
if(a[2]>a[0]/2.00||a[2]>a[1]/2.00)
continue;
if(!Distance(a[0],a[1],a[2]))
{
--n;
continue;
}
printf("%0.2f,%0.2f,%0.2f\n",a[0],a[1],a[2]);
fprintf(fp,"\t<geom_object>\n");
fprintf(fp,"\t\t<cylinder label = ”gp%d“>\n",n);
fprintf(fp,"\t\t\t<bottom>[%0.2f,%0.2f,.0]</bottom>\n",a[0],a[1]);
fprintf(fp,"\t\t\t<top>[%0.2f,%0.2f,.05]</top>\n",a[0],a[1]);
fprintf(fp,"\t\t\t<radius> %0.2f </radius>\n",a[2]);
fprintf(fp,"\t\t</cylinder>\n");
fprintf(fp,"\t\t<res>[2,2,2]</res>\n");
fprintf(fp,"\t\t<velocity>[0.1,-0.1,0.0]</velocity>\n");
fprintf(fp,"\t\t<temperature>12</temperature>\n");
fprintf(fp,"\t</geom_object>\n");
}
printf("\nnum = %d\n",--n);
fclose(fp);
}
int Distance(double x,double y,double r)
{
p=(Parameter *)malloc(sizeof(LEN));
if(1==n)
{
head=p;
p->x=x;
p->y=y;
p->r=r;
}
else
{
if(!Cross(x,y,r))
{
free(p);
return 0;
}
p1->next=p;
p->x=x;
p->y=y;
p->r=r;
}
p1=p;
p1->next=NULL;
return 1;
}
int Cross(double x,double y,double r)
{
int i;
Parameter *p2;
for(p2=head,i=1;i<n;i++,p2=p2->next)
{
if((x - p2->x)*(x - p2->x)+(y - p2->y)*(y - p2->y) < (r + p2->r)*(r + p2->r))
return 0;
}
return 1;
}
|
|