# Learning Objectives

Create a class that defines a vector object in homogeneous coordinates, and provides functions for retrieving as well as displaying information about the vector. Also overload operators to accomplish vector arithmetic.

# Introduction

Vectors are a convenient and concise way of representing both location and motion in either 2D or 3D space. The attributes of a vector, and operations performed on vectors, are basic computational necessities. For this reason, a vector class that provides this information will be extremely useful. In this lab, you shall write a class to work with vectors, and in future labs, you will extend the functions of this class.

Separate classes for 2D and 3D vectors are not necessary. The xy-plane is already embedded in the 3D space, and this plane is simply a copy of the 2D space. Therefore, to represent a 2D vector in a 3D space, it is sufficient to set the third coordinate of the 3D space, z, equal to zero. (This will shorten our work for class, but may not always be the preferred solution in an actual game.)

It is actually easier to work with 3D vectors that have four components instead of three. Certainly, this is not obvious now, but later it will allow us to represent transformations of objects using matrix multiplication. When 4D vectors are used for 3D space, the coordinates are called homogeneous coordinates. We shall refer to the fourth coordinate as the w-coordinate, and its value will always be equal to one.

In this lab, you will create a class that will define vectors and performs some simple vector computations, and any program may call upon the functions of this class. Most object oriented programming languages already have a class named vector, so you need to use a different name for your class (vector3D, for example).

The Vector3D Class

Your Vector3D class should have the following functions

- A constructor Vector3D. When the constructor is called it should set default values of <0,0,0,1> for the vector.
- A function called
SetRectGivenRectthat will set the rectangular coordinates for the vector given an input of rectangular coordinates in both 2D and 3D.- A function called
printRectthat will display the rectangular coordinates to the screen in an easily readable fashion.- A function called
printMagthat will display the magnitude to the screen in an easily readable fashion.- A function called
getMagthat returns the magnitude of the vector.- A function called
getMagSqthat returns the magnitude of the vector squared.- A function called
getXthat returns the x-coordinate.- A function called
getYthat returns the y-coordinate.- A function called
getZthat returns the z-coordinate.Your vector class should store the following variables.

- x
- y
- z
- w
You should overload operators to accomplish the following

- Vector Addition
- Vector Subtraction
- Scalar Multiplication of a Vector
- Normalization of a Vector

# Resources

# Preparatory Thoughts

- Are there any computations that you will be coding that will require error trapping?
- What will be the 4D homogeneous coordinates of the 3D vector (x, y, z)? Also, what will be the 4D homogeneous coordinates of the 2D vector (x, y)?
- What modifications are needed so that your class can also handle the instantiation of 2D vectors?

# Laboratory Procedures

1. Create a program that asks the user to enter the rectangular components for two vectors (either 2D or 3D) along with a scalar

- Print both vectors to the screen accompanied by their magnitudes and magnitudes squared.
- Print the sum of the two vectors accompanied by the magnitude.
- Print the difference of the two vectors accompanied by the magnitude
- Print the scalar times the first vector along with the magnitude
- Print the first vector as the product of its magnitude and unit direction.

2. Execute the program for your instructorâ€™s verification. **Instructor Initials:**

3. Save your class files! You will use them again in future labs (including the very next lab).

# Postlab Questions

- In game development, what could be the downside to using 3D vectors with $v_z=0$ to represent 2D vectors?
- Why use operator overloading instead of function calls to accomplish vector arithmetic?