TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Lee Mac on February 19, 2010, 07:55:06 PM

Title: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac 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-)

(http://www.theswamp.org/screens/leemac/fern.png)

(http://www.theswamp.org/screens/leemac/sierpinski.png)

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:

(http://www.theswamp.org/screens/leemac/sierpinski2.png)

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  :-)

(http://www.theswamp.org/screens/leemac/IteratedMaps.png)

(http://www.theswamp.org/screens/leemac/Example.png)

Enjoy,

Lee
Title: Re: Barnsley's Fern
Post by: pkohut on February 19, 2010, 09:00:03 PM
Nice
Title: Re: Barnsley's Fern
Post by: Lee Mac on February 19, 2010, 09:17:43 PM
Thanks PKohut  :-)
Title: Re: Barnsley's Fern
Post by: qjchen 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 :)
Title: Re: Barnsley's Fern
Post by: Lee Mac 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 (http://www.theswamp.org/index.php?topic=30966.0)

Lee
Title: Re: Barnsley's Fern
Post by: CAB 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 :-(
Title: Re: Barnsley's Fern
Post by: Lee Mac 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
Title: Re: Barnsley's Fern
Post by: qjchen 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
Title: Re: Barnsley's Fern
Post by: Lee Mac 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
Title: Re: Barnsley's Fern
Post by: Mark on February 20, 2010, 02:48:06 PM
that is some cool stuff.
Title: Re: Barnsley's Fern
Post by: Lee Mac on February 20, 2010, 03:45:39 PM
that is some cool stuff.

Thanks Mark  8-)
Title: Re: Barnsley's Fern
Post by: LE3 on February 20, 2010, 03:50:36 PM
gave it a try and can say:

Te quedo a toda madre!
Title: Re: Barnsley's Fern
Post by: Lee Mac 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  :-)
Title: Re: Barnsley's Fern
Post by: LE3 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
Title: Re: Barnsley's Fern
Post by: Lee Mac 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-)
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: wizman on February 20, 2010, 07:35:05 PM
Excellent Lee, thanks for sharing :-)
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac on February 20, 2010, 07:46:26 PM
Excellent Lee, thanks for sharing :-)

Thanks Wizman  :-)
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac 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
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: pkohut 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:
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac 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...
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: pkohut 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
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: hermanm 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.

Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: pkohut 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.
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac 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
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac on February 21, 2010, 01:29:33 PM
Found a small bug - bug squashed, code updated.  8-)
Title: Re: Barnsley's Fern & Sierpinski's Triangle
Post by: Lee Mac on February 21, 2010, 06:04:27 PM
One more  8-)

(http://www.theswamp.org/screens/leemac/itmap1.png)