题目要求:
分割三角形的方法是从一个大的等边三角形开始,将其三边的中点进行连线,分成相同的四个三角形,除中间外的三个三角形再重复上述过程,直到满足给定条件的层次数为止.
(原贴地址: http://topic.csdn.net/u/20081126/19/ea2362d5-613a-464d-8567-0ac64ca858a2.html)
模块代码:
- Type point
- x As Single
- y As Single
- End Type
- Sub drawit(ByVal deep As Long, ByRef pp1 As point, ByRef pp2 As point, ByRef pp3 As point)
- Dim pp(2) As point
- pp(0).x = (pp1.x + pp2.x) / 2
- pp(0).y = (pp1.y + pp2.y) / 2
- pp(1).x = (pp1.x + pp3.x) / 2
- pp(1).y = (pp1.y + pp3.y) / 2
- pp(2).x = (pp3.x + pp2.x) / 2
- pp(2).y = (pp3.y + pp2.y) / 2
- If deep = 1 Then
- Form1.ForeColor = QBColor(Int(Rnd * 16))
- For j = 0 To 2
- Form1.Line (pp(j).x, pp(j).y)-(pp((j + 1) Mod 3).x, pp((j + 1) Mod 3).y)
- Next
- Exit Sub
- End If
- If deep > 1 Then
- drawit 1, pp1, pp2, pp3
- drawit deep - 1, pp1, pp(0), pp(1)
- drawit deep - 1, pp2, pp(0), pp(2)
- drawit deep - 1, pp3, pp(2), pp(1)
- End If
- End Sub
窗体代码:
- Dim p(2) As point
- Private Sub Form_Load()
- Me.AutoRedraw = True
- p(0).x = Rnd
- p(0).y = 0
- p(1).x = 0
- p(1).y = 1
- p(2).x = 1
- p(2).y = 1
- Me.Scale (0, 0)-(1.1, 1.1)
- Me.Line (p(0).x, p(0).y)-(p(1).x, p(1).y)
- Me.Line (p(1).x, p(1).y)-(p(2).x, p(2).y)
- Me.Line (p(0).x, p(0).y)-(p(2).x, p(2).y)
- End Sub
- Private Sub Form_Click()
- drawit 5, p(0), p(1), p(2)
- End Sub
运行效果: