AutoLisp DCL Equation Graphing

here is a start. equations have to be input in Lisp format. prototype, barely tested beyond initial ah ha!

Both line and polyline mode are enabled. The program remembers variables from run to run, so if you input a new equation, it will come up on next run.



(defun c:graph ()

  (setq dcl_id (load_dialog "c:\\lisp\\graph\\graph.dcl"))
  (if (< dcl_id 0)
    (progn
      (alert "The graph.DCL file could not be loaded.")
      (exit) ) )

  (if (not (new_dialog "graph" dcl_id))
    (progn
      (alert "DCL file loaded but not definition, internal problem with files")
      (exit) ) )

  (setgraphvars)

  (action_tile "graphlines" "(savevars) (done_dialog 2)")
  (action_tile "graphpolylines" "(savevars) (done_dialog 3)")
  (action_tile "cancel" "(done_dialog 0)")

  (setq ddiag (start_dialog))

  (unload_dialog dcl_id)

  (if (= ddiag 2)
    (graphlines) )
  
  (if (= ddiag 3)
    (graphpolylines) )
)


(defun savevars	()
  (setq xmin (atof (get_tile "xmin")))
  (setq xmax (atof (get_tile "xmax")))
  (setq xinc (atof (get_tile "xinc")))

  (setq a_dim (atof (get_tile "a_dim")))
  (setq b_dim (atof (get_tile "b_dim")))
  (setq c_dim (atof (get_tile "c_dim")))
  (setq d_dim (atof (get_tile "d_dim")))

  (setq str_equation (get_tile "equation"))
)


(defun setgraphvars ()
  (if xmin
    (set_tile "xmin" (rtos xmin 2 2))
    (set_tile "xmin" "-3.0") )

  (if xmax
    (set_tile "xmax" (rtos xmax 2 2))
    (set_tile "xmax" "3.0") )

  (if xinc
    (set_tile "xinc" (rtos xinc 2 2))
    (set_tile "xinc" "0.1") )

  (if a_dim
    (set_tile "a_dim" (rtos a_dim 2 2))
    (set_tile "a_dim" "2") )

  (if b_dim
    (set_tile "b_dim" (rtos b_dim 2 2))
    (set_tile "b_dim" "3") )

  (if c_dim
    (set_tile "c_dim" (rtos c_dim 2 2))
    (set_tile "c_dim" "4") )

  (if d_dim
    (set_tile "d_dim" (rtos d_dim 2 2))
    (set_tile "d_dim" "5") )
  
			;initial equation ax^2 + bx + c	
  (if (null str_equation)
    (set_tile "equation" "(+ (* a X X) (* b X) c)")
    (set_tile "equation" str_equation) ) )


(defun resetgraph ()
  (set_tile "xmin" "-3.0")
  (set_tile "xmax" "3.0")
  (set_tile "xinc" "0.1")

  (set_tile "a_dim" "2")
  (set_tile "b_dim" "3")
  (set_tile "c_dim" "4")
  (set_tile "d_dim" "5")

  (setq str_equation "(+ (* a X X) (* b X) c)") ;initial equation
  (set_tile "equation" str_equation)	; ax^2 + bx + c
)


(defun func_eval (x strfunc / a b c d)
  (setq	a a_dim   b b_dim   c c_dim   d d_dim )
  (setq result (eval (read strfunc)))
)


(defun graphlines ()	;working with all global variables from savevars
		
  (setq strfunc str_equation)	; could clean this up

  (setq numlines (/ (- xmax xmin) xinc))
  (setq i 1)
  (setq x1 xmin)
  (setq y1 (func_eval x1 strfunc))

  (repeat (fix numlines)

    (setq x2 (+ xmin (* i xinc)))
    (setq y2 (func_eval x2 strfunc))

    (setq pt1 (list x1 y1))
    (setq pt2 (list x2 y2))

    (command "line" pt1 pt2)
    (command)

    (setq x1 x2)
    (setq y1 y2)

    (setq i (1+ i))
  )
)

		
(defun graphpolylines ()   ;working with all global variables from savevars

  (setq strfunc str_equation)	; eval read strfunc

  (setq numlines (/ (- xmax xmin) xinc))
  (setq x1 xmin)
  (setq y1 (func_eval x1 strfunc))
  (setq pt1 (list x1 y1))

  (command "pline" pt1)

  (setq i 1)
  (repeat (fix numlines)

    (setq x2 (+ xmin (* i xinc)))
    (setq y2 (func_eval x2 strfunc))

    (setq pt2 (list x2 y2))

    (command pt2)
    (setq i (1+ i))
  )

  (command)

)

Graphs below are cos, sin and cos * sin
(cos X) White
(sin X) Red
(* (sin X) (cos X)) Blue

Advertisements