项目活动1:Image图片向左旋转90°
算法图解:
01 首先看二维图
25个像素点,向左转90°后,每个像素点都“跑”(变换,transform)到了新的位置。
02 其次,再看一维图
虽然我们面对的LED点阵是二维的,但每个像素点只对应于一个索引值(就像在数轴上的情况一样),这就告诉我们,程序中,我们最终要面对的还是一维的情况。
a字符串包括冒号(:)在内,总共有29个字符(character),索引值从0到28。
我们要做的,就是按图上的对应关系,把a[4]放到b[0],a[10]放到b[1],a[16]放到b[2]……以此类推,最终完成将Image图片“左转90°”的变换过程。
这些步骤是有规律可循的,规律就在a和b两个字符串的索引值的关系中,这需要两个for循环(两个递增的for循环计数索引变量i和j)来一起实现。
micropython程序:
from microbit import * a="00900:09990:90909:00900:00900" b="" while True: display.show(Image(a)) sleep(1000) #旋转 for j in range(0,5): for i in range(0,5): b=b+a[6*i+j] if j<4: b=b+":" #print(b) display.show(Image(b)) sleep(1000) b=""
以下的4个项目活动,分析、解决问题的方法与项目活动1类似,请自行结合“图”进行分析研究,寻找出算法中的逻辑。
项目活动2:Image图片向右旋转90°
micropython程序:
from microbit import * a="00900:09990:90909:00900:00900" b="" while True: display.show(Image(a)) sleep(1000) #旋转 for j in range(0,5): for i in range(0,5): b=b+a[6*(4-i)+j] if j<4: b=b+":" #print(b) display.show(Image(b)) sleep(1000) b=""
项目活动3:Image图片旋转180°
micropython程序:
from microbit import * a="09999:90000:90099:90009:09999" b="" while True: display.show(Image(a)) sleep(1000) for i in range(0,29): b=b+a[28-i] #print(b) display.show(Image(b)) sleep(1000) b=""
项目活动4:Image图片的左右翻转(镜像)
micropython程序:
from microbit import * a="09999:90000:90099:90009:09999" b="" while True: display.show(Image(a)) sleep(1000) for j in range(1,6): for i in range(1,6): b=b+a[6*j-i-1] if j<5: b=b+":" #print(b) display.show(Image(b)) sleep(1000) b=""
项目活动5:Image图片的上下翻转(镜像)
micropython程序:
from microbit import * a="09999:90000:90099:90009:09999" b="" while True: display.show(Image(a)) sleep(1000) for i in range(0,5): b=b+a[6*(4-i):6*(4-i)+5] if i<4: b=b+":" #print(b) display.show(Image(b)) sleep(1000) b=""