Bonsoir,
Voilà un programme Python donnant une figure:
###################################################################
# Un ensemble de points - Bernard-Maths - 11 Janvier 2025
###################################################################
###################################################################
# Importations
###################################################################
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
###################################################################
def maxfig():
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
mng.window.state('zoomed')
###################################################################
def DistancePointSegment(x,y,xA,yA,xB,yB):
ps=(x-xA)*(xB-xA)+(y-yA)*(yB-yA)
D2=(xB-xA)**2+(yB-yA)**2
if ps>D2:
return (x-xB)**2+(y-yB)**2
if ps<0:
return (x-xA)**2+(y-yA)**2
else:
a, b, c = yB-yA, xA-xB, xB*yA-xA*yB
return abs(a*x+b*y+c)**2/D2
###################################################################
N=50000
xA, yA = np.random.random(), np.random.random()
xB, yB = np.random.random(), np.random.random()
xC, yC = np.random.random(), np.random.random()
xD, yD = np.random.random(), np.random.random()
plt.close()
fig=plt.figure(linewidth=10,facecolor = 'gold',edgecolor='red')
plt.axis('equal')
plt.title('Ensemble de points', color='darkviolet', fontsize=40, fontname='Comic Sans MS', fontweight='bold')
for k in range(N):
x, y = np.random.random(), np.random.random()
Dist1=DistancePointSegment(x,y,xA,yA,xB,yB)
Dist2=DistancePointSegment(x,y,xC,yC,xD,yD)
if Dist1<Dist2:
plt.plot(x,y,'.c')
else:
plt.plot(x,y,'.m')
plt.plot([xA, xB],[yA, yB],'b',linewidth=5)
plt.plot([xC, xD],[yC, yD],'r',linewidth=5)
maxfig()
plt.show()
Cordialement,
Rescassol