Home > Articles

  • Print
  • + Share This
This chapter is from the book

Points, Vectors, and Vector Operations

The next concept we cover is the point. A point is a coordinate in space. Let’s use a point as an example: (3, 4, 0). The first number is the point’s x coordinate, the second number is the y coordinate, and the third is the z coordinate. Point B shown in Figure 4.2 describes a location that is three units to the right and four units up from the origin.

Figure 4.2

Figure 4.2 A vector between the origin at Point A and another location at Point B

Vectors are expressed similarly to points but are different. A vector is defined as having both a magnitude and a direction. Let’s take our previous point in Figure 4.2 but change it to a vector: (3, 4, 0). It’s still expressed the same way, but there is a difference. Instead of describing a specific point in space, (3, 4, 0) describes the movement between one point and another. For this, you use the Pythagorean theorem. The first point in this vector is five units away from the second point in the vector because the square root of three squared plus four squared is four. This is covered in more detail later in this chapter. Because a square root can only be positive as it is the addition of two squared numbers, the vector is moving in a positive direction three units over and four units up. This vector can originate anywhere; it doesn’t matter. All it is describing is movement from one point to another.

Both vectors and points are contained in a float4 vector type. At first, this doesn’t seem to make sense, because points and vectors have only three coordinates. So what is the fourth component for? The fourth component differentiates whether the float4 is a point or a vector. Points have the value 1 and vectors have the value 0 as the last value in a float4.

Several operations can be performed on both points and vectors. Points can be subtracted to create vectors. Points cannot be added together. Vectors can be multiplied by scalars to scale them up and down. In the previous paragraph, you learned that points have a final value of 1. Any legal operation done on points and vectors will result in the final coordinate value equaling either 1 or 0. In this overview, we focus on two special vector operations used extensively in computer graphics: dot product and cross product.

The dot product of two vectors is found by multiplying the x component of each vector and adding it to the product of the y component of each vector.

ab=axbx+ayby=|a||b|cos(θ)

The dot product also gives the cosine of an angle between two vectors that both have a length of 1. This simple explanation doesn’t explain what you can do with the dot product or why it’s relevant to your journey as a graphics programmer. At its heart, the dot product is a way of measuring the impact one vector has on another.

A good example of a vector effect is in the video game Mario Kart. In Mario Kart, there are areas of the track that give you a speed boost. If your cart isn’t moving, the speed boost doesn’t do anything because the dot product augments what you’re already doing. The speed boost only helps with forward momentum, so if you’re primarily moving sideways, it won’t help you very much.

Let’s say you get a speed boost of two in the forward direction, but you’re not moving forward. Your forward vector is (0, 0). The speed boost vector is (0, 2). The dot product of these vectors is (0 * 0) + (0 * 2), which is still zero. However, if you’re moving forward at one unit but sideways at three units, the dot product has no effect on your sideways movement. In this case, the dot product would be (3 * 0) + (1 * 2), which equals 2. It doesn’t matter how fast you are moving sideways—it will never affect the final result because the speed boost targets only one axis of movement.

The dot product in graphics programming is used, among other things, to determine how light interacts with an object. For lighting, it finds the cosine of the angle created by the surface normal and the direction the ray of light is coming from. This is explained further as you read through this chapter.

The cross product is related to the dot product, but it is different. The dot product is concerned with the degree of similarity between magnitudes within the same dimension that are perpendicular to one another. It compares x-values to x-values. It doesn’t care what the y-value or the z-value is. The cross product does. The cross product is used to find the axis of rotation between two vectors, as shown in Figure 4.3.

Figure 4.3

Figure 4.3 How to calculate the cross product

  • + Share This
  • 🔖 Save To Your Account