• No results found

Chapter 5 Operations on Vectors

5.12 Linear Algebra Identities

Figure 5.18 lists some useful vector identities. Many of these identities are obvious, but they are listed here just for the sake of completeness. All of these identities can be derived from the defini- tions given in earlier sections.

Identity Comments

Commutative property of vector addition. Definition of vector subtraction.

Associative property of vector addition. Associative property of scalar multiplication. Scalar multiplication distributes over vector addition. Multiplying a vector by a scalar scales the magnitude by a factor equal to the absolute value of the scalar.

Figure 5.16: Clockwise turn Figure 5.17: Counterclockwise turn

In a left-handed coordinate system,a×b(not shown) points toward you. In a right-handed coordinate system, a×b points away from you.

In a left-handed coordinate system, a×b (not shown) points away from you. In a right-handed coordinate system, a×b points toward from you.

Identity Comments

The magnitude of a vector is nonnegative.

The Pythagorean theorem applied to vector addition. Triangle rule of vector addition. (No side can be longer than the sum of the lengths of the other two sides.) Commutative property of dot product.

Vector magnitude defined using dot product. Associative property of scalar multiplication with dot product.

Dot product distributes over vector addition and subtraction.

The cross product of any vector with itself is the zero vector. (Because any vector is parallel with itself.) Cross product is anti-commutative.

Negating both operands to the cross product results in the same vector.

Associative property of scalar multiplication with cross product.

Cross product distributes over vector addition and subtraction.

The dot product of any vector with the cross product of that vector and another vector is zero. (The cross product produces a perpendicular vector, and the dot product of perpendicular vectors is zero.)

Figure 5.18: Table of vector algebra identities

5.13 Exercises

1. Evaluate the following vector expressions: a.

b. c. d. e.

2. Normalize the following vectors: a.

b.

3. Evaluate the following vector expressions: a.

b.

4. Compute the distance between the following pairs of points: a.

b.

5. Evaluate the following vector expressions: a.

b.

6. Compute the angle between the vectors [1, 2] and [–6, 3]. 7. Given the two vectors

8. Compute the value of

9. A man is boarding a plane. The airline has a rule that no carry-on item may be more than two feet long, two feet wide, or two feet tall. The man has a very valuable sword that is three feet long. He is able to carry the sword on board with him. How is he able to do this? What is the longest possible item that he could carry on?

10. Verify Figure 5.7 on page 56 mathematically.

11. Is the coordinate system used in Figure 5.13 on page 63 a left-handed or right-handed coordinate system?

12. Assume that Texas is flat. A minute of latitude is approximately 1.15 miles in length. At the authors’ latitude (see Section 3.2.1), a minute of longitude is approximately 0.97 miles in length. There are 60 minutes in one degree of latitude or longitude. How far apart are the authors?

68

Chapter 5: Operations on Vectors

TEAM

FLY

A Simple 3D Vector

A Simple 3D Vector

Class

The preceding chapters have focused on the theory of 3D math. In this chapter, we’ll turn for the first time to the practice of 3D math by introducing a C++ class to represent a 3D vector.

6.1 Class Interface

Good class design always begins with the question, “What operations do I want to perform using this class, and what values do I want to perform them on?” In other words, “What do I want this class todo?” We know that we will use this class to store thex,y, andzvalues for a 3D vector. We also know we will need some basic operations, such as the ability to:

n Access the individual components of the vector (x,y, andz).

n Assign one vector to another.

n Compare two vectors for equality.

From Chapter 5 we know that we will need to perform the following vector operations:

n Set a vector to the zero vector.

n Negate a vector.

n Compute the magnitude of a vector.

n Multiply or divide a vector by a scalar.

n Normalize a vector.

69

This chapter puts theory into practice with a simple C++ vector class, Vector3. It is divided into three main sections.

n Section 6.1 discusses the operations we want to implement inVector3.

n Section 6.2 contains the complete code listing forVector3.

n Section 6.3 discusses some of the design decisions embodied in classVector3(and all of the other C++ classes in this book).

n Add or subtract two vectors.

n Compute the distance between two points (expressed as vectors).

n Compute the dot product of two vectors.

n Compute the cross product of two vectors.

6.2 Class Vector3 Definition

Below is the complete listing for Vector3.h, which contains the definition of classVector3:

Listing 6.1: Vector3.h

///////////////////////////////////////////////////////////////////////////// //

// class Vector3 — a simple 3D vector class //

/////////////////////////////////////////////////////////////////////////////

class Vector3 { public:

// Public representation: Not many options here.

float x,y,z;

// Constructors

// Default constructor leaves vector in // an indeterminate state

Vector3() {}

// Copy constructor

Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z) {}

// Construct given three values

Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}

// Standard object maintenance

// Assignment. We adhere to C convention and // return reference to the lvalue

Vector3 &operator =(const Vector3 &a) { x = a.x; y = a.y; z = a.z; return *this;

}

// Check for equality

bool operator ==(const Vector3 &a) const { return x==a.x && y==a.y && z==a.z; }