9
\$\begingroup\$

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.

\$\endgroup\$

3 Answers 3

13
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jul 12, 2011 at 8:27
  • \$\begingroup\$ I was about to write up a more complex method involving the dot product even though I've used this simple method before, doh! \$\endgroup\$ Commented Jul 12, 2011 at 8:30
  • \$\begingroup\$ What do you mean by "The problem is that you'll always end up is the second case"? \$\endgroup\$ Commented Jul 12, 2011 at 8:32
  • \$\begingroup\$ @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 :) \$\endgroup\$ Commented Jul 12, 2011 at 8:36
  • \$\begingroup\$ 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°. \$\endgroup\$
    – XGouchet
    Commented Jul 12, 2011 at 8:39
1
\$\begingroup\$

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.

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

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

\$\endgroup\$
2
  • \$\begingroup\$ 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] \$\endgroup\$ Commented Jul 12, 2011 at 14:33
  • \$\begingroup\$ @Richard Fabian You are right, i forgot the Triangle inequality! \$\endgroup\$
    – FxIII
    Commented Jul 12, 2011 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .