Distance Between Two Vectors?

Started by
23 comments, last by ironhand69 15 years, 4 months ago
Hi If I want to know the distance between two vectors is it just simply V1.length - v2.length? Seem to simple somehow Cheers Mark
Advertisement
It's (v1 - v2).length
We just covered this in linear algebra and here are the forumlas for vectors in any dimension:

Euclidean distance
In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" distance between two points that one would measure with a ruler, which can be proven by repeated application of the Pythagorean theorem. By using this formula as distance, Euclidean space becomes a metric space (even a Hilbert space). The associated norm is called the Euclidean norm.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Incorrect , that is the lenght of a vector .
Without specifing the metrix used , your question is vague.
I suppose you want to compute the distance beetween two oriented vectors don't you ?
There are 2 ways
the first is to compute the distance using the application point of these 2 vectors, the other is finding the closest point on segments using the direction vectors.
Its up to you to use this methods to your benefit.
Elaborate more deeply yout problem if you want a correct answer

Hi thanks for you replies,

Basically I have two vectors. 1) Is the current location of my robot, the other is it's way point. I want to work out the distance between the two.

So that I can work out hoe close the robot is to it's Way Point. I'm trying to find a solution to a problem where the steering command makes the robot thrash about as it approaches the way point. So I thought if I knew the distance between them then I'd have something to tune the steering cmd with

Thanks

Mark
You should do it the way Erik Rufelt mentioned:

Subtract the second vector from the first, and then compute the length of the resulting vector:

float calcdist(vector3d v1, v2)
{
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

Hope this helps!
To explain why your original question was ambiguous:

A vector has a start point and an end point. How can you find the distance between a four points? However, most of the time the starting point is not used, and is assumed to be the origin (this is probably the way your vector class works, and the way you think about vectors). In that context, it still doesn't make sense, but you could have asked for the distance between the two end points.

Equally valid interpretation of your original question could have been:
What is the distance between the two start points?
What is the minimum distance between the two line segments going from the start point and end point of each vector?
Quibbling a bit here..

A vector is not a point, nor is a vector 2 points.

A vector is a direction and magnitude.

Most game programmers use their vector type for both vectors and points, but in the math world thats actualy a big no-no.

vector = vector + vector
vector = vector - vector
vector = point - point
vector = vector * scaler
vector = vector / scaler

point = point + vector
point = point - vector

now, 'point + point' does not yield a vector, nor does it yield a point. The same goes for 'point * scaler' and so on.

In the math world if you asked what the distance is between two vectors, you would most likely get an ANGLE as an answer.

"Those two vectors are 15 degrees apart"

Quote:Original post by Rockoon1
A vector is not a point, nor is a vector 2 points.


Agreed, but a vector has a start point and an endpoint. (I guess the actual terms are initial point and terminal point. Or tail and head.) point + point of course does not yield a vector, it makes no sense to add points. It also doesn't make sense to subtract points, and I think a particularly pedantic mathematician would say that is no more valid than adding points and getting a vector.

If a mathematician gave me an angle when I asked for a distance, I would have scoffed. Perhaps I am wrong, but an angle does not give distance. Any reasonable mathematician would have balked at that question and said it was extremely ambiguous. My post was to clarify why it was ambiguous.
Quote:Original post by Ezbez
Quote:Original post by Rockoon1
A vector is not a point, nor is a vector 2 points.


Agreed, but a vector has a start point and an endpoint. (I guess the actual terms are initial point and terminal point. Or tail and head.)

No.
A vector is something that behaves like a vector from an algebraic point of view, no points are involved. When we have vectors we can introduce "points" in our algebraic toybox as something that can be summed to vectors to get other points in a way that, for some popular vector spaces, gives affine spaces that match our geometric intuition.

What has an initial point and a terminal point is the line segment, which is not a vector because it cannot be summed meaningfully to other segments.
Instead of addition, multiplication by a scalar and other arithmetic with coordinates, geometric loci like segments, being sets of points, have set operations.
Less philosophically, both n-dimensional vectors and n-dimensional points are uniquely represented by n numbers, while a line segment is determined by its two endpoints which amount to 2n numbers: it cannot be a vector.
Quote:
point + point of course does not yield a vector, it makes no sense to add points. It also doesn't make sense to subtract points, and I think a particularly pedantic mathematician would say that is no more valid than adding points and getting a vector.

There is a substantial difference, which you seem to miss, between subtracting and adding points: subtracting points gives vectors by definition, adding points is nonsensical.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement