循环生成四组数

来源:百度知道 编辑:UC知道 时间:2024/06/05 09:06:02
用随机颜色画一组(x1,y1)-(x2,y2)为随机坐标,长度为100的线段。。
咋画呢?

我不知道这算不算随机,因为我只是产生一组随机坐标,再产生一个随机的角度,看一下这一随机角度能不能产生一个另一个点(x2,y2)也在区域内。

#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <Graphics.h>
main()
{
int i,j,color,flag=1;
int maxx,maxy;
int gdriver=DETECT,gmode,errorcode;
double x1,y1,x2,y2,len=100,theta;

randomize();
initgraph(&gdriver,&gmode,"");
if ((errorcode=graphresult())!=grOk)
{
printf("Graph error:%s\nPress any key to exit:",grapherrormsg(errorcode));
getch();
return 1;
}
maxx=getmaxx();
maxy=getmaxy();
color=rand()%MAXCOLORS;
while(flag)
{
x1=rand()%maxx;
y1=rand()%maxy;
theta=rand()%360;
x2=x1+len*cos(theta*M_PI/180);
y2=y1+len*sin(theta*M_PI/180);
if (x2>0&&x2<maxx&&y2>0&&y2<maxy)