#---------------------------------------------------------------------- # Program to support simple orthographic 3D projection # # Copyright (C) October 2018 -- Dr. William T. Verts #---------------------------------------------------------------------- Sine = 0.5 # 30 degrees Cosine= sqrt(3) / 2.0 # 30 degrees Scale2D = 10.0 Origin2D = [0,0] def Project3D (P3D): # P3D is 3D point, list [X,Y,Z] global Scale2D,Origin2D X = Origin2D[0] + (P3D[0] + P3D[2] * Cosine) * Scale2D Y = Origin2D[1] - (P3D[1] + P3D[2] * Sine) * Scale2D Result = [X,Y] return Result # Result is a 2D point, list [X,Y] def SetOrigin2D(P2D): global Origin2D Origin2D = P2D return def SetScale2D(NewScale): global Scale2D Scale2D = NewScale return def INT(N): return int(round(N)) def addLine3D(Canvas, P0, P1, NewColor=black): # P0 and P1 are 3D points [X,Y,Z] P0_2D = Project3D(P0) P1_2D = Project3D(P1) addLine(Canvas, INT(P0_2D[0]), INT(P0_2D[1]), INT(P1_2D[0]), INT(P1_2D[1]), NewColor) return def addPoint(Q0,Q1): X = Q0[0] + Q1[0] Y = Q0[1] + Q1[1] Z = Q0[2] + Q1[2] return [X,Y,Z] def addHouse(Canvas, HouseOrigin, NewColor): # HouseOrigin = [X,Y,Z] 3D point def Line (P0,P1): addLine3D(Canvas, addPoint(HouseOrigin, P0), addPoint(HouseOrigin, P1), NewColor) return Line([0,0,0], [6,0,0]) Line([0,0,0], [0,3,0]) Line([6,0,0], [6,3,0]) Line([0,3,0], [3,4,0]) Line([3,4,0], [6,3,0]) Line([0,0,5], [6,0,5]) Line([0,0,5], [0,3,5]) Line([6,0,5], [6,3,5]) Line([0,3,5], [3,4,5]) Line([3,4,5], [6,3,5]) Line([0,0,0], [0,0,5]) Line([0,3,0], [0,3,5]) Line([3,4,0], [3,4,5]) Line([6,3,0], [6,3,5]) Line([6,0,0], [6,0,5]) return def Main(): Canvas = makeEmptyPicture(800,600) SetOrigin2D([400,300]) SetScale2D(20.0) addLine3D(Canvas, [10,0,0], [-10,0,0], red) addLine3D(Canvas, [0,10,0], [0,-10,0], green) addLine3D(Canvas, [0,0,10], [0,0,-10], blue) for X in range(-100,+100, 8): for Z in range(-100, +100, 8): addHouse(Canvas, [X,0,Z], black) show(Canvas) return