经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++ 实现旋转蛇错觉的详细代码
来源:jb51  时间:2021/9/27 11:06:04  对本文有异议

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

“旋转蛇”错觉

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

绘制扇形

函数solidpie(left, top, right, bottom, stangle, endangle)可以绘制无边框的填充扇形。其中(left, top)、(right, bottom)为扇形对应圆的外切矩形的左上角、右下角坐标,stangle、endangle为扇形的起始角、终止角(单位为弧度)

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. float PI = 3.14159;
  8. initgraph(600, 600);
  9. int centerX = 300;
  10. int centerY = 300;
  11. int radius = 200;
  12. circle(centerX, centerY, radius); // 画出对应的圆边框
  13. int left = centerX - radius; // 圆外切矩形左上角x坐标
  14. int top = centerY - radius; // 圆外切矩形左上角y坐标
  15. int right = centerX + radius; // 圆外切矩形右下角x坐标
  16. int bottom = centerY + radius; // 圆外切矩形右下角y坐标
  17. solidpie(left, top, right, bottom, PI / 6, PI / 3); // 画出填充扇形
  18. _getch();
  19. closegraph();
  20. return 0;
  21. }

RGB颜色模型

EasyX可以设定绘图颜色:

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

也可以采用数字形式:

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

绘制一组扇形单元

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

为了进一步强化这种错觉,我们让每个黑、白扇形的角度为PI/60,红、青扇形的角度为PI/30。一组青、白、红、黑扇形角度和为PI/10,逆时针依次绘制20组单元

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. float PI = 3.14159;
  8. initgraph(600, 600);
  9. setbkcolor(RGB(128, 128, 128)); // 设置背景颜色为白色
  10. cleardevice(); // 以背景颜色清空画布
  11.  
  12. int centerX = 300;
  13. int centerY = 300;
  14. int radius = 200;
  15. int left = centerX - radius; // 圆外切矩形左上角x坐标
  16. int top = centerY - radius; // 圆外切矩形左上角y坐标
  17. int right = centerX + radius; // 圆外切矩形右下角x坐标
  18. int bottom = centerY + radius; // 圆外切矩形右下角y坐标
  19.  
  20. int i;
  21. float offset;
  22. for (i = 0; i < 20; i++)
  23. {
  24. offset = i * PI / 10;
  25. setfillcolor(RGB(0, 240, 220)); // 青色
  26. solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);
  27. setfillcolor(RGB(255, 255, 255)); // 白色
  28. solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);
  29. setfillcolor(RGB(200, 0, 0)); // 红色
  30. solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);
  31. setfillcolor(RGB(0, 0, 0)); // 黑色
  32. solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);
  33. }
  34. _getch();
  35. closegraph();
  36. return 0;
  37. }

循环嵌套

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

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. float PI = 3.14159;
  8. initgraph(1200, 800);
  9. setbkcolor(RGB(128, 128, 128)); // 设置背景颜色为灰色
  10. cleardevice();
  11.  
  12. int centerX, centerY;
  13. int radius;
  14. int i;
  15. float offset;
  16. float totalOffset = 0; // 不同半径之间的角度偏移量
  17. for (centerX = 200; centerX < 1200; centerX += 400)
  18. {
  19. for (centerY = 200; centerY < 800; centerY += 400)
  20. {
  21. for (radius = 200; radius > 0; radius -= 50)
  22. {
  23. int left = centerX - radius;
  24. int top = centerY - radius;
  25. int right = centerX + radius;
  26. int bottom = centerY + radius;
  27. for (i = 0; i < 20; i++)
  28. {
  29. offset = i * PI / 10 + totalOffset;
  30. setfillcolor(RGB(0, 240, 220)); // 青色
  31. solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);
  32. setfillcolor(RGB(255, 255, 255)); // 白色
  33. solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);
  34. setfillcolor(RGB(200, 0, 0)); // 红色
  35. solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);
  36. setfillcolor(RGB(0, 0, 0)); // 黑色
  37. solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);
  38. }
  39. totalOffset += PI / 20; // 不同半径间角度偏移
  40. }
  41. }
  42. }
  43. _getch();
  44. closegraph();
  45. return 0;
  46. }

HSV颜色模型

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

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. float PI = 3.14159;
  8. initgraph(600, 600);
  9. setbkcolor(RGB(255, 255, 255));
  10. cleardevice();
  11.  
  12. int centerX = 300;
  13. int centerY = 300;
  14. int radius = 200;
  15. int left = centerX - radius;
  16. int top = centerY - radius;
  17. int right = centerX + radius;
  18. int bottom = centerY + radius;
  19.  
  20. int i;
  21. int step = 10;
  22. COLORREF color;
  23. for (i = 0; i < 360; i += step)
  24. {
  25. color = HSVtoRGB(i, 1, 1); // HSV设置的颜色
  26. setfillcolor(color);
  27. solidpie(left, top, right, bottom, i * PI / 180, (i + step) * PI / 180);
  28. }
  29. _getch();
  30. return 0;
  31. }

按键切换效果

利用while循环和_getch()函数,可以实现每次按键后,重新生成随机颜色。另外,利用srand()函数对随机函数初始化,避免每次运行的随机颜色都一样

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6. int main()
  7. {
  8. float PI = 3.14159;
  9. initgraph(800, 600);
  10. setbkcolor(RGB(128, 128, 128)); // 设置背景颜色为灰色
  11. cleardevice();
  12. srand(time(0)); // 随机种子函数
  13.  
  14. int centerX, centerY;
  15. int radius;
  16. int i;
  17. float offset;
  18. float totalOffset; // 不同半径之间的角度偏移量
  19.  
  20. while (1)
  21. {
  22. for (centerX = 100; centerX < 800; centerX += 200)
  23. {
  24. for (centerY = 100; centerY < 600; centerY += 200)
  25. {
  26. totalOffset = 0; // 同一半径内各组扇形之间的角度偏移量
  27. float h = rand() % 180; // 随机色调
  28. COLORREF color1 = HSVtoRGB(h, 0.9, 0.8); // 色调1生成的颜色1
  29. COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8); // 色调2生成的颜色2
  30. for (radius = 100; radius > 0; radius -= 20)
  31. {
  32. int left = centerX - radius;
  33. int top = centerY - radius;
  34. int right = centerX + radius;
  35. int bottom = centerY + radius;
  36. for (i = 0; i < 20; i++)
  37. {
  38. offset = i * PI / 10 + totalOffset;
  39. setfillcolor(color1); // 青色
  40. solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset);
  41. setfillcolor(RGB(255, 255, 255)); // 白色
  42. solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset);
  43. setfillcolor(color2); // 红色
  44. solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset);
  45. setfillcolor(RGB(0, 0, 0)); // 黑色
  46. solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset);
  47. }
  48. totalOffset += PI / 20; // 不同半径间角度偏移
  49. }
  50. }
  51. }
  52. _getch();
  53. }
  54. closegraph();
  55. return 0;
  56. }

在这里插入图片描述

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号