软件使用
EasyX、VS2013
效果图1
效果图2
具体实现
人脑处理高对比度颜色(比如黑和白)的时间要比处理低对比度颜色短很多,而这之间的时间差会让图片产生相对运动的效果,这就是旋转蛇错觉。
图上黑色和白色角度都是PI / 60,红色和青色都是PI / 30,而一组角(即黑+白+红+青)一共是PI / 10,所以一共要20组来组成一个圆。如图所示,一个成型的圆是由4个半径不同的圆组成的(先显示半径大的,再逐个显示半径小的),并且这些圆两两之间相差PI / 20。最后循环输出6个成型的圆即可
先看会用到的easyX库中的函数:
//该函数使用当前填充样式绘制无外框的填充扇形
void solidpie(
int left,//圆外切矩形左上角x坐标
int top,//圆外切矩形左上角y坐标
int right,//圆外切矩形右下角y坐标
int bottom,//圆外切矩形右下角y坐标
double stangle,//扇形的起始角的弧度
double endangle//扇形的终止角的弧度
);
//这个函数用于设置当前设备绘图背景色,学过html+css的可能会觉得熟悉
void setbkcolor(COLORREF color);
setbkcolor(RGB(128, 128, 128)); //背景设置为灰色
//这个函数用于设置当前设备填充颜色
void setfillcolor(COLORREF color);
setfillcolor(RGB(255, 255, 255)); //填充为白色
效果图1源代码
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
int main()
{
float PI = 3.14159;
initgraph(1200, 800);
setbkcolor(RGB(128, 128, 128)); //背景设置为灰色
cleardevice(); //用当前背景色清空设备
int coreX = 0, coreY = 0, radius = 0; //圆形坐标和半径
int top_left_x = coreX - radius;
int top_left_y = coreY - radius;
int bot_right_x = coreX + radius;
int bot_right_y = coreY + radius;
float shift = 0 , begin = 0;
/*
因为要生成多个圆形,所以需要用嵌套for循环
这里整个图表长1200,宽800
所以每个原点横坐标分别是200、600、1000
纵坐标分别是200和600
*/
for (coreX = 200; coreX < 1200; coreX += 400)
{
for (coreY = 200; coreY < 800; coreY += 400)
{
for (radius = 200; radius > 0; radius -= 50) //每个圆由4个圆组成
{
//每个圆位置不同,外切矩形的坐标也不同
int top_left_x = coreX - radius;
int top_left_y = coreY - radius;
int bot_right_x = coreX + radius;
int bot_right_y = coreY + radius;
for (int i = 0; i <= 19; i++)
{
begin = i * PI / 10 + shift; //shift是偏移量
setfillcolor(RGB(0, 240, 220));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin, begin + PI / 30);
setfillcolor(RGB(255, 255, 255));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + PI / 30, begin + 3 * PI / 60);
setfillcolor(RGB(200, 0, 0));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 3 * PI / 60, begin + 5 * PI / 60);
setfillcolor(RGB(0, 0, 0));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 5 * PI / 60, begin + 6 * PI / 60);
}
shift += PI / 20;
}
}
}
_getch();
closegraph();
return 0;
}
效果图2源代码
#include <conio.h>
#include <graphics.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 coreX = 0, coreY = 0, radius = 0;
int top_left_x = coreX - radius;
int top_left_y = coreY - radius;
int bot_right_x = coreX + radius;
int bot_right_y = coreY + radius;
float shift = 0, begin = 0;
while (1)
{
for (coreX = 100; coreX < 800; coreX += 200)
{
for (coreY = 100; coreY < 600; coreY += 200)
{
float h = rand() % 180;
COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);
COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);
for (radius = 100; radius > 0; radius -= 20)
{
int top_left_x = coreX - radius;
int top_left_y = coreY - radius;
int bot_right_x = coreX + radius;
int bot_right_y = coreY + radius;
for (int i = 0; i <= 19; i++)
{
begin = i * PI / 10 + shift;
setfillcolor(color1);
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin, begin + PI / 30);
setfillcolor(RGB(255, 255, 255));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + PI / 30, begin + 3 * PI / 60);
setfillcolor(color2);
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 3 * PI / 60, begin + 5 * PI / 60);
setfillcolor(RGB(0, 0, 0));
solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 5 * PI / 60, begin + 6 * PI / 60);
}
shift += PI / 20;
}
}
}
_getch();
}
return 0;
}