溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++如何實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺效果

發(fā)布時(shí)間:2021-09-27 10:47:06 來(lái)源:億速云 閱讀:178 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下C++如何實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺效果,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

“旋轉(zhuǎn)蛇”錯(cuò)覺

繪制錯(cuò)覺圖片,使靜止的圓盤看起來(lái)有在轉(zhuǎn)動(dòng)的錯(cuò)覺

繪制扇形

函數(shù)solidpie(left, top, right, bottom, stangle, endangle)可以繪制無(wú)邊框的填充扇形。其中(left, top)、(right, bottom)為扇形對(duì)應(yīng)圓的外切矩形的左上角、右下角坐標(biāo),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);                   // 畫出對(duì)應(yīng)的圓邊框
	int left = centerX - radius;                        // 圓外切矩形左上角x坐標(biāo)
	int top = centerY - radius;                         // 圓外切矩形左上角y坐標(biāo)
	int right = centerX + radius;                       // 圓外切矩形右下角x坐標(biāo)
	int bottom = centerY + radius;                      // 圓外切矩形右下角y坐標(biāo)
	solidpie(left, top, right, bottom, PI / 6, PI / 3); // 畫出填充扇形
	_getch();
	closegraph();
	return 0;
}

RGB顏色模型

EasyX可以設(shè)定繪圖顏色:

setbkcolor(WHITE);                                  // 設(shè)置背景顏色為白色
setlinecolor(RED);                                  // 設(shè)置線條顏色為紅色
setfillcolor(GREEN);                                // 設(shè)置填充顏色為綠色
cleardevice();                                      // 以背景顏色清空畫布

也可以采用數(shù)字形式:

setbkcolor(RGB(255, 255, 255));                                  // 設(shè)置背景顏色為白色
setlinecolor(RGB(255, 0, 0));                                  // 設(shè)置線條顏色為紅色
setfillcolor(RGB(0, 255, 0));                                // 設(shè)置填充顏色為綠色

繪制一組扇形單元

人腦處理高對(duì)比度顏色(如黑和白)的時(shí)間,要比處理低對(duì)比度顏色(如紅與青)短很多。我們會(huì)先感知到黑白圖案,后感知到紅青圖案,這個(gè)時(shí)間差會(huì)讓圖片產(chǎn)生相對(duì)運(yùn)動(dòng)的效果,所以我們會(huì)有圖片的錯(cuò)覺

為了進(jìn)一步強(qiáng)化這種錯(cuò)覺,我們讓每個(gè)黑、白扇形的角度為PI/60,紅、青扇形的角度為PI/30。一組青、白、紅、黑扇形角度和為PI/10,逆時(shí)針依次繪制20組單元

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

int main()
{
	float PI = 3.14159;
	initgraph(600, 600);
	setbkcolor(RGB(128, 128, 128));                                  // 設(shè)置背景顏色為白色
	cleardevice();                                      // 以背景顏色清空畫布

	int centerX = 300;
	int centerY = 300;
	int radius = 200;
	int left = centerX - radius;                        // 圓外切矩形左上角x坐標(biāo)
	int top = centerY - radius;                         // 圓外切矩形左上角y坐標(biāo)
	int right = centerX + radius;                       // 圓外切矩形右下角x坐標(biāo)
	int bottom = centerY + radius;                      // 圓外切矩形右下角y坐標(biāo)

	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;
}

循環(huán)嵌套

利用雙重for循環(huán)語(yǔ)句,可以繪制出多層圓盤。先繪制半徑大的,在繪制半徑小的覆蓋。不同半徑的扇形之間有PI/20的角度偏移量。另外,對(duì)圓心坐標(biāo)進(jìn)行循環(huán)遍歷就可以實(shí)現(xiàn)多個(gè)圓盤效果

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

int main()
{
	float PI = 3.14159;
	initgraph(1200, 800);
	setbkcolor(RGB(128, 128, 128));                                                                     // 設(shè)置背景顏色為灰色
	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是一種根據(jù)顏色的直觀特性創(chuàng)建的顏色模型。H是Hue的首字母,表示色調(diào),取值范圍為0360,刻畫不同色彩;S是Saturation的首字母,表示飽和度,取值范圍為01,表示混合了白色的比例,值越高顏色越鮮艷;V是Value的首字母,表示明度,取值范圍為0~1,等于0時(shí)為黑色,等于1時(shí)最明亮

#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設(shè)置的顏色
		setfillcolor(color);
		solidpie(left, top, right, bottom, i * PI / 180, (i + step) * PI / 180);
	}
	_getch();
	return 0;
}

按鍵切換效果

利用while循環(huán)和_getch()函數(shù),可以實(shí)現(xiàn)每次按鍵后,重新生成隨機(jī)顏色。另外,利用srand()函數(shù)對(duì)隨機(jī)函數(shù)初始化,避免每次運(yùn)行的隨機(jī)顏色都一樣

#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));                                                                     // 設(shè)置背景顏色為灰色
	cleardevice();
	srand(time(0));                                                                                     // 隨機(jī)種子函數(shù)

	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;                                                                           // 同一半徑內(nèi)各組扇形之間的角度偏移量
				float h = rand() % 180;                                                                    // 隨機(jī)色調(diào)
				COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);                                                   // 色調(diào)1生成的顏色1
				COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);                                             // 色調(diào)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++如何實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺效果

看完了這篇文章,相信你對(duì)“C++如何實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺效果”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI