def show_shape(vertex_set): """ Demonstrate the 3D animated transformations of shapes composed of vertices and edges. """ cycle_period= 100 rad_one_deg = math.pi/180.0 rad_angle = 0.0 for i in range(len(faces_tk)): for j in range(len(faces_tk[i])): faces_tk[i][j] = faces_tk[i][j] * matrix_transforms.T_scaling(50 , 50, 50) # Animation by rotation around 3 axes. for i in range (500): # Faces. for i in range(len(faces_tk)): faces_tk[i] = faces_tk[i] * matrix_transforms.T_roty(-rad_angle) faces_tk[i] = faces_tk[i] * matrix_transforms.T_rotx(rad_angle*0.2) faces_tk[i] = faces_tk[i] * matrix_transforms.T_rotz(rad_angle*1.3) view_faces =faces_tk[i] * matrix_transforms.T_translate( 300.0, 300.0, 0.0) fourd_shape_2_twod_line(view_faces , '#ffff00', 1) rad_angle = rad_one_deg * 1.0 canvas_1.update() # This refreshes the drawing on the canvas. canvas_1.after(cycle_period) # This makes execution pause for 100 milliseconds. canvas_1.delete('lines_1')
def scale_shift_3d_object(object_vertices, scale_y, scale_z, x_shift, y_shift): """ This places a 3D piece "object" at a convenient viewing position and at a a convenient size. """ view_vertices = copy.deepcopy(object_vertices) for h in range(len(view_vertices)): for j in range(len(view_vertices[h])): view_vertices[h][ j] = view_vertices[h][j] * matrix_transforms.T_scaling( scale_x, scale_y, scale_z) view_vertices[h][ j] = view_vertices[h][j] * matrix_transforms.T_translate( x_shift, y_shift, 0.0) return view_vertices
w = 0.1 h = 6.0 # Locate the shape-set (for temporary display purposes only) at x_shift, y_shift. x_shift = 200.0 y_shift = 100.0 scale_x = 40.0 scale_y = 40.0 scale_z = 40.0 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # Create (instantiate) objects and position them. assemblage_A = stripface(30, x0, y0, z0, w, h) # Make an instance of the planar object. for h in range( len(assemblage_A)): # Disposition and setup - Shift to a new location. assemblage_A[h] = assemblage_A[h] * matrix_transforms.T_translate( 2.2, 2.2, 0) #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # Dynamic (animated) rotation of the object). def transform_display(object_vertices, rad_angle, kula): """ Transform and then Display. There are two very distinct and separate operations here: A) Rotate the object around the X, Y and Z axes. This alters the position of every vertex permanently. B) Translate and scale each vertex temporarily for viewing. These operations are performed on a separate copy of the rotated object. After each view the copy has no worth - it will be overwritten when the next view is created. """ #1: Strips: Rotate Assemblage. Any other transforms may be applied to the vertices here. Their effect will be permanent. # For this example only rotation by a common angle is considered. for h in range(len(object_vertices)):
new_z = 0.0 new_w = 1 new_vertex = [new_x, new_y, new_z, new_w] homogenous_4d_array.append(new_vertex) homogenous_4d_mat = numpy.matrix(homogenous_4d_array) return homogenous_4d_mat #======================================================================= # CONVERT 2D SHAPES TO 4D MATRICES (NUMPY FORM) fish_2d24d_mat = twod_shape_2_homogeneous_matrix(tiny_fish) # Scale the tiny fish up into a big fish bigfish_mat = fish_2d24d_mat * matrix_transforms.T_scaling(150.0, 200.0, 0.0) # Dynamic rotation of fish_1 around axes within the shape bigfish_view = bigfish_mat * matrix_transforms.T_translate( 300.0, 300.0, 0.0) # Viewing convenience. fourd_shape_2_twod_line(bigfish_view, 'yellow', 4) for i in range(160): bigfish_mat = bigfish_mat * matrix_transforms.T_rotx( rad_angle) # Rotation about X axis. bigfish_mat = bigfish_mat * matrix_transforms.T_roty( rad_angle) # Rotation about Y axis. bigfish_mat = bigfish_mat * matrix_transforms.T_rotz( rad_angle) # Rotation about Z axis. bigfish_view = bigfish_mat * matrix_transforms.T_translate( 300.0, 300.0, 0.0) # Viewing convenience. fourd_shape_2_twod_line(bigfish_view, 'green', 1) rad_angle = rad_one_deg * 5.0 root.mainloop()
twod_line.append(bbb[i][1]) canvas_1.create_line(twod_line, width=line_width, fill=kula) return twod_line #======================================================================== # Convert 2D set SET to 4D matrices (NUMPY FORM). fish_4d_set_mat = [] for set in range(len(fish_set)): temp = twod_shape_2_homogeneous_matrix(fish_set[set]) fish_4d_set_mat.append(temp) # Move origin to center-of-rotation(cor). for set in range(len(fish_set)): fish_4d_set_mat[ set] = fish_4d_set_mat[set] * matrix_transforms.T_translate( -300.0, -330.0, 0.0) # Dynamic rotation of fish_1 around axes within the shape for i in range(100): for set in range(len(fish_4d_set_mat)): fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_rotx( rad_angle) # X rotation. fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_roty( rad_angle) # Y rotation. fish_4d_set_mat[set] = fish_4d_set_mat[set] * matrix_transforms.T_rotz( rad_angle) # Z rotation. view_fish_set = fish_4d_set_mat[set] * matrix_transforms.T_translate( 300.0, 280.0, 0.0) fourd_shape_2_twod_line(view_fish_set, kulas[set], 1) rad_angle = rad_one_deg * 10.0