Welcome back.
In this video we're going to use structs to build a little project by actually improving our point struct
that we build by adding a method that allows us to calculate the distance between two points.
But before we do that, I wanted to add another thing that I didn't add in the last video, and that
is that it's common practice to make structs immutable by declaring all fields as read only in providing
only get accessors for properties.
Okay, so that means that I'm going to make this my primary properties.
And I'm also going to make sure that I don't have setters.
So I'll only have getters.
Okay.
And then therefore obviously what I have here is not going to work anymore.
So I will need to make sure that I assign the values directly during initialization.
So here new point two would be let's say 20 and 30 okay.
So how do we add these two cool methods.
The first thing that would really help is to make them all double.
Because whenever you're calculating differences or distances, they will never be whole numbers or very
rarely be whole numbers.
So let's make just sure that everything is going to be a double and then everything here can stay the
same.
However, now we're going to have a method public double distance two, which is just going to take
a point in.
And I'm going to call this one other okay.
And what this will do is it will return.
Well actually more than just x I like what the IDE had to say there, but it's not enough.
So I'm going to create a variable double d x which will be other x minus its own x and double d y,
which will be other y minus y.
And then the result will be the square root of the math class square root of the x times the x plus
the y.
If you aren't sure as to what happened here, well, the distance between two points is the length of
the line segment that connects the two points in a plane.
The formula is to find the distance between two points is usually given by.
So what we have is the square root here.
What do we have?
In the square root we have the sum of two values.
What are the two values?
Well the first one is the square of the distance between the two axis.
So one x minus the other x and the distance between one y and the other y, so y2 minus y1 and also
squared.
So out of this sum then the root the square root will give us the distance.
So this is I think math ninth grade or so, eighth grade, maybe 10th grade.
I'm not sure.
It depends on the country you live in, and also on the type of school that you go to.
In Germany we have three different schools.
So I think in the Hauptschule it's like ninth grade, if at all.
In the in gymnasium it will probably be already in eighth grade or maybe even earlier.
I'm not sure.
Something like that.
So that's how we get the distance and now let's actually use it.
So let's say we want to see the distance between P1 and P2.
Therefore let's go ahead and create a double distance variable which will be p1 dot distance to p2.
Okay.
And now just write it onto the console.
So the distance between points and then whatever distance is And what I'm going to say is this is something
we haven't seen yet.
I'm going to say I want two points after the decimal point.
So let's see how this is going to look like.
So you see the distance here is 1414.
And if you look at the distance so those we have an x distance of ten and the y distance of ten.
So ten squared is 100 plus 100.
So we had we have it twice right.
So it's 200.
And then the square root of 200.
And that is 14 comma 14 okay.
So you probably see this number or saw this number before because it is 1.414 is the square root of
two okay.
So now we could obviously use more complex numbers.
Let's get rid of P3.
But the general idea here is still the same.
So this F2 maybe you might be wondering.
So it's floating two.
We're just saying after the floating point number give me two values.
I could put that to three.
Then I will get more details.
So 14142.
So we're getting closer to what the actual square root will be.
So now you could say, okay, I want to have more details.
So go all the way to five.
So then you see 14214 after the decimal point.
So five point values after the floating point okay.
So that's pretty much our small little example which allows us to calculate the distance between two
points okay.
So that's it for this video.
See you in the next one.