Triangle de Sierpinski sur Excel
Utiliser une feuille Excel pour générer le triangle de Sierpinski
Vidéo sur @SparksMaths
Code VBA
Sub draw()Dim cel1 As RangeDim cel2 As RangeDim cel3 As RangeDim cel As RangeDim i As IntegerDim j As IntegerDim l As SingleDim Phi As SingleSet cel1 = Range("A1")Set cel2 = Range("A1").Offset(0, 200)Set cel3 = Range("A1").Offset(100, 100)cel1.Interior.Color = vbRedcel2.Interior.Color = vbBluecel3.Interior.Color = vbGreenSet cel = cel1.Offset(25, 100)cel.Interior.Color = vbBlackFor i = 1 To 10000j = Int((3 - 1 + 1) * Rnd() + 1)Select Case jCase 1l = Sqr((cel1.Column - cel.Column) ^ 2 + (cel1.Row - cel.Row) ^ 2)Phi = WorksheetFunction.Atan2((cel1.Column - cel.Column), (cel1.Row - cel.Row))Case 2l = Sqr((cel2.Column - cel.Column) ^ 2 + (cel2.Row - cel.Row) ^ 2)Phi = WorksheetFunction.Atan2((cel2.Column - cel.Column), (cel2.Row - cel.Row))Case 3l = Sqr((cel3.Column - cel.Column) ^ 2 + (cel3.Row - cel.Row) ^ 2)Phi = WorksheetFunction.Atan2((cel3.Column - cel.Column), (cel3.Row - cel.Row))End SelectSet cel = Cells(cel.Row + Fix(0.5 * l * Sin(Phi)), cel.Column + Fix(0.5 * l * Cos(Phi)))Select Case jCase 1cel.Interior.Color = vbRedCase 2cel.Interior.Color = vbBlueCase 3cel.Interior.Color = vbGreenEnd SelectNextEnd Sub
Commentaires