Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Solution: Tower Defense Game

Looking for understanding on this problem

Can somebody go into detail and help me understand how to solve this problem, please!!

Write code to determine how far they are from one another using the Distance Formula. The Distance Formula can be broken down into the following steps

  • Subtract the distance between the X coordinates of the Tower and Enemy and square it
  • Subtract the distance between the Y coordinates of the Tower and Enemy and square it
  • Add the two square values
  • Obtain the square root of the resulting value */

// Enter your code here

I really appreciate the help I'm new to coding

1 Answer

Robert Bird
Robert Bird
26,122 Points


I often wonder why coding exercises mix programming with math, if you don't understand the math, the programming becomes even harder to understand. Understanding this problem is more math than programming. For this problem you need to know the Pythagorean theorem which says that if you have a right triangle, then a^2 + b^2 = c^2 where a is a side of the triangle, b is a side of the triangle and c is the hypotenuse (the diagonal side we are trying to figure out). Suppose that two points are (2,3) and (5,7). then we can subtract the x values 5-2 = 3 which is a side of the triangle, subtract the y values 7-3 = 4 which is the b side of the triangle. that means that c^2 is equal to 3^2 + 4^2 which is equal to 9 + 16 = 25. If 25 is the diagonal squared then in order to get the diagonal we need to take the square root of 25, which is 5. Therefore, given any two coordinates we can figure out the distance. The video kind of assumes you already know this, not sure why they assume this. I hope that helps but it probably wont so here is a video explaining it a lot better than I can.


Just know that your confusion does not really have anything to do with coding, but rather with math.