Last time, we wrote a C program to calculate the distance between two user-specified points. This time, we're actually going to do two calculations. We're still going to use two user-specified points. The first calculation will be to figure out at what speed we'd have to travel from the first point to the second point to make our total travel time a specific number, a specific amount of time. In this case, we'll use three seconds. The other thing we'll calculate is in what direction and more precisely, what degrees we would have to travel to get from the first point to the second point. So let's go sling some C code. Now, you probably already know this, but to calculate the speed, we're going to need to know the distance between the two points so that we can figure out how fast we have to move between those two points to make the movement take three seconds, and part of being a programmer is recognizing parts of problems or even complete problems that you've solved before so that you can reuse them in other places. So in the previous lecture, we calculated distance, so we're now going to need to do that here. I've already pound included math.h in my solution and I've copied and pasted all the work we did last time except for actually printing out the distance, because we don't need to print out the distance. What we're going to have to do is, we're going to have to calculate distance, speed, and direction between the two points, and then we will print out speed and direction. So we know the distance between the two points and we know that our speed will be the distance we need to travel divided by the time. So this is our standard, rate times time equal distance equation, where speed is rate. So if rate times time equal distance, then rate equal distance divided by time. Let's not use three here. Let's actually pound define a constant called the time to move, and we'll set that to three because we're doing it in three seconds and that just makes our code easier to read. So down here, instead of using three, I'll use time to move, and let's print out the speed right now. So I'll do a printf, labeling my output with a format specifier for a float, that's to two decimal points, and that will be speed. I'll want a new line after this, and I actually know I'll want the new line between my input and output. So I'll just add that now also, and let's run it. I'm going to use the same points I used from last time, and our speed comes up as 0.47. So we know the distance between these two points is 1.41 and we know we have three seconds to move 1.41, and 1.41 divided by three is 0.47. So we know we got the right answer. Just like I said last time, if you're going to test your code to make sure it's doing the math properly, then you have to make sure that you know the right answer, so you can check to make sure your code is generating the correct answer. For direction, we're going to need to use some trigonometry and in fact, for direction, the order in which we do this calculation matters. So when we were squaring these numbers, it didn't matter. But if we're going to move from point one to point two, we need to do this math in the opposite direction, and because we're going to be calculating DeltaX and DeltaY for the direction as well, let's declare a couple of more variables, and I'm going to grab this and put it here. But, as I just said, we need to move from point one to point two. So we have to calculate the Delta in this direction. If that doesn't make sense to you, go ahead and draw a picture and you'll see why that's true. I can now change this code to say deltaX, and in fact, that's a little easier to read. So I'm glad we're making this change. We'll do deltaY, and I'll just type it pointTwoY minus pointOneY and I'll replace this with deltaY, and in fact, now we could put this all on one line because it fits just fine. So now, it's clear as we do our direction that we are using the same deltaX and deltaY as we are for distance, and that's just a little clear as you read the code. Now, I said we needed to use trigonometry, which should imply that we ought to go look at math.h in the C standard library to see what we can find there. So let's go do that. Well, it turns out that the C standard library is our friend as usual and particularly math.h is our friend. So if we look at the trigonometric functions, you'll see that there are a couple for arctangent. So arctangent, let me grab atan2 because that one will actually work properly based on the signs of the x and y differences that we come up with, and that's going to matter to us because we care about the direction. So we're actually going to get an answer in the range of negative Pi to positive Pi. You can see I've highlighted that here on the web page, and that really is negative 180 degrees to 180 degrees. So we will have to convert from radians to degrees after we get the arctangent two. As usual, we have an f, we have regular one, we have an l, so we'll use atan2f and that will give us the angle between the two points. Now, arctangent two, in case you're unfamiliar with it, is opposite over adjacent, and so what that means is we need the y as our first argument as it shows right here, and we need the x as the second argument as it shows right here. So we're going to have to have our deltaY, a change in y first and then a change in x. So we know we're going to do atan2f, and we also know from the documentation in this pop-up help that we need to provide the deltaY first and then deltaX. Now, we know atan2f returns the degrees in radians, but let's print it out anyway and then we can fix that to be in degrees instead. I'll do it to two decimal points again, and I'll actually provide units as well in this case. We didn't provide units for speed because we don't have specific units for speed in this problem, but we certainly know our direction is going to be in degrees. Although we also know it's not in-degrees yet. So let's run it again. So 0.79 degrees is obviously not correct here. In fact, the direction between these two points is exactly 45 degrees. So I'm printing out radians instead. Let's go ahead and fix that. To do that, I'll take our current direction, which is in radians and I'll multiply it by 180 divided by, there's actually a constant in math.h called M_PI. Now, I'm getting red squiggles here, and it turns out that different environments grab this and different environments don't. So I'm actually going to add a definition for M_PI, up above in my code. So up here, I'll show you that new stuff I'm going to add and here you go. So basically what this says is, if M_PI is not defined because it might be defined, then go ahead and define M_PI as, this is PI to a certain number of decimal places. I like to use M_PI down here because many see environments do support M_PI. So coding using M_PI is the right way to do it consistently across environments. It's just that in Visual Studio, we need to add to this to actually get it to compile. I'm going to go ahead and run the code to make sure it works, and as you can see, we get 45 degrees. One more thing I want to show you before we stop is, I can write this line of code in a different way. Instead of saying it this way, I can say, direction multiply equal 180 divided by M_PI. So this is just a shortcut. It's called syntactic sugar. It makes it easier to write the code, and for experienced C programmers, it's easier to read the code as well. So we can do this with plus equals, and minus equals, and divide equals, and multiply equals, and even percent equals for integers. So this is just a shortcut. If you're uncomfortable using this, you don't have to. But over time as you develop your C programming skills, you're going to find that you actually like to use this. We will run our code one more time and it works properly, and that's our solution to this problem. To recap. In this lecture, we calculated how fast we would need to move to get from one point to another in a particular period of time, and we also calculated the angle or the degrees at which we'd have to move to get from the first point to the second point, remembering that zero is to the right and angle increases as we rotate counterclockwise. This is pretty standard for scientific computations. We also get some more practice programming, both with basic math and with the functions in math.h.