Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw perpendicular line of fixed length at a point of another line [closed]

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. enter image description here

The value of C and D will be used for accomplishing another objective using the Shapely library.

like image 592
Sourav Avatar asked Feb 04 '26 14:02

Sourav


1 Answers

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])

enter image description here

And the coordinates of CD:

>>> c.x, c.y
(12.316718427000252, 31.341640786499873)
>>> d.x, d.y
(17.683281572999746, 28.658359213500127)
like image 85
Georgy Avatar answered Feb 06 '26 03:02

Georgy