projection_3d.py

Created by gasmat2012

Created on January 19, 2026

3.19 KB

Projects a 3D object. The x, y and z axes are modelled as lines in red, green and blue respectively. Needs descartes.py for the graphics, which also needs draw_line.py to draw lines. __________________

Commands: Up Arrow: Increment angle along x-axis. Down Arrow: Excrement angle along x-axis. Left Arrow: Increment angle along y-axis. Right Arrrow: Excrement angle along y-axis. Right Parenthesis: Increment angle along z-axis. Left Parenthesis: Excrement angle along z-axis. Plus Key: Zoom in on the origin. Minus Key: Zoom out on the origin.

More commands and improvements are yet to come; if you have any ideas please let me know on TI-Planet here: https://tiplanet.org/forum/viewtopic.php?f=100&t=27255


from math import *
from descartes import *
from ion import *
from time import *

def cube(s):
  c=[[[-s,s,s],[-s,s,-s]],
      [[-s,s,-s],[-s,-s,-s]],
      [[-s,-s,-s],[-s,-s,s]],
      [[-s,-s,s],[-s,s,s]],
      [[s,s,-s],[s,s,s]],
      [[s,s,s],[s,-s,s]],
      [[s,-s,s],[s,-s,-s]],
      [[s,-s,-s],[s,s,-s]],
      [[-s,s,-s],[s,s,-s]],
      [[-s,-s,-s],[s,-s,-s]],
      [[-s,-s,s],[s,-s,s]],
      [[-s,s,s],[s,s,s]]]
  return c

def axes(xa,ya,za,bgcol):
  R([[[-200,0,0],[200,0,0]]],xa,ya,za,'red',bgcol,False)
  R([[[0,-200,0],[0,200,0]]],xa,ya,za,'green',bgcol,False)
  R([[[0,0,-200],[0,0,200]]],xa,ya,za,'blue',bgcol,False)

def proj(c,col,bgcol,vertices=True):
  for e in c:
    v0,v1=e[0],e[1]
    line(v0[0],v0[1],v1[0],v1[1],col,bgcol,True)
    if vertices:
      fill(int(v0[0])-2,int(v0[1])+2,4,4,'red')

def R(c,xa,ya,za,col,bgcol,vertices=True):
  pc=[]
  for e in c:
    v0,v1=e
    x0,y0,z0=v0
    x1,y1,z1=v1

    pv0=[x0,cos(xa)*y0+-sin(xa)*z0,sin(xa)*y0+cos(xa)*z0]
    x0,y0,z0=pv0
    pv0=[cos(ya)*x0+sin(ya)*z0,y0,-sin(ya)*x0+cos(ya)*z0]
    x0,y0,z0=pv0
    pv0=[cos(za)*x0+-sin(za)*y0,sin(za)*x0+cos(za)*y0,z0]

    pv1=[x1,cos(xa)*y1+-sin(xa)*z1,sin(xa)*y1+cos(xa)*z1]
    x1,y1,z1=pv1
    pv1=[cos(ya)*x1+sin(ya)*z1,y1,-sin(ya)*x1+cos(ya)*z1]
    x1,y1,z1=pv1
    pv1=[cos(za)*x1+-sin(za)*y1,sin(za)*x1+cos(za)*y1,z1]

    pc.append([pv0,pv1])
  proj(pc,col,bgcol,vertices)

def graph():
  
  xa,ya,za=0,0,0
  dxa,dya,dza=0,0,0
  side=30
  draw=True
  while True:
    if keydown(KEY_UP):
      xa+=pi/40
      dxa+=1
      draw=True
    if keydown(KEY_DOWN):
      xa-=pi/40
      dxa-=1
      draw=True
    if keydown(KEY_RIGHT):
      ya-=pi/40
      dya-=1
      draw=True
    if keydown(KEY_LEFT):
      ya+=pi/40
      dya+=1
      draw=True
    if keydown(KEY_RIGHTPARENTHESIS):
      za+=pi/40
      dza+=1
      draw=True
    if keydown(KEY_LEFTPARENTHESIS):
      za-=pi/40
      dza-=1
      draw=True
    if keydown(KEY_PLUS):
      side+=1
      draw=True
    if keydown(KEY_MINUS):
      side-=1
      draw=True
    if draw:
      mulx,divx=dxa//gcd(40,dxa),40//gcd(40,dxa)
      muly,divy=dya//gcd(40,dya),40//gcd(40,dya)
      mulz,divz=dza//gcd(40,dza),40//gcd(40,dza)
      fill(-160,111,320,222,'black')
#    string('θx = '+str(round(xa,3)),-150,100,'black','red')
      if dxa==0:
        string('θx = 0',-150,100,'black','red')
      else:
        string('θx = '+(str(mulx)[:-1]) if abs(mulx)==1 else str(mulx)+'pi/'+str(divx),-150,100,'black','red')
#    string('θy = '+str(round(ya,3)),-150,80,'black','blue')
      if dya==0:
        string('θy = 0',-150,80,'black','green')
      else:
        string('θy = '+(str(muly)[:-1]) if abs(muly)==1 else str(muly)+'pi/'+str(divy),-150,80,'black','green')
#    string('θz = '+str(round(za,3)),-150,60,'black','green')
      if dza==0:
        string('θz = 0',-150,60,'black','blue')
      else:
        string('θz = '+(str(mulz)[:-1]) if abs(mulz)==1 else str(mulz)+'pi/'+str(divz),-150,60,'black','blue')
      string('x',140,100,'red','black')
      string('y',140,80,'green','black')
      string('z',140,60,'blue','black')
      R(cube(side),xa,ya,za,'white','black')
      axes(xa,ya,za,'black')
      draw=False
    sleep(0.05)

graph()

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.