ITeye博客

vb分割三角形

cjc · 2008-11-27 09:32 · Markdown

题目要求:

分割三角形的方法是从一个大的等边三角形开始,将其三边的中点进行连线,分成相同的四个三角形,除中间外的三个三角形再重复上述过程,直到满足给定条件的层次数为止.

(原贴地址: http://topic.csdn.net/u/20081126/19/ea2362d5-613a-464d-8567-0ac64ca858a2.html)

 

模块代码:

  1. Type point
  2. x As Single
  3. y As Single
  4. End Type
  5. Sub drawit(ByVal deep As Long, ByRef pp1 As point, ByRef pp2 As point, ByRef pp3 As point)
  6. Dim pp(2) As point
  7. pp(0).x = (pp1.x + pp2.x) / 2
  8. pp(0).y = (pp1.y + pp2.y) / 2
  9. pp(1).x = (pp1.x + pp3.x) / 2
  10. pp(1).y = (pp1.y + pp3.y) / 2
  11. pp(2).x = (pp3.x + pp2.x) / 2
  12. pp(2).y = (pp3.y + pp2.y) / 2
  13. If deep = 1 Then
  14. Form1.ForeColor = QBColor(Int(Rnd * 16))
  15. For j = 0 To 2
  16. Form1.Line (pp(j).x, pp(j).y)-(pp((j + 1) Mod 3).x, pp((j + 1) Mod 3).y)
  17. Next
  18. Exit Sub
  19. End If
  20. If deep > 1 Then
  21. drawit 1, pp1, pp2, pp3
  22. drawit deep - 1, pp1, pp(0), pp(1)
  23. drawit deep - 1, pp2, pp(0), pp(2)
  24. drawit deep - 1, pp3, pp(2), pp(1)
  25. End If
  26. End Sub

窗体代码:

  1. Dim p(2) As point
  2. Private Sub Form_Load()
  3. Me.AutoRedraw = True
  4. p(0).x = Rnd
  5. p(0).y = 0
  6. p(1).x = 0
  7. p(1).y = 1
  8. p(2).x = 1
  9. p(2).y = 1
  10. Me.Scale (0, 0)-(1.1, 1.1)
  11. Me.Line (p(0).x, p(0).y)-(p(1).x, p(1).y)
  12. Me.Line (p(1).x, p(1).y)-(p(2).x, p(2).y)
  13. Me.Line (p(0).x, p(0).y)-(p(2).x, p(2).y)
  14. End Sub
  15. Private Sub Form_Click()
  16. drawit 5, p(0), p(1), p(2)
  17. End Sub

运行效果:

 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。