1
\$\begingroup\$

Below we have distances from the origin calculated in two different ways, giving the Euclidean distance, the Manhattan distance and the Chebyshev distance.

Euclidean space

Euclidean distance is what we use to calculate the magnitude of vectors in 2D/3D games, and that makes sense to me:

Let's say we have a vector that gives us the range a spaceship with limited fuel can travel. If we calculated this with Manhattan metric, our ship could travel a distance of X if it were travelling horizontally or vertically, however the second it attempted to travel diagonally it could only tavel X/2!

So like I say, Euclidean distance does make sense. However, I still don't quite get how we calculate 'real' distances from the vector's magnitude.

Here are two points, purple at (2,2) and green at (3,3).

points

We can take two points away from each other to derive a vector.

Let's create a vector to describe the magnitude and direction of purple from green:

|d| = purple - green
|d| = (purple.x, purple.y) - (green.x, green.y)
|d| = (2, 2) - (3, 3)
|d| = <-1,-1>

vector

Let's derive the magnitude of the vector via Pythagoras to get a Euclidean measurement:

euc_magnitude = sqrt((x*x)+(y*y))
euc_magnitude = sqrt((-1*-1)+(-1*-1))
euc_magnitude = sqrt((1)+(1)) 
euc_magnitude = sqrt(2)
euc_magnitude = 1.41

Now, if the answer had been 1, that would make sense to me, because 1 unit (in the direction described by the vector) from the green is bang on the purple.

But it's not. It's 1.41. 1.41 units is the direction described, to me at least, makes us overshoot the purple by almost half a unit:

Where I think the purple should be

So what do we do to the magnitude to allow us to calculate real distances on our point graph?

Worth noting I'm a beginner just working my way through theory. Haven't programmed a game in my life!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I'm struggling to quiet the idea in my head that, "You must be trolling." I hope you realize that this is how "real" distances work, we live in an euclidean universe. -- Try starting from your X that marks the spot, stepping once forward, one more time left, and measuring how far you are away from the X with a real tape measure. \$\endgroup\$
    – MickLH
    Commented May 27, 2014 at 13:23
  • \$\begingroup\$ Now you're thinking with the Chebyshev metric. ;) See how the green Chebyshev box touches the point (1, 1), but the red Euclidean circle doesn't? This might be your experience of distance if you're a King in Chess, but it's not the one we're used to in everyday life. \$\endgroup\$
    – DMGregory
    Commented May 27, 2014 at 13:25
  • \$\begingroup\$ @MickLH sadly, not trolling. But I now see why you`d think I was! \$\endgroup\$
    – Starkers
    Commented May 27, 2014 at 13:53

1 Answer 1

3
\$\begingroup\$

Vector magnitude IS the "real distance" - no conversion necessary.

To see this, draw a scale diagram of your question on paper, and take some measurements with a ruler. Believe it or not, the distance between the green and purple dots really is bigger than the distance between the 0 and 1 ticks on the axis. About 41% bigger.

Here I've taken your diagram and made a tracing paper copy, then rotated it down to the x-axis so we can see this visually:

Measuring

Another way to think about it is to draw a circle of radius one around the purple point:

Circle check

Is the green dot outside the circle? Then it must be further away than a distance of one. It looks like the green dot is more than a third of the radius further away, but less than adding another half the radius, so somewhere in 1.33333... < d < 1.5

If you measure it, you can confirm that the Euclidean distance between these two points, sqrt(2)=1.414... correctly reflects the visible distance between them.

\$\endgroup\$
2
  • \$\begingroup\$ Don't worry though, I made this same mistake once in woodshop. A hasty bit of head math led me to believe that a 1x1 bar cut on a 45-degree diagonal would match up flush with the end of another uncut bar. Wasted some wood that day... :( \$\endgroup\$
    – DMGregory
    Commented May 27, 2014 at 13:28
  • \$\begingroup\$ Ah of course! I see now. I think I need some practise with actual code instead of poking around theory land. Encouraging that you made the same mistake! :) \$\endgroup\$
    – Starkers
    Commented May 27, 2014 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .