FREELessons: 13Length: 1.2 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.4 Calculating Distance

In this lesson, we'll implement the selection of the second measuring point. Then we can calculate the distance!

3.4 Calculating Distance

Hi, and welcome back to Get Started In Augmented Reality for iOS. In this lesson, we will finish the measuring part of the project, and also calculate the distance. This means we have to implement the else case here. Before we do that, I'm going to add a struct that is called Measurement And contains a start point and an end point. It also gets an init function that will set both of these values. Back in the LS clause, I'm going to create a new measurement containing the active starting point and the current point. Afterwards I can set is measuring to false. Just for reference we don't need this here, I'm going to store measurements in a list of measurements. This property also need to be created and initialized. And of course, it's not a constant but a variable. Now, it's time to turn on the point again with addMeasurementPoint. The UI's already set up to show and hide the info label, but the touchesBegan function doesn't care. So let's add the condition that the thankfully should not be hidden to the god clause. So the user can't add points when we don't have tracking. Let's build, and run, and try it out. Right now the info label doesn't update and reflect the state of the application truly. So back in the project. I'm going to update the label and in the if, else statement to either say start or stop measuring. The final part of this lesson is about calculating the distance. We already have the structure for it, the measurement struct. I just need to add another property, the distance, and calculate the distance in the initializer. I'm going the explain how to do that in terms of vector mathematics, because I find it more elegant than just throwing numbers around. First, I want to get the vector that goes from the start to the end point. I can calculate that by subtracting the end vector from the start vector, and that can be done by subtracting the components separately. After we have that, we can simply calculate the length of the vector, our distance, by calculating the square root of the sum of the components squared. So vector x times vector x, vector y times vector y, and vector c times vector c. Of course not as arguments but using plus signs. For now I can display it in the console with a simple print statement. So let's build and run again and try to take some measurements from the references I set up. The first test shows 29.3 centimeters. Not bad. It should have been about 30. So let's try something longer, the measuring tape. I'm aiming for three meters. Well, it almost got it. It is at 2 meters and 93 centimeters. Not perfect, but close. Now, I can add points as much as I like, and create measurement paths along the way. All that's left to do in the next lesson is to display the line between the points, and also show the distance next to it. See you there.

Back to the top