Author Topic: [challenge] Sierpinski triangle  (Read 6806 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: [challenge] Sierpinski triangle
« Reply #15 on: January 04, 2016, 06:14:18 PM »
Both interesting methods!

Here is a direct translation of my above recursive solution into an iterative solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun sierpinski ( p1 p2 p3 n / lst m1 m2 m3 )
  2.     (setq lst (list (list p1 p2 p3)))
  3.     (repeat n
  4.         (foreach x lst
  5.             (setq m1  (mid (car  x) (cadr  x))
  6.                   m2  (mid (car  x) (caddr x))
  7.                   m3  (mid (cadr x) (caddr x))
  8.                   lst (append (cdr lst)
  9.                           (list (list (car x)  m1 m2)
  10.                                 (list m1 (cadr x) m3)
  11.                                 (list m2 m3 (caddr x))
  12.                           )
  13.                       )
  14.             )
  15.         )
  16.     )
  17.     (foreach x lst
  18.         (entmake (cons '(0 . "SOLID") (mapcar 'cons '(13 10 11 12) (cons (last x) x))))
  19.     )
  20. )
  21. (defun mid ( a b )
  22.     (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) a b)
  23. )

Test program as before:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test1 ( / p1 p2 p3 )
  2.     (if (and (setq p1 (getpoint "\nSpecify 1st point of triangle: "))
  3.              (setq p2 (getpoint "\nSpecify 2nd point of triangle: " p1))
  4.              (setq p3 (getpoint "\nSpecify 3rd point of triangle: " p1))
  5.         )
  6.         (sierpinski
  7.             (trans p1 1 0)
  8.             (trans p2 1 0)
  9.             (trans p3 1 0)
  10.             (progn (initget 6) (cond ((getint "\nSpecify number of iterations <5>: ")) (5)))
  11.         )
  12.     )
  13.     (princ)
  14. )

Fun challenge gile!  :-)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: [challenge] Sierpinski triangle
« Reply #16 on: January 05, 2016, 09:51:55 AM »
For variety: VBA (I very rarely use it...). Simple recursive solution.
Code: [Select]
Sub Sierpinski_Triangle()
    Dim pt1 As Variant
    Dim pt2 As Variant
    Dim pt3 As Variant
    Dim num As Integer
    pt1 = ThisDrawing.Utility.GetPoint(, "Point 1: ")
    pt2 = ThisDrawing.Utility.GetPoint(pt1, "Point 2: ")
    pt3 = ThisDrawing.Utility.GetPoint(pt1, "Point 3: ")
    num = ThisDrawing.Utility.GetInteger("Number of iterations: ")
    DrawSierpinski pt1, pt2, pt3, num
End Sub

Function DrawSierpinski(pt1 As Variant, pt2 As Variant, pt3 As Variant, num As Integer)
    If num = 0 Then
        DrawTriangle pt1, pt2, pt3
    Else
        DrawSierpinski pt1, MidPoint(pt1, pt2), MidPoint(pt1, pt3), num - 1
        DrawSierpinski pt2, MidPoint(pt1, pt2), MidPoint(pt2, pt3), num - 1
        DrawSierpinski pt3, MidPoint(pt1, pt3), MidPoint(pt2, pt3), num - 1
    End If
End Function

Function DrawTriangle(pt1 As Variant, pt2 As Variant, pt3 As Variant) As AcadSolid
    Set DrawTriangle = ThisDrawing.ModelSpace.AddSolid(pt1, pt2, pt3, pt3)
End Function

Function MidPoint(pt1 As Variant, pt2 As Variant) As Variant
    Dim pt(0 To 2) As Double
    pt(0) = (pt1(0) + pt2(0)) / 2
    pt(1) = (pt1(1) + pt2(1)) / 2
    pt(2) = (pt1(2) + pt2(2)) / 2
    MidPoint = pt()
End Function

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: [challenge] Sierpinski triangle
« Reply #17 on: March 08, 2016, 05:23:28 PM »
This challenge inspired me to publish a dedicated program on my site, with an accompanying write-up: Sierpinski Triangle.

Thank you gile for posting an interesting topic for us to explore :-)