• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

### #Actualhaegarr

Posted 26 February 2014 - 02:11 AM

Inverse trigonometric functions are applied to a length ratio which is a scalar value; there is no definition for vectors / points! With your formula you would need to calculate a division of two vectors what also is not defined that way.

Looking at the illustration in the OP, you want to compute the angle between the principal x vector

x = [ 1 0 ]T

and the vector v between point p1 and point p2

v = p2 - p1

The arccos computes the angle between the adjacent and the hypothenuse, where both of these legs are what you have given as x and v. Taking advantage of the dot-product

v . x = |v| * |x| * cos( <v,x> )

where the angle <v,x> is your theta, you get

<v,x> = arccos( ( v . ) / ( |v| * |x| ) )

Now one of the vectors, namely x, is a unit vector, i.e. has length |x| == 1, so that the formula is simplified to

<v,x> = arccos( ( v . ) / |v| )

Furthermore the y component of x is always 0 and its x component is always 1, so the dot-product is simplified to
v . x = vx * 1 + vy * 0 = vx
and hence the formula becomes

<v,x> = arccos( vx / |v| )

EDIT: The OP mentioned to compute "the smaller angle between the line and the x axis", which, following the more detailed requirements in one of the following posts, is too broadly interpreted by the above solution.

### #2haegarr

Posted 26 February 2014 - 02:07 AM

Inverse trigonometric functions are applied to a length ratio which is a scalar value; there is no definition for vectors / points! With your formula you would need to calculate a division of two vectors what also is not defined that way.

Looking at the illustration in the OP, you want to compute the angle between the principal x vector

x = [ 1 0 ]T

and the vector v between point p1 and point p2

v = p2 - p1

The arccos computes the angle between the adjacent and the hypothenuse, where both of these legs are what you have given as x and v. Taking advantage of the dot-product

v . x = |v| * |x| * cos( <v,x> )

where the angle <v,x> is your theta, you get

<v,x> = arccos( ( v . ) / ( |v| * |x| ) )

Now one of the vectors, namely x, is a unit vector, i.e. has length |x| == 1, so that the formula is simplified to

<v,x> = arccos( ( v . ) / |v| )

Furthermore the y component of x is always 0 and its x component is always 1, so the dot-product is simplified to
v . x = vx * 1 + vy * 0 = vx
and hence the formula becomes

<v,x> = arccos( vx / |v| )

EDIT: The OP mentioned to compute "the smaller angle between the line and the x axis", but it appears in the following posts that instead the directed angle from the x axis to the line is what is of interest. The approach above computes the smaller angle, not the directed one. So yep, use the atan2 instead.

### #1haegarr

Posted 25 February 2014 - 07:06 AM

Inverse trigonometric functions are applied to a length ratio which is a scalar value; there is no definition for vectors / points! With your formula you would need to calculate a division of two vectors what also is not defined that way.

Looking at the illustration in the OP, you want to compute the angle between the principal x vector

x = [ 1 0 ]T

and the vector v between point p1 and point p2

v = p2 - p1

The arccos computes the angle between the adjacent and the hypothenuse, where both of these legs are what you have given as x and v. Taking advantage of the dot-product

v . x = |v| * |x| * cos( <v,x> )

where the angle <v,x> is your theta, you get

<v,x> = arccos( ( v . ) / ( |v| * |x| ) )

Now one of the vectors, namely x, is a unit vector, i.e. has length |x| == 1, so that the formula is simplified to

<v,x> = arccos( ( v . ) / |v| )

Furthermore the y component of x is always 0 and its x component is always 1, so the dot-product is simplified to
v . x = vx * 1 + vy * 0 = vx
and hence the formula becomes

<v,x> = arccos( vx / |v| )

PARTNERS