ofVec2f align() question

I want to find out if two vectors are pointing in the same direction. Here is the result from 4 tests

I thought that two vectors are pointing in the same direction, the x components need to both be positive or negative, as with the y value.

In the first example, y for the first vector is 11 and for the 2nd vector -3, but align() function returns true, yet actually the first vector is pointing south west and the 2nd vector north west.

I checked the numbers in excel and yes they are > 0 as the function suggests, but is the function wrong, or have I got the wrong use for this function?

ofVec2f vector1;  
ofVec2f vector2;  
vector1.set(-27, 11);  
vector2.set(-22, -3);  
printf("is aligned = %i\n", vector1.align(vector2));  

Many thanks

I *think* the function might be wrong:

return (x*vec.x + y*vec.y) > 0.0;  

You want both the x + y multiplied to each be above 0, not as a whole.

So should be something like

return ((x*vec.x) > 0.0 && (y*vec.y) > 0.0);  

What do you think?

The align method might not have the most self-explanatory name. What it does is to return true if the angle between the vectors is less than 90 degrees. I think it is doing this correctly. It’s comparing the dot-product with zero.


Maybe it should be called something else to make its purpose clearer. Any ideas?
What is the functionality you were looking for?

maybe useful is something more general ?

I personally wouldn’t find that function very useful - since it’s just testing between one perpendicular and the other. I would be interested in:

a) a being able to test a more specific range of alignment (ie, between 0.9 and 1)

b) being able to test the opposite, ie, are the vectors facing toward each other – this is useful for flocking, for example, where you could have objects test if they can “see” each other.

I guess the more specific you could get with the test, the better – the problem is that it’s a little bit hard to wrap your head around the dot product, and it’s simpler to want to ask the question, are they within 10 degrees of each other, etc, are they within 20 degrees of being 180 degrees from each other, etc. I remember the first time I started to work with flocking I has trouble wrapping my head around altering that variable since it wasn’t in degrees at all.

hope that’s useful input -
at any rate, thanks for all the additions to ofVectorMath !!
take care,

ps one tricky thing about the dot product I think is that is assumes that the vectors are the same size (same length) – in order for the suggestion I threw up above to work you’d want to normalize them before you do the dot product, otherwise doing the dot product bteween two vectors pointing in the same direction but one being small, would be close to 0, even if they share the same angle. The applet you posted online is a good way to check that.

there is good info here, and even an example where they calculate the nearest angle between two vectors, which is probably a more useful function :