I have 2 parallel lines (line1 = [(1,3),(4,3)]
and line2 = [(1,2),(4,2)]
):
Both have the same slope if I calculate it manually with m = (y-y1)/(x-x1)
:
line1: m1 = (3-3)/(1-4) = 0
line2: m2 = (2-2)/(1-4) = 0
m1 = m2 and therefore the lines are parallel, like shown in the picture.
But if I create shapely linestrings from line1
and line2
the slope are no longer equal...
import matplotlib.pyplot as plt
from shapely.geometry import LineString
line1 = LineString([(1,3),(4,3)])
line2 = LineString([(1,2),(4,2)])
fig, ax = plt.subplots()
ax.plot(*line1.xy)
ax.plot(*line2.xy)
xs1, xe1 = line1.coords[0]
ys1, ye1 = line1.coords[1]
m1 = (ys1-ye1)/(xs1-xe1)
xs2, xe2 = line2.coords[0]
ys2, ye2 = line2.coords[1]
m2 = (ys2-ye2)/(xs2-xe2)
print(m1,m2)
This prints -0.5
for m1
and -2.0
for m2
...
But why is it not 0 or at least equal?
CodePudding user response:
line1.cord[0] returns x1,y1 not x1,x2
import matplotlib.pyplot as plt
from shapely.geometry import LineString
line1 = LineString([(1,3),(4,3)])
line2 = LineString([(1,2),(4,2)])
fig, ax = plt.subplots()
ax.plot(*line1.xy)
ax.plot(*line2.xy)
xs1, ys1 = line1.coords[0]
xe1, ye1 = line1.coords[1]
m1 = (ye1-ys1)/(xe1-xs1)
xs2, ys2 = line2.coords[0]
xe2, ye2 = line2.coords[1]
m2 = (ye2-ys2)/(xe2-xs2)
print(m1,m2)