Dominique Makowski
Dominique Makowski

Reputation: 1673

Python - Derive segment coordinates from length and angle

I face a seemingly simple problem that somehow I didn't manage to solve, despite looking into several trigonometry and geometry intros.

I have a 2D space in which x=0; y=0 is the centre. I would like, given some position x1, y1 (being the coordinates of one end of the segment), and a length and an angle (0 denoting vertical lines), to find the coordinates of the other end of the segment.

In other words, being able to move from one set of parameters (x1; y1; angle; length) to (x1; y1; x2; y2) and vice versa.

Thanks a lot,

Upvotes: 1

Views: 119

Answers (1)

Blupper
Blupper

Reputation: 408

For this you want to use sine and cosine. Here is some example code:

from math import cos, sin, radians

a = radians(45)
l = 10
x1, y1 = (10, 15)

x2 += sin(a) * l
y2 += cos(a) * l

Here is an article about how and why this works.

Upvotes: 2

Related Questions