cube_3d_v2_lisible.py

Created by escartem

Created on May 28, 2024

1.88 KB

Version lisible de cube_3d.py


from math import *
from ion import *
from kandinsky import *

### VARS ###
CUBE = [77,111,72,108,123,89,68,102,126,90,114,80]
CUBE_DATA = [[-50+100*int(e) for e in list(bin(j)[3:])] for j in CUBE]
CONFIG = [0,0,300,0,0,0]
WIDTH,HEIGHT=320,222
OFFSET_X,OFFSET_Y=WIDTH//2,HEIGHT//2
M_SPEED,R_SPEED, PRECISION = 5, 0.05, 50
KEYS_DATA = {0:(0,-M_SPEED),3:(0,M_SPEED),1:(1,-M_SPEED),2:(1,M_SPEED),45:(2,M_SPEED),46:(2,-M_SPEED),30:(3,-R_SPEED),31:(4,-R_SPEED),32:(5,-R_SPEED),42:(3,R_SPEED),43:(4,R_SPEED),44:(5,R_SPEED)}
COLORS = [(0,0,0),(255,255,255)]
FRAME_0, RENDER_ALT = False, True
STORAGE = []

### MATH ###
xrot=lambda x,y,z: yrot(x,y*cos(CONFIG[3])-z*sin(CONFIG[3]),z*cos(CONFIG[3])+y*sin(CONFIG[3]))
yrot=lambda x,y,z: zrot(x*cos(CONFIG[4])-z*sin(CONFIG[4]),y,z*cos(CONFIG[4])+x*sin(CONFIG[4]))
zrot=lambda x,y,z: point(x*cos(CONFIG[5])-y*sin(CONFIG[5]),y*cos(CONFIG[5])+x*sin(CONFIG[5]),z)
point=lambda x,y,z: [(CONFIG[2]*(x+CONFIG[0]))/(CONFIG[2]+z)+CONFIG[0]+OFFSET_X,(CONFIG[2]*(y+CONFIG[1]))/(CONFIG[2]+z)+CONFIG[1]+OFFSET_Y]

### RENDER ###
clear = lambda: fill_rect(0,0,WIDTH,HEIGHT,COLORS[1])

def line(x1,y1,z1,x2,y2,z2,p):
  x1,y1=xrot(x1,y1,z1)
  x2,y2=xrot(x2,y2,z2)
  cx,cy,mx,my=(x1-x2)/p,(y1-y2)/p,x2,y2
  
  for i in range(p+1):
    STORAGE.append((int(mx),int(my))) if RENDER_ALT else set_pixel(int(mx),int(my),COLORS[0])
    mx,my=mx+cx,my+cy

def render(draw,color=0):
  for e in draw:
    set_pixel(e[0],e[1],COLORS[color])

### MAIN ###
while True:
  for line_data in CUBE_DATA:
    if RENDER_ALT:
      render(STORAGE[0:PRECISION+1],1)
      if FRAME_0: del STORAGE[:PRECISION+1]
    line(*line_data, p=PRECISION)
    
    RENDER_ALT and render(STORAGE[-(PRECISION+1):-1])
    
  not RENDER_ALT and clear()

  FRAME_0 = True
  
  for key in KEYS_DATA.keys():
    if keydown(key): CONFIG[KEYS_DATA[key][0]] += KEYS_DATA[key][1]
  
  CONFIG[4] += R_SPEED
  CONFIG[5] -= R_SPEED

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.