错觉图片生成实验 - 扭曲的黑白棋盘 - 知乎

2020/11/07 11:10
阅读数 46

这学期的线下C语言课程,大一同学们学完旋转蛇案例后(童晶:第4章 旋转蛇(《C和C++游戏趣味编程》配套教学视频)),布置了一次错觉图片生成实验的PBL,给了大家5天时间分组完成代码、ppt报告。

这次,介绍邵一宸、汤刘阳、李树斌、詹振威同学实现的扭曲的黑白棋盘。由于同学们学习C语言刚一个月,还没有正式学习数组、函数等语法知识,因此代码可能不够完善。以下提供了分步骤的实现思路、代码,大家可以参考。

首先是同学们调研的目标效果:

知乎视频www.zhihu.com图标

在明暗交错的棋盘格中,人脑对于黑白亮度感知有快慢,由视网膜传输到人脑的初级视皮层(Visual Cortex),以及更高级别的处理区域后,产生了视觉偏差,所以会产生球面镜的效果。

1 画一个小白块儿

2 画出半个棋盘

3 画出完整的棋盘

4 添加错觉小正方形

5 填补错觉小正方形

#include<easyx.h>
#include<conio.h>
#include<stdio.h>
#define width 5         //定义常量:小小正方形的宽
#define device 3        //定义常量;小小正方形和小正方形的缝隙大小

int main()
{
	initgraph(570, 570);                          //设置窗口大小
	int left_1 = 30, top_1 = 0, right_1 = 60, bottom_1 = 30;
	fillrectangle(left_1, top_1, right_1, bottom_1);//画出第一个小方块
	int i, j;										//定义行数i和列数j

	for (i = 1; i <= 10; i++) {
		for (j = 1; j <= 9; j++) {
			left_1 = left_1 + 60;
			right_1 = right_1 + 60;
			fillrectangle(left_1, top_1, right_1, bottom_1);
		}
		left_1 = 30;
		right_1 = 60;
		top_1 = top_1 + 60;
		bottom_1 = bottom_1 + 60;
		fillrectangle(left_1, top_1, right_1, bottom_1);
	}     //画出第一组白块,填充一半棋盘

	int left_2 = 0, top_2 = 30, right_2 = 30, bottom_2 = 60;
	fillrectangle(left_2, top_2, right_2, bottom_2);
	for (i = 1; i <= 9; i++) {
		for (j = 1; j <= 10; j++) {
			left_2 = left_2 + 60;
			right_2 = right_2 + 60;
			fillrectangle(left_2, top_2, right_2, bottom_2);
		}
		left_2 = 0;
		right_2 = 30;
		top_2 = top_2 + 60;
		bottom_2 = bottom_2 + 60;
		fillrectangle(left_2, top_2, right_2, bottom_2);
	}      //画出第二组白块,填充完整棋盘

	int left_3 = 210 + device, top_3 = 120 - device - width, right_3, bottom_3;
	right_3 = left_3 + width;
	bottom_3 = top_3 + width;
	fillrectangle(left_3, top_3, right_3, bottom_3);

	int left_4 = 360 - device - width, top_4 = 120 - device - width, right_4, bottom_4;
	right_4 = left_4 + width;
	bottom_4 = top_4 + width;
	fillrectangle(left_4, top_4, right_4, bottom_4);

	int left_5 = 120 - device - width, top_5 = 360 - device - width, right_5, bottom_5;
	right_5 = left_5 + width;
	bottom_5 = top_5 + width;
	fillrectangle(left_5, top_5, right_5, bottom_5);

	int left_6 = 450 + device, top_6 = 360 - device - width, right_6, bottom_6;
	right_6 = left_6 + width;
	bottom_6 = top_6 + width;
	fillrectangle(left_6, top_6, right_6, bottom_6);

	int left_7 = 210 - device - width, top_7 = 120 + device, right_7, bottom_7;
	right_7 = left_7 + width;
	bottom_7 = top_7 + width;
	fillrectangle(left_7, top_7, right_7, bottom_7);

	int left_8 = 360 + device, top_8 = 120 + device, right_8, bottom_8;
	right_8 = left_8 + width;
	bottom_8 = top_8 + width;
	fillrectangle(left_8, top_8, right_8, bottom_8);

	int left_9 = 120 + device, top_9 = 360 + device, right_9, bottom_9;
	right_9 = left_9 + width;
	bottom_9 = top_9 + width;
	fillrectangle(left_9, top_9, right_9, bottom_9);

	int left_10 = 450 - device - width, top_10 = 360 + device, right_10, bottom_10;
	right_10 = left_10 + width;
	bottom_10 = top_10 + width;
	fillrectangle(left_10, top_10, right_10, bottom_10);

	int left_11 = 270 + device, top_11 = 120 - device - width, right_11, bottom_11;
	right_11 = left_11 + width;
	bottom_11 = top_11 + width;
	fillrectangle(left_11, top_11, right_11, bottom_11);

	int left_12 = 300 - device - width, top_12 = 120 - device - width, right_12, bottom_12;
	right_12 = left_12 + width;
	bottom_12 = top_12 + width;
	fillrectangle(left_12, top_12, right_12, bottom_12);

	int left_13 = 270 - device - width, top_13 = 120 + device, right_13, bottom_13;
	right_13 = left_13 + width;
	bottom_13 = top_13 + width;
	fillrectangle(left_13, top_13, right_13, bottom_13);

	int left_14 = 300 + device, top_14 = 120 + device, right_14, bottom_14;
	right_14 = left_14 + width;
	bottom_14 = top_14 + width;
	fillrectangle(left_14, top_14, right_14, bottom_14);

	int left_15 = 120 + device, top_15 = 270 - device - width, right_15, bottom_15;
	right_15 = left_15 + width;
	bottom_15 = top_15 + width;
	fillrectangle(left_15, top_15, right_15, bottom_15);

	int left_16 = 120 - device - width, top_16 = 270 + device, right_16, bottom_16;
	right_16 = left_16 + width;
	bottom_16 = top_16 + width;
	fillrectangle(left_16, top_16, right_16, bottom_16);

	int left_17 = 120 - device - width, top_17 = 300 - device - width, right_17, bottom_17;
	right_17 = left_17 + width;
	bottom_17 = top_17 + width;
	fillrectangle(left_17, top_17, right_17, bottom_17);

	int left_18 = 120 + device, top_18 = 300 + device, right_18, bottom_18;
	right_18 = left_18 + width;
	bottom_18 = top_18 + width;
	fillrectangle(left_18, top_18, right_18, bottom_18);

	int left_19 = 270 - width - device, top_19 = 450 - device - width, right_19, bottom_19;
	right_19 = left_19 + width;
	bottom_19 = top_19 + width;
	fillrectangle(left_19, top_19, right_19, bottom_19);

	int left_20 = 270 + device, top_20 = 450 + device, right_20, bottom_20;
	right_20 = left_20 + width;
	bottom_20 = top_20 + width;
	fillrectangle(left_20, top_20, right_20, bottom_20);

	int left_21 = 300 - device - width, top_21 = 450 + device, right_21, bottom_21;
	right_21 = left_21 + width;
	bottom_21 = top_21 + width;
	fillrectangle(left_21, top_21, right_21, bottom_21);

	int left_22 = 300 + device, top_22 = 450 - device - width, right_22, bottom_22;
	right_22 = left_22 + width;
	bottom_22 = top_22 + width;
	fillrectangle(left_22, top_22, right_22, bottom_22);

	int left_23 = 450 - device - width, top_23 = 270 - device - width, right_23, bottom_23;
	right_23 = left_23 + width;
	bottom_23 = top_23 + width;
	fillrectangle(left_23, top_23, right_23, bottom_23);

	int left_24 = 450 + device, top_24 = 270 + device, right_24, bottom_24;
	right_24 = left_24 + width;
	bottom_24 = top_24 + width;
	fillrectangle(left_24, top_24, right_24, bottom_24);

	int left_25 = 450 + device, top_25 = 300 - device - width, right_25, bottom_25;
	right_25 = left_25 + width;
	bottom_25 = top_25 + width;
	fillrectangle(left_25, top_25, right_25, bottom_25);

	int left_26 = 450 - device - width, top_26 = 300 + device, right_26, bottom_26;
	right_26 = left_26 + width;
	bottom_26 = top_26 + width;
	fillrectangle(left_26, top_26, right_26, bottom_26);

	int t;
	for (t = 1; t <= 3; t++) {
		left_3 = left_3 - 30;
		top_3 = top_3 + 30;
		right_3 = right_3 - 30;
		bottom_3 = bottom_3 + 30;
		fillrectangle(left_3, top_3, right_3, bottom_3);
	}
	for (t = 1; t <= 3; t++) {
		left_4 = left_4 + 30;
		top_4 = top_4 + 30;
		right_4 = right_4 + 30;
		bottom_4 = bottom_4 + 30;
		fillrectangle(left_4, top_4, right_4, bottom_4);
	}
	for (t = 1; t <= 3; t++) {
		left_5 = left_5 + 30;
		top_5 = top_5 + 30;
		right_5 = right_5 + 30;
		bottom_5 = bottom_5 + 30;
		fillrectangle(left_5, top_5, right_5, bottom_5);
	}
	for (t = 1; t <= 3; t++) {
		left_6 = left_6 - 30;
		top_6 = top_6 + 30;
		right_6 = right_6 - 30;
		bottom_6 = bottom_6 + 30;
		fillrectangle(left_6, top_6, right_6, bottom_6);
	}

	for (t = 1; t <= 3; t++) {
		left_7 = left_7 - 30;
		top_7 = top_7 + 30;
		right_7 = right_7 - 30;
		bottom_7 = bottom_7 + 30;
		fillrectangle(left_7, top_7, right_7, bottom_7);
	}

	for (t = 1; t <= 3; t++) {
		left_8 = left_8 + 30;
		top_8 = top_8 + 30;
		right_8 = right_8 + 30;
		bottom_8 = bottom_8 + 30;
		fillrectangle(left_8, top_8, right_8, bottom_8);
	}

	for (t = 1; t <= 3; t++) {
		left_9 = left_9 + 30;
		top_9 = top_9 + 30;
		right_9 = right_9 + 30;
		bottom_9 = bottom_9 + 30;
		fillrectangle(left_9, top_9, right_9, bottom_9);
	}

	for (t = 1; t <= 3; t++) {
		left_10 = left_10 - 30;
		top_10 = top_10 + 30;
		right_10 = right_10 - 30;
		bottom_10 = bottom_10 + 30;
		fillrectangle(left_10, top_10, right_10, bottom_10);
	}

	_getch();                                     //暂停等待用户输入
	return 0;                                     //返回值0
}

总结体会

编程最大的乐趣在于不断挑战,不断探索

有时遇到困难个人的力量不足也需要依靠团队的力量

庸人自扰,贤者自渡

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部