 CoCalc Public Filesunits1819 / graphics / lectures / perspectivedemoshare.sagews
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]]

cube_verts

# 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],cube_verts[edge]],color='black')

# show the plot P
P.show()

# 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/vertex, f*vertex/vertex,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],proj_cube_verts[edge]],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+3, vertex+1,vertex]
trans_cube_verts.append(trans_vertex)

trans_cube_verts

# add the edges of the new translated cube to P
for edge in cube_edges:
P+=line([trans_cube_verts[edge],trans_cube_verts[edge]],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/vertex, f*vertex/vertex,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],proj_trans_cube_verts[edge]],color='red')

P

# 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')

show(P,spin=true)



if 2>1 and 4> 3:
print('yes')