Last active
August 12, 2022 21:53
-
-
Save donatj/25911dac4171920e2827371d7104ffd3 to your computer and use it in GitHub Desktop.
Micropython Spinning Cube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from machine import Pin, I2C | |
from ssd1306 import SSD1306_I2C | |
i2c=I2C(1,sda=Pin(26), scl=Pin(27), freq=400000) | |
oled = SSD1306_I2C(128, 64, i2c) | |
class Point3D : | |
x = 0; | |
y = 0; | |
z = 0; | |
def __init__(this,x, y, z) : | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
def rotateX(this,angle) : | |
rad = angle * math.pi / 180; | |
cosa = math.cos(rad); | |
sina = math.sin(rad); | |
y = this.y * cosa - this.z * sina; | |
z = this.y * sina + this.z * cosa; | |
return Point3D(this.x, y, z); | |
def rotateY(this,angle) : | |
rad = angle * math.pi / 180; | |
cosa = math.cos(rad); | |
sina = math.sin(rad); | |
z = this.z * cosa - this.x * sina; | |
x = this.z * sina + this.x * cosa; | |
return Point3D(x, this.y, z); | |
def rotateZ(this,angle) : | |
rad = angle * math.pi / 180; | |
cosa = math.cos(rad); | |
sina = math.sin(rad); | |
x = this.x * cosa - this.y * sina; | |
y = this.x * sina + this.y * cosa; | |
return Point3D(x, y, this.z); | |
def project(this,width, height, fov, viewerDistance) : | |
factor = fov / (viewerDistance + this.z); | |
x = this.x * factor + width / 2; | |
y = -this.y * factor + height / 2; | |
return Point3D(x, y, this.z); | |
img_width = 128; | |
img_height = 64; | |
width = 0.8; | |
height = 0.8; | |
depth = 0.8; | |
hWidth = width / 2; | |
hHeight = height / 2; | |
hDepth = depth / 2; | |
# Define the 8 vertices of the cube. | |
vertices = [Point3D(-hWidth, hHeight, -hDepth), Point3D(hWidth, hHeight, -hDepth), Point3D(hWidth, -hHeight, -hDepth), Point3D(-hWidth, -hHeight, -hDepth), Point3D(-hWidth, hHeight, hDepth), Point3D(hWidth, hHeight, hDepth), Point3D(hWidth, -hHeight, hDepth), Point3D(-hWidth, -hHeight, hDepth)] | |
faces = [ [ 0, 1, 2, 3 ], [ 1, 5, 6, 2 ], [ 5, 4, 7, 6 ], [ 4, 0, 3, 7 ], [ 0, 4, 5, 1 ], [ 3, 2, 6, 7 ] ]; | |
nnn = 0 | |
while True: | |
nnn=nnn+1 | |
angleX = -35 + nnn; | |
nnn+=1/2 | |
angleZ = 15 + nnn; | |
nnn+=1/3 | |
angleY = -30 + nnn; | |
t = [] | |
for v in vertices: | |
rotated = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ); | |
t.append(rotated.project(img_width, img_height, 256, 6)) | |
oled.fill(0) | |
for f in faces: | |
oled.line(int(t[f[0]].x), int(t[f[0]].y), int(t[f[1]].x), int(t[f[1]].y), 1) | |
oled.line(int(t[f[1]].x), int(t[f[1]].y), int(t[f[2]].x), int(t[f[2]].y), 1) | |
oled.line(int(t[f[2]].x), int(t[f[2]].y), int(t[f[3]].x), int(t[f[3]].y), 1) | |
oled.line(int(t[f[3]].x), int(t[f[3]].y), int(t[f[0]].x), int(t[f[0]].y), 1) | |
oled.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment