### Author Topic: Fractal Fun  (Read 15087 times)

0 Members and 1 Guest are viewing this topic. ##### Fractal Fun
« on: November 15, 2009, 10:44:38 AM »
Yet another Mathematical Thread A few codes demonstrating fractals and the like:

The Mandelbrot Set: Code: [Select]
`;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;;;                                                     ;;;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤;;;;                                                     ;;;;                                                     ;;;;        --=={  Mandelbrot Set Fractal  }==--         ;;;;                                                     ;;;;  The function will create a representation of the   ;;;;  Mandelbrot set by iteration of the funtion:        ;;;;                                                     ;;;;     f(z) = z^2 + c                                  ;;;;                                                     ;;;;  Where 'z' & 'c' are Complex. The fractal colour is ;;;;  determined by the number of iterations taken to    ;;;;  for the norm of the Complex Number to reach a      ;;;;  limit (less than a maxmimum iteration number).     ;;;;                                                     ;;;;  NOTE:-                                             ;;;;  Fractal Calculation is CPU intensive and may take  ;;;;  a long time to be generated.                       ;;;;                                                     ;;;;  With the current settings 360,000 points are       ;;;;  generated. Increase the xInc and yInc to reduce    ;;;;  this number, and lower the iteration limit to      ;;;;  decrease the calculation time of each point.       ;;;;                                                     ;;;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;;;                                                     ;;;;  AUTHOR:                                            ;;;;                                                     ;;;;  Copyright © Lee McDonnell, November 2009.          ;;;;                                                     ;;;; { Contact: Lee Mac @ TheSwamp.org, CADTutor.net }   ;;;;                                                     ;;;;                                                     ;;;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;;;                                                     ;;;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤;;(defun c:mfract (/ iLim xMin xMax yMin yMax xInc yInc x y a b i)  (vl-load-com)  (setvar "PDMODE" 0) (setvar "PDSIZE" 0)    (setq iLim 255  ;; Iteration Limit        xMin -2.0 xMax  1.0  ;; Image Size        yMin -1.5 yMax  1.5        xInc 0.005 yInc 0.005) ;; Resolution  (setq x (- xMin xInc))  (while (<= (setq x (+ x xInc)) xMax)        (setq y (- yMin yInc))    (while (<= (setq y (+ y yInc)) yMax)      (setq i 0 a 0. b 0.) ;; Mandelbrot      (while (and (< (norm a b) 4.)                  (<= (setq i (1+ i)) iLim))        (setq new (z^2 a b) a (+ (car new) x) b (+ (cadr new) y)))      (pnt (list x y) (1+ (rem i 255)))))  (princ))(defun pnt (pt col)  (entmakex    (list (cons 0 "POINT") (cons 10 pt) (cons 62 col))))(defun norm (x y)  (+ (* x x) (* y y)))(defun z^2 (x y)  (list (- (* x x) (* y y)) (* 2 x y)))`
Julia Set: http://www.theswamp.org/screens/leemac/Julia%20Set.png

Code: [Select]
`;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;;;                                                     ;;;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤;;;;                                                     ;;;;                                                     ;;;;          --=={  Julia Set Fractal  }==--            ;;;;                                                     ;;;;  The function will create a representation of the   ;;;;  Julia set by iteration of the funtion:             ;;;;                                                     ;;;;     f(z) = z^2 + c                                  ;;;;                                                     ;;;;  Where 'z' & 'c' are Complex. The fractal colour is ;;;;  determined by the number of iterations taken to    ;;;;  for the norm of the Complex Number to reach a      ;;;;  limit (less than a maxmimum iteration number).     ;;;;                                                     ;;;;  'c' is constant for each calculation - which sets  ;;;;  the Julia set apart from the Mandelbrot set, (in   ;;;;  which 'c' is set to zero for each point).          ;;;;                                                     ;;;;  NOTE:-                                             ;;;;  Fractal Calculation is CPU intensive and may take  ;;;;  a long time to be generated.                       ;;;;                                                     ;;;;  With the current settings 272,000 points are       ;;;;  generated. Increase the xInc and yInc to reduce    ;;;;  this number, and lower the iteration limit to      ;;;;  decrease the calculation time of each point.       ;;;;                                                     ;;;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;;;                                                     ;;;;  AUTHOR:                                            ;;;;                                                     ;;;;  Copyright © Lee McDonnell, November 2009.          ;;;;                                                     ;;;; { Contact: Lee Mac @ TheSwamp.org, CADTutor.net }   ;;;;                                                     ;;;;                                                     ;;;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;;;                                                     ;;;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤;;(defun c:jfract (/ iLim xMin xMax yMin yMax xInc yInc x y a b i)  (vl-load-com)  (setvar "PDMODE" 0) (setvar "PDSIZE" 0)    (setq iLim 255  ;; Iteration Limit        xMin -1.7 xMax  1.7  ;; Image Size        yMin -1.0 yMax  1.0        xInc 0.005 yInc 0.005 ;; Resolution        Re[c] -0.8   ;; Real Coefficient of Julia Constant        Im[c] 0.156  ;; Imaginary Coefficient of Julia Constant        )   (setq x (- xMin xInc))  (while (<= (setq x (+ x xInc)) xMax)        (setq y (- yMin yInc))    (while (<= (setq y (+ y yInc)) yMax)      (setq i 0 a x b y) ;; Julia      (while (and (< (norm a b) 4.)                  (<= (setq i (1+ i)) iLim))        (setq new (z^2 a b) a (+ (car new) Re[c]) b (+ (cadr new) Im[c])))      (pnt (list x y) (1+ (rem i 255)))))  (princ))(defun pnt (pt col)  (entmakex    (list (cons 0 "POINT") (cons 10 pt) (cons 62 col))))(defun norm (x y)  (+ (* x x) (* y y)))(defun z^2 (x y)  (list (- (* x x) (* y y)) (* 2 x y)))`
Koch Snowflake: Code: [Select]
`;; Koch Snowflake  ~  by Lee McDonnell(defun c:koch (/ pt rad ptLst p p1 p2 p3 p4 ang dis nLst)  (if (and (setq pt  (getpoint "\nSpecify Centre Point: "))           (setq rad (getdist "\nSpecify Radius: ")))    (progn      (setq ptLst (list (polar pt (/ pi 2.)  rad)                        (polar pt (/ pi -6.) rad)                        (polar pt (/ (* 7 pi) 6.) rad)) p (poly ptLst))      (while (and (not (initget "Yes No"))                  (/= "No" (getkword "\nAgain? [Y/N] <Yes> : ")))                (setq ptLst (append ptLst (list (car ptLst))))                (while (cadr ptLst)          (setq p1  (car ptLst) p2 (cadr ptLst) ang (angle p1 p2)                                dis (/ (distance p1 p2) 3.)                p3  (polar p1 ang dis) p4 (polar p1 ang (* 2. dis)))                    (setq nLst  (append nLst (list p3 (polar p3 (+ ang (/ pi 3.)) dis) p4 p2))                ptLst (cdr ptLst)))        (entdel p)        (setq ptLst nLst nLst nil p (poly ptLst))        (princ (strcat "\nNumber of Vertices: " (itoa (length ptLst)))))))  (princ))(defun poly (lst)    (entmakex    (append (list (cons 0 "LWPOLYLINE")     (cons 100 "AcDbEntity")                  (cons 100 "AcDbPolyline") (cons 90 (length lst)) (cons 70 1))            (mapcar (function (lambda (x) (cons 10 x))) lst))))`

WARNING:
The above codes are CPU intensive, and require generating 200,000+ points (depending on the parameters set), use them at your own risk!

Enjoy!

Lee

« Last Edit: February 22, 2010, 01:27:45 PM by Lee Mac » ##### Re: Fractal Fun
« Reply #1 on: November 15, 2009, 11:23:22 AM »
Very nice Lee.
More Fractal Fun
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970 ##### Re: Fractal Fun
« Reply #2 on: November 15, 2009, 02:36:46 PM »
Thanks Alan,

I'm never sure how many people will actually be interested in my "Mathematical threads"... but I know there are a few guys here who revel in the maths The Julia set is my favourite - there are so many variations, just by altering the Real and Imaginary values of the constant (c)...for example, see this page: http://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials ##### Re: Fractal Fun
« Reply #3 on: November 15, 2009, 06:48:04 PM »
Variations on the Julia Set:   #### Tankman

• Guest ##### Re: Fractal Fun
« Reply #4 on: November 15, 2009, 07:01:01 PM »
Like wow! Lee Mac!

Better that the graffiti you're know for!  ##### Re: Fractal Fun
« Reply #5 on: November 15, 2009, 07:04:19 PM »
Like wow! Lee Mac!

Better that the graffiti you're know for! Haha very funny... I remember that thread... #### m4rdy

• Newt
• Posts: 62 ##### Re: Fractal Fun
« Reply #6 on: November 15, 2009, 09:14:29 PM »
One of good site for learning and playing fractals ##### Re: Fractal Fun
« Reply #7 on: November 16, 2009, 01:37:49 PM »
Very nice!  ##### Re: Fractal Fun
« Reply #8 on: November 16, 2009, 02:51:08 PM »
This is a zoomed in version of the last Variation of the above (with a colour change).

You can really see the self-replicating behaviour in this one   ##### Re: Fractal Fun
« Reply #9 on: November 16, 2009, 05:32:29 PM »
Ok, I'm getting a bit addicted to generating these...

This one is a zoomed in version of the first of the three variations in the above post: #### JCTER

• Guest ##### Re: Fractal Fun
« Reply #10 on: November 16, 2009, 05:45:19 PM »
I've been playing around with your julia fractal routine and having a lot of fun, but boy it's like doing actual 3d renders, lol... it's time consuming to get a decent "resolution/size"

I got one I think will look sweet and have some neat things to show, but I'm going to set it to run as I walk out the door today.

Awesome project you took up there, Lee Mac thanks for sharing, so much. ##### Re: Fractal Fun
« Reply #11 on: November 16, 2009, 06:00:34 PM »
Thanks James There are various methods for calculating these fractals, but some have their drawbacks - for example, there is the IIM method (Inverse Iteration Method), which uses the function f(z)-> sqrt(z-c), but this means that you can't get the colour, and can also lead to problems.

The method that I am using iterates the function until it either reaches an iteration limit (set to 255), or when the "norm" of the complex number (modulus/size if you will) exceeds a certain limit. And the colour of that particular point is determined by the number of iterations taken to exceed that value.

But, you've got to be careful with this, as there is only finite memory allocated to AutoCAD, and that will put a limit on the number of points that can be generated.

My last image pushes it close, with around 1,300,000 points, but to get better images I tend to increase the resolution first (so that image generation is quick), and when I have identified a spot on the image that might be interesting to "blow up", I set the relevant image coordinates, and then take the resolution down to around 0.005.

However, as you can probably guess, the upper bound on the number of calcs the computer has to perform is somewhere around:

((y_max - y_min) / y_Resolution) x ((x_max - x_min) / x_Resolution) x Iteration_Limit

So, for my example:

((1.7 - (-1.7)) / 0.005) x ((1.0 - (-1.0)) / 0.005) x 255  =  69,360,000 Calculations

But, they are so worth it Lee ##### Re: Fractal Fun
« Reply #12 on: November 16, 2009, 07:57:37 PM »
A very zoomed in image of the Mandelbrot Set... so much going on... #### JCTER

• Guest ##### Re: Fractal Fun
« Reply #13 on: November 17, 2009, 08:53:34 AM »
figure I'll have to 'zoom in' more on a spot in this one... it wasn't as tight as I thought it would be.  Came out awesome though.

Julia set

#### GDF ##### Re: Fractal Fun
« Reply #14 on: November 17, 2009, 09:58:04 AM »
I love me fractals...geometry of deterministic chaos...very fluid...from snowflakes to galaxy clusters.
Thanks sharing...
Why is there never enough time to do it right, but always enough time to do it over?