Sub ColorerDecolorer() 'bouton
If IsError(Application.Caller) Then Exit Sub
With ActiveSheet.DrawingObjects(Application.Caller)
If .Text Like "Col*" Then _
ColorerAireEntreCourbes [X_1], [Y_1], [X_2], [Y_2] Else _
Sheets("Source").Range("A4:B" & Rows.Count).Delete xlUp 'conserver 1 point pour la mise en forme
.Text = IIf(.Text Like "Col*", "Décolorer", "Colorer") & " l'aire entre les courbes"
End With
End Sub
Sub ColorerAireEntreCourbes(x1, y1, x2, y2)
Dim n&, deb, fin, e, x(), y(), i&, j&, a
n = 2000 'nombre de points
x1 = x1: y1 = y1: x2 = x2: y2 = y2 'matrices, plus rapides
With Application: deb = .Max(.Min(x1), .Min(x2)): fin = .Min(.Max(x1), .Max(x2)): End With
e = (fin - deb) / n
ReDim x(1 To n + 1, 1 To 1): ReDim y(1 To n + 1, 1 To 1)
For i = 1 To n Step 2
x(i, 1) = deb + (i - 1) * e
x(i + 1, 1) = x(i, 1)
j = Application.Match(x(i, 1), x1)
a = (y1(j + 1, 1) - y1(j, 1)) / (x1(j + 1, 1) - x1(j, 1))
y(i, 1) = y1(j, 1) + a * (x(i, 1) - x1(j, 1))
j = Application.Match(x(i, 1), x2)
a = (y2(j + 1, 1) - y2(j, 1)) / (x2(j + 1, 1) - x2(j, 1))
y(i + 1, 1) = y2(j, 1) + a * (x(i, 1) - x2(j, 1))
Next
With Sheets("Source").[A3].Resize(n)
.Value = x: .Columns(2).Value = y
.Name = "X": .Columns(2).Name = "Y"
End With
End Sub