Last time, we calculated the speed and direction for traveling between two user specified points in three seconds. This time, we're going to solve a different problem and the problem we're going to solve is we're going to calculate a velocity vector to move from one point to another in three seconds. So that should sound similar to the previous lecture's problem. A velocity vector is just another way to represent speed and direction, where that information is contained in the x and y components of the velocity vector. Our starting point for our code is just the code from the previous lecture, so I've got my TIME_TO_MOVE constant, I've got my M_PI constant. I'm going to have some variables, I'm going to get user input. I even do what I'm calling a sanity check in this lecture because this is how we calculated speed and direction before. And I'll run the code just to make sure we start off with it working properly. [SOUND] And it works just like it did before. [SOUND] Okay, so let's calculate and print a velocity vector. And I want to have a new data type for a vector so I'm going to come up here to the top of my main function. And we know how to define user-defined types, we use struct. I'm going to call this Vector and I'm going to have two fields or members in this struct. I'm going to have a component for x and I'm going to have a component for y. Remember for convenience, we say typedef struct Vector Vector. That just makes it a little easier for us to declare variables of this data type. So down here calculate and print a velocity vector. It's going to be a vector, and I'm going to call it velocity and this is actually one of those cases where I don't initialize the fields when I declare the variable because I'm going to calculate each separately. And in fact, I know I'm going to want to delta x and delta y, so I'm going to just grab those and I'm going to put them here in their own little section of code. Because I know that velocity.x, the x component of the velocity vector is going to be how far do I have to move in x divided by how much time do I have to move into x. Similarly, the velocity in y is going to be how far do I have to move in y divided by how long do I have to move in y. And now we can print out the velocity vector. So we'll do something new with our output here. We're going to say velocity vector and I'm going to print it out with the x component to two decimal places and the y component to two decimal places inside parentheses. So it's very common when people are printing out vector values to provide them in this format between parentheses with a comma between the x and y values. That means I need to provide two values for us to print velocity.x and velocity.y and I'll add a new line. And I'm actually going to use different numbers for the two points at this point because I want to move six in x and six in y. So that my velocity vector which is really in units per second will actually come out to just be two and two. So that will be an easy check for us. So we'll run our code and I'll make sure I'm moving six units in each and as you can see my velocity vector is 2, 2. Now, we don't know if those speed is correct, we'll certainly know that the direction is correct. So let's actually run again, using our typical 1, 1, 2, 2 points and we can see that our velocity vector is 0.33, 0.33. And that makes sense, because if we have three seconds to go from one to two, we only printed out to two decimal places here. So this makes sense, right? We're doing great. So that's one common way to calculate a velocity vector, is to figure how far do we have to move in x, divided by how long we have to move in x. How far do we have to move in y, divided by how long we have to move and then, the velocity vector is in as I said units per second in this particular case. There is another common way for calculating a velocity vector, and I want to show you that other common way as well. So, I'm going to scroll down here and the first thing I say is calculate a unit direction vector. So, this will also be a vector and I'll call it unit direction. So, a unit vector is a vector whose magnitude or length is one. So we use unit vectors in scientific computations regularly to just indicate a direction without any associated magnitude. So the different components of our unit direction vector are really easy to calculate. It's the difference in x, And the difference in y, Although we're not quite done yet, because we don't know the length of this vector. At this point, our unit direction vector is just a vector from the first point to the second point. So it has a magnitude that is potentially much larger than one. The way we can normalize this vector, which means take the magnitude out of it, and make it a unit vector with a magnitude of one, is we divide both of the components of the vector by the length of the vector. And we know how to calculate the length of this vector, because it's the same Pythagorean Theorem math as we did for calculating the distance. So I'm going to write that code and then we'll come back and talk about it. So the length of our vector at this point is just the x component squared plus the y component squared and take the square root of that. So that's the length. Now to actually normalize our vector, we'll say unit direction.x. And remember that shortcut to divide equal length unit direction y divide equal length. And now the magnitude of our unit direction vector is actually one. So now it finally is a unit vector pointing in the direction from the first point to the second point. The next thing we do to get a velocity vector, is to actually multiply each of these components by speed. Because remember, we are just representing speed and direction in a single vector rather than as two separate variables speed and direction. I know there are two fields of this vector an x and y component. But in scientific calculations and in engineering, we regularly work with vectors, rather than with separate speeds and directions, or directions and magnitudes, if you want to think about it that way. So sort of getting an understanding how vectors work is a really good idea. Okay so, we have to still calculate the velocity vector and we are just going to modify the velocity vector we already have. So velocity in x is just unitDirection in x times speed and velocity in y is just the unitDirection in y times speed. And I know I already printed out the velocity vector up here, so I'll go grab that code and bring it down here. And we'll run one more time and we'll go 1, 1 to 2, 2, and as you can see the velocity vector that we did using the unit direction and speed is identical to the velocity vector that we calculated originally. So that's two ways to calculate a velocity vector. The first way, which might have felt really intuitive to you, the distance in x divided by how long we're traveling that distance, and the same for y. More commonly, we end up with a unit direction vector that we multiply by some magnitude, in this case speed, because we're coming up with the velocity vector. To recap, in this lecture we calculated velocity vectors in two different ways and as a reminder, velocity vectors are a far more common way to capture speed and direction information in science and engineering.