1 00:00:00,780 --> 00:00:04,780 Now that we know how to use the static square root method in the math class, 2 00:00:04,780 --> 00:00:08,440 let's use it to finish coding up the DistanceTo method, in the Point class. 3 00:00:08,440 --> 00:00:13,470 The last thing we needed to do to complete the Cartesian distance formula, 4 00:00:13,470 --> 00:00:17,630 is to add these two values together and take the square root. 5 00:00:17,630 --> 00:00:19,830 We can actually do that in a single statement. 6 00:00:21,090 --> 00:00:29,165 We can say Math.Sqrt of xDiffSquared + yDiffSquared. 7 00:00:32,325 --> 00:00:33,580 There we go. 8 00:00:33,580 --> 00:00:35,610 We've coded up the cartesian distance formula. 9 00:00:36,780 --> 00:00:38,390 Now we just need to return the result. 10 00:00:41,600 --> 00:00:43,700 Let's test our method in main. 11 00:00:43,700 --> 00:00:49,104 Let's clear out this code and then get the distance between 12 00:00:49,104 --> 00:00:54,075 this point at (4, 2) and the coordinates at (5, 13 00:00:54,075 --> 00:00:59,071 5) and print the result using console.rightline. 14 00:01:02,871 --> 00:01:05,430 Now let's open the console to compile and run this. 15 00:01:13,097 --> 00:01:15,550 Looks like we got a compiler error. 16 00:01:15,550 --> 00:01:18,780 It says the name Math does not exist in the current context. 17 00:01:18,780 --> 00:01:22,690 And this is back in the point.cs file, line 22. 18 00:01:22,690 --> 00:01:23,570 So let's go back there. 19 00:01:25,160 --> 00:01:25,740 I see. 20 00:01:25,740 --> 00:01:30,248 We forgot to add the using System. 21 00:01:34,829 --> 00:01:35,530 All right. 22 00:01:35,530 --> 00:01:36,150 Let's run this again. 23 00:01:38,880 --> 00:01:39,990 Here's another compiler error. 24 00:01:39,990 --> 00:01:43,750 It says cannot implicitly convert type double to int. 25 00:01:44,840 --> 00:01:50,222 And this is in point.cs, line twenty four. 26 00:01:50,222 --> 00:01:54,610 Again they're Math.Sqrt. 27 00:01:54,610 --> 00:01:57,410 This error must be saying that it can't convert the double 28 00:01:57,410 --> 00:02:00,540 that's returned from Math.Sqrt to an integer. 29 00:02:01,900 --> 00:02:05,480 Remember that the documentation for Math.Sqrt said it returns a double? 30 00:02:06,970 --> 00:02:10,710 This is one of those rare times when we don't care about the decimal value 31 00:02:10,710 --> 00:02:11,270 of the result. 32 00:02:12,760 --> 00:02:15,150 We're only using whole numbers in our game. 33 00:02:15,150 --> 00:02:19,060 An invader can't be halfway between two grid squares. 34 00:02:19,060 --> 00:02:21,990 Truncating the decimal value we get from this formula 35 00:02:21,990 --> 00:02:25,050 gives us the correct distance in discrete units. 36 00:02:25,050 --> 00:02:27,890 Remember that when we cast a double to an integer 37 00:02:27,890 --> 00:02:30,460 it removes the decimal portion of the value. 38 00:02:30,460 --> 00:02:32,460 This is called truncation. 39 00:02:32,460 --> 00:02:34,200 And that's what we want to do. 40 00:02:34,200 --> 00:02:35,490 So we can just type int. 41 00:02:35,490 --> 00:02:37,770 in parentheses here to get the desired effect. 42 00:02:39,390 --> 00:02:43,540 This will cast the result of Math.Sqrt to an integer. 43 00:02:43,540 --> 00:02:46,670 Now let's save, compile and run again. 44 00:02:48,220 --> 00:02:51,180 All right so we see the result is three. 45 00:02:51,180 --> 00:02:55,520 So the distance between point (4, 2) and coordinate (5,5) is 3. 46 00:02:55,520 --> 00:03:00,570 If you like, you can verify on a calculator that this is correct. 47 00:03:01,920 --> 00:03:04,830 Let's take a final look at the DistanceTo method before we move on. 48 00:03:06,860 --> 00:03:09,140 There are five lines in the DistanceTo method. 49 00:03:10,280 --> 00:03:13,590 It might surprise you to hear that we could have written all this in a single 50 00:03:13,590 --> 00:03:14,190 line of code. 51 00:03:15,210 --> 00:03:18,400 Let me type it here underneath the existing code. 52 00:03:18,400 --> 00:03:22,010 Then we can talk about why we might prefer one way over the other. 53 00:03:22,010 --> 00:03:26,989 So the cartesian distance formula is the square root 54 00:03:31,649 --> 00:03:38,530 Of the sum of the square for this. 55 00:03:38,530 --> 00:03:41,790 We use another method in the math class called POW for power. 56 00:03:42,920 --> 00:03:45,410 This allows us to raise a value to a power. 57 00:03:45,410 --> 00:03:46,585 In this case, two. 58 00:03:56,005 --> 00:04:02,255 So we have the square root of the sum of the squared differences. 59 00:04:02,255 --> 00:04:09,015 So we get X minus x and Y minus y so this code here, 60 00:04:09,015 --> 00:04:15,620 does the exact same thing as this code up here. 61 00:04:17,570 --> 00:04:22,160 As you can see it puts the entire math formula on a single line. 62 00:04:22,160 --> 00:04:25,180 One advantage of putting the code all in the same line, 63 00:04:25,180 --> 00:04:27,680 is that it gets rid of most of the variables. 64 00:04:27,680 --> 00:04:29,660 The fewer variables there are, 65 00:04:29,660 --> 00:04:32,810 the less likely you are to type the wrong one in the wrong place. 66 00:04:34,060 --> 00:04:38,628 Say, for example, that I typed xDiff here instead of yDiff. 67 00:04:40,829 --> 00:04:43,130 >> That's a very common mistake when programming. 68 00:04:43,130 --> 00:04:47,019 Because the variables look so similar, it's not obvious what the problem is. 69 00:04:48,360 --> 00:04:52,850 Just because you can put all the code on a single line, doesn't mean you should. 70 00:04:52,850 --> 00:04:55,930 In fact, this is actually a good approach to programming. 71 00:04:57,380 --> 00:04:59,860 You see, by coding it this way, 72 00:04:59,860 --> 00:05:03,713 we've broken up the solution into smaller parts that are easier to think about. 73 00:05:03,713 --> 00:05:07,774 Solutions that are easier to think about are easier to code. 74 00:05:08,900 --> 00:05:12,100 Once you think you have a correct solution implemented, 75 00:05:12,100 --> 00:05:14,520 you can test it with known values. 76 00:05:14,520 --> 00:05:18,390 After you verify that the solution is correct, you can always go back and 77 00:05:18,390 --> 00:05:21,540 refactor the code into a less verbose form. 78 00:05:21,540 --> 00:05:25,860 Then run the same tests again to verify that the changes didn't break anything. 79 00:05:26,900 --> 00:05:31,290 This is a very common pattern for designing, refactoring, optimizing and 80 00:05:31,290 --> 00:05:32,260 refining software.