concept line segment in category python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.
Figure 2.8: The dinosaur’s points with a line segment connecting the first two points (6, 4) and (3, 1)
![]()
The line segment is actually the collection consisting of the points (6, 4) and (3, 1) as well as all of the points lying on the straight line between them. The draw function automatically colors all of the pixels at those points blue. The Segment class is a useful abstraction because we don’t have to build every segment from the points that make up our geometric object (in this case, the dinosaur). Drawing 20 more segments, we get the complete outline of the dinosaur (figure 2.9).
With the techniques from the last two chapters and a little creativity, you can render any 2D or 3D figure you can think of. Whole objects, characters, and worlds can be built from line segments and polygons defined by vectors. But, there’s still one thing standing in between you and your first feature-length, computer-animated film or life-like action video game—you need to be able to draw objects that change over time.
It’s less obvious, but a linear combination like 0.25u + 0.75v also lies on the line segment between u and v (figure 4.26). Specifically, this is the point 75% of the way from u to v. Likewise, 0.6u + 0.4v is 40% of the way from u to v, and so on.
Figure 4.26: The point 0.25u + 0.75v lies on the line segment connecting u and v, 75% of the way from u to v. You can see this concretely with u = (-2, 2) and v = (6, 6).
![]()
In fact, every point on the line segment between two vectors is a “weighted average” like this, having the form su + (1 − s)v for some number s between 0 and 1. To convince you, figure 4.27 shows the vectors su + (1 − s)v for u = (-1, 1) and v = (3, 4) for 10 values of s between 0 and 1 and then for 100 values of s between 0 and 1.
Figure 4.27: Plotting various weighted averages of (-1, 1) and (3, 4) with 10 values of s between 0 and 1 (left) and 100 values of s between 0 and 1 (right)
![]()
![]()
The key idea here is that every point on a line segment connecting two vectors u and v is a weighted average and, therefore, a linear combination of points u and v. With this in mind, we can think about what a linear transformation does to a whole line segment.
Any point on the line segment connecting u and v is a weighted average of u and v, so it has the form s · u + (1 − s) · v for some value s. A linear transformation, T, transforms u and v to some new vectors T(u) and T(v). The point on the line segment is transformed to some new point T(s · u + (1 − s) · v) or s · T(u) + (1 − s) · T(v). This is, in turn, a weighted average of T(u) and T(v), so it is a point that lies on the segment connecting T(u) and T(v) as shown in figure 4.28.
Figure 4.28: A linear transformation T transforms a weighted average of u and v to a weighted average of T(u) and T(v). The original weighted average lies on the segment connecting u and v, and the transformed one lies on the segment connecting T(u) and T(v).
![]()
Because of this, a linear transformation T takes every point on the line segment connecting u and v to a point on the line segment connecting T(u) and T(v). This is a key property of linear transformations: they send every existing line segment to a new line segment. Because our 3D models are made up of polygons and polygons are outlined by line segments, linear transformations can be expected to preserve the structure of our 3D models to some extent (figure 4.29).