CoCalc Public Filesunits1819 / graphics / lectures / perspectivedemoshare.sagewsOpen in with one click!
Author: Killian O'Brien
Views : 79
# Execute each group of commands by pressing Shift+Enter on the cells. # Click and drag the 3-d plots to view them from different angles.
# set up symbolic variables x, y, z (in fact we only need x really for using the implicit_plot3d command I think) var('x,y,z')
# set the distance of projection plane from origin and draw the projection plane (in blue) and the origin f=2 P=implicit_plot3d(z==f, (x, -3, 3), (y, -3,3), (z, -3,3),opacity=0.5) P+=point([0,0,0])
# Executing the command 'P' will show us the 3-D plot P show(P)
# define the vertices of a cube cube_verts=[[1,-1,5],[1,1,5],[-1,1,5],[-1,-1,5],[1,-1,7],[1,1,7],[-1,1,7],[-1,-1,7]]
# define the edges of the cube as pairs of vertex labels cube_edges=[[0,1],[1,2],[2,3],[3,0],[0,4],[1,5],[2,6],[3,7],[4,5],[5,6],[6,7],[7,4]] cube_edges
# add the edges of the cube to the plot P for edge in cube_edges: P+=line([cube_verts[edge[0]],cube_verts[edge[1]]],color='black')
# show the plot P
# apply the perspective projection transformation to the vertices of the cube # storing the resulting vertices in a new array proj_cube_verts proj_cube_verts=[] for vertex in cube_verts: proj_vertex=[f*vertex[0]/vertex[2], f*vertex[1]/vertex[2],f] proj_cube_verts.append(proj_vertex)
# confirm that the projected vertices look right proj_cube_verts
# add the lines edges joining the projected vertices to the 3-d plot in green for edge in cube_edges: P+=line([proj_cube_verts[edge[0]],proj_cube_verts[edge[1]]],color='green')
# look at the resulting plot # An interesting experiment is to orient the 3-d plot so that the z-axis is going 'into' the screen. # Then zoom into the plot (use mouse wheel) keeping the origin in the middle of the diagram. # Eventually the origin will 'disappear', i.e. you will have just moved past it. At this precise moment # your 'eye' is at the origin. You will notice that the green perspective projection of the real cube exactly lines up # with the real cube visible through the transparent projection plane. P
# creates a second set of cube vertices by translating the original ones 3 units # in the x direction and 1 in the y direction. Store the results in a new array trans_cube_verts trans_cube_verts=[] for vertex in cube_verts: trans_vertex=[vertex[0]+3, vertex[1]+1,vertex[2]] trans_cube_verts.append(trans_vertex)
# add the edges of the new translated cube to P for edge in cube_edges: P+=line([trans_cube_verts[edge[0]],trans_cube_verts[edge[1]]],color='black')
# You should now see two cubes in the plot P
# Apply the perspective projection to this new cube proj_trans_cube_verts=[] for vertex in trans_cube_verts: proj_vertex=[f*vertex[0]/vertex[2], f*vertex[1]/vertex[2],f] proj_trans_cube_verts.append(proj_vertex)
# Add the projected edges of the second cube, in red for edge in cube_edges: P+=line([proj_trans_cube_verts[edge[0]],proj_trans_cube_verts[edge[1]]],color='red')
# Add all the projector lines to P for vertex in cube_verts: P+=line([[0,0,0],vertex],color='green') for vertex in trans_cube_verts: P+=line([[0,0,0],vertex],color='red')
if 2>1 and 4> 3: print('yes')