Tim Korn
Tim Korn

Reputation: 23

How can I calculate the angle between two lines with the same starting point?

I'm working straight on a small HTML canvas game and I need here the angle between a straight line and the vertical. How can I calculate this? My straight line is as follows:

(example line)

var line = {x_start: 192, x_end: 288, y_start: 256, y_end};

This is what I whant to know in an visual way:

Visual illustration

Output of this get_degree() should be the angle in a degree format

Upvotes: 1

Views: 1318

Answers (1)

Nerdragen
Nerdragen

Reputation: 3174

If the input is consistently trying to calculate the angle against the vertical, then this shouldn't be too hard. First, subtract the start values for the end values, find the tangent angle, then add 90. If the x on the endpoint is less than the x on the starting point, then add 180.

deltaY = 320 - 256 = 64

deltaX = 288 - 192 = 96

Tangent angle of (deltaY/deltaX) = 30.96 degrees.

Since endpoint of x is greater than the start point, then you add 90. Else add 180. You can use atan() to calculate the tangent degree, but it returns radians, so you can convert it to degrees by multiplying it by 180/pi.

Upvotes: 1

Related Questions