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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12502
  • 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: 12502
  • 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: 12502
  • 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: 10395
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: 12502
  • 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: 12502
  • 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: 28708
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: 12502
  • 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: 12502
  • 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: 12502
  • 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-)

wizman

  • Bull Frog
  • Posts: 283
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #15 on: February 20, 2010, 07:35:05 PM »
Excellent Lee, thanks for sharing :-)

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #16 on: February 20, 2010, 07:46:26 PM »
Excellent Lee, thanks for sharing :-)

Thanks Wizman  :-)

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #17 on: February 20, 2010, 08:50:34 PM »
I have added a quick program to the first post, allowing you guys to create your own fractals, (that use three rules) - for example the Sierpinski Triangle and other variations  :-)

Lee

pkohut

  • Guest
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #18 on: February 20, 2010, 08:56:19 PM »
Just ran them locally, and will repeat my earlier comment - Nice!!

It occurs to me that these might be good candidates for someone wanting to learn CUDA. Any volunteers?  :kewl:

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #19 on: February 20, 2010, 09:00:36 PM »
Just ran them locally, and will repeat my earlier comment - Nice!!

It occurs to me that these might be good candidates for someone wanting to learn CUDA. Any volunteers?  :kewl:

Thanks Paul  8-)

Not heard of CUDA before, but is that the technique of encoding complicated graphics using 'rules' that define iterated maps, so that instead of a bitmap format, the graphic size is reduced? Or am I way off the mark...

pkohut

  • Guest
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #20 on: February 20, 2010, 09:36:54 PM »
Instead of the algorithms being CPU bound, you have the GPU do the computations instead.
So instead of doing the loop X number of times (50000 in your app) you set up a pipeline
and send it to the GPU which does Y number of computation in parallel (I think, based on
the number of GPU cores???, maybe more based on the number of shaders???).

http://www.nvidia.com/object/cuda_home_new.html

hermanm

  • Bull Frog
  • Posts: 282
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #21 on: February 20, 2010, 10:21:40 PM »
Why spend your time learning technology which is hardware-specific?

http://en.wikipedia.org/wiki/CUDA

Quote
Unlike OpenCL, CUDA-enabled GPUs are only available from NVIDIA (GeForce 8 series and above, Quadro and Tesla).[

http://en.wikipedia.org/wiki/OpenCL

Quote
OpenCL gives any application access to the Graphical Processing Unit for non graphical computing.

Personally, I am using an ATI card and am quite happy with it.

If you don't know where you are going, you might not get there.
  - L.P. Berra

pkohut

  • Guest
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #22 on: February 21, 2010, 12:25:51 AM »
Why spend your time learning technology which is ?

http://en.wikipedia.org/wiki/CUDA

Quote
Unlike OpenCL, CUDA-enabled GPUs are only available from NVIDIA (GeForce 8 series and above, Quadro and Tesla).[

http://en.wikipedia.org/wiki/OpenCL

Quote
OpenCL gives any application access to the Graphical Processing Unit for non graphical computing.

Personally, I am using an ATI card and am quite happy with it.



Ouch. Bit testy, eh?  Anyways, OpenCL would be a fine choice to learn as well. The idea is to explore outside ones normal boundaries.

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #23 on: February 21, 2010, 09:38:10 AM »
3 Rules is OK, but you can get even more variation with the choice of two or three - and a choice of colour  8-)

First post updated  :-)

Enjoy!

Lee

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #24 on: February 21, 2010, 01:29:33 PM »
Found a small bug - bug squashed, code updated.  8-)

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Barnsley's Fern & Sierpinski's Triangle
« Reply #25 on: February 21, 2010, 06:04:27 PM »
One more  8-)