Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the illusion effect of rotating snake in C++

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The editor will share with you how C++ can achieve the illusion of rotating snake. I hope you will get something after reading this article. Let's discuss it together.

"rotating snake" illusion

Draw an illusion picture so that the stationary disc looks like it is turning

Draw a sector

The function solidpie (left, top, right, bottom, stangle, endangle) can draw a filled sector without borders. Where (left, top) and (right, bottom) are the upper-left and lower-right coordinates of the circumscribed rectangle corresponding to the fan-shaped circle, and stangle and endangle are the starting and ending angles (in radians) of the fan-shaped.

# include # include # include int main () {float PI = 3.14159; initgraph (600,600); int centerX = 300; int centerY = 300; int radius = 200; circle (centerX, centerY, radius); / / draw the corresponding circle border int left = centerX-radius / / the upper left corner of the circumscribed rectangle x coordinate int top = centerY-radius; / / the upper left corner of the circular circumscribed rectangle y coordinate int right = centerX + radius; / / the lower right corner of the circumscribed rectangle x coordinate int bottom = centerY + radius / / solidpie (left, top, right, bottom, PI / 6, PI / 3) in the lower right corner of the circle tangent rectangle; / / draw the filled sector _ getch (); closegraph (); return 0;} RGB color model

EasyX can set the drawing color:

Setbkcolor (WHITE); / / set the background color to white setlinecolor (RED); / / set the line color to red setfillcolor (GREEN); / / set the fill color to green cleardevice () / / empty the canvas with the background color

It can also be in digital form:

Setbkcolor (RGB (255,255,255)); / / set the background color to white setlinecolor (RGB (255,0,0)); / / set the line color to red setfillcolor (RGB (0,255,0)); / / set the fill color to green to draw a set of sector cells

The brain takes much less time to process high-contrast colors (such as black and white) than low-contrast colors (such as red and cyan). We will perceive the black and white pattern first, and then the red and green pattern. This time difference will make the picture have the effect of relative motion, so we will have the illusion of the picture.

To further reinforce this illusion, we make the angle of each black and white fan PI/60, and the angle of red and green fan PI/30. A group of green, white, red and black fan angles are PI/10, and 20 groups of units are drawn counterclockwise

# include # include # include int main () {float PI = 3.14159; initgraph (600,600); setbkcolor (RGB (128,128,128)); / / set background color to white cleardevice (); / / clear canvas int centerX = 300 with background color Int centerY = 300; int radius = 200; int left = centerX-radius; / / the upper left corner of the circumscribed rectangle x coordinates int top = centerY-radius; / / the upper left corner of the circumscribed rectangle int right = centerX + radius / / the lower right corner of the circle tangent rectangle x coordinate int bottom = centerY + radius; / / the y coordinate 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 #include #include 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 #include #include 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 #include #include #include 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; iTunes +) {offset = I * PI / 10 + totalOffset; setfillcolor (color1) / / Cyan solidpie (left, top, right, bottom, offset, 2 * PI / 60 + offset); setfillcolor (RGB (255,255,255)) / / White solidpie (left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset); setfillcolor (color2) / / Red solidpie (left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset); setfillcolor (RGB (0,0,0)) / / Black solidpie (left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);} totalOffset + = PI / 20 / / Angle offset between different radii} _ getch ();} closegraph (); return 0;}

After reading this article, I believe you have a certain understanding of "how C++ achieves the illusion effect of rotating snake". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report