[TOC]

1.利用数组记录多个随机圆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>

int main() {
int width = 600;
int height = 600;
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
srand(time(0));

//定义三个数组存储所有圆的圆心坐标和半径
int x[100];
int y[100];
int r[100];
int rmin = 8;
int rmax = 50;
int circlenum = 100;

for (int i = 0; i < circlenum; i++) {
x[i] = rand() % width;
y[i] = rand() % height;
r[i] = rand() % (rmax - rmin + 1) + rmin;
}

//遍历画图
for (int i = 0; i < circlenum; i++) {
setlinecolor(RED);
setfillcolor(YELLOW);
fillcircle(x[i], y[i], r[i]);
}

_getch();
return 0;

}

微信截图_20240711124734

2.每次增加一个随机圆

每隔一百毫秒,添加一个随机圆并绘制。

int circlenum = 0;
float x, y, r;//新增圆的属性

while(circlenum<100){
x = rand() % width;
y = rand() % height;
r = rand() % (rmax - rmin + 1) + rmin;

xarray[circlenum] = x;
yarray[circlenum] = y;
rarray[circlenum] = r;
circlenum++;
​ `setlinecolor(RED);` ​ `setfillcolor(YELLOW);` ​ `fillcircle(x,y,r);`
Sleep(100);

}

微信截图_20240711135733

3.新圆不和已有圆相交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>
#include<math.h>

//求解两个点之间的距离
float Dist(float x1, float y1, float x2, float y2) {
float result;
result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}

//判断两个圆是否相交
int xiangjiao(float x1, float y1, float r1,float x2, float y2,float r2) {
if (Dist(x1, y1, x2, y2) < r1 + r2) return 1;
return 0;
}

//生成随机整数
int randbetween(int min, int max) {
int r = rand() % (max - min + 1) + min;
return 1;
}

int main() {
int width = 600;
int height = 600;
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
srand(time(0));

int xarray[100];
int yarray[100];
int rarray[100];
int rmin = 8;
int rmax = 50;
int circlenum = 0;
float x, y, r;//新增圆的属性
int isnewcircleOK;//用于判断新生成的圆是否满足条件
int i = 0;

while(circlenum<100){
isnewcircleOK = 0;
while (isnewcircleOK == 0) {
x = rand() % width;
y = rand() % height;
r = randbetween(rmin, rmax);

for (i = 0; i < circlenum; i++)
if (xiangjiao(xarray[i], yarray[i], rarray[i], x, y, r)) break;
if (i == circlenum) isnewcircleOK = 1;
}
xarray[circlenum] = x;
yarray[circlenum] = y;
rarray[circlenum] = r;
circlenum++;

setlinecolor(RED);
setfillcolor(YELLOW);
fillcircle(x,y,r);

Sleep(10);
}

_getch();
closegraph();
return 0;

}

微信截图_20240711142536

4.新圆半径最大化

while(circlenum<100){
isnewcircleOK = 0;
while (isnewcircleOK == 0) {
x = rand() % width;
y = rand() % height;
r = rmin;//新圆的半径开始设置为最小半径

for (i = 0; i < circlenum; i++)
if (xiangjiao(xarray[i], yarray[i], rarray[i], x, y, r)) break;
if (i == circlenum) isnewcircleOK = 1;
}

isnewcircleOK = 0;//继续设置为不ok,接下来要让这个新圆的半径最大
while (isnewcircleOK == 0 && r < rmax) {
r++;
for (int j = 0; j < circlenum; j++) {
if (xiangjiao(xarray[i], yarray[i], rarray[i], x, y, r)) {
isnewcircleOK = 1;
break;
}
}
}

微信截图_20240711143335