软件使用
EasyX、VS2013
效果图
具体实现
用代码实现将圆填充整个画面,并且圆要尽可能的大,然后按空格会出现不同类型的圆
源代码
#include <time.h>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
//计算两点间的距离
float Dist2Points(float x1, float y1, float x2, float y2);
//判断两个圆是否相交
bool isIntersect(float x1, float y1, float r1, float x2, float y2, float r2);
int randBetweenMinMax(int min, int max);
//一共有4种类型的圆
void drawCircles1(float x, float y, float r);
void drawCircles2(float x, float y, float r);
void drawCircles3(float x, float y, float r);
void drawCircles4(float x, float y, float r);
int main()
{
int width = 600, height = 600;
initgraph(width, height);
setbkcolor(WHITE);
cleardevice();
srand(time(0));
int xArray[1000], yArray[1000], rArray[1000]; //存放圆的横纵坐标和直径
int rmin = 8, rmax = 50, circleNum = 0; //随机生成圆的半径大小在rmin到rmax之间
int x, y, r;
int drawMode = 1;
while (circleNum < 1000)
{
if (_kbhit())
{
char input = _getch();
//按下空格,屏幕清空,以下一个形式显示圆
if (input == ' ')
{
circleNum = 0;
cleardevice();
drawMode = drawMode % 4 + 1;
}
}
x = rand() % width;
y = rand() % height;
r = randBetweenMinMax(rmin, rmax);
bool flag1 = false;
for (int i = 0; i < circleNum; i++)
{
//判断当前的圆是否和其他圆相交
if (isIntersect(x, y, r, xArray[i], yArray[i], rArray[i]))
{
flag1 = true;
break;
}
}
if (flag1)
continue; //如果相交就跳过下面的步骤,重新生成
while (r < rmax) //如果不相交,就让这个圆尽可能地大,大到不和别的圆相交为止
{
r++;
bool flag2 = false;
for (int i = 0; i < circleNum; i++)
{
if (isIntersect(x, y, r, xArray[i], yArray[i], rArray[i]))
{
flag2 = true;
break;
}
}
if (flag2)
break;
}
//记录这个圆的数据
xArray[circleNum] = x;
yArray[circleNum] = y;
rArray[circleNum] = r;
circleNum++;
if (drawMode == 1)
drawCircles1(x, y, r);
else if (drawMode == 2)
drawCircles2(x, y, r);
else if (drawMode == 3)
drawCircles3(x, y, r);
else
drawCircles4(x, y, r);
Sleep(10);
}
_getch();
closegraph();
return 0;
}
float Dist2Points(float x1, float y1, float x2, float y2)
{
float result;
result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}
bool isIntersect(float x1, float y1, float r1, float x2, float y2, float r2)
{
if (Dist2Points(x1, y1, x2, y2) < r1 + r2)
return true;
return false;
}
int randBetweenMinMax(int min, int max)
{
return rand() % (max - min + 1) + min;
}
void drawCircles1(float x, float y, float r)
{
setlinecolor(BLACK);
setfillcolor(RGB(255, 255, 0));
fillcircle(x, y, r);
}
void drawCircles2(float x, float y, float r)
{
float h = rand() % 360;
COLORREF color = HSVtoRGB(h, 0.6, 0.8);
setlinecolor(WHITE);
setfillcolor(color);
fillcircle(x, y, r);
}
void drawCircles3(float x, float y, float r)
{
while (r > 0)
{
float h = rand() % 360;
COLORREF color = HSVtoRGB(h, 0.6, 0.8);
setlinecolor(WHITE);
setfillcolor(color);
fillcircle(x, y, r);
r -= 5;
}
}
void drawCircles4(float x, float y, float r)
{
while (r > 0)
{
float h = rand() % 360;
COLORREF color = HSVtoRGB(h, 0.9, 0.8);
setlinecolor(color);
circle(x, y, r);
r -= 5;
}
}