#---------------------------------------------------------------------- # Program to implement 3D Orthographic Projection # # Copyright (C) April 13, 2018 -- Dr. William T. Verts #---------------------------------------------------------------------- def INT (N): return int(round(N)) #---------------------------------------------------------------------- # Projeection routines #---------------------------------------------------------------------- Origin2D = [0,0] # Location of the <0,0,0> point on the canvas Scale2D = 1.0 # Number of Pixels (screen coordinates) per Unit (world coordinates) Cosine30 = 0.866 # math.cos(math.radians(30.0)) # Projection angle Sine30 = 0.5 # math.sin(math.radians(30.0)) # Projection angle def Project3D (P3D): # Projects P3D [X,Y,Z] (world) into 2 dimensions [X,Y] (screen) global Origin2D,Scale2D X = Origin2D[0] + (P3D[0] + P3D[2] * Cosine30) * Scale2D Y = Origin2D[1] - (P3D[1] + P3D[2] * Sine30) * Scale2D return [X,Y] def SetOrigin2D(P2D): global Origin2D Origin2D = P2D return def SetScale2D (N): global Scale2D Scale2D = N return #---------------------------------------------------------------------- # Geometry routines that deal with [X,Y,Z] and [X,Y] points #---------------------------------------------------------------------- def addLine3D (Canvas, P0, P1, NewColor=black): # P0 and P1 are [X,Y,Z] 3D points in World coordinates PP0 = Project3D(P0) # PP0 and PP1 are [X,Y] 2D points in Canvas coordinates PP1 = Project3D(P1) addLine(Canvas, INT(PP0[0]), INT(PP0[1]), INT(PP1[0]), INT(PP1[1]), NewColor) return #---------------------------------------------------------------------- # Test Driver Program #---------------------------------------------------------------------- def Main(): Canvas = makeEmptyPicture(600,400) SetOrigin2D([300,200]) SetScale2D(10.0) addLine3D(Canvas, [-10,0,0], [+10,0,0], black) addLine3D(Canvas, [0,-10,0], [0,+10,0], blue) addLine3D(Canvas, [0,0,-10], [0,0,+10], red) show(Canvas) return