TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Lee Mac on November 15, 2009, 10:44:38 AM

Title: Fractal Fun
Post by: Lee Mac on November 15, 2009, 10:44:38 AM
Yet another Mathematical Thread   :-P

A few codes demonstrating fractals and the like:

The Mandelbrot Set:

(http://www.theswamp.org/screens/leemac/Mandelbrot%20Set.png)

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

(http://www.theswamp.org/screens/leemac/Koch%20Snowflake.gif)

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


Title: Re: Fractal Fun
Post by: CAB on November 15, 2009, 11:23:22 AM
Very nice Lee.
More Fractal Fun (http://www.theswamp.org/index.php?action=search2;params=YWR2YW5jZWR8J3wxfCJ8YnJkfCd8MiwxMXwifHNob3dfY29tcGxldGV8J3x8InxzdWJqZWN0X29ubHl8J3x8Inxzb3J0X2RpcnwnfGRlc2N8Inxzb3J0fCd8cmVsZXZhbmNlfCJ8c2VhcmNofCd8RnJhY3RhbA==)
Title: Re: Fractal Fun
Post by: Lee Mac 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   8-)

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 (http://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials)
Title: Re: Fractal Fun
Post by: Lee Mac on November 15, 2009, 06:48:04 PM
Variations on the Julia Set:

(http://www.theswamp.org/screens/leemac/Julia%20Set2.png)

(http://www.theswamp.org/screens/leemac/Julia%20Set3.png)

(http://www.theswamp.org/screens/leemac/Julia%20Set4.png)

Title: Re: Fractal Fun
Post by: Tankman on November 15, 2009, 07:01:01 PM
Like wow! Lee Mac!

Better that the graffiti you're know for!   :lmao:
Title: Re: Fractal Fun
Post by: Lee Mac on November 15, 2009, 07:04:19 PM
Like wow! Lee Mac!

Better that the graffiti you're know for!   :lmao:

Haha very funny... I remember that thread...  :-D
Title: Re: Fractal Fun
Post by: m4rdy on November 15, 2009, 09:14:29 PM
One of good site for learning and playing fractals
http://www.takayaiwamoto.com/Fun_Math_by_CAD.html (http://www.takayaiwamoto.com/Fun_Math_by_CAD.html)
Title: Re: Fractal Fun
Post by: Lee Mac on November 16, 2009, 01:37:49 PM
Very nice!  :-)
Title: Re: Fractal Fun
Post by: Lee Mac 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  :wink:

(http://www.theswamp.org/screens/leemac/Julia%20Set5.png)
Title: Re: Fractal Fun
Post by: Lee Mac 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:

(http://www.theswamp.org/screens/leemac/Julia%20Set6.png)

Title: Re: Fractal Fun
Post by: JCTER 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.
Title: Re: Fractal Fun
Post by: Lee Mac 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  :wink:

Glad you like it mate,

Lee
Title: Re: Fractal Fun
Post by: Lee Mac on November 16, 2009, 07:57:37 PM
A very zoomed in image of the Mandelbrot Set... so much going on...

(http://www.theswamp.org/screens/leemac/Mandelbrot%20Set2.png)
Title: Re: Fractal Fun
Post by: JCTER 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

Title: Re: Fractal Fun
Post by: GDF 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...
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 11:26:57 AM
Nice one James!  :-)

If you want to add a colour shift, so that it doesn't use the standard Magenta's, Yellows etc, add it like this:

Code: [Select]
(pnt (list x y) (1+ (rem [color=red](+[/color] i [color=red]60)[/color] 255)))))
You can add whatever number you like to shift the colour set by that amount  :-)

Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 11:43:43 AM
Nice one James!  :-)

If you want to add a colour shift, so that it doesn't use the standard Magenta's, Yellows etc, add it like this:

Code: [Select]
(pnt (list x y) (1+ (rem [color=red](+[/color] i [color=red]60)[/color] 255)))))
You can add whatever number you like to shift the colour set by that amount  :-)



ooohhhh you beat me to my next experiment... I was hoping to see if I could manipulate the color to get a more standard range.

My next thought is to see about using RGB color instead of ACI color, though I'm not sure how to specify it... I imagine if I wanted a "monochromatic" output, it'd be a static number for either RG -or- B, and then a range for the remaining two colors, making sure they're equal, for example:

120,250,120
120,225,120
120,200,120
120,175,120

and so forth.

That'd take me some researchin' though, but I'm sure someone's already done this part, out there in the net.  But I'll be out of the office for lunch today so it'll have to be after work.
Title: Re: Fractal Fun
Post by: Shinyhead on November 17, 2009, 01:25:28 PM
someone make a routine to make these fractal beasts!  :-o
http://www.skytopia.com/project/fractal/2mandelbulb.html (http://www.skytopia.com/project/fractal/2mandelbulb.html)
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 02:05:25 PM
someone make a routine to make these fractal beasts!  :-o
http://www.skytopia.com/project/fractal/2mandelbulb.html (http://www.skytopia.com/project/fractal/2mandelbulb.html)
The only possible way I could imagine creating a complicated 3d shape is to create a single cross section at a time, creating a closed polyline or the points in order, and somehow getting the software to LOFT them from one to the next without making the software chuck a wobbly... which would be an impossible task for Autocad, lol.  Autocad has been known by me to fart and die over some cross sections with equal # of segments let alone something that may go from 120,000 segments to 250,000 segments and other variations of such gigundous numbers.

However if you take all those closed plines and make them regions instead, so that they are solidly filled when using a 'realistic' visual style, it might make for some neat visuals all the same.
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 03:23:14 PM
Wow! those 3D fractals look awesome! But I think it is a task that is too much for AutoCAD to handle somehow...

As for the RGB colour, yes, it could be done - I shall experiment :)
Title: Re: Fractal Fun
Post by: Maverick® on November 17, 2009, 03:40:59 PM
I read "Fraggle". 

Fail.  :-(

Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 05:39:27 PM
Ok, using James' idea -

RGB Variations  :lol:

(http://www.theswamp.org/screens/leemac/Julia%20Set%209.png)
(http://www.theswamp.org/screens/leemac/Julia%20Set8.png)
(http://www.theswamp.org/screens/leemac/Julia%20Set7.png)
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 05:53:31 PM
SWEET!!

That's pretty much exactly what I was getting at.

Now I'm going to have to try my hand at duplicating that.

Something tells me you've done it in 2 lines, max, and it'll take me 20+  :-D
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 05:55:15 PM
3 lines actually  :wink:

I'll give you a hint, RGB is DXF 420, 32 bit  :-)
Title: Re: Fractal Fun
Post by: T.Willey on November 17, 2009, 05:58:22 PM
Now just to get a good printer/plotter, and frame them as modern art, and change $1000 ( min. ) per.
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 05:59:26 PM
Now just to get a good printer/plotter, and frame them as modern art, and change $1000 ( min. ) per.

Haha I wish...  :-D
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 06:06:51 PM
And a Yellow just for fun  :-D

(http://www.theswamp.org/screens/leemac/Julia%20Set10.png)
Title: Re: Fractal Fun
Post by: T.Willey on November 17, 2009, 06:10:09 PM
Now just to get a good printer/plotter, and frame them as modern art, and change $1000 ( min. ) per.

Haha I wish...  :-D

Setup a website that people will be able to download them as wall papers, and see what happens.   :wink:

Sometimes people will just buy something because it is different, and they like to be on the cutting edge.  There is some art out there that is worth money, but to me is worthless, not priceless.  Eye of the beholder ..... and all.
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 06:18:47 PM
3 lines actually  :wink:

I'll give you a hint, RGB is DXF 420, 16 bit  :-)

I got as far as finding the 420 code.  I'm working on how it converts 255,0,0 to (420 . 16711680)

Then I should be ok :)

ETA:  using this chart http://easycalculation.com/hex-converter.php now I see what the DXF reference on my desk meant by "hex converted to decimal"

Now the hard part...
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 06:21:02 PM
Now just to get a good printer/plotter, and frame them as modern art, and change $1000 ( min. ) per.

Haha I wish...  :-D

http://shop.deviantart.com/wallart/?modView=none&qh=__in:digitalart/fractals

Title: Re: Fractal Fun
Post by: Maverick® on November 17, 2009, 06:29:18 PM
Call yours Pterofractals!
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 06:29:38 PM
Ok, I'll help you on that, I think I was wrong to say that the rgb code is 16-bit,

The code is calculated as follows...

(2^16) * Red  +  (2^8) * Green + Blue
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 06:32:09 PM
Ok, I'll help you on that, I think I was wrong to say that the rgb code is 16-bit,

The code is calculated as follows...

(2^16) * Red  +  (2^8) * Green + Blue

oh sweet.  that helps me so much.  the math to do that was what was killing me... it was a large leap for someone who is not educated on this stuff :( heh.
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 06:53:42 PM
How did you specify your color?  I'm getting a big brain frozen.

Did you use a variable assigned as a list concatenated to combine the R,B,G once converted to decimal values?  I'm feeling like I shouldn't play with the big boys :P

Here is where I've started modifying but I'm super unsure about how to get the 'decimal number' to actually form itself from the R, G, B parts.

Code: [Select]
     (pnt (list x y) (1+ (rem i 255)))))

  (princ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;This is where JamesCannon starts botching it up;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq rgbcol '(* (expt 2 16) i)) 0 0)

(defun pnt (pt col)
  (entmakex
    (list (cons 0 "POINT") (cons 10 pt) (cons 420 rgbcol))))
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 07:04:19 PM
Ok, I'll let you have a lookie  :-P

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) (rgb 0 0 (1+ (rem (* 10 i) 255))))))

  (princ))

(defun pnt (pt col)
  (entmakex
    (list (cons 0 "POINT") (cons 10 pt) (cons 420 col))))

(defun norm (x y)
  (+ (* x x) (* y y)))

(defun z^2 (x y)
  (list (- (* x x) (* y y)) (* 2 x y)))

(defun rgb (r g b)
  (+ (* 65536 r) (* 256 g) b))

The above should make a Blue one - I multiplied the iteration number (i) by 10 just to get more variation in the colour :-)
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 07:07:00 PM
Btw, the list when defined with an apostrophe, as in:

Code: [Select]
(setq rgbcol '(* (expt 2 16) i)) 0 0)
Will not be evaulated, you would need to use the "list" function, when you need to evaluate terms inside a list  :wink:

Just curious, are you using the VLIDE/Notepad++/Vim etc or just plain Notepad?
Title: Re: Fractal Fun
Post by: JCTER on November 17, 2009, 07:08:47 PM
  (+ (* 65536 r) (* 256 g) b))

d'oh

I was stuck on thinking I'd use strings, concatenate them, and convert it to a real or something... I'm ... clueless and awkward.

thank you so much!  I was thinking it wasn't an addition, for some reason, but a concatenation of RGB... I misunderstood what a 24bit number really means, I guess.


and I'm using plain notepad  :-D I got more comfortable using the VLIDE, but for some reason just didn't... this time... though I use Notepad++ for other things, too, and have used it for lisp before.
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 07:13:52 PM
 (+ (* 65536 r) (* 256 g) b))

d'oh

I was stuck on thinking I'd use strings, concatenate them, and convert it to a real or something... I'm ... clueless and awkward.

thank you so much!  I was thinking it wasn't an addition, for some reason, but a concatenation of RGB... I misunderstood what a 24bit number really means, I guess.

No problems mate - I didn't want to give you the solution right away, its always nice when you work towards it yourself  :-)  Much more satisfaction  :-)

and I'm using plain notepad  :-D I got more comfortable using the VLIDE, but for some reason just didn't... this time... though I use Notepad++ for other things, too, and have used it for lisp before.

Cool - it was only that I noticed a few of your brackets were out, 's all - so i was going to recommend you using the VLIDE or similar if you weren't already  :-)
Title: Re: Fractal Fun
Post by: wizman on November 17, 2009, 08:33:52 PM
I'm enjoying your fractals lee, thanks for sharing... :-)
Title: Re: Fractal Fun
Post by: Lee Mac on November 17, 2009, 08:35:22 PM
I'm enjoying your fractals lee, thanks for sharing... :-)

Thanks Wizman  :-)

Haven't seen you on here for a while - nice to speak to you again  :-)
Title: Re: Fractal Fun
Post by: wizman on November 17, 2009, 08:40:52 PM
out of lisp for months andI got a lot of catching up to do lee, keep up your good work.
Title: Re: Fractal Fun
Post by: Lee Mac on November 18, 2009, 05:26:48 AM
out of lisp for months andI got a lot of catching up to do lee, keep up your good work.

Thanks Wiz, I look forward to seeing your code again  :wink:
Title: Re: Fractal Fun
Post by: JCTER on November 18, 2009, 08:36:41 AM
Not entirely what I expected, so I'll have to rethink my methods.

Still came out cool.. I'm gonna have fun playing with the colors :)

Title: Re: Fractal Fun
Post by: Lee Mac on November 18, 2009, 09:51:22 AM
Yeah, I too need to work out a better way of using the RGB range - still looks pretty cool though  :wink:
Title: Re: Fractal Fun
Post by: JCTER on November 18, 2009, 09:57:55 AM
Yeah, I too need to work out a better way of using the RGB range - still looks pretty cool though  :wink:
Code: [Select]
      (pnt (list x y) (rgb
(* i 1.2)                      ;;;;; red
 (expt i 1.1)                  ;;;;; green
 (1+ (rem (* 25 i) 255))))))   ;;;;; blue

I tried incorporating the iteration into it, but this seemed to come out not at all like I expected.  I was trying to get it to provide varying strengths of blue, without showing flat grey on the low iterations, and closer to black, instead... so fun to play with.  To quote Patrick from Spongebob Squarepants, and countless others before him:

Quote
MATH IS POWER!
Title: Re: Fractal Fun
Post by: Lee Mac on November 18, 2009, 10:01:40 AM
Quote
MATH IS POWER!

Haha, so true  :-D
Title: Re: Fractal Fun
Post by: Lee Mac on November 18, 2009, 02:09:17 PM
Slightly off topic, and not really a fractal - but I thought you might be interested in the Butterfly Curve  :-)

This one I have made 3D  8-)

Code: [Select]
(defun c:butterfly_curve (/ s)
  (setq s (* -10.05 pi))
  (entmake (list (cons 0 "POLYLINE") (cons 66 1) (cons 70 8)))
  (while (<= (setq s (+ s 0.05)) (* 10. pi))
    (entmake
      (list (cons 0 "VERTEX") (cons 70 32)
            (cons 10 (list (* (sin s) (- (exp (cos s)) (* 2. (cos (* 4. s))) (expt (sin (/ s 12.)) 5)))
                           (* (cos s) (- (exp (cos s)) (* 2. (cos (* 4. s))) (expt (sin (/ s 12.)) 5)))
                           (* -1. (cos (* 4. s))))))))
  (entmake (list (cons 0 "SEQEND")))
  (princ))

Enjoy!
Title: Re: Fractal Fun
Post by: JCTER on November 19, 2009, 09:36:53 AM
there we go.  This is more along the lines of where I was hoping to go.
Title: Re: Fractal Fun
Post by: JCTER on November 19, 2009, 10:42:36 AM
Code: [Select]
Had an idear.  I have no software to triangulate a surface from a given point grid, but it might come out interesting if someone does.

I've modified the code shown below to also provide a Z variation of the points based upon iteration, similar to how it gets the color.  Forgive me if I did not place the added bits in the most elegant or proper position for the intent I tried to accomplish... I'm very amateur.

[code]

(defun c:jfract (/ iLim xMin xMax yMin yMax xInc yInc x y [color=red]z zFac [/color]a b i)
  (vl-load-com)
  (setvar "PDMODE" 0) (setvar "PDSIZE" 0)
  
  (setq iLim 255  ;; Iteration Limit

        xMin -0.3 xMax  0.7  ;; Image Size
        yMin -0.2 yMax  0.3

        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

[color=red] zFac 0.05    ;; Elevation range modifier[/color]

        )

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

[color=red] (setq z (* (* i (/ (+ xInc yInc) 2)) zFac))[/color]

        (setq new (z^2 a b) a (+ (car new) Re[c]) b (+ (cadr new) Im[c])))

      (pnt (list x y [color=red]z[/color]) (rgb
(* i 1) ;;;;; red
 (* i 1) ;;;;; green
 (1+ (rem (expt 1.1 i) 255)))))) ;;;;; blue

  (princ))

(defun pnt (pt col)
  (entmakex
    (list (cons 0 "POINT") (cons 10 pt) (cons 420 col))))

(defun norm (x y)
  (+ (* x x) (* y y)))

(defun z^2 (x y)
  (list (- (* x x) (* y y)) (* 2 x y)))

(defun rgb (r g b)
  (+ (* 65536 r) (* 256 g) b))



Code: [Select]
[color=red] (setq z (* (* i (/ (+ xInc yInc) 2)) zFac))[/color]
To explain what I was doing here...

I basically wanted the iterative number to also cause a variation in the Z axis, similar to how the iterative number causes a variation in the color value.  I also wanted the Z variation to kind of logically match the size/scale/density of the image you're trying to produce, so I incorporated the average of the xInc and yInc values multiplied by the iterative number.  I'm testing how this works on a variety of xInc/yInc values to see if it works as I intended.  Given that the Z value was going to be 0-255 (I think, since it's based on i) I knew that the Z variation would need to be scaled down for a more logical 'terrain' showing... thus I multiple the whole thing by the new variable "zFac" which is set to 0.05 in the above code.

I don't have any software that includes a "DRAPE" tool, or any surface triangulation tools, and even if I did, considering that some of our fractals have upward toward 1,000,000 points, I am not so sure it would be possible to do so... but it still makes for some fun imagery. :)[/code]
Title: Re: Fractal Fun
Post by: Lee Mac on November 19, 2009, 11:19:50 AM
Nice one James - nice idea about using the Z-Coord  :wink:
Title: Re: Fractal Fun
Post by: JCTER on November 19, 2009, 02:32:27 PM
SW ISO view with the plan view beside it. 

This is fun... you got me a little obsessed now LeeMac  :-D Thanks for the toys.  :lmao:
Title: Re: Fractal Fun
Post by: Lee Mac on November 19, 2009, 03:08:54 PM
Wow that is a little weird James! Not what I would have expected at all... I would love to be able to create the Mandelbulb, as shown in a previous link..

They are a little addictive though.. just trying different variations and waiting of the surprise of what is going to appear!  :-P
Title: Re: Fractal Fun
Post by: JCTER on November 19, 2009, 03:17:54 PM
Wow that is a little weird James! Not what I would have expected at all... I would love to be able to create the Mandelbulb, as shown in a previous link..

They are a little addictive though.. just trying different variations and waiting of the surprise of what is going to appear!  :-P

After looking at it, my first thought was "Hey, that's familiar!" and reminded me of: http://www.theswamp.org/index.php?topic=22296.0

Title: Re: Fractal Fun
Post by: Lee Mac on November 19, 2009, 03:31:45 PM
I can see why a triangulation would be good, looking at Daniel's image  ;-)
Title: Re: Fractal Fun
Post by: T.Willey on November 23, 2009, 01:44:34 PM
Lee,

  Maybe you should check out this thread.  I knew there was one already, but couldn't find it earlier.

[ http://www.theswamp.org/index.php?topic=17746.0 ]
Title: Re: Fractal Fun
Post by: Lee Mac on November 23, 2009, 01:56:52 PM
Lee,

  Maybe you should check out this thread.  I knew there was one already, but couldn't find it earlier.

[ http://www.theswamp.org/index.php?topic=17746.0 ]

Wow! Tim you just made me feel so inadequate in comparison to that guy!   :ugly:  :angel:
Title: Re: Fractal Fun
Post by: T.Willey on November 23, 2009, 02:12:38 PM
Lee,

  Maybe you should check out this thread.  I knew there was one already, but couldn't find it earlier.

[ http://www.theswamp.org/index.php?topic=17746.0 ]

Wow! Tim you just made me feel so inadequate in comparison to that guy!   :ugly:  :angel:

We are all learning to some extent.  Don't worry.  You have passed me way by with this.  :-)
Title: Re: Fractal Fun
Post by: JCTER on November 23, 2009, 02:45:26 PM
DANG! I had this set to run over the weekend... got it to zoomALL, for the screenshot, but I did a VIEW command to hit the SEI view, to show the variance in the Z elevation... and AutoCAD CRASHED!!!  :realmad:  It also failed to save the drawing for recovery, so I couldn't get it back :(

I'm going to try again tonight, and set the density slightly smaller to help.  Here's the SS anyhow...
Title: Re: Fractal Fun
Post by: Lee Mac on November 23, 2009, 02:53:03 PM
Wow! that is nice James  :lol:

But be careful about the memory limitations of AutoCAD... you are probably generating a few million points here..  ;-)
Title: Re: Fractal Fun
Post by: JCTER on November 23, 2009, 02:55:47 PM
Yea, I know, I keep pushing the envelope a bit.

I just want to get a decent DWG with the Z-effect done as I wanted... and then find some sort of triangulation routine.
Title: Re: Fractal Fun
Post by: HasanCAD on February 22, 2010, 09:47:17 AM
nice one

thanks
Title: Re: Fractal Fun
Post by: Lee Mac on February 22, 2010, 12:45:25 PM
Thanks Asos  :-)
Title: Re: Fractal Fun
Post by: JCTER on March 07, 2011, 11:15:08 AM
So I have a much more powerful computer now, than I did when I first was messing with this.

 :evil:
Title: Re: Fractal Fun
Post by: Lee Mac on March 07, 2011, 02:59:49 PM
So I have a much more powerful computer now, than I did when I first was messing with this.

 :evil:

lol Have fun James  8-)
Title: Re: Fractal Fun
Post by: Lee Mac on March 07, 2011, 03:10:50 PM
BTW, the latest stuff is here:

http://lee-mac.com/fractals.html (http://lee-mac.com/fractals.html)