cg-Cad

Lisp »Tips 'n Tricks »Funzioni ricorsive in AutoLISP »1 | 2 | 3 | 4 | 5 | 6 | 7 a , b , c , d , e >II >III >IV >V >VI >VII

C-Curve

Alla base del mio lisp c'è questo lisp (o meglio una parte di esso, quella di colore blu):

;;; -*-  Mode: LISP -*- Syntax: AutoLISP (C) Benjamin Olasov 1988
;;;      Functions to generate recursively defined drawings.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; File: DRAGON.LSP Copyright (C) Benjamin Olasov    Graphic Systems, Inc. ;;;
;;; Inquiries:                                                              ;;;
;;;                                                                         ;;;
;;;     Benjamin Olasov                                                     ;;;
;;;     Graphic Systems, Inc.:                                              ;;;
;;;                                                                         ;;;
;;;                    New York, NY:   PH (212) 725-4617                    ;;;
;;;                    Cambridge, MA:  PH (617) 492-1148                    ;;;
;;;                    MCI-Mail:       GSI-NY   344-4003                    ;;;
;;;                    Arpanet:        olasov@cs.columbia.edu               ;;;
;;;                                                                         ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; This program is provided 'as is' without warranty of any kind, either 
;; expressed or implied, including, but not limited to the implied warranties of
;; merchantability and fitness for a particular purpose.  The entire risk as to
;; the quality and performance of the program is with the user.  Should the 
;; program prove defective, the user assumes the entire cost of all necessary 
;; servicing, repair or correction. 
;; AutoLisp and AutoCad are registered trademarks of AutoDesk, Inc.


(VMON)
(GC)
(TEXTSCR)
(PROMPT "\nC:DRAGON and C:C-CURVE generate recursively defined drawings.")

; Random number generator
(DEFUN RAN ()
 (SETQ SEED 
 (IF SEED 
  (REM 
   (+ (* SEED 15625.7) 0.21137152) 1) 0.3171943
  )
 )
)

(DEFUN C:DEMO () 
 (GRAPHSCR) 
 (DEFUN *ERROR* (MSG)(PRINC MSG))
 (SETQ CEN (GETVAR "VIEWCTR")
       HGT (GETVAR "VIEWSIZE")
       DIST1 HGT
 )
 (WHILE (/= 1 2)
  (SETQ DIST2 (* (RAN) DIST1)
        DIST3 (* (RAN) DIST1)
           A1 (* (/ PI 2.0) (RAN))
           A2 (* (/ PI 2.0) (RAN))
           P1 (POLAR CEN A1 DIST2)
           P2 (POLAR CEN A2 DIST3)
           LENG (DISTANCE P1 P2)
           MIN-LENG (MAX (/ HGT 161.8) (/ LENG 160.0)) 
               ANGL (ANGLE P1 P2)
              LEVEL 0)
  (DRAGON LENG ANGL (+ 0.0 1.0) MIN-LENG) 
  (REDRAW)
 )
 (PRINC)
)

(DEFUN PLOT-LINE (L A)
 (GRDRAW BASE (POLAR BASE A L) LEVEL)
 (SETQ BASE (POLAR BASE A L))
)

(DEFUN C-CURVE (LEN ANG MIN-LEN)
 (SETQ LEVEL (1+ LEVEL))
 (PRINC "\nLevel ")
 (PRINC LEVEL)
 (COND 
  ((< LEN MIN-LEN)
   (PRINC "\nPLOT-LINE at level ")
   (PRINC LEVEL)
   (PLOT-LINE LEN ANG)
  )
  (T 
   (C-CURVE (/ LEN (SQRT 2.0))(+ ANG (/ PI 4.0)) MIN-LEN)
   (C-CURVE (/ LEN (SQRT 2.0))(- ANG (/ PI 4.0)) MIN-LEN)
  )
 )
 (PROGN (SETQ LEVEL (1- LEVEL))
        (PRINC "\nLevel ")
        (PRINC LEVEL)
 )
)

(DEFUN C:C-CURVE (/ BASE PT2 ANGL MIN-LENG CURVE-VAR)
 (GRAPHSCR)
 (SETQ LENG (DISTANCE (SETQ BASE (GETPOINT "\nStarting point: "))
  (SETQ PT2 (GETPOINT BASE "\nDigitize length/ angle: "))) 
       ANGL (ANGLE BASE PT2) 
       MIN-LENG (MAX (/ (GETVAR "VIEWSIZE") 100.0) (/ LENG 160.0)) 
       LEVEL 0) 
 (C-CURVE LENG ANGL MIN-LENG) 
 (GETSTRING "\nPress Return to clear: ") 
 (REDRAW)
)

(DEFUN DRAGON (LEN ANG SIGN MIN-LEN)
 (SETQ LEVEL (1+ LEVEL))
 (PRINC "\nLevel ")
 (PRINC LEVEL)
 (COND 
  ((< LEN MIN-LEN)
   (PRINC "\nPLOT-LINE called at level ")
   (PRINC LEVEL)
   (PLOT-LINE LEN ANG)
  )
  (T 
   (DRAGON (/ LEN (SQRT 2.0))(+ ANG (* SIGN (/ PI 4.0))) +1.0 MIN-LEN)
   (DRAGON (/ LEN (SQRT 2.0))(- ANG (* SIGN (/ PI 4.0))) -1.0 MIN-LEN)
  )
 )
 (PROGN (SETQ LEVEL (1- LEVEL))
        (PRINC "\nLevel ")
        (PRINC LEVEL)
 )
)

(DEFUN C:DRAGON (/ BASE PT2 ANGL MIN-LENG CURVE-VAR) 
 (GRAPHSCR) 
 (SETQ LENG (DISTANCE (SETQ BASE (GETPOINT "\nStarting point: ")) 
  (SETQ PT2 (GETPOINT BASE "\nDigitize length/ angle: "))) 
        ANGL (ANGLE BASE PT2) 
        MIN-LENG (MAX (/ (GETVAR "VIEWSIZE") 100.0) (/ LENG 160.0)) 
        LEVEL 0) 
 (DRAGON LENG ANGL (+ 0.0 1.0) MIN-LENG) 
 (GETSTRING "\nPress RETURN to clear: ") 
 (REDRAW)
)

(PROMPT "\n\nType DRAGON or C-CURVE to generate a recursively defined drawing.")
(PRINC)

CCURVE.LSP

;;;
;;;    ccurve.lsp - 8 Marzo 2004
;;;    (C) 2004 by Claudio Piccini.
;;;    www.cg-cad.com
;;;    
;;;    C-Curve
;;;
;;;    Basato su:
;;;    DRAGON.LSP Copyright (C)1988 Benjamin Olasov
;;;    Graphic Systems, Inc.
;;;
(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (command "_line" p1 p3 "")
   (setq p1 (polar p1 ang lung))
  )
  (progn
   (cc (/ lung (sqrt 2.0))(+ ang 0.785398))
   (cc (/ lung (sqrt 2.0))(- ang 0.785398))
  )
 )
)

(defun c:ccurve (/ snapp p1 p2 ang lung n)
 (setvar "cmdecho" 0)
 (setq snapp (getvar "osmode"))
 (command "_osnap" "_non")
 (setq p1 (getpoint "\nClicca un'estremita' della curva"))
 (setq p2 (getpoint p1 "\nClicca l'altra estremita': "))
 (setq n (getint "\nNumero iterazioni: "))
 (setq ang (angle p1 p2))
 (setq lung (distance p1 p2))
 (setq n (/ lung n))
 (cc lung ang) 
 (setvar "osmode" snapp)
 (command "_redraw")
 (setvar "cmdecho" 1)
 (princ)
)
;;;eof

c-curve

Variazioni in C-curve

; converte angolo da gradi in radianti
(defun g2r (a)
 (* pi (/ a 180.0))
)

(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (command "_line" p1 p3 "")
   (setq p1 (polar p1 ang (- lung (/ lung (sqrt 2.0)))))
  )
  (progn
   (cc (/ lung (sqrt 2.0))(+ ang (g2r 45)))
   (cc (/ lung (sqrt 2.0))(- ang (g2r 45)))
  )
 )
)
...

dettaglio

...
(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (setq p4 (polar p3 ang lung))
   (command "_line" p3 p4 "")
   (setq p1 (polar p1 ang lung))
  )
  (progn
   (cc (/ lung (sqrt 2.0))(+ ang (g2r 45)))
   (cc (/ lung (sqrt 2.0))(- ang (g2r 45)))
  )
 )
)
...

dettaglio

...
(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (command "_line" p1 p3 "")
   (setq p1 (polar p1 ang lung))
  )
  (progn
   (cc (/ lung (sqrt 2.0))(+ (* ang -1)(g2r 45)))
   (cc (/ lung (sqrt 2.0))(- (* ang -1)(g2r 45)))
  )
 )
)
...

...
(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (command "_line" p1 p3 "")
   (setq p1 (polar p1 ang lung))
  )
  (progn
   (cc (/ lung (sqrt 3.0))(+ (* ang -1)(g2r 30)))
   (cc (/ lung (sqrt 2.0))(- (* ang -1)(g2r 45)))
  )
 )
)
...

25 iterazioni: il cavo del telefono



dettaglio100 iterazioni: le onde del mare.



...
(defun cc (lung ang)
 (if (< lung n)
  (progn
   (setq p3 (polar p1 ang lung))
   (command "_line" p1 p3 "")
   (setq p1 (polar p1 ang lung))
  )
  (progn
   (cc (/ lung (sqrt 2.0))(+ (* ang -1)(g2r 30)))
   (cc (/ lung (sqrt 2.0))(- (* ang -1)(g2r 45)))
  )
 )
)
...

*Una variante minima e dalle onde del mare nasce un frattale.



dettaglio

Lisp »Tips 'n Tricks

Ultimo Aggiornamento_Last Update: 8 Marzo 2004