concept teapot in category python

appears as: teapot, The teapot, teapot, teapots
Math for Programmers: 3D graphics, machine learning, and simulations with Python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.

Figure 4.3: Rotating the teapot by 1° at a time, 45 times in a row, beginning with the upper left-hand corner

In the next few examples, we modify the vectors defining the teapot and then re-render it so that we can see the geometric effect. As a first example, we can scale all of the vectors by the same factor. The following function, scale2, multiplies an input vector by the scalar 2.0 and returns the result:

from vectors import scale 
  def scale2(v): 
      return scale(2.0, v)

This scale2(v) function has the same form as the transform(v) function given at the top of this section: when passed a 3D vector as input, scale2 returns a new 3D vector as output. To execute this transformation on the teapot, we need to transform each of its vertices. We can do this triangle by triangle. For each triangle that we use to build the teapot, we create a new triangle with the result of applying scale2 to the original vertices:

original_triangles = load_triangles() #1 
  scaled_triangles = [ 
      [scale2(vertex) for vertex in triangle] #2 
      for triangle in original_triangles #3 
  ]

Now that we’ve got a new set of triangles, we can draw them by calling draw_model(scaled_triangles). Figure 4.5 shows the teapot after this call, and you can reproduce this in by running the file scale_teapot.py in the source code.

Figure 4.5: Applying scale2 to each vertex of each triangle gives us a teapot that is twice as big.
Figure 4.6: The teapot is bigger and moved to the left as we hoped!

Different scalar multiples change the size of the teapot by different factors, and different translation vectors move the teapot to different positions in space. In the exercises that follow, you’ll have a chance to try different scalar multiples and translations, but for now, let’s focus on combining and applying more transformations.