Comment écrire un code python pour trouver le point d'intersection entre deux droites ?

Exemple de code python pour trouver le point d'intersection entre deux droites:

Tracer les droites:

import matplotlib.pyplot as plt
import numpy as np

m1, b1 = 1.0, 2.0 # slope & intercept (line 1)
m2, b2 = 4.0, -3.0 # slope & intercept (line 2)

x = np.linspace(-10,10,500)

plt.plot(x,x*m1+b1)
plt.plot(x,x*m2+b2)

plt.xlim(-2,8)
plt.ylim(-2,8)

plt.title('How to find the intersection of two straight lines ?', fontsize=8)

plt.grid(linestyle='dotted')

plt.savefig("two_straight_lines_intersection_point_01.png", bbox_inches='tight')

Comment écrire un code python pour trouver le point d'intersection entre deux droites ?
Comment écrire un code python pour trouver le point d'intersection entre deux droites ?

Trouver le point d'intersection:

xi = (b1-b2) / (m2-m1)
yi = m1 * xi + b1

print('(xi,yi)',xi,yi)

donne ici (xi,yi) 1.6666666666666667 3.666666666666667:

Tracer le point d'intersection:

plt.scatter(xi,yi, color='black' )

plt.savefig("two_straight_lines_intersection_point_02.png", bbox_inches='tight')

Comment écrire un code python pour trouver le point d'intersection entre deux droites ?
Comment écrire un code python pour trouver le point d'intersection entre deux droites ?

Autre exemple

import matplotlib.pyplot as plt
import numpy as np

m1, b1 = 0.1, 2.0 # slope & intercept (line 1)
m2, b2 = 2.0, -3.0 # slope & intercept (line 2)

x = np.linspace(-10,10,500)

plt.plot(x,x*m1+b1,'k')
plt.plot(x,x*m2+b2,'k')

plt.xlim(-2,8)
plt.ylim(-2,8)

plt.title('How to find the intersection of two straight lines ?', fontsize=8)

xi = (b1-b2) / (m2-m1)
yi = m1 * xi + b1

print('(xi,yi)',xi,yi)

plt.axvline(x=xi,color='gray',linestyle='--')
plt.axhline(y=yi,color='gray',linestyle='--')

plt.scatter(xi,yi, color='black' )

plt.savefig("two_straight_lines_intersection_point_03.png", bbox_inches='tight')

Comment écrire un code python pour trouver le point d'intersection entre deux droites ?
Comment écrire un code python pour trouver le point d'intersection entre deux droites ?

Références

Image

of