Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!) Example of a unit vector bisecting two connected line segments
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.

share|improve this question

3 Answers 3

up vote 12 down vote accepted

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).

share|improve this answer
    
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.

share|improve this answer
    
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:

result

The summing vectors do not need to be unit vectors but simply equal in length so, if |A| >= |B|, you can:

equivalent result

which is more numerically stable since you have only a fraction and the bigger denominator

how sum works

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.