Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Hildreth
16,170 PointsUsing Static Methods C# Objects
In the Using Static Methods, I am confused on how the two codes mean the same thing, particularly the 2. The code used to be:
int xDiff = X - x;
int yDiff = Y - y;
int xDiffSquared = xDiff * xDiff;
int yDiffSquared = yDiff * yDiff;
return (int)Math.Sqrt(xDiffSquared + yDiffSquared);
Then it was refactored into this:
return (int)Math.Sqrt(Math.Pow(X-x, 2) + Math.Pow(Y-y, 2));
Can someone help break this down for me so I understand it better? What does the 2 do in the refactored code?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! In the refactored code you are using Math.Pow. This method accepts 2 numbers. The first number is the base and the second number is the exponent. So if we had 2 and 3 in there this would be equivalent to two to the third power or 8. So to square any number we first pass in that number and then use 2 as the power. If we'd passed in a 4 and a 2 the result would be 16. Here we're passing in the difference of two X points and two Y points and then squaring them. Which is what happens in the middle section of your code posted above. Hope this helps!
Daniel Hildreth
16,170 PointsDaniel Hildreth
16,170 PointsYes thank you. I missed the part in the video where the 2 meant that it was the exponent for the Math.Pow method.