Author Topic: Barnsley's Fern & Sierpinski's Triangle  (Read 12551 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Barnsley's Fern & Sierpinski's Triangle
« on: February 19, 2010, 07:55:06 PM »
Had a bit of spare time, just thought I'd treat you to a bit fun with a recursively iterated mapping  8-)





Code: [Select]
(defun c:fern (/ ptlst pt probability)
  ;; Lee Mac  ~  20.02.10
  (repeat 50000    
    (setq probability (rng))

    (Point (setq pt  (cond (  (< probability 0.01)
                              (iterate pt '((0.0 0.0)
                                            (0.0 0.16)) '(0.0 0.0)))

                           (  (<= 0.01 probability 0.86)
                              (iterate pt '(( 0.85 0.04)
                                            (-0.04 0.85)) '(0.0 1.6)))

                           (  (< 0.86 probability 0.93)
                              (iterate pt '((0.20 -0.26)
                                            (0.23  0.22)) '(0.0 1.6)))

                           (t (iterate pt '((-0.15 0.28)
                                            ( 0.26 0.24)) '(0.0 0.44)))))))
  (princ))

(defun c:sierpinski (/ ptlst pt probability)
  ;; Lee Mac  ~  20.02.10
  (repeat 50000    
    (setq probability (rng))

    (Point (setq pt (cond (  (< probability 0.333)
                             (iterate pt '((0.5 0.0)
                                           (0.0 0.5)) '(0.0 0.0)))

                          (  (<= 0.333 probability 0.666)
                             (iterate pt '((0.5 0.0)
                                           (0.0 0.5)) '(0.5 0.0)))
                          
                          (t (iterate pt '((0.5 0.0)
                                           (0.0 0.5)) '(0.25 0.5)))))))
  (princ))

(defun iterate (point matrix vector)
  (mapcar
    (function +)
      (mapcar
        (function
          (lambda (row)
            (apply (function +)
                   (mapcar (function *) row point)))) matrix) vector))

(defun rng (/ modulus multiplier increment random) ;; Stig
  (if (not seed) (setq seed (getvar "DATE")))
  (setq modulus    4294967296.0 multiplier 1664525 increment 1
        seed       (rem (+ (* multiplier seed) increment) modulus)
        random     (/ seed modulus)))

(defun Point (pt)
  (entmakex (list (cons 0 "POINT") (cons 10 pt) (cons 62 40))))

A Variation on Sierpinski's Triangle:



You can alter the various matrices and vectors to get different shaped ferns  :-)

<<---=={  EDIT  }==--->>

Had a bit of time, so I quickly put together a small program to allow you guys to easily generate these kind of fractals   8-)

Experiment with various rotations and translations, and see what you get after a few iterations  :-)





Enjoy,

Lee
« Last Edit: February 21, 2010, 01:29:14 PM by Lee Mac »

pkohut

  • Guest
Re: Barnsley's Fern
« Reply #1 on: February 19, 2010, 09:00:03 PM »
Nice

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #2 on: February 19, 2010, 09:17:43 PM »
Thanks PKohut  :-)

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: Barnsley's Fern
« Reply #3 on: February 20, 2010, 12:31:28 AM »
Hi, Lee Mac

I also do some similar work

http://www.theswamp.org/index.php?topic=11255.0

I hope you like it also :)
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #4 on: February 20, 2010, 07:29:47 AM »
Hi, Lee Mac

I also do some similar work

http://www.theswamp.org/index.php?topic=11255.0

I hope you like it also :)

Very nice Chen!

I think I stumbled across that thread in the past - nice work  :-)

Similar also to this:


http://www.theswamp.org/index.php?topic=30966.0


Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Barnsley's Fern
« Reply #5 on: February 20, 2010, 07:50:21 AM »
I keep an Australian Tree Furn right outside my window. Looks very similar.
Just wish I could understand the math :-(
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #6 on: February 20, 2010, 07:58:56 AM »
Just wish I could understand the math :-(

Thanks Alan  :-)

As far as the math is concerned - I think it probably looks more complicated than it is  :-)

You can apply a matrix to a point [ in this case (x,y) ], the matrix can transform the point through scaling, rotation, shearing, reflection. And a vector may be added to perform a translation.

In the Barnsley fern, you start with the origin (0,0), and, depending upon a probability, you apply one of four matrix/vector combinations to get the next point, then use that point to get the next, etc.

The method is very similar to using vla-transformby, using a transformation matrix  :-)

Hope this helps!  :-)

Lee

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: Barnsley's Fern
« Reply #7 on: February 20, 2010, 08:17:01 AM »
Lee Mac, ~ the bitmap like fractal pictures of you are very beautiful

And a similar post as follows, writen by my good Chinese friend, highflybird~

http://www.theswamp.org/index.php?topic=17746.msg214644#msg214644

There is another pure Autolisp version, let me search

It seems not published in theswamp, but here

http://www.mjtd.com/BBS/dispbbs.asp?BoardID=3&replyID=49419&id=58371&skin=0

http://www.mjtd.com/BBS/dispbbs.asp?boardid=3&replyid=49419&id=58371&page=1&skin=0&landlord=0&Star=2


give some example
« Last Edit: February 20, 2010, 08:22:46 AM by yuanqiu »
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #8 on: February 20, 2010, 11:25:05 AM »
Thanks Chen,

highflybird's fractals are amazing! Nice idea to use the picture box in DCL - probably much quicker than generating the points in AutoCAD..

I love that Fractal from Newton's method in the link you provided - that is beautiful.  :-)

Thanks

Lee

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Barnsley's Fern
« Reply #9 on: February 20, 2010, 02:48:06 PM »
that is some cool stuff.
TheSwamp.org  (serving the CAD community since 2003)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #10 on: February 20, 2010, 03:45:39 PM »
that is some cool stuff.

Thanks Mark  8-)

LE3

  • Guest
Re: Barnsley's Fern
« Reply #11 on: February 20, 2010, 03:50:36 PM »
gave it a try and can say:

Te quedo a toda madre!

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #12 on: February 20, 2010, 05:04:11 PM »
gave it a try and can say:

Te quedo a toda madre!


Thanks Luis (I think!)

Updated first post to include the Sierpinski Triangle  :-)

LE3

  • Guest
Re: Barnsley's Fern
« Reply #13 on: February 20, 2010, 05:16:14 PM »
gave it a try and can say:

Te quedo a toda madre!


Thanks Luis (I think!)

Updated first post to include the Sierpinski Triangle  :-)

That it is an expression used in Spanish, to say in short cool ! - don't worried

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Barnsley's Fern
« Reply #14 on: February 20, 2010, 05:59:05 PM »
gave it a try and can say:

Te quedo a toda madre!


Thanks Luis (I think!)

Updated first post to include the Sierpinski Triangle  :-)

That it is an expression used in Spanish, to say in short cool ! - don't worried

Haha Thanks Luis  8-)