Sibbs Gambling
Sibbs Gambling

Reputation: 20355

How to extend a line segment in Python?

I have a line segment defined as its starting and ending points.

L = [(x1, y1), (x2, y2)] 

So

               (x1, y1)                       (x2, y2)
L:                A-------------------------------B

I now wish to extend the line by pulling apart these two points like so

              a                                         a
L:      A'--------A-------------------------------B-----------B'

So I need to update the coordinates of point A and B.

Suppose A'A = B'B = a

How to do it in Python?

This question may be quite related, but mine mainly focuses on the algorithm that does the task, instead of visualizing it in the figure.

Upvotes: 4

Views: 2674

Answers (1)

poke
poke

Reputation: 387825

Using vector math:

B = A + v
where
   v = B - A = (x2-x1, y2-y1)
   ||v|| = sqrt((x2-x1)^2 + (y2-y1)^2)

The normalized vector v^ with ||v^|| = 1 is: v^ = v / ||v||

To get the values of A' and B' you can now use the direction
of v^ and the length of a:
   B' = B + a * v^
   A' = A - a * v^

Upvotes: 3

Related Questions