## [distance to] tile Edit

The [distance to] tile gives the distance to another object, and there's a [from] modifier to specify a second object/position (which is set to [position] by default if you don't use it).

Note that "[distance to] [object B] [from] [object A]" is different from "[distance to] [object B] [position] [from] [object A] [position]", as the latter gives the distance between the two precise points (which means it gives [(] [object B] [position] [minus] [object A] [position] [)] [length]), whereas the first one gives the shortest distance between a point of object A, and a point of object B. What does that mean? If the object A is a ball for example, then the distance from A to B is the distance from the point on the side of the ball which is the closest to B, and B. Whereas if you take the distance from A center to B, it's the distance from the center of A, so it gives an answer that is radius of the ball bigger than the distance from A to B since the center is radius of the ball further than the point on the edge.

Kode:

[number variable: distance AB][equals][distance to][object B][from][object A]

or if you want the distance to object B from within object A's brain, you can omit the last two tiles:

[number variable: distance AB][equals][distance to][object B]

## How it is implemented Edit

You need to know the **Pythagorean theorem**, which is among the most famous theorem of Mathematics. Basically, it says that in a right triangle, the square of the hypotenuse (the opposite side from the right angle) is equal to the sum of the squares of the other sides.

Like all famous theorems, there are tons of different proves for it. A simple visual one is given here.

How to use it to calculate a distance? You need to know about cartesian coordinates. Those are describing the position of a point in a space (in Project Spark, it's a 3-dimensional space, which means that there are 3 coordinates x, y and z). Those are used to define vectors, or points with the "vector" variable (again, I insist on the fact that a point isn't actually a vector). We're interested in knowing the distance between two points, which is also the "length" of the vector from one point to the other (which as I explained in the vector tutorial is the second point minus the first point).

How does the length of that vector, which is the distance between the two points, relate to the Pythagorean theorem? Well, what is great about the usual cartesian coordinates we use in most cases (and so also in Project Spark), is that the x, y and z coordinates define the position of a point from a point O, with three unit vectors (ie of length 1) that are perpendicular. If you don't know what I mean, look at this picture: [1] In Project Spark, the unit vectors that define the vector are east (for x), world up (for y), north (for z). On the picture, the vector from the origin to P is 2 * "east" + 4 * "world up" + 3 * "north" (of course, the world vectors don't correspond to what you would expect in that particular picture, because they use different unit vectors, but the idea is there) So in the end, we have perpendicular vectors, so we have right triangles...

We would only need to look at one right triangle if we were in a two-dimensional space, like the xy plane. Indeed, there a right triangle whose hypotenuse is the segment between the origin (let's call it O), ie the point (0, 0, 0), and the foot of perpendicular from the point to the xy plane (which I'm going to call H from now on). If we take the example of the figure above, the two sides are given by the segments of length 4 and 2.

So

(therefore the distance is square root of 20)
Great, but that's not the distance OP yet, only between O and H. Indeed, in a three-dimensional space, we need a second right triangle to find out the formula. Here, it is the triangle whose vertices are O, P, and H. So a second Pythagorean theorem gives us:

So in the end, the general formula is given by two Pythagorean theorems, the first one tells us that:

and the second one:

Thus, the final formula for a vector (x, y, z), which Project Spark uses when you put the [length] tile, is:

So to calculate the distance between two points, which is the length of the vector from one point to the other:

I recommend reading this page, which explains the cartesian coordinate sytem (with three unit vectors perpendicular to each other) in more depth.