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)
53 minutes ago, Emmanuel Delay said:

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

@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

You should change "pol" to "_pline"

  • Thanks 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
29 minutes ago, GLAVCVS said:

Maybe something like this?

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

Posted
21 minutes ago, Emmanuel Delay said:

Oh wait...

Here's a script I wrote 15 years ago

@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
1 hour ago, 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)
)

 

@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
1 hour ago, GLAVCVS said:
A slightly more complete version. Maybe it will be useful to someone someday.

It's very magical and visual!!!

  • 3 weeks later...
Posted

Am I too late for this party? Anyway, here's my try:

(defun c:fibonacci()
  (setq pi2 (/ PI 2.0) as0 (cons 0 "line") col '(62 . 3))
    (defun fib(ins dir dim)   
      (entmake (list as0 (cons 10 ins) (cons 11 (setq p2 (polar ins (- dir pi2) dim)))))
      (entmake (list as0 (cons 10 p2) (cons 11 (setq p3 (polar p2 dir dim)))))
      (entmake (list as0 (cons 10 p3) (cons 11 (setq p4 (polar p3 (+ dir pi2) dim)))))
      (entmake (list (cons 0 "ARC") (cons 10 p4) (cons 40 dim) (cons 50 (- dir pi)) (cons 51 (- dir  pi2)) col))
      (setq dir (+ dir pi2))
    )
  (setq a 1 b 1 dir 0 ins '(0 0))
  (repeat 19
    (fib ins (setq dir (+ pi2 dir)) a)
    (setq c (+ a b) a b b c ins p3)
  )
)

 

  • Like 1
Posted
1 hour ago, fuccaro said:
Am I too late for this party? Anyway, here's my try:

Nice try! 👋

  • Like 1
Posted

What about true Fibonacci spiral? Of course, it is true only on vertexes.

 

(defun c:fib ( / p q u a n da p1 lst)
  (setq p '(0.0 0.0 0.0)
        q (/ (+ 1 (sqrt 5)) 2)
        u (atan (/ (* 2 (log q)) pi))
        a 0.0
        n 10
        da (/ pi n 0.5)
  )
  (repeat (+ 1 (* n 2))
    (setq p1 (polar p a (expt q (/ (* 2 a) pi))))
    ;(entmakex (list '(0 . "POINT") (cons 10 p1)))
    (setq lst (cons (cons 11 p1) lst))
    (setq a (+ a da))
  )

  (entmakex
    (append
      (list
       '(0 . "SPLINE")
       '(100 . "AcDbEntity")
       '(100 . "AcDbSpline")
       '(70 . 1064)
       '(71 . 3)
        (cons 74 (length lst))
        (list 12 (sin u) (cos u) 0.0)
        (list 13 (sin u) (cos u) 0.0)
      )
      (reverse lst)
    )
  )
  (princ)
)

 

Posted

Nice
Fibonacci is symmetry and predictability. Everything becomes predictable when it shows you everything it's doing too soon.
However, there are some that, if you wait long enough, surprise you.

I suppose some of you know what I'm talking about.

Posted

Recaman, for example

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...