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++旋转蛇内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++实现旋转数组的二分查找

    本文实例讲述了C++实现旋转数组的二分查找方法,分享给大家供大家参考.具体方法如下: 题目要求: 旋转数组,如{3, 4, 5, 1, 2}是{1, 2, 3, 4, 5}的一个旋转,要求利用二分查找查找里面的数. 这是一道很有意思的题目,容易考虑不周全.这里给出如下解决方法: #include <iostream> using namespace std; int sequentialSearch(int *array, int size, int destValue) { int pos

  • 使用c++实现OpenCV绘制图形旋转矩形

    目录 功能函数 // 绘制旋转矩形 void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect &rotatedrect,const cv::Scalar &color,int thickness, int lineType) { // 提取旋转矩形的四个角点 cv::Point2f ps[4]; rotatedrect.points(ps); // 构建轮廓线 std::vector<std::vector<cv::Poin

  • C++实现一维向量旋转算法

    在<编程珠玑>一书的第二章提到了n元一维向量旋转算法(又称数组循环移位算法)的五种思路,并且比较了它们在时间和空间性能上的区别和优劣.本文将就这一算法做较为深入的分析.具体如下所示: 一.问题描述 将一个n元一维向量向左旋转i个位置.例如,假设n=8,i=3,向量abcdefgh旋转为向量defghabc.简单的代码使用一个n元的中间向量在n步内可完成该工作.你能否仅使用几十个额外字节的内存空间,在正比于n的时间内完成向量的旋转? 二.解决方案 思路一:将向量x中的前i个元素复制到一个临时数组

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

    参考 <C和C++游戏趣味编程> 童晶 "旋转蛇"错觉 绘制错觉图片,使静止的圆盘看起来有在转动的错觉 绘制扇形 函数solidpie(left, top, right, bottom, stangle, endangle)可以绘制无边框的填充扇形.其中(left, top).(right, bottom)为扇形对应圆的外切矩形的左上角.右下角坐标,stangle.endangle为扇形的起始角.终止角(单位为弧度) #include <graphics.h>

  • pthon贪吃蛇游戏详细代码

    本文实例为大家分享了pthon贪吃蛇游戏的具体代码,供大家参考,具体内容如下 在写Python游戏项目时,最重要的时python中的pygame库.安装pygame库和用法在我CSDN博客另一篇文章上.这里就不详细说了,下边时运行游戏界面. 下边是详细的代码和注释 import pygame,sys,random,time from pygame.locals import * #从pygame模块导入常用的函数和常量 #定义颜色变量 black_colour = pygame.Color(0,

  • JavaScript TypeScript实现贪吃蛇游戏完整详细流程

    目录 项目背景及简介 多模块需求分析 场景模块需求 食物类模块需求 记分牌模块需求 蛇类模块需求 控制模块需求 项目搭建 ts转译为js代码 package.json包配置文件 webpack.config.js打包工具配置 项目结构搭建 html文件 css文件(这里使用的是less) 项目页面 多模块搭建 完成Food(食物)类 完成ScorePanel(记分牌)类 完成Snake(蛇)类 完成GameControl(控制)类 完成index类(启动项目) 项目启动 总结 项目背景及简介 t

  • JavaScript贪吃蛇小组件实例代码

    1 写在前面 看来<JavsScript高级编程>,想做一个小demo练练自己的手,选择了贪吃蛇游戏.由于以前都是用c#写的,将贪吃蛇写到一个类里面,然后一个一个小方法的拆分,只向外提供需要提供的方法.这样就可以将贪吃蛇作为一个模块,任何地方都可以复用的.然而,用js进行编写的时候,由于不能很好的利用js语言的特性进行模块化编程,所以第一版的实现完全采用面向过程的方式,将函数中所需要的变量全部声明为全局变量.虽然这样也能够实现功能,但是做不到复用,而且定义非常多的最顶层变量,污染了全局变量.写

  • vue使用exif获取图片旋转,压缩的示例代码

    <template> <div> <input type="file" id="upload" accept="image" @change="upload" /> </div> </template> <script> export default { data() { return { picValue:{}, headerImage:'' }; },

  • Java实现贪吃蛇游戏的示例代码

    目录 项目演示 项目实战 1. 游戏的主启动类 2. 游戏的面板 3. 数据中心 4. 绘制静态面板 5. 绘制静态小蛇 6. 绘制动态小蛇 7. 设置游戏状态 8. 让蛇动起来 9. 绘制食物布局 10. 游戏失败判定 11. 积分获取系统 12. 游戏优化 项目演示 项目演示地址 项目实战 1. 游戏的主启动类 作为贪吃蛇游戏的主启动类,构建了顶级窗口,可以容纳各种面板, package Snake; import javax.swing.*; /** * 游戏的主启动类 */ public

  • Vue.js实现实例搜索应用功能详细代码

    实例搜索应用 实现效果: 实现代码与注释: <!DOCTYPE html> <html> <head> <title>实例搜索</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style type=&q

  • touch.js 拖动、缩放、旋转 (鼠标手势)功能代码

    可以实现手势操作:拖动.缩放.旋转.封装好的脚本方法是这样的: var cat = window.cat || {}; cat.touchjs = { left: 0, top: 0, scaleVal: 1, //缩放 rotateVal: 0, //旋转 curStatus: 0, //记录当前手势的状态, 0:拖动, 1:缩放, 2:旋转 //初始化 init: function ($targetObj, callback) { touch.on($targetObj, 'touchsta

  • php获取从百度搜索进入网站的关键词的详细代码

    分享一个php获取从百度搜索进入网站的关键词的代码,有需要的朋友可以参考一下: 代码: 复制代码 代码如下: <?php function search_word_from() { $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:''; if(strstr( $referer, 'baidu.com')){ //百度 preg_match( "|baidu.+wo?r?d=([^\\&]*)|i

  • jquery实现LED广告牌旋转系统图片切换效果代码分享

    本文实例讲述了jquery实现LED广告牌旋转系统图片切换效果,分享给大家供大家参考.具体如下: js模拟路边巨大显示屏上广告切换效果,不得不相信js做到了,而且让你无话可说的逼真效果. LED广告显示器上图片切换效果,场景也类似,效果相当震撼看得我目瞪口呆,热爱特效的你可不要错过哈! 运行效果图: -------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的jquery实现LED广告牌旋转

随机推荐