Home > OS >  Why do 3 Vector2s/Points, on a straight line, not have equal angles when split into 2 lines?
Why do 3 Vector2s/Points, on a straight line, not have equal angles when split into 2 lines?

Time:03-20

As the title says, I'm trying to deduplicate Vector2s that appear on the same line.

My data set looks like this:

(197, 59)
(197, 73)
(197, 76)

(Obviously, all on the same vertical line)

I've searched for an found lots of answers telling me how to get the Angle of a line between two points (after Normalisation):

 float radians = Mathf.Acos(
                   Mathf.Clamp(
                     Vector2.Dot(from, to), 
                       -1f, 1f)));

Or

float dx = to.x - from.x;
float dy = to.y - from.y;
            
float radians = Math.Atan2(dy, dx) 

Or

   Vector2.Angle(from, to)

Or (I'm not sure this is at all right - but I don't think the code is where my problem is)

float radians = MathF.Asin(Vector2.Dot(from, to) / from.magnitude  to.magnitude);

But I'm getting a different answer for A->B than from B->C.... Obviously I'm missing something somewhere, but as you might be able to tell my Math knowledge is awful. Could someone point me in the right direction?

You can see the results from Vector2.Angle() on my entire data set below:

Getting angle of (197.00, 73.82)=>(197.00, 59.52). Normalised: (0.94, 0.35)=>(0.96, 0.29): Result 3.730666
Getting angle of (197.00, 76.62)=>(197.00, 73.82). Normalised: (0.93, 0.36)=>(0.94, 0.35): Result 0.7127182
Getting angle of (183.50, 140.60)=>(197.00, 116.29). Normalised: (0.79, 0.61)=>(0.86, 0.51): Result 6.904529
Getting angle of (197.00, 116.29)=>(197.00, 76.62). Normalised: (0.86, 0.51)=>(0.93, 0.36): Result 9.300751
Getting angle of (190.75, 158.58)=>(183.50, 140.60). Normalised: (0.77, 0.64)=>(0.79, 0.61): Result 2.279588
Getting angle of (197.00, 163.32)=>(190.75, 158.58). Normalised: (0.77, 0.64)=>(0.77, 0.64): Result 0.07661668
Getting angle of (197.00, 59.52)=>(197.00, 163.32). Normalised: (0.96, 0.29)=>(0.77, 0.64): Result 22.85009

Result from Mathf.Atans (as above) .... that fact the results are all different not only from each other but from above really shows just how stuck I am:

Getting angle of (197.00, 73.82)=>(197.00, 59.52). Normalised: (0.94, 0.35)=>(0.96, 0.29): Result -1.244842
Getting angle of (197.00, 76.62)=>(197.00, 73.82). Normalised: (0.93, 0.36)=>(0.94, 0.35): Result -1.206062
Getting angle of (183.50, 140.60)=>(197.00, 116.29). Normalised: (0.79, 0.61)=>(0.86, 0.51): Result -0.9772647
Getting angle of (197.00, 116.29)=>(197.00, 76.62). Normalised: (0.86, 0.51)=>(0.93, 0.36): Result -1.118683
Getting angle of (190.75, 158.58)=>(183.50, 140.60). Normalised: (0.77, 0.64)=>(0.79, 0.61): Result -0.8971169
Getting angle of (197.00, 163.32)=>(190.75, 158.58). Normalised: (0.77, 0.64)=>(0.77, 0.64): Result 2.263658
Getting angle of (197.00, 59.52)=>(197.00, 163.32). Normalised: (0.96, 0.29)=>(0.77, 0.64): Result 2.063599

Atans without normalisation gives me the following. Progress!.... I still don't understand anything and would still really appreciate even a link to a tutorial or book or video or something! :D

Getting angle of (197.00, 73.82)=>(197.00, 59.52). Normalised: (197.00, 73.82)=>(197.00, 59.52): Result -1.570796
Getting angle of (197.00, 76.62)=>(197.00, 73.82). Normalised: (197.00, 76.62)=>(197.00, 73.82): Result -1.570796
Getting angle of (183.50, 140.60)=>(197.00, 116.29). Normalised: (183.50, 140.60)=>(197.00, 116.29): Result -1.063928
Getting angle of (197.00, 116.29)=>(197.00, 76.62). Normalised: (197.00, 116.29)=>(197.00, 76.62): Result -1.570796
Getting angle of (190.75, 158.58)=>(183.50, 140.60). Normalised: (190.75, 158.58)=>(183.50, 140.60): Result -1.953735
Getting angle of (197.00, 163.32)=>(190.75, 158.58). Normalised: (197.00, 163.32)=>(190.75, 158.58): Result -2.492572
Getting angle of (197.00, 59.52)=>(197.00, 163.32). Normalised: (197.00, 59.52)=>(197.00, 163.32): Result 1.570796

(Removing Normalisation from Vector2.Angle() gives the same results.... Why?!)

CodePudding user response:

"Vector" can mean different things depending on contexts. In a geometric context it usually represent either a point or a direction. The term 'vector' is sometimes used interchangeably with direction, but I think it it use useful to be more specific.

When computing angles they must represent directions since taking the angle of two points is meaningless. So you need to convert the points to directions. For example by taking the direction from the central point to the left and right points:

var direction1 = myPoints[0] - myPoints[1];
var direction2 = myPoints[2] - myPoints[1];
 var angle = Vector2.Angle(direction1 , direction2 );

and you should get an angle for half a circle.

I would argue that it would be clearer to use distinct Point2 and Direction2 types to avoid any confusion or misuse. And while some vector libraries has taken that approach, Unity3D have not.

  • Related