Jump to content

Recommended Posts

Posted (edited)

Lee Mac has interesting programs: Attractors, Fractals, Sierpinski Triangle...
Has anyone tried to build a Fibonacci sequence?
Representation of Fibonacci numbers in the form of squares with a side of the appropriate size.

(defun fib (n)
  (if (or (= n 1) (= n 2))
    1   
    (+ (fib (- n 1)) (fib (- n 2)))  
  )
)

(defun c:Fibnum ( / n result)
    (setq n (getint "\nEnter the number of the Fibonacci: "))
  
    (if (> n 0)
    (progn
      (setq result (fib n)) 
      (princ (strcat "\nFibonacci number for n=" (itoa n) ": " (rtos result 2 2))) 
    )
  )
  (princ)
)

 

Fibonacci.png  ⁉️

Edited by Nikon
Posted (edited)

EDIT: oh wait ... I got a mistake somewhere.

It makes a spiral, but not the Fibonacci spiral exactly.

 

(vl-load-com)

;; https://www.cadtutor.net/forum/topic/75640-arc-start-and-end-point-help/
;; Arc Endpoints  -  Lee Mac
;; Returns the endpoints of an Arc expressed in WCS
(defun LM:ArcEndpoints ( ent / cen nrm rad )
    (setq ent  (entget ent)
          nrm  (cdr (assoc 210 ent))
          cen  (cdr (assoc 010 ent))
          rad  (cdr (assoc 040 ent))
    )
    (mapcar
        (function
            (lambda ( ang )
                (trans (mapcar '+ cen (list (* rad (cos ang)) (* rad (sin ang)) 0.0)) nrm 0)
            )
        )
        (list (cdr (assoc 50 ent)) (cdr (assoc 51 ent)))
    )
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun drawArc (cen rad sAng eAng)
 (entmakex (list (cons 0 "ARC")
                 (cons 10  cen)
                 (cons 40  rad)
                 (cons 50 sAng)
                 (cons 51 eAng)))
)

(defun deg2rad (deg)
	(* pi (/ deg 180.0))
)

(defun c:Fibonacci ( / it ind arc fib fib_prev cen prev_cen rad sAng eAng endpts) 

	(setq it (getint "\nHow many iterations (above 2 please): "))

;; iteration 1
	(setq ind 1)
	(setq fib_prev 1)
	(setq fib 1)
	
	(setq 
		cen (list 0.0 0.0)
		rad 1.0
		sAng 0
		eAng (deg2rad 90)
	)
	(setq arc (drawArc cen rad sAng eAng))
	
;; iteration 2
	(setq ind 2)
	(setq fib_prev 1)
	(setq fib 1)
	
	(setq 
		cen (list 0.0 0.0)
		rad 1.0
		sAng (deg2rad 90)
		eAng (deg2rad 180)
	)
	
	(setq arc (drawArc cen rad sAng eAng))
	
	;; iteration 3 and further 
	(while (<= ind it)
		(setq ind (+ ind 1))
		(setq fib_prev fib)
		(setq fib (+ fib_prev fib))
		
		(setq endpts (LM:ArcEndpoints arc))
		
		(setq 
			sAng eAng       				;; start angle is the previous end angle
			eAng (+ eAng (deg2rad 90))		;; end angle: add 90°
			cen (polar (nth 1 endpts) (+ eAng (deg2rad 90)) fib) 				;; new center: endpoint of the previous arc; polar => 90° extra of endArc, dist of fibonacci length
			rad fib
		)
		(setq arc (drawArc cen rad sAng eAng))
	)
	

	(princ)
)

 

 

 

Edited by Emmanuel Delay
  • Like 1
Posted (edited)
  On 3/25/2025 at 8:30 AM, Emmanuel Delay said:

It makes a spiral, but not the Fibonacci spiral exactly.

Expand  

@Emmanuel Delay Thanks, the code creates a spiral at the origin of coordinates, but I'm interested in the possibility
of creating squares with corresponding sides... for example, up to 19 iterations...
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, …

 

fib-squares.png

for example, start with a large square (for iteration 19 - 4181), 
and then build clockwise squares with sides 2584, 1597, 987, respectively...

Edited by Nikon
Posted

Maybe something like this?

(defun c:GLAVCVSfibo (/ n cierraOtro p1 p2 p3 p4)
  (defun cierraOtro (f1 f2)
    (setq d (+ f1 f2)
	  ang (angle p2 p3)
    )
    (command "pol" p3 (setq p2 (polar p3 ang d)) (setq p3 (polar p2 (- ang i) d)) (setq p4 (polar p3 (- ang i i) d)) (polar p4 (- ang PI i) d) "")
  )
  (setq n (getint "\nFibonaciCAD: Specifies number of sequences: "))
  (setq f1 0 f2 1 i (/ PI 2))
  (command "pol" '(0 0) (setq p2 '(-1 0)) (setq p3 '(-1 1)) '(0 1) '(0 0) "")
  (repeat n
    (setq f f2 f2 (+ f1 f2) f1 f)
    (cierraOtro f1 f2)
  )
  (princ)
)

 

  • Like 1
Posted

Oh wait...

Here's a script I wrote 15 years ago

 

;
;  Fibonacci spiral
;

(defun fib ()
  (princ "\nThis command draws a Fibonacci spiral.  On my computer the max number of lines is a bit less than 50")
  (setq number_of_lines (getint "\nDraw a Fibonacci spiral.\nHow many sides: "))
  
  (setq insertPoint         (list 0 0))
  (setq previousInsertPoint (list 0 0))
  (setq twoPointsBack       (list 0 0))

  (setq key 1)
  (repeat 
    number_of_lines
    (progn
        ; length of the  line
      (setq newLength (fibonacci key))
        ; the newest line is drawn +90° of the previous line
      (setq remainder (rem key 4) )
      (setq angle (* remainder (/ PI 2.0)) )
        ; draw the line.  Retrieve the secondary point (that point will be the insert point of the next line)
      (setq secondaryPoint (drawNewLine insertPoint angle newLength ))

        ; ARC
      (setq radius (distance previousInsertPoint insertPoint))
      (setq center twoPointsBack)
      (setq start_angle (* (- remainder 1) (/ PI 2.0)) )
      (setq end_angle angle)
        
      (entmake
        (entmake_arc center radius start_angle end_angle)   ; listed properties of an arc.  ready to entmake
      )
      
        ;; prepare variables for new iteration
      (setq oldLength newLength)
      (setq twoPointsBack previousInsertPoint)
      (setq previousInsertPoint insertPoint)
      (setq insertPoint secondaryPoint)
      (setq key (+ 1 key) )
    )
  )
  (princ)
)

(defun drawNewLine (insertPoint angle distance / secondaryPoint)
    (setq secondaryPoint (polar insertPoint angle distance))
    (entmake
      (mapcar 'cons
        (list 0 100 8 62 100 10 11)
        (list "LINE" "AcDbEntity" "0" 3 "AcDbLine" insertPoint secondaryPoint)
      )
    )
  secondaryPoint
)

(defun fibonacci (index / new_value)
  
  (setq new_value nil) ; New value.  Each value (except the first two) is the sum of two previous values.  results in: 1 1 2 3 5 8 13 21 ...
  (setq val_a 1)    ; 1 value back
  (setq val_b 1)    ; 2 values back

  (if (= index 0)
    (progn
    )
  )
  (if (= index 1)
    (progn
      (setq new_value 1)
    )
  )
  (if (= index 2)
    (progn
      (setq new_value 1)
    )
  )
  (if (> index 2)
    (progn
      (setq i 0)
      (repeat (- index 2)
        (progn
          (setq new_value (+ val_a val_b))
          ; preapre for new iteration
          (setq val_a val_b)
          (setq val_b new_value)
        )
      )
    )
  )
  new_value
)

(defun entmake_arc (Center Radius start_angle end_angle / opts)
   (setq opts
    (mapcar 'cons
      (list 0 100 100 10 40 210 100 50 51)
      (list 
        "ARC" 
        "AcDbEntity" 
        "AcDbCircle"
        Center
        Radius
        '(0 0 1)
        "AcDbArc"
        start_angle
        end_angle
      )
    )
  )
  ;(std-%entmake-template elist opts '(10 40 50 51))
  opts
)

(defun c:fib ()
  (fib)
)

 

  • Like 1
Posted
  On 3/25/2025 at 11:03 AM, GLAVCVS said:

Maybe something like this?

Expand  

@GLAVCVS Yes, it's very good. Thanks!!!

Posted
  On 3/25/2025 at 11:22 AM, Emmanuel Delay said:

Oh wait...

Here's a script I wrote 15 years ago

Expand  

@Emmanuel Delay Thanks, that's great too!
With my little knowledge of lisp, I was going to use something like this:

(setq pt1 (getpoint "\nSpecify a point1 to draw the square: ")) 
  (setq side1 233.0)
  (command "_.RECTANGLE" pt1 (list (+ (car pt1) side1) (+ (cadr pt1) side1)))
  (setq pt2 (getpoint "\nSpecify a point2 to draw the square.: "))
  (setq side2 144.0)
  (command "_.RECTANGLE" pt2 (list (+ (car pt2) side2) (+ (cadr pt2) side2)))
  ........  ........

 

Posted
  On 3/25/2025 at 11:03 AM, GLAVCVS said:

Maybe something like this?

(defun c:GLAVCVSfibo (/ n cierraOtro p1 p2 p3 p4)
  (defun cierraOtro (f1 f2)
    (setq d (+ f1 f2)
	  ang (angle p2 p3)
    )
    (command "pol" p3 (setq p2 (polar p3 ang d)) (setq p3 (polar p2 (- ang i) d)) (setq p4 (polar p3 (- ang i i) d)) (polar p4 (- ang PI i) d) "")
  )
  (setq n (getint "\nFibonaciCAD: Specifies number of sequences: "))
  (setq f1 0 f2 1 i (/ PI 2))
  (command "pol" '(0 0) (setq p2 '(-1 0)) (setq p3 '(-1 1)) '(0 1) '(0 0) "")
  (repeat n
    (setq f f2 f2 (+ f1 f2) f1 f)
    (cierraOtro f1 f2)
  )
  (princ)
)

 

Expand  

@Nikon You should keep one thing in mind: the actual number of iterations will be 1 more than you specify.
I forgot to consider the first one, which is done before the loop.
Therefore, you should change 'repeat n' to 'repeat (- n 1)'

  • Thanks 1
Posted (edited)

A slightly more complete version. Maybe it will be useful to someone someday.

(defun c:GLAVCVSfibo (/ n cierraOtro p1 p2 p3 p4 i osmant cja f1 f2)
  (defun cierraOtro (f1 f2 / d ang)
    (setq d (+ f1 f2)
	  ang (angle p2 p3)
    )
    (command "_pline" (setq p1 p3) (setq p2 (polar p3 ang d)) (setq p3 (polar p2 (- ang i) d)) (setq p4 (polar p3 (- ang i i) d)) (polar p4 (- ang PI i) d) "")
    (command "_arc" "c" p4 p3 p1)
    (ssadd (entlast) cja)
  )
  (setq n (getint "\nFibonaciCAD: Specifies number of sequences: "))
  (setq f1 0 f2 1 i (/ PI 2) osmant (getvar "osmode"))
  (setvar "osmode" 0)
  (command "_pline" (setq p1 '(0 0)) (setq p2 '(-1 0)) (setq p3 '(-1 1)) (setq p4 '(0 1)) '(0 0) "")
  (command "_arc" "c" p4 p3 p1 "")
  (setq cja (ssadd))
  (ssadd (entlast) cja)
  (repeat (- n 1)
    (cierraOtro f1 f2)
    (setq f f2 f2 (+ f1 f2) f1 f)
  )
  (command "_pedit" "_m" cja "" "" "_j" 0 "")
  (setvar "osmode" osmant)
  (princ)
)

 

Edited by GLAVCVS
  • Like 1
Posted
  On 3/25/2025 at 2:38 PM, GLAVCVS said:
A slightly more complete version. Maybe it will be useful to someone someday.
Expand  

It's very magical and visual!!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...