# Find a unit vector exactly halfway between two connected line segments

Seems like a quick and easy question but I haven't been able to find exactly what I'm looking for so:

How to calculate a unit length vector that is pointing along the line which is exactly 50% of the angle of two connected line segments?

A picture speaks a thousands words (that are also better than my explanation!) So basically I want to calculate the blue unit vector given the two red line segments (which are actually 3 points and are therefore guaranteed to be connected)

The red segments are of arbitrary length, and the result doesn't have to be a unit it would just be easier for me.
It would also be handy to have a way to force the resultant vector to point in a certain direction (relative to the input segments), this isn't essential as I think I can work this one out - as the input line segments ultimately form an n-gon.

Any examples would be ideal in C++ please, but other languages welcome.

Many thanks for any pointers.

Create and normalize two vectors from your red segment, starting from their comon vertex, then add the results (component by component). Then you can normalize the output if you want to get a unit vector.

The problem is that you'll always end up is the second case, because the angle between 2 vectors will always be less than 180°. But then of course you can just create the opposite vector and see which one suits better your purpose.

Also there's a special case when both vectors are aligned and the average will be 0 (but you can easily detect it).

• Excellent I think this is exactly what I'm looking for! I knew it was easy :) I'll leave it open just to see what other responses I get... Thanks – Adam Naylor Jul 12 '11 at 8:27
• I was about to write up a more complex method involving the dot product even though I've used this simple method before, doh! – CiscoIPPhone Jul 12 '11 at 8:30
• What do you mean by "The problem is that you'll always end up is the second case"? – CiscoIPPhone Jul 12 '11 at 8:32
• @CiscoIPPhone I believe he is refering to my diagram. A dot product solution is more than welcome CiscoIPPhone, at the least i'll give it a +1 :) – Adam Naylor Jul 12 '11 at 8:36
• Indeed i was refering to your image. Using a dot product, you can get the angle between the two vectors, but again it'll stay under 180°. – XGouchet Jul 12 '11 at 8:39

I think you can get the direction to be consistent by treating this as if you're generating a 2D vertex normal. That is:

1. Take each of the red vectors, swap the x and y components and negate one of them to create the normals.

2. Normalize them.

3. Sum those two vectors, and renormalize.

You'll probably also want to test for the case where the two red lines are overlapping each other - the final renormalization will try to divide by zero there.

• Thanks Adam, does this approach have a name? I'd like to look into it further... – Adam Naylor Jul 13 '11 at 15:33
• Not that I know of. The normal vector generation trick is explained at stackoverflow.com/questions/1243614/… – Adam Jul 13 '11 at 19:17

Let A and B be your vectors: The summing vectors do not need to be unit vectors but simply equal in length so, if |A| >= |B|, you can: which is more numerically stable since you have only a fraction and the bigger denominator The same result can be obtained by substraction, once again the vectors has to be equal in length

This works only for non-convex angles; you can simply test if your angle is convex and multiply H by -1

• I don't think this is going to work if |A| != |B| ... consider A=[0,1] B=[100,0], the result is not [.707,.707] – Richard Fabian Jul 12 '11 at 14:33
• @Richard Fabian You are right, i forgot the Triangle inequality! – FxIII Jul 12 '11 at 15:47