4

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.

Community
  • 1
  • 1
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • 2
    You do it in python just like in any other language - by computing the translation and adding to the coordinates, this is not even close to the programming issue – lejlot Oct 12 '13 at 17:00
  • 2
    I think this is a math problem rather than a python problem. – Leon Young Oct 12 '13 at 17:02

1 Answers1

3

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^
poke
  • 369,085
  • 72
  • 557
  • 602