I have two points A (10,20) and B (15,30). The points generate a line AB. I need to draw a perpendicular line, CD, on point B with a length of 6 (each direction 3 units) in Python.
I already have some properties of line AB using the following code:
from scipy import stats
x = [10,15]
y = [20,30]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
How can I calculate the location of C and D. I need their X and Y value.

The value of C and D will be used for accomplishing another objective using the Shapely library.
Since you are interested in using Shapely, the easiest way to get the perpendicular line that I can think of, is to use parallel_offset method to get two parallel lines to AB, and connect their endpoints:
from shapely.geometry import LineString
a = (10, 20)
b = (15, 30)
cd_length = 6
ab = LineString([a, b])
left = ab.parallel_offset(cd_length / 2, 'left')
right = ab.parallel_offset(cd_length / 2, 'right')
c = left.boundary[1]
d = right.boundary[0] # note the different orientation for right offset
cd = LineString([c, d])

And the coordinates of CD:
>>> c.x, c.y
(12.316718427000252, 31.341640786499873)
>>> d.x, d.y
(17.683281572999746, 28.658359213500127)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With