C++ 实现旋转蛇错觉的详细代码

 更新时间:2021年09月27日 09:45:28   作者:cqu_shuai  
这篇文章主要介绍了C++ 实现旋转蛇错觉的详细代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

参考 《C和C++游戏趣味编程》 童晶

“旋转蛇”错觉

绘制错觉图片,使静止的圆盘看起来有在转动的错觉

绘制扇形

函数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 radius = 200;
	circle(centerX, centerY, radius);                   // 画出对应的圆边框
	int left = centerX - radius;                        // 圆外切矩形左上角x坐标
	int top = centerY - radius;                         // 圆外切矩形左上角y坐标
	int right = centerX + radius;                       // 圆外切矩形右下角x坐标
	int bottom = centerY + radius;                      // 圆外切矩形右下角y坐标
	solidpie(left, top, right, bottom, PI / 6, PI / 3); // 画出填充扇形
	_getch();
	closegraph();
	return 0;
}

RGB颜色模型

EasyX可以设定绘图颜色:

setbkcolor(WHITE);                                  // 设置背景颜色为白色
setlinecolor(RED);                                  // 设置线条颜色为红色
setfillcolor(GREEN);                                // 设置填充颜色为绿色
cleardevice();                                      // 以背景颜色清空画布

也可以采用数字形式:

setbkcolor(RGB(255, 255, 255));                                  // 设置背景颜色为白色
setlinecolor(RGB(255, 0, 0));                                  // 设置线条颜色为红色
setfillcolor(RGB(0, 255, 0));                                // 设置填充颜色为绿色

绘制一组扇形单元

人脑处理高对比度颜色(如黑和白)的时间,要比处理低对比度颜色(如红与青)短很多。我们会先感知到黑白图案,后感知到红青图案,这个时间差会让图片产生相对运动的效果,所以我们会有图片的错觉

为了进一步强化这种错觉,我们让每个黑、白扇形的角度为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);
	setbkcolor(RGB(128, 128, 128));                                  // 设置背景颜色为白色
	cleardevice();                                      // 以背景颜色清空画布

	int centerX = 300;
	int centerY = 300;
	int radius = 200;
	int left = centerX - radius;                        // 圆外切矩形左上角x坐标
	int top = centerY - radius;                         // 圆外切矩形左上角y坐标
	int right = centerX + radius;                       // 圆外切矩形右下角x坐标
	int bottom = centerY + radius;                      // 圆外切矩形右下角y坐标

	int i;
	float offset;
	for (i = 0; i < 20; i++)
	{
		offset = i * 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(200, 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;
}

循环嵌套

利用双重for循环语句,可以绘制出多层圆盘。先绘制半径大的,在绘制半径小的覆盖。不同半径的扇形之间有PI/20的角度偏移量。另外,对圆心坐标进行循环遍历就可以实现多个圆盘效果

#include <graphics.h>
#include <conio.h>
#include <stdio.h>

int main()
{
	float PI = 3.14159;
	initgraph(1200, 800);
	setbkcolor(RGB(128, 128, 128));                                                                     // 设置背景颜色为灰色
	cleardevice();

	int centerX, centerY;
	int radius;
	int i;
	float offset;
	float totalOffset = 0;                                                                              // 不同半径之间的角度偏移量
	
	for (centerX = 200; centerX < 1200; centerX += 400)
	{
		for (centerY = 200; centerY < 800; centerY += 400)
		{
			for (radius = 200; radius > 0; radius -= 50)
			{
				int left = centerX - radius;
				int top = centerY - radius;
				int right = centerX + radius;
				int bottom = centerY + radius;
				for (i = 0; i < 20; i++)
				{
					offset = i * PI / 10 + totalOffset;
					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(200, 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 += PI / 20;                                                                // 不同半径间角度偏移
			}
		}
	}
	_getch();
	closegraph();
	return 0;
}

HSV颜色模型

HSV是一种根据颜色的直观特性创建的颜色模型。H是Hue的首字母,表示色调,取值范围为0360,刻画不同色彩;S是Saturation的首字母,表示饱和度,取值范围为01,表示混合了白色的比例,值越高颜色越鲜艳;V是Value的首字母,表示明度,取值范围为0~1,等于0时为黑色,等于1时最明亮

#include <graphics.h>
#include <conio.h>
#include <stdio.h>

int main()
{
	float PI = 3.14159;
	initgraph(600, 600);
	setbkcolor(RGB(255, 255, 255));
	cleardevice();

	int centerX = 300;
	int centerY = 300;
	int radius = 200;
	int left = centerX - radius;
	int top = centerY - radius;
	int right = centerX + radius;
	int bottom = centerY + radius;

	int i;
	int step = 10;
	COLORREF color;
	for (i = 0; i < 360; 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;
}

按键切换效果

利用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 radius;
	int i;
	float offset;
	float totalOffset;                                                                              // 不同半径之间的角度偏移量

	while (1)
	{
		for (centerX = 100; centerX < 800; centerX += 200)
		{
			for (centerY = 100; centerY < 600; centerY += 200)
			{
				totalOffset = 0;                                                                           // 同一半径内各组扇形之间的角度偏移量
				float h = rand() % 180;                                                                    // 随机色调
				COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);                                                   // 色调1生成的颜色1
				COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);                                             // 色调2生成的颜色2
				for (radius = 100; radius > 0; radius -= 20)
				{
					int left = centerX - radius;
					int top = centerY - radius;
					int right = centerX + radius;
					int bottom = centerY + radius;
					for (i = 0; i < 20; i++)
					{
						offset = i * PI / 10 + totalOffset;
						setfillcolor(color1);                                                    // 青色
						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);                                                      // 红色
						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 += PI / 20;                                                                // 不同半径间角度偏移
				}
			}
		}
		_getch();
	}
	closegraph();
	return 0;
}

在这里插入图片描述

到此这篇关于C++ 实现旋转蛇错觉的详细代码的文章就介绍到这了,更多相关C++旋转蛇内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++实现LeetCode(11.装最多水的容器)

    C++实现LeetCode(11.装最多水的容器)

    这篇文章主要介绍了C++实现LeetCode(11.装最多水的容器),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++ Boost PointerContainer智能指针详解

    C++ Boost PointerContainer智能指针详解

    智能指针是一种像指针的C++对象,但它能够在对象不使用的时候自己销毁掉。虽然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷贝和赋值操作),因此很少有人使用。它是Boost各组件中,应用最为广泛的一个
    2022-11-11
  • C++基于CreateToolhelp32Snapshot获取系统进程实例

    C++基于CreateToolhelp32Snapshot获取系统进程实例

    这篇文章主要介绍了C++基于CreateToolhelp32Snapshot获取系统进程实例,是Windows应用程序设计中非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • Opencv透视变换综合实例详解

    Opencv透视变换综合实例详解

    这篇文章主要为大家详细介绍了Opencv透视变换综合实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • C++设计模式之模板方法模式

    C++设计模式之模板方法模式

    这篇文章主要介绍了C++设计模式之模板方法模式,本文讲解了什么是模板方法模式、模板方法模式的UML类图、模板方法模式的使用场合等内容,需要的朋友可以参考下
    2014-10-10
  • C++中的std::initializer_list使用解读

    C++中的std::initializer_list使用解读

    这篇文章主要介绍了C++中的std::initializer_list使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Qt实现给窗口绘制阴影的示例代码

    Qt实现给窗口绘制阴影的示例代码

    这篇文章主要为大家详细介绍了Qt实现给窗口绘制阴影的方法,文中的示例代码讲解详细,对我们学习Qt有一定的帮助,感兴趣的可以了解一下
    2022-11-11
  • C++递归算法处理岛屿问题详解

    C++递归算法处理岛屿问题详解

    这篇文章主要介绍了用递归算法解决岛屿问题的流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • C++智能指针模板应用详细介绍

    C++智能指针模板应用详细介绍

    从比较简单的层面来看,智能指针是RAII(Resource Acquisition Is Initialization,资源获取即初始化)机制对普通指针进行的一层封装。这样使得智能指针的行为动作像一个指针,本质上却是一个对象,这样可以方便管理一个对象的生命周期
    2022-08-08
  • C++中的运算符和运算符优先级总结

    C++中的运算符和运算符优先级总结

    这篇文章主要介绍了C++中的运算符和运算符优先级总结,主要整理了算术、关系、逻辑、位和赋值运算符的用法,需要的朋友可以参考下
    2016-05-05

最新评论