# Relative orientation of two vectors

1. Jul 15, 2013

### xonxt

Hello. I'm sorry if this is a really trivial question, but I'm not that good with vector arithmetics, so I have to ask.

Let's say I have two objects on a 2D plane, that face some direction. I have (x,y) coordinates of each object, and I have an angle each object is facing. The relative coordinates are not important to me, what I need to find out, is if the objects are facing each other, and to compute some parameter X which, for lack of better terms, tells me how "much" are they facing each other. A relative angle, if you wish.
Here's an illustration of what I need, approximately:

Does a thing like that exist, or do I need to invent it? :D

2. Jul 15, 2013

### micromass

The inner product should be what you're looking for.

Given two vectors $\mathbf{v}$ and $\mathbf{w}$ (both emanating from the origin). Then you have

$$\cos(\alpha) = \frac{\mathbf{v}\cdot \mathbf{w}}{\|\mathbf{v}\|\|\mathbf{w}\|}$$

For example, given $\mathbf{v} = (1,2,3)$ and $\mathbf{w} = (5,1,2)$, then

$$\mathbf{v}\cdot \mathbf{w} = 1\cdot 5 + 2\cdot 1 + 3\cdot 2 = 13$$

Thus

$$\cos(\alpha) = \frac{13}{\sqrt{14}\sqrt{30}}$$

3. Jul 15, 2013

### mathman

The example you show is confusing. For all your cases the vectors are in opposite direction.

4. Jul 15, 2013

### xonxt

Well, maybe I phrased it wrong. Would it help, if I told you that it is a computer vision problem. I have computed the angle two objects are facing, now I need know if these two objects are facing towards or away from each other?

I was thinking of something more simple. First of all, I son't have vector coordinates, I only have angles.
Secondly, I thought along the lines of something like this:

$$X = \frac{180 - |\alpha - \beta|}{180}$$

I will only get a X = [0; 1] that way, but it's a start.

5. Jul 15, 2013

### gerben

$\cos(\alpha - \beta)$ is 1 when they are in the same direction and -1 when they are in opposite direction. When they are somewhat in the same direction it is smaller than 1 but still positive (the smaller the less they are in the same direction) and when they are somewhat in the opposite direction it is larger than -1 but still negative. So, for what you want you could use $-\cos(\alpha - \beta)$

6. Jul 15, 2013

### xonxt

Well, that's almost what I'm looking for. The only downside is with that I can only determine if both vectors are directed somewhat along one line, but it doesn't show me if they are facing inwards or outwards.
Basically here is what we get. Cases A and B are similar and we don't get any information whether they're facing each other or not:

7. Jul 15, 2013

### gerben

The way you drew it case A and case B are the same, (about) the same angle alpha and beta (alpha around 200 degrees and beta around 45 degrees) and since they are somewhat opposite you get a positive number. In case C you will also get a positive number, it is about -cos(100 - 250)

8. Jul 15, 2013

### gopher_p

Maybe start with the easier problem of determining if one of the objects is facing the other. Also, you might want to think about precisely defining "facing", keeping in mind that you'd like to permit different degrees of "facing". Sometimes writing down exactly what you're trying to do leads to a fairly obvious solution. If you can define what you mean by "A is facing B", then "A and B are facing each other" should mean "A is facing B and B is facing A", right?

9. Jul 15, 2013

### Seydlitz

I find using dot product most convenient but it's up to you. If the object is facing each other exactly then their dot product will be -1.

Using distance method to decide if they're facing opposite each other or face-to-face: Imagine there's a point on the object face vector. Calculate the distance between that point with the second object face vector point. If they're facing outward, then the distance between those two points will be larger then the relative distance between the center of the objects. If they're facing inwards the distance will be less than it.