目录
一、绘制扇形
1、绘制扇形
一个五颜六色的圆可以由多种不同颜色扇形组成
其中绘制一个扇形采用函数——
solidpie(left, top, right, bottom, stangle, endangle);
(left,top)表示圆外切矩形左上角
(right, bottom) 表示外切矩形右下角
(stangle, endangle ) 表示扇形的起始角,终止角
完整代码如下
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
int centerX = 300;
int centerY = 300;
int centerR = 200;//圆的圆心X,Y以及半径
circle(centerX, centerY, centerR);
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top= centerY - centerR;//圆外切矩形左上角Y坐标
int right= centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
solidpie(left, top, right, bottom, PI / 6, PI / 3);//画填充扇形,角度为PI/6到PI/3
_getch();
closegraph();
return 0;
}
即可绘制出如图的扇形:
2、RGB颜色模型
EasyX可以设定绘图颜色,在以上代码的基础上添加代码
setbkcolor(WHITE);//设置背景颜色为白色
cleardevice();//以背景颜色清空画布
setlinecolor(RED);//设置线条颜色为红色
setfillcolor(GREEN);//设置填充颜色为绿色
注意上述代码和绘图函数的先后顺序
完整代码如下:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
int centerX = 300;
int centerY = 300;
int centerR = 200;//圆的圆心X,Y以及半径
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top= centerY - centerR;//圆外切矩形左上角Y坐标
int right= centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
setbkcolor(WHITE);//设置背景颜色为白色
cleardevice();//以背景颜色清空画布
setlinecolor(RED);//设置线条颜色为红色
setfillcolor(GREEN);//设置填充颜色为绿色
circle(centerX, centerY, centerR);
solidpie(left, top, right, bottom, PI / 6, PI / 3);//画填充扇形,角度为PI/6到PI/3
_getch();
closegraph();
return 0;
}
运行结果如下:
颜色除了用大写英文单词 WHITE, BLACK, GREEN ,BLUE ,YELLOW 表示也可以用数字形式表示
根据三原色原理,任何颜色均可以用红(Red),绿( Green),蓝(Blue)组成,对于其中任意分量,规定0为最暗,255为最亮
通过重复设置填充颜色可以实现如下样例:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
int centerX = 300;
int centerY = 300;
int centerR = 200;//圆的圆心X,Y以及半径
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top= centerY - centerR;//圆外切矩形左上角Y坐标
int right= centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
setbkcolor(RGB(120,120,120));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
setlinecolor(RGB(0,0,0));//设置线条颜色为黑色
circle(centerX, centerY, centerR);
setfillcolor(RGB(0, 255, 0));//设置填充颜色为绿色
solidpie(left, top, right, bottom, 0, PI / 2);//画填充扇形,角度为0到PI/2
setfillcolor(RGB(255, 255, 255));//设置填充颜色为白色
solidpie(left, top, right, bottom, PI / 2,PI );//画填充扇形
setfillcolor(RGB(255, 0, 0));//设置填充颜色为红
solidpie(left, top, right, bottom,PI , 3 * PI /2);//画填充扇形
setfillcolor(RGB(0, 0, 0));//设置填充颜色为红
solidpie(left, top, right, bottom, 3 * PI /2, 2*PI );//画填充扇形
_getch();
closegraph();
return 0;
}
二、旋转蛇错觉
旋转蛇其中一原理是——人脑处理高对比度颜色(比如黑、白)的时间比处理低对比度(比如红、青)短很多,所以先感知黑白图案,再感知到红青图案,这个时间差会让图片产生相对运动的效果,导致产生图片旋转的错觉
因此我们先绘制一个扇形,再重复绘制同一扇形单元就可以凑出一个由 青,白,红,黑组成的旋转圆了
1、绘制一个扇形单元
为了强化这种错觉,我们令每个黑白扇形角度为PI/60,红黑扇形角度为PI/30,一组青,白,红,黑的扇形角度和为PI/10,逆时针依次绘制20组即可绘制出圆
代码实现如下:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
int centerX = 300;
int centerY = 300;
int centerR = 200;//圆的圆心X,Y以及半径
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top= centerY - centerR;//圆外切矩形左上角Y坐标
int right= centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
setbkcolor(RGB(120,120,120));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
setfillcolor(RGB(0,240, 220));//设置填充颜色为青
solidpie(left, top, right, bottom, 0, 2*PI / 60);//画填充扇形
setfillcolor(RGB(255, 255, 255));//设置填充颜色为白色
solidpie(left, top, right, bottom, 2 * PI / 60 , 3 * PI / 60);//画填充扇形
setfillcolor(RGB(255, 0, 0));//设置填充颜色为红
solidpie(left, top, right, bottom, 3 * PI / 60, 5 * PI / 60);//画填充扇形
setfillcolor(RGB(0, 0, 0));//设置填充颜色为黑
solidpie(left, top, right, bottom, 5 * PI / 60, 6 * PI / 60);//画填充扇形
_getch();
closegraph();
return 0;
}
2、利用for循环重复绘制
对于for(A;B;C)语句——A为设定循环起始条件,B为循环结束条件,C是对循环变量进行改变
其中 自增运算符 i++ 等价于 i=i+1 和 自减运算符 i-- 等价于i=i-1
eg. for (i = 1;i <= 5;i = i + 1)
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int i;
for (i = 1;i <= 5;i = i + 1)
printf("%d\n", i);
_getch();
return 0;
}
for语句中,首先执行 i = 1;
然后判断 i <= 5 是否满足;
满足,执行printf,再运行i = i+1;
不满足,循环结束;
有了for循环,每一个扇形单元偏移 PI/10 就可以到下一组的位置,移动20次就可以绘制完整个圆
完整代码如下:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
int centerX = 300;
int centerY = 300;
int centerR = 200;//圆的圆心X,Y以及半径
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top= centerY - centerR;//圆外切矩形左上角Y坐标
int right= centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
setbkcolor(RGB(120,120,120));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
int i;
float offset;
for (i = 0;i < 20;i++)//偏移20次
{
offset = i * PI / 10;//每组偏移PI/10
setfillcolor(RGB(0, 240, 220));//设置填充颜色为青
solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(255, 255, 255));//设置填充颜色为白色
solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(255, 0, 0));//设置填充颜色为红
solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(0, 0, 0));//设置填充颜色为黑
solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形
}
_getch();
closegraph();
return 0;
}
最后结果如图
3、for循环的嵌套
对于for的嵌套循环
基本是 for外层的第一次循环——>内层从第一次到最后循环结束 ——> for的外层的第二次循环——>内层从第一次到最后循环结束 ——>直到for外层所有循环结束
eg.由双重for循环结构可以构造一九九乘法表如下
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int i;
int j;
for (i =0;i <= 9;i++)//行数
{
printf("%3d", i);//每列开始前标明此列是什么数
for (j = 1;j <= 9;j++)
{
if(i==0)//多设置了个第0行,如果是0行,输出j的所有列号
printf("%3d", j);
else if ( j > i)//当列数大于行数的时候不再打印退出
break;
else if(i!=0)//第0行以外的行数正常输出相乘结果
printf("%3d",i*j);//输出整数占三个字符用于对齐
}
printf("\n");
}
_getch();
return 0;
}
总之掌握双重for循环之后,我们可以用于构造多层圆盘,先绘制大扇形,再绘制小扇形(先绘制会被后绘制遮挡)
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
setbkcolor(RGB(128,128,128));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
int centerX = 300;
int centerY = 300;
int centerR;//圆的圆心X,Y以及半径
int i;
float offset;
float totaloffset = 0;
for(centerR = 200;centerR>0;centerR= centerR-50)//修改半径
{
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top = centerY - centerR;//圆外切矩形左上角Y坐标
int right = centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
for (i = 0;i < 20;i++)//偏移20次
{
offset = i * PI / 10+ totaloffset;//每组偏移PI/10
setfillcolor(RGB(0, 240, 220));//设置填充颜色为青
solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(255, 255, 255));//设置填充颜色为白色
solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(255, 0, 0));//设置填充颜色为红
solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(0, 0, 0));//设置填充颜色为黑
solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形
}
totaloffset = totaloffset + PI / 20;//不同半径的角度偏量
}
_getch();
return 0;
}
运行结果如下图所示:
4、HSV颜色模型
除了上述的RGB颜色模型以外,还有一种根据颜色的直观特性创建的颜色模型——HSV模型
H是Hue首字母,表示色调,取值范围为0~360,红色为0,绿色为120,蓝色为240;
S是Saturation首字母,表示饱和度,取值范围为0~1,值越高颜色越鲜艳;
V是Value的首字母,表示明度,取值范围0~1,0黑1白;
利用HSV中H的改变可以直接改变颜色,不会像RGB中三原色互相限制
利用for循环修改H的值可以得到变化的光谱上各单色效果
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float PI = 3.14159;
initgraph(600, 600);
setbkcolor(RGB(128, 128, 128));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
int centerX = 300;
int centerY = 300;
int centerR=200;//圆的圆心X,Y以及半径int left = centerX - centerR;//圆外切矩形左上角X坐标
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top = centerY - centerR;//圆外切矩形左上角Y坐标
int right = centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
int i;
int step=10;
COLORREF color;//定义颜色变量
for (i = 0;i < 360;i=i+step)
{
color = HSVtoRGB(i, 1, 1);//HSV设置的颜色
setfillcolor(color);//设置填充颜色
solidpie(left, top, right, bottom, i * PI / 180, (i+step) * PI /180);
}
_getch();
return 0;
}
其中:
COLORREF color ——定义了颜色变量
HSVtoRGB(a, b, c)——将HSV设置的颜色转换为RGB颜色
setfillcolor(color)——将设置的颜色变量设置为填充颜色
5、按键切换效果
利用while和_getch()函数可以实现每次案件后,重新随机生成随机颜色。另外,利用srand()函数,对随机函数初始化,避免每次运行的随机颜色都一样
完整代码如下:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
int main()
{
float PI = 3.14159;
initgraph(800, 600);
setbkcolor(RGB(128,128,128));//设置背景颜色为灰色
cleardevice();//以背景颜色清空画布
srand(time(0));
int centerX , centerY;
int centerR;//圆的圆心X,Y以及半径
int i;
float offset;
float totaloffset;
while (1)//重复执行
{
for (centerX = 100;centerX < 800;centerX = centerX + 200)//对多个圆的X坐标循环
{
for (centerY = 100;centerY < 600;centerY = centerY + 200)//对多个圆Y坐标进行循环
{
totaloffset = 0;//同半径各组扇形之间的角度偏移
float h = rand() % 180;//随机色调
COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);
COLORREF color2 = HSVtoRGB(h+180, 0.9, 0.8);
//根据色调1、2生成颜色1、2
//原理还是之前的其他颜色与黑白颜色对比度导致的识别时间差异,所以只用选两个颜色
for (centerR = 100;centerR > 0;centerR = centerR -20)//修改半径
{
int left = centerX - centerR;//圆外切矩形左上角X坐标
int top = centerY - centerR;//圆外切矩形左上角Y坐标
int right = centerX + centerR;//圆外切矩形右下角X坐标
int bottom = centerY + centerR;//圆外切矩形右下角Y坐标
for (i = 0;i < 20;i++)//偏移20次
{
offset = i * PI / 10 + totaloffset;//每组偏移PI/10
setfillcolor(color1);//设置填充颜色色调1
solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(255, 255, 255));//设置填充颜色为白色
solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);//画填充扇形
setfillcolor(color2);//设置填充颜色色调2
solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);//画填充扇形
setfillcolor(RGB(0, 0, 0));//设置填充颜色为黑
solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);//画填充扇形
}
totaloffset = totaloffset + PI / 20;//不同半径的角度偏量
}
}
}
_getch();
}
return 0;
}
其中,对于srand(time(0))表示用当前时间来对随机函数初始化,由于每次运行程序的时间不一样,因此生成的随机数也不一样,要获得当前时间,还需要再代码前面加上文件包含指令
#include<time.h>