Nikon Posted Monday at 12:32 PM Posted Monday at 12:32 PM (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) ) Edited Monday at 12:34 PM by Nikon Quote
Emmanuel Delay Posted Tuesday at 08:30 AM Posted Tuesday at 08:30 AM (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 Tuesday at 08:33 AM by Emmanuel Delay 1 Quote
Nikon Posted Tuesday at 09:03 AM Author Posted Tuesday at 09:03 AM (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, … for example, start with a large square (for iteration 19 - 4181), and then build clockwise squares with sides 2584, 1597, 987, respectively... Edited Tuesday at 09:24 AM by Nikon Quote
marko_ribar Posted Tuesday at 10:42 AM Posted Tuesday at 10:42 AM Look for my attachments posted in this topic... (You have to be logged to download attached files...) Here is link : https://www.theswamp.org/index.php?topic=12813.255 1 Quote
GLAVCVS Posted Tuesday at 11:03 AM Posted Tuesday at 11:03 AM 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) ) 1 Quote
GLAVCVS Posted Tuesday at 11:06 AM Posted Tuesday at 11:06 AM You should change "pol" to "_pline" 1 Quote
Emmanuel Delay Posted Tuesday at 11:22 AM Posted Tuesday at 11:22 AM 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) ) 1 Quote
Nikon Posted Tuesday at 11:34 AM Author Posted Tuesday at 11:34 AM On 3/25/2025 at 11:03 AM, GLAVCVS said: Maybe something like this? Expand @GLAVCVS Yes, it's very good. Thanks!!! Quote
Nikon Posted Tuesday at 11:44 AM Author Posted Tuesday at 11:44 AM 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))) ........ ........ Quote
GLAVCVS Posted Tuesday at 12:05 PM Posted Tuesday at 12:05 PM 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)' 1 Quote
GLAVCVS Posted Tuesday at 02:38 PM Posted Tuesday at 02:38 PM (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 Tuesday at 02:45 PM by GLAVCVS 1 Quote
Nikon Posted Tuesday at 03:57 PM Author Posted Tuesday at 03:57 PM 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!!! Quote
Recommended Posts
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.