VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "递归"
   ClientHeight    =   3405
   ClientLeft      =   1095
   ClientTop       =   1515
   ClientWidth     =   3675
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   3405
   ScaleWidth      =   3675
   Begin VB.PictureBox picture1 
      Height          =   2775
      Left            =   240
      ScaleHeight     =   2715
      ScaleWidth      =   2715
      TabIndex        =   0
      Top             =   360
      Width           =   2775
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
 Private Sub Picture1_Click()
  Dim n As Integer
  n = InputBox("输入n 的值 ")                   ' 递归层次
  picture1.Print Tab(5); "递归 n=" & n & "时的图形"
  picture1.Scale (0, 600)-(600, 0)           ' 自定义坐标系
  Call triangle(30, 320, 570, 30, 570, n)       ' 调用画三角形过程
End Sub
    
Private Sub triangle(x1!, x2!, x3!, y1!, y2!, k%)
  Dim u1!, u2!, v1!, V2!
   If (k > 1) Then                          ' 当没有到底层，递归调用
      u1 = (x1 + x2) / 2    ' 三条边的中点
      u2 = (x2 + x3) / 2
      v1 = (y1 + y2) / 2
      Call triangle(u1, x2, u2, v1, y2, k - 1)       '递归调用
      Call triangle(x1, u1, x2, y1, v1, k - 1)
      Call triangle(x2, u2, x3, y1, v1, k - 1)
   Else
      picture1.Line (x1, y1)-(x3, y1)          ' 到达递归底层画三角形
      picture1.Line (x1, y1)-(x2, y2)
      picture1.Line (x2, y2)-(x3, y1)
   End If
End Sub

