【数学之美】--3D CUBE--有魔性的掌控
本帖最后由 rzyzzxw 于 2018-10-28 18:56 编辑这是一个有魔性的画面
盯着它我看了好久
mpython这样厉害
让我们为它痴狂
http://v.youku.com/v_show/id_XMzg3MjMwMTYwNA==.html?spm=a2h3j.8428770.3416059.1
代码的作者是掌控团队的唐工
写的代码字母都认识别的不懂呀
代码放在下面大家可以学习啦
如果您搞明白了要记得在回复中讲给我们听啊
【代码】
https://github.com/labplus-cn/mPython/blob/master/examples/oled/3D%20cube.py
#本代码作者掌控团队小唐攻城狮
# 3D CUBE MicroPython version with ESP32 and ssd1306 OLED
from machine import Pin, I2C
from micropython import const
from time import sleep_ms
from math import sin, cos
from mpython import *
X = const(64)
Y = const(32)
f = [ for _ in range(8)]
cube = ((-20,-20, 20), (20,-20, 20), (20,20, 20), (-20,20, 20),
(-20,-20,-20), (20,-20,-20), (20,20,-20), (-20,20,-20))
while True:
for angle in range(0, 361, 3):# 0 to 360 deg 3step
for i in range(8):
r= angle * 0.0174532# 1 degree
x1 = cube * sin(r) + cube * cos(r)# rotate Y
ya = cube
z1 = cube * cos(r) - cube * sin(r)
x2 = x1
y2 = ya * cos(r) - z1 * sin(r)# rotate X
z2 = ya * sin(r) + z1 * cos(r)
x3 = x2 * cos(r) - y2 * sin(r)# rotate Z
y3 = x2 * sin(r) + y2 * cos(r)
z3 = z2
x3 = x3 + X
y3 = y3 + Y
f = x3# store new values
f = y3
f = z3
display.fill(0)# clear
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)
display.line(int(f), int(f), int(f), int(f), 1)# cross
display.line(int(f), int(f), int(f), int(f), 1)# cross
display.DispChar('3D CUBE', 0, 0)
display.show()# display
sleep_ms(1) x1 = cube * sin(r) + cube * cos(r)# rotate Y
y2 = ya * cos(r) - z1 * sin(r)# rotate X
x3 = x2 * cos(r) - y2 * sin(r)# rotate Z
大致看了一下原理应该是xyz三点旋转,每次更新坐标点之后,由各个点之间画12条直线,底部画2条交叉线,组成正立方体3D效果,至于双重循环怎么赋值的,因为Python我不熟,还是初学者。
页:
[1]