Hugh
Hugh

Reputation: 23

How to understand the vector math behind glm::distance and glm::length?

I found the following page: https://glm.g-truc.net/0.9.4/api/a00131.html

And I am having issues with understanding why the following two are equivalent...

The following code:

 glm::vec3 vec = vector1 - vector2;  
 distance = glm::length(vec);

Is equivalent to:

 distance = glm::distance(vector1, vector2); 

How is it that the length of the vector resulting in subtracting two vectors is equivalent to the distance between the vectors?

Upvotes: 1

Views: 5275

Answers (1)

flirion
flirion

Reputation: 141

This diagram might help understand what is happening here: we have two vectors, a and b, which correspond to your vector1 and vector2. The difference vector of those two reaches from b to a. Its length equals exactly the distance between the two coordinates that describe a and b. If you switch a and b around, the difference vector simply changes direction.

enter image description here

Upvotes: 4

Related Questions