格雅百科

Easyx在vs2010中教程_基于easyX和vs使用c++绘制坐标系并实时显示

格雅百科

https://www.gyballet.com/how-to-vc2010-win https://www.gyballet.com/zh-cn/简介 https://www.gyballet.com/shujiaw/article/details/82969394 官网:https://www.gyballet.com/

使用教程

源码如下:

#include

int main()

{

int gdriver, gmode, i, j; /*清屏*/

gdriver = 检测;

initgraph(&gdriver, &gmode, ""); /*初始化图形界面*/

for (i = 120; i <= 400; i = i + 40) /*设置起点为120,终点为400,表格宽度为40*/

对于 (j = 120; j <= 400; j++)

{

putpixel(i, j, 黄色); /*绘制点*/

putpixel(j, i, 黄色);

}

系统(“暂停”);

closegraph(); /*退出图形界面*/

返回0;

}

效果如图L:画圆:源码如下:

#include // 参考图形库头文件

#包括

int main()

{

initgraph(640, 480); // 创建一个大小为 640x480 像素的绘图窗口

圆(200, 200, 100); // 画一个圆,圆心 (200, 200),半径 100

_getch(); // 按任意键继续

closegraph(); // 关闭绘图窗口

返回0;

}

效果如下图: 坐标 在EasyX中,坐标有两种:物理坐标和逻辑坐标。

物理坐标 物理坐标是描述设备的坐标系。

坐标原点位于设备左上角,X轴向右为正,Y轴向下为正,测量单位为像素。

坐标原点、坐标轴方向、缩放比例不可更改。

逻辑坐标 逻辑坐标是在程序中绘图时使用的坐标系。

默认坐标原点为窗口左上角,X轴向右为正,Y轴向下为正,测量单位为点。

默认情况下,逻辑坐标和物理坐标一一对应,1个逻辑点等于1个物理像素。

本手册中未注明的坐标均指逻辑坐标。

请参阅

setorigin:修改坐标原点。 setaspectratio:修改坐标轴方向。 setaspectratio:修改缩放比例。

下面是如何表达图像:具体参见参考资料。使用预定义的常量来表示颜色

其中,离线下载链接:

由于现在有些算法是在Windows下用C语言编写的,C语言没有自己的图形界面库,所以在调试时非常不方便。相信很多使用C语言开发的朋友都会被这个困扰。所以最近我基于easyX写了一个实时绘制数据的程序。我希望它能给你一些帮助。

easyX是一个只能在windows上使用的c++图形库,一般只能在vs或vc上使用(我用的是vs)。有很多限制。如果你想用,就老老实实安装吧。软件方面,先安装vs,再安装easyX。 easyX 易于安装。

先给大家看看效果:

静态显示(无网格):

静态显示(带网格,网格线覆盖刻度值的负号,万岁):

动态绘图(无网格):

动态绘图(带网格):

动态绘制时,使用循环队列,然后每次都会重绘队列中的所有元素

接下来是代码:

首先是循环队列的代码:

queue.h

#ifndef _QUEUE_H

#define _QUEUE_H

//循环队列的大小

#define MAXSIZE 1000

typedef 结构队列{

int 数据[MAXSIZE];

int 前、后;

}QueueInt;

typedef 结构queueintfloat {

浮点数据[MAXSIZE];

int 前、后;

}QueueFloat;

void queueInitFloat(QueueFloat* 队列);

int insertFloat(QueueFloat* 队列,float a);

int deleteFloat(QueueFloat* 队列);

int getSizeFloat(QueueFloat* 队列);

void queueInitInt(QueueInt* 队列);

int insertInt(QueueInt* 队列, int a);

int deleteInt(QueueInt* 队列);

int getSizeInt(QueueInt* 队列);

#endif

队列.cpp

#include "queue.h"

void queueInitFloat(QueueFloat* 队列) {

队列->前面 = 0;

队列->后方 = 0;

for (int i = 0; i < MAXSIZE; i++)

队列->数据[i] = 0;

}

int insertFloat(QueueFloat* 队列, float a) {

if ((队列->后 + 1)%MAXSIZE==队列->前) {

返回1;

}

队列->数据[队列->rear++] = a;

返回0;

}

int deleteFloat(QueueFloat* 队列) {

if (队列->前==队列->后) {

返回1;

}

队列->front++;

返回0;

}

int getSizeFloat(QueueFloat* 队列) {

int res = 队列->后 - 队列->前;

如果(分辨率 < 0){

res += MAXSIZE;

}

返回分辨率;

}

void queueInitInt(QueueInt* 队列) {

队列->前面 = 0;

队列->后方 = 0;

for (int i = 0; i < MAXSIZE; i++)

队列->数据[i] = 0;

}

int insertInt(QueueInt* 队列, int a) {

if ((队列->后部 + 1) % MAXSIZE == 队列->前部) {

返回1;

}

队列->数据[队列->rear++] = a;

队列->后方%= MAXSIZE;

返回0;

}

int deleteInt(QueueInt* 队列) {

if (队列->前==队列->后) {

返回1;

}

队列->front++;

队列->前面%= MAXSIZE;

返回0;

}

int getSizeInt(QueueInt* 队列) {

int res = 队列->后 - 队列->前;

如果(分辨率 < 0){

res += MAXSIZE;

}

返回分辨率;

}

接下来是坐标系的绘制和绘图代码

绘制坐标系统.h

#pragma Once

#ifndef 绘制坐标系统

#定义绘图坐标系统

#include "queue.h"

/*

这是绘制坐标系的函数

1,轴

2。规模

3。起源

4。网格

5。相框

6。箭头

*/

//轴结构

结构轴{

int xlow, xhigh;

int ylow, yhigh;

int centerx,centery;

浮斧,ay; //压缩

int 颜色;

};

//刻度结构

结构规模{

int openflag; //是否开启

int 刻度; //缩放尺寸

int 鳞片;

int 颜色;

};

结构网格{

int openflag;

int 颜色;

};

typedef 结构坐标系 {

struct Axis轴;

struct Scale 刻度;

int Zerox, Zeroy; //坐标系原点

struct Grid 网格;

int 长度、宽度; //图片的长和宽

int 颜色; //背景颜色

int borderx,刺绣; //边界空出范围

}坐标系;

void坐标系统初始化(坐标系统* cs);

void createCS(坐标系* cs);

voidplotInt(坐标系统* cs, int* data, int len, int color);

voidplotFloat(坐标系统* cs, float* data, int len, int color);

void dynamicPlotInt(CooperativeSystem* cs,QueueInt* 队列,int color);

void addInt(QueueInt* 队列,int a);

#endif // !DrawCooperativeSystem

绘制坐标系统.cpp

#include "drawCooperativeSystem.h"

#include // 参考图形库头文件

#包括

#包括

#包括

#include "queue.h"

void坐标系统初始化(坐标系统* cs){

//帧初始化

cs->长度 = 1280;

CS->宽度 = 780;

//坐标轴初始化

cs->axis.xhigh = 100;

cs->axis.xlow = -100;

cs->axis.yhigh = 100;

cs->axis.ylow = 0;

cs->www.gyballet.comx = (cs->axis.xhigh + cs->axis.xlow) / 2;

cs->www.gyballet.comy = (cs->axis.yhigh + cs->axis.ylow) / 2;

int len1 = cs->axis.xhigh - cs->axis.xlow;

int len2 = cs->axis.yhigh - cs->axis.ylow;

cs->www.gyballet.com = cs->长度*1.0 / len1;

cs->axis.ay = cs->宽度*1.0 / len2;

cs->axis.color = 黑色;

//坐标原点初始化

cs->zerox = 0;

cs->零 = 50;

//刻度初始化

cs->scale.openflag = 1;

cs->scale.scalex = 10;

cs->scale.scaley = 10;

cs->scale.color = 黑色;

//网格初始化

cs->grid.openflag = 1;

cs->grid.color = 黑色;

//边界空范围

cs->borderx = 10;

cs->刺绣 = 10;

cs->颜色 = 白色;

//创建窗口

HWND hwnd = initgraph(cs->长度 + cs->borderx, cs->宽度 + cs->刺绣, EW_SHOWCONSOLE);

settrop2(R2_MASKPEN); //添加光栅看起来更好

}

int transX(坐标系* cs, float x) {

x -= cs->www.gyballet.comx;

x *= cs->www.gyballet.com;

int k = x;

如果 (x - k > 0.5)

k++;

返回k;

}

int transY(坐标系* cs, float y) {

y -= cs->www.gyballet.comy;

y *= cs->axis.ay;

int k = y;

如果 (y - k > 0.5)

k++;

返回k;

}

void createCS(坐标系* cs) {

//设置原点

int avex = (cs->length + cs->borderx) / 2;

int avey = (cs->宽度 + cs->刺绣) / 2;

setorigin(avex, avey);

//设置缩放比例和轴方向

设置纵横比(1,-1); //翻转y轴,向上为正

//设置背景颜色

setbkcolor(cs->颜色);

cleardevice();

//绘制坐标轴

setlinecolor(cs->axis.color); //设置颜色

setlinestyle(PS_SOLID,3); //线条样式:实线,宽度

线(-cs->长度/2,0,cs->长度/2,0);

线(transX(cs,cs->zerox),cs->width/2, transX(cs,cs->zerox),-cs->width/2);

//画箭头

浮动角=15/180.0*3.14159; //箭头角度

浮动len3=20;

浮点x11 = cs->zerox*1.0;

float x12 = cs->zerox*1.0 + len3 * tan(角度)/cs->www.gyballet.com;

float x13 = cs->zerox*1.0 - len3 * tan(角度) / cs->www.gyballet.com;

float y11 = cs->axis.yhigh*1.0;

float y12 = cs->axis.yhigh*1.0 - len3 / cs->axis.ay;

float y13 = cs->axis.yhigh*1.0 - len3/cs->axis.ay;

线(transX(cs,x11),cs->宽度/2,transX(cs, x12 ),cs->宽度/2-len3);

线(transX(cs, x11), cs->宽度 / 2, transX(cs, x13), cs->宽度 / 2 - len3);

float x21 = cs->axis.xhigh;

float x22 = cs->axis.xhigh - len3 / cs->www.gyballet.com;

float x23 = cs->axis.xhigh - len3 / cs->www.gyballet.com;

float y21 = cs->zeroy;

float y22 = cs->zeroy+len3* tan(角度) / cs->axis.ay;

float y23 = cs->zeroy - len3 * tan(角度) / cs->axis.ay;

行(transX(cs, x21), transY(cs, y21), transX(cs, x22), transY(cs, y22));

行(transX(cs, x21), transY(cs, y21), transX(cs, x23), transY(cs, y23));

// 绘制比例

if (cs->scale.openflag) {

int len4 = 5; //标尺的高度

float rlen = 50 / cs->www.gyballet.com, rwid = 30 / cs->axis.ay; //缩放值长宽

int 偏差 = 2; //刻度值与坐标轴

的距离

字符[10];

int i = 0;

设置纵横比(1, 1); //将y轴向上翻转为负值

settextcolor(cs->scale.color); //设置文字颜色

setbkmode(透明); //背景透明

while (cs->zerox - i * cs->scale.scalex > cs->axis.xlow) {

setlinecolor(cs->axis.color); //设置刻度颜色

setlinestyle(PS_SOLID, 3); //线条样式:实线,宽度

line(transX(cs, cs->zerox - i * cs->scale.scalex), transY(cs, cs->zeroy), transX(cs, cs->zerox - i * cs->scale. scalex), transY(cs, cs->zeroy) - len4);

RECT r = { transX(cs, cs->zerox - i * cs->scale.scalex - rlen / 2), transY(cs, cs->zerox) + 偏差,transX(cs, cs->zerox - i * cs->scale.scalex + rlen / 2),transY(cs, cs->zeroy + rwid) + 偏差 };

sprintf(s, "%d", cs->zerox - i * cs->scale.scalex);

// printf("%s\n", s);

绘制文本(s, &r, DT_CENTER | DT_TOP | DT_SINGLELINE);

//绘制网格

if (cs->grid.openflag&&i!=0) {

setlinecolor(cs->grid.color); //设置网格颜色

setlinestyle(PS_DASH, 1); //线条样式:虚线,宽度

line(transX(cs, cs->zerox - i * cs->scale.scalex), transY(cs, cs->axis.ylow), transX(cs, cs->zerox - i * cs-> scale.scalex), transY(cs, cs->axis.yhigh));

}

i++;

}

i = 1;

while (cs->zerox + i * cs->scale.scalex < cs->axis.xhigh) {

setlinecolor(cs->axis.color); //设置刻度颜色

setlinestyle(PS_SOLID, 3); //线条样式:实线,宽度

line(transX(cs, cs->zerox + i * cs->scale.scalex), transY(cs, cs->zeroy), transX(cs, cs->zerox + i * cs->scale. scalex), transY(cs, cs->zeroy) - len4);

RECT r = { transX(cs, cs->zerox + i * cs->scale.scalex - rlen / 2), transY(cs, cs->zerox) + 偏差,transX(cs, cs->zerox + i * cs->scale.scalex + rlen / 2),transY(cs, cs->zeroy + rwid) + 偏差 };

sprintf(s, "%d", cs->zerox + i * cs->scale.scalex);

// printf("%s\n", s);

绘制文本(s, &r, DT_CENTER | DT_TOP | DT_SINGLELINE);

//绘制网格

if (cs->grid.openflag&&i != 0) {

setlinecolor(cs->grid.color); //设置网格颜色

setlinestyle(PS_DASH, 1); //线条样式:虚线,宽度

line(transX(cs, cs->zerox + i * cs->scale.scalex), transY(cs, cs->axis.ylow), transX(cs, cs->zerox + i * cs-> scale.scalex), transY(cs, cs->axis.yhigh));

}

i++;

}

i = 0;

while (cs->zeroy - i * cs->scale.scaley > cs->axis.ylow) {

setlinecolor(cs->axis.color); //设置刻度颜色

setlinestyle(PS_SOLID, 3); //线条样式:实线,宽度

行(transX(cs, cs->zerox), transY(cs, cs->zeroy + i * cs->scale.scaley), transX(cs, cs->zerox) + len4, transY(cs, cs->zeroy + i * cs->scale.scaley));

RECT r = { transX(cs, cs->zerox - rlen) - 偏差, transY(cs,cs->zeroy + i * cs->scale.scaley - rwid / 2),transX(cs, cs- >zerox) - 偏差,transY(cs,cs->zeroy + i * cs->scale.scaley + rwid / 2) };

sprintf(s, "%d", cs->zeroy - i * cs->scale.scaley);

// printf("%s\n", s);

绘制文本(s, &r, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);

//绘制网格

if (cs->grid.openflag&&i != 0) {

setlinecolor(cs->grid.color); //设置网格颜色

setlinestyle(PS_DASH, 1); //线条样式:虚线,宽度

line(transX(cs, cs->axis.xlow), transY(cs, cs->zeroy + i * cs->scale.scaley), transX(cs, cs->axis.xhigh), transY( cs, cs->zeroy + i * cs->scale.scaley));

}

i++;

}

i = 1;

while (cs->zeroy + i * cs->scale.scaley < cs->axis.yhigh) {

setlinecolor(cs->axis.color); //设置刻度颜色

setlinestyle(PS_SOLID, 3); //线条样式:实线,宽度

line(transX(cs, cs->zerox), transY(cs, cs->zeroy - i * cs->scale.scaley), transX(cs, cs->zerox) + len4, transY(cs, cs->zeroy - i * cs->scale.scaley));

RECT r = { transX(cs, cs->zerox - rlen) - bias, transY(cs,cs->zeroy - i * cs->scale.scaley - rwid / 2),transX(cs, cs->zerox) - bias, transY(cs,cs->zeroy - i * cs->scale.scaley + rwid / 2) };

sprintf(s, "%d", cs->zeroy + i * cs->scale.scaley);

// printf("%s\n", s);

drawtext(s, &r, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);

//绘制网格

if (cs->grid.openflag&&i != 0) {

setlinecolor(cs->grid.color); //设置网格颜色

setlinestyle(PS_DASH, 1); //线的样式:虚线,宽度

line(transX(cs, cs->axis.xlow), transY(cs, cs->zeroy - i * cs->scale.scaley), transX(cs, cs->axis.xhigh), transY(cs, cs->zeroy - i * cs->scale.scaley));

}

i++;

}

}

}

void setRange(CoordinateSystem*cs, int xlow, int xhigh, int ylow, int yhigh) {

cs->axis.ylow = ylow;

cs->axis.yhigh = yhigh;

cs->axis.xlow = xlow;

cs->axis.xhigh = xhigh;

cs->www.gyballet.comx = (cs->axis.xlow + cs->axis.xhigh) / 2;

cs->www.gyballet.comy = (cs->axis.ylow + cs->axis.yhigh) / 2;

int len1 = cs->axis.xhigh - cs->axis.xlow;

int len2 = cs->axis.yhigh - cs->axis.ylow;

cs->www.gyballet.com = cs->length*1.0 / len1;

cs->axis.ay = cs->width*1.0 / len2;

cs->scale.scalex =(int) (100 / cs->www.gyballet.com)/10*10;

cs->scale.scaley = (int)(100 / cs->axis.ay) / 10*10;

if (cs->scale.scalex <= 0)

cs->scale.scalex=(int)(100 / cs->www.gyballet.com);

if (cs->scale.scaley <= 0)

cs->scale.scaley=(int)(100 / cs->axis.ay);

if (cs->scale.scalex <= 0)

cs->scale.scalex = 1;

if (cs->scale.scaley <= 0)

cs->scale.scaley = 1;

cs->zerox = 0;

cs->zeroy = cs->www.gyballet.comy;

}

//直接给一组数据,将数据绘制出来

void plotInt(CoordinateSystem* cs, int* data, int len, int color) {

//找出最大最小值

int min = data[0];

int max = data[0];

for (int i = 1; i < len; i++) {

if (data[i] > max) {

max = data[i];

}

if (data[i] < min) {

min = data[i];

}

}

int range = max - min;

coordinateSystemInit(cs);

setRange(cs, 0-len*0.1, len*1.1, min-range*0.1, max+range*0.1);

createCS(cs);

setlinecolor(color);

setaspectratio(1, -1);

setlinestyle(PS_SOLID, 2);

for (int i = 0; i < len - 1; i++) {

line(transX(cs,i + 1),transY(cs, data[i]),transX(cs, i + 2),transY(cs, data[i + 1]));

}

for (int i = 0; i < len; i++) {

putpixel(transX(cs,i + 1),transY(cs, data[i]),color);

}

}

//直接给一组数据,将数据绘制出来

void plotFloat(CoordinateSystem* cs, float* data, int len, int color) {

//找出最大最小值

float min = data[0];

float max = data[0];

for (int i = 1; i < len; i++) {

if (data[i] > max) {

max = data[i];

}

if (data[i] < min) {

min = data[i];

}

}

float range = max - min;

coordinateSystemInit(cs);

setRange(cs, 0 - len * 0.1, len*1.1, min - range * 0.1, max + range * 0.1);

createCS(cs);

setlinecolor(color);

setaspectratio(1, -1);

setlinestyle(PS_SOLID, 2);

for (int i = 0; i < len - 1; i++) {

line(transX(cs, i + 1), transY(cs, data[i]), transX(cs, i + 2), transY(cs, data[i + 1]));

}

for (int i = 0; i < len; i++) {

putpixel(transX(cs, i + 1), transY(cs, data[i]), color);

}

//

}

void dynamicPlotInt(CoordinateSystem* cs, QueueInt* queue, int color) {

int len = getSizeInt(queue);

int min, max;

if (len == 0) {

min = 0;

max = 100;

}else if (len==1) {

min = queue->data[queue->front] - 10;

max = queue->data[queue->front] + 10;

}else {

int p = (queue->front+1)%MAXSIZE;

min = queue->data[queue->front];

max = queue->data[queue->front];

while (p% MAXSIZE != queue->rear) {

if (queue->data[p ]>max) {

max = queue->data[p ];

}

if (queue->data[p ] < min) {

min = queue->data[p ];

}

p++;

p %= MAXSIZE;

}

}

if (min == max) {

min = min - 10;

max = max + 10;

}

float range = max - min;

// coordinateSystemInit(cs);

if (len <= 10) {

setRange(cs, -10, 10, min - range * 0.1, max + range * 0.1);

}

else {

setRange(cs, 0 - len * 0.1, len*1.1, min - range * 0.1, max + range * 0.1);

}

BeginBatchDraw();//开始批量画图

createCS(cs);

if (len == 0) {

EndBatchDraw();

return;

}

setlinecolor(color);

setaspectratio(1, -1);

setlinestyle(PS_SOLID, 2);

int i = 0;

int k = 0;

for (int p = queue->front; (p+1)%MAXSIZE != queue->rear;p++,p%=MAXSIZE,i++) {

line(transX(cs, i + 1), transY(cs,queue->data[p]), transX(cs, i + 2), transY(cs, queue->data[(p+1)%MAXSIZE]));

k++;

if (k == 10) {

// FlushBatchDraw();

k = 0;

}

}

EndBatchDraw();

// i = 0;

// for (int p = queue->front; p%MAXSIZE != queue->rear; p++, p %= MAXSIZE, i++) {

// putpixel(transX(cs, i + 1), transY(cs, queue->data[p]), color);

// }

// for (int i = 0; i < len; i++) {

// putpixel(transX(cs, i + 1), transY(cs, data[i]), color);

// }

}

void addInt(QueueInt* queue, int a) {

if (insertInt(queue, a)) {

deleteInt(queue);

insertInt(queue, a);

}

}

最后是绘制代码

绘制静态数据代码:

#include

#include

#include

#include // 引用图形库头文件

#include

#include "drawCoordinateSystem.h"

#include "queue.h"

int main() {

CoordinateSystem cs;

coordinateSystemInit(&cs);

float a[1001];

int j = 0;

for (float i = 0; i < 2 * 3.14; i += 3.14 / 500) {

a[j++] = 10 * sin(i);

}

plotFloat(&cs, a, 1000, RED);

getchar();

return 0;

}

绘制动态数据代码:

#include

#include

#include

#include

#include // 引用图形库头文件

#include

#include "drawCoordinateSystem.h"

#include "queue.h"

void fun(void* p) {

CoordinateSystem cs;

coordinateSystemInit(&cs);

while (1) {

dynamicPlotInt(&cs,&queue,RED);

Sleep(20);

}

_endthread();

}

int main(){

queueInitInt(&queue);

_beginthread(fun, 0, NULL);

float t = 0;

getchar();

while (1) {

if (int(t+1) % 500 == 0) {

addInt(&queue, 100 * sin(t / 100));

}else

addInt(&queue,100*sin(t/100));

Sleep(10);

t++;

printf("%d\n",getSizeInt(&queue));

}

return 0;

}

这里动态绘制只写了绘制int类型的代码,float类型的其实也基本一样,复制一下就好,如果各位有需求,可以自己动手或评论或者私信,我来加上。

观看与网易云课堂的童晶老师的视屏教学

#include

#include //Sleep()函数

#include //srand()函数

#include //sqrt()函数

//画黄色的圆

void DrawCicles1(float x,float y,float r)

{

setlinecolor(RGB(0,0,0));

setfillcolor((RGB(255,255,0)));

fillcircle(x,y,r);

}

//画随机圆

void DrawCicles2(float x,float y,float r)

{

float h=rand()%360;

COLORREF color=HSVtoRGB(h,0.6,0.8);

setlinecolor(RGB(255,255,255));

setfillcolor(color);

fillcircle(x,y,r);

}

//画同心圆

void DrawCicles3(float x,float y,float r)

{

while(r>0)

{

float h=rand()%300;

COLORREF color=HSVtoRGB(h,0.6,0.8);

setlinecolor(RGB(255,255,255));

setfillcolor(color);

fillcircle(x,y,r);

r-=5;

}

}

//画亮一点的同心圆

void DrawCicles4(float x,float y,float r)

{

while(r>0)

{

float h=rand()%300;

COLORREF color=HSVtoRGB(h,0.9,0.8);

setlinecolor(RGB(255,255,255));

setfillcolor(color);

fillcircle(x,y,r);

r-=5;

}

}

//计算两个圆心的距离

float Dis2Points(float x1,float y1,float x2,float y2)

{

float result;

result=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

return result;

}

//计算两个圆是否相交

int isTowCiclesIntersect(float x1,float y1,float r1,float x2,float y2,float r2)

{

if (Dis2Points(x1,y1,x2,y2)

{

return 1;

}

return 0;

}

//随机生成圆的半径

int randBetweenMinMax(int min,int max)

{

int r=rand()%(max-min+1)+min;

return r;

}

int main()

{

int width=600;//画布宽

int height=600;//画布宽

initgraph(width,height);//生成画布

setbkcolor(RGB(255,255,255));//设置背景颜色

cleardevice();//清屏

srand(time(0));

int drawMode=0;//颜色选择模式

int xArray[200];//圆的x坐标数组

int yArray[200];//圆的y坐标数组

int rArray[200];//圆的半径坐标数组

int rmin=8;//圆的半径最小

int rmax=50;//圆的半径最大

int circleNum=0;//初始化圆的数量

float x,y,r;//新圆的x、y、r

int isNewCircleOk;//判断是否生成圆

int i,j;

while(circleNum<200)//圆的数量最多为200

{

isNewCircleOk=0;

while(isNewCircleOk==0)

{

if (kbhit())//空格改变颜色

{

char input=_getch();

if (input==' ')

{

circleNum=0;

cleardevice();

drawMode=drawMode+1;

if (drawMode>4)

{

drawMode=1;

}

}

}

//随机生成圆的对应坐标

x=rand()%width;

y=rand()%height;

r=rmin;

//判断两圆是否相交,如果相交就重新生成

for (i=0;i

{

if (isTowCiclesIntersect(xArray[i],yArray[i],rArray[i],x,y,r))

{

break;

}

}

if (i==circleNum)

{

isNewCircleOk=1;

}

}

//这里是为了随机生成的圆大一点,尽量圆是相切

isNewCircleOk=0;

while (isNewCircleOk==0&&r

{

r++;

for (j=0;j

{

if (isTowCiclesIntersect(xArray[j],yArray[j],rArray[j],x,y,r))

{

isNewCircleOk=1;

break;

}

}

}

//重新生成的对应坐标复制

xArray[circleNum]=x;

yArray[circleNum]=y;

rArray[circleNum]=r;

circleNum++;

//这里是颜色模式选择

if (drawMode==1)

{

DrawCicles1(x,y,r);

}

if (drawMode==2)

{

DrawCicles2(x,y,r);

}

if (drawMode==3)

{

DrawCicles3(x,y,r);

}

if (drawMode==4)

{

DrawCicles4(x,y,r);

}

Sleep(100);//延时0.1秒

}

_getch();

closegraph();//关闭

return 0;

}

如有错误,地方请指教,主要作为个人学习记录

发表评论 (已有0条评论)

还木有评论哦,快来抢沙发吧~