demo3d.py

Created by squarepoint

Created on November 22, 2022

1.75 KB

Yay, you can do 3d on your calculator! :D (i may make a 3d game soon)

Thanks to Antarctus for the draw line function


from math import cos,sin,pi
from kandinsky import *
from time import sleep

MODEL=[]

def cube3d(x1,y1,z1,x2,y2,z2):
  line3d(x1,y1,z1,x1,y2,z1)
  line3d(x1,y1,z1,x2,y1,z1)
  line3d(x2,y1,z1,x2,y2,z1)
  line3d(x1,y2,z1,x2,y2,z1)
  line3d(x1,y1,z2,x1,y2,z2)
  line3d(x1,y1,z2,x2,y1,z2)
  line3d(x2,y1,z2,x2,y2,z2)
  line3d(x1,y2,z2,x2,y2,z2)
  line3d(x1,y1,z1,x1,y1,z2)
  line3d(x2,y1,z1,x2,y1,z2)
  line3d(x1,y2,z1,x1,y2,z2)
  line3d(x2,y2,z1,x2,y2,z2)

def line3d(x1,y1,z1,x2,y2,z2):
  point3d(x1,y1,z1)
  point3d(x2,y2,z2)

def point3d(x,y,z):
  MODEL.append([x,y,z])

def line(x1,y1,x2,y2,c):
  w=x2-x1
  h=y2-y1
  if abs(w)>=abs(h):
    d=h/w
    for i in range(0,w,(w>0)*2-1):
      set_pixel(x1+i,y1+int(d*i+0.5),c)
  else:
    d=w/h
    for i in range(0,h,(h>0)*2-1):
      set_pixel(x1+int(d*i+0.5),y1+i,c)

def render(cosy,siny,cosz,sinz):
  fill_rect(0,0,320,222,(0,0,0))
  draw_string("Full engine coming soon...",30,202,(255,255,255),(0,0,0))
  for i in range(0,len(MODEL),2):
    x1=MODEL[i][0]
    y1=MODEL[i][1]
    z1=MODEL[i][2]
    x2=MODEL[i+1][0]
    y2=MODEL[i+1][1]
    z2=MODEL[i+1][2]
    #Rotations
    x1,z1=yturn(x1,z1,cosy,siny)
    x2,z2=yturn(x2,z2,cosy,siny)
    x1,y1=zturn(x1,y1,cosz,sinz)
    x2,y2=zturn(x2,y2,cosz,sinz)
    #Projections
    px1=int(x1*100/(z1+300))
    py1=int(y1*100/(z1+300))
    px2=int(x2*100/(z2+300))
    py2=int(y2*100/(z2+300))
    line(px1+160,py1+100,px2+160,py2+100,(255,255,255))

def yturn(x,z,cosy,siny):
  return z*siny+x*cosy,z*cosy-x*siny

def zturn(x,y,cosz,sinz):
  return x*cosz-y*sinz,x*sinz+y*cosz

cube3d(-100,-100,-100,100,100,100)
yrot=0
zrot=0

while True:
  cosy=cos(yrot)
  siny=sin(yrot)
  cosz=cos(zrot)
  sinz=sin(zrot)
  render(cosy,siny,cosz,sinz)
  yrot+=pi/90
  zrot=sin(yrot)
  sleep(0.01)

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.