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 trial

C# C# Objects Methods Using Static Methods

Liam Andersson
Liam Andersson
1,396 Points

The coding regarding the distance formula

Jeremy wrote:

  int yDiff = X - x;
  int xDiff = Y - y;

Why though? Make it simple I'm only 15 and it was hard enough to learn the:

https://www.varsitytutors.com/hotmath/hotmath_help/topics/distance-formula

Edit: I also do not understand Math.Pow and what it means, I looked it up but I didnt get anywhere!

5 Answers

Jaroslaw Adamowicz
Jaroslaw Adamowicz
11,634 Points

Hey Liam,

I hope you didn't give up yet!

First of all, the all Cartesian difference, power and square root are more related to math than programming itself. So don't worry if you don't get everything right away!

When I was at your age, I learned Pascal programming language all by myself from the book. I had no one to ask for help, Internet was not big thing back then and I didn't had access to it, also Wikipedia and Treehouse didn't exist yet!

What I'm trying to say here is: I know it all can be little confusing. And it is quite painful when you discover more and more pitfalls along the way. But that is normal, give it a time, do your research and eventually you will figure all out.

Now back to your question...

Code you actually posted in your question isn't correct:

  int yDiff = X - x;
  int xDiff = Y - y;

If you look little closer you will realize it should be:

  int xDiff = X - x;
  int yDiff = Y - y;

And if you check video, that is exactly what Jeremy has on the screen :)

It might seem to be small mistake, but from now on I'm not even sure if you understand what you are asking for.

Next topic - difference (with simple math)

To help you out a little, let's talk about difference and how it is important. Please consider following example:

Let's say you are 15 years old. I'm 35 years old. What is our age difference? How to calculate it?

Let's try to somehow code it:

int ageOfLiam = 15;
int ageOfJarek = 35;

Now let's see how we can calculate difference:

int ageDifference1 = ageOfLiam - ageOfJarek; // it gives -20
int ageDifference2 = ageOfJarek - ageOfLiam; // it gives 20

Ok, so as you can see we have 2 possibilities, but which one is correct? The real answer is both!

But wait! One is negative? How is that possible?

Well it's actually easy, first difference called ageDifference1 tells us how much your age is before mine (in other words how many years I need to get to be at your age). Second one called ageDifference2 tells us how much my age is before yours, in other words: how many years you need to be at my age.

As you can see from above example, we count difference by subtracting one thing from another. Depending on the order we will get either positive or negative number, but it still will be the same value (regardless of sign).

That is exactly how we tell the distance between two different points (like cities, trees, roads, etc.). We subtract their position (at least in single dimension).

Important note: as you should know, distance needs to be positive. There is no much use of negative distance in our real world.

Back to Cartesian formula

Normally it would require to use something called absolute value, to properly get positive distance value after subtraction.

But with formula for two dimensional distance (2D) we are using power of 2 on both subtractions. Using power of two on any integral number (positive or negative) gives always positive value.

Square root is backward operation to power of 2.

At this point on, you don't really need to understand why and how this formula works. You only need to properly code it :)

But as you will discover in future, you can't be a very good Programmer(or even decent) without proper math skills. Solution to many problems you will face in programming world comes back to simple (or not so simple) math.

And remember, it is not an excuse if it wasn't at school. If it wasn't yet, it may either never be, so it is your responsibility to gain that knowledge and clear all gaps. There are probably tons of useful books you could choose from to help you out in either math or programming. So pump up your math skills and get back to coding (or learning how to).

Good luck!

Cheers! Jarek

Keith Whatling
Keith Whatling
17,752 Points

I struggle with some of the maths too, try khan academy for vids on maths. I need a visual breakdown so that helps, math.pow is taking a number to the power of something.

the number is just that a number, the power is the number of times you are going to multiply that number by its self. so

2 to the power of 2 is 2 x 2

2 to the power of 3 is 2 x 2 x 2 so multiplied by 2 three times, the power of three.

2 to the power of 5 would be 2 x 2 x 2 x 2 x 2.

Lots of coding has only basic maths, you can learn as you go. dont let it stop you.

Liam Andersson
Liam Andersson
1,396 Points

so when he writes Math.Pow(Y - y, 2) he means that it should be: Y - y * Y - y?

And what does that even mean? Y - y should be 0?

Liam Andersson
Liam Andersson
1,396 Points

Is that also some kind of math that I haven't read about? I'm from Sweden and a lot of the math I will learn at a "Gymnasium" and that is next year..

Liam Andersson
Liam Andersson
1,396 Points

And I promiste, it wont stop me! I'm living a dream when sitting at treehouse and coding. Sadly I dont always have time!

Michael Milone
Michael Milone
1,474 Points

To clarify, the capital Y does not refer to the variable being passed in, but it is an instance variable when the class gets constructed - this is why it is common to put "this" in front of variables to denote that they are instance variables.

Liam Andersson
Liam Andersson
1,396 Points

I dont fully understand tbh

Michael Milone
Michael Milone
1,474 Points

when we say: int xDiff = Y - y; the capital Y refers to: public readonly int Y

Liam Andersson
Liam Andersson
1,396 Points

But how can you power such a statement?

Michael Milone
Michael Milone
1,474 Points

Y-y just means whatever is stored in Y minus whatever is stored in y.

Liam Andersson
Liam Andersson
1,396 Points

Ah Okay, and then that powered by ”2” in this case.