Jump to content

Equation Graph by autolisp


exceed
 Share

Recommended Posts

y2x2.gif

 

; Equation Graph by autolisp - 2022.06.20 exceed
; command list : yx1, yx2, yx3, yx4, yx5, yx6, y2x2 (circle)
; not a complete routine just for fun
; https://www.cadtutor.net/forum/topic/75463-equation-graph-by-autolisp/
; If it's for your homework, it's not a good choice to use it. 
; Use a program that draws accurate graphs.  matlab, etc. Excel is also good.

(defun c:yx1 ( / a b startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax + B ")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x) b))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)


(defun c:yx2 ( / a b c startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax^2 + Bx + C")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x x) (* b x) c))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)



(defun c:yx3 ( / a b c d startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax^3 + Bx^2 + Cx + D")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq d (getreal "\n Input D = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x x x) (* b x x) (* c x) d))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)



(defun c:yx4 ( / a b c d e startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax^4 + Bx^3 + Cx^2 + Dx + E ")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq d (getreal "\n Input D = "))
  (setq e (getreal "\n Input E = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x x x x) (* b x x x) (* c x x) (* d x) e))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)


(defun c:yx5 ( / a b c d e f startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax^5 + Bx^4 + Cx^3 + Dx^2 + Ex + F ")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq d (getreal "\n Input D = "))
  (setq e (getreal "\n Input E = "))
  (setq f (getreal "\n Input F = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x x x x x) (* b x x x x) (* c x x x) (* d x x) (* e x) f))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)


(defun c:yx6 ( / a b c d e f g startx endx steps deltax ptlist pt mspace tmp myobj ylist )
  (princ "\n y = Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G ")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq d (getreal "\n Input D = "))
  (setq e (getreal "\n Input E = "))
  (setq f (getreal "\n Input F = "))
  (setq g (getreal "\n Input G = "))
  (setq startx (getreal "\n Input Start X = "))
  (setq endx (getreal "\n Input End X = "))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (* a x x x x x x) (* b x x x x x) (* c x x x x) (* d x x x) (* e x x) (* f x) g))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ylist (vl-sort ylist '<))
  (setq ymin (car ylist))
  (setq ymax (last ylist))
  (setq ptlist (reverse ptlist))

  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis startx endx ymin ymax steps)
 
  (princ)
)



(defun c:y2x2 ( / a b c startx endx steps deltax ptlist pt mspace tmp myobj xlist ylist xmin xmax ymin ymax )
  (princ "\n (x - A)^2 + (y - B)^2 = C^2")
  (setq a (getreal "\n Input A = "))
  (setq b (getreal "\n Input B = "))
  (setq c (getreal "\n Input C = "))
  (setq startx (- a c))
  (setq endx (+ a c))
  (setq steps 100)
  (cond 
    ((<= startx endx) 
      (setq x startx)
    )
    ((> startx endx)
      (setq x endx)
      (setq endx startx)
      (setq startx x)
    )
  )
  (setq deltax (/ (- endx startx) steps))
  (setq xlist '())
  (setq ylist '())
  (setq ptlist '())
  (repeat (+ steps 1)
    (setq y (+ (sqrt (abs (- (+ (- (* c c) (* x x)) (* 2 a x)) (* a a)))) b))
    (setq xlist (cons x xlist))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )
  (setq ptlist (reverse ptlist))
  (setq x startx)
  (repeat (+ steps 1)
    (setq y (+ (sqrt (abs (- (+ (- (* c c) (* x x)) (* 2 a x)) (* a a)))) b))
    (setq y (- (* 2 b) y))
    (setq xlist (cons x xlist))
    (setq ylist (cons y ylist))
    (setq pt (list x y 0.0))
    (setq ptlist (cons pt ptlist))
    (setq x (+ x deltax))
  )

  (setq xlist (vl-sort xlist '<))
  (setq ylist (vl-sort ylist '<))
  (setq xmin (car xlist))
  (setq xmax (last xlist))
  (setq ymin (car ylist))
  (setq ymax (last ylist))


  ; https://www.afralisp.net/archive/methods/list/addpolyline_method.htm
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq ptlist (apply 'append ptlist))
  (if (= (rem (length ptlist) 3) 0)
    (progn
      (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1)) ) )
      (vlax-safearray-fill tmp ptlist)
      (setq myobj (vla-addPolyline mspace tmp))
    )
    (princ "\nerror: Polyline could not be created")
  )
  (vlax-put-property myobj 'color 3)
  
  (ex:drawaxis xmin xmax ymin ymax steps)
 
  (princ)
)


(defun ex:drawaxis ( xmin xmax ymin ymax steps / deltax xaxisline yaxisline xaxisarrow1 xaxisarrow2 xaxisarrow3 xaxisarrow4 yaxisarrow1 yaxisarrow2 yaxisarrow3 yaxisarrow4 xaxistext1 xaxistext2 yaxistext1 yaxistext2 )
  (setq deltax (/ (- xmax xmin) steps))
  (setq xaxisline (vla-addline mspace (vlax-3d-point (list (- xmin (* (/ steps 10) deltax)) 0 0))(vlax-3d-point (list (+ xmax (* (/ steps 10) deltax)) 0 0))))
  (setq yaxisline (vla-addline mspace (vlax-3d-point (list 0 (- ymin (* (/ steps 10) deltax)) 0))(vlax-3d-point (list 0 (+ ymax (* (/ steps 10) deltax)) 0))))
  (setq xaxisarrow1 (vla-addline mspace (vlax-3d-point (list (- xmin (* (/ steps 20) deltax)) (- 0 (* (/ steps 100) deltax)) 0))(vlax-3d-point (list (- xmin (* (/ steps 10) deltax)) 0 0))))  
  (setq xaxisarrow2 (vla-addline mspace (vlax-3d-point (list (- xmin (* (/ steps 20) deltax)) (+ 0 (* (/ steps 100) deltax)) 0))(vlax-3d-point (list (- xmin (* (/ steps 10) deltax)) 0 0))))  
  (setq xaxisarrow3 (vla-addline mspace (vlax-3d-point (list (+ xmax (* (/ steps 20) deltax)) (- 0 (* (/ steps 100) deltax)) 0))(vlax-3d-point (list (+ xmax (* (/ steps 10) deltax)) 0 0))))  
  (setq xaxisarrow4 (vla-addline mspace (vlax-3d-point (list (+ xmax (* (/ steps 20) deltax)) (+ 0 (* (/ steps 100) deltax)) 0))(vlax-3d-point (list (+ xmax (* (/ steps 10) deltax)) 0 0))))  
  (setq yaxisarrow1 (vla-addline mspace (vlax-3d-point (list (- 0 (* (/ steps 100) deltax)) (- ymin (* (/ steps 20) deltax)) 0))(vlax-3d-point (list 0 (- ymin (* (/ steps 10) deltax)) 0))))
  (setq yaxisarrow2 (vla-addline mspace (vlax-3d-point (list (+ 0 (* (/ steps 100) deltax)) (- ymin (* (/ steps 20) deltax)) 0))(vlax-3d-point (list 0 (- ymin (* (/ steps 10) deltax)) 0))))
  (setq yaxisarrow3 (vla-addline mspace (vlax-3d-point (list (- 0 (* (/ steps 100) deltax)) (+ ymax (* (/ steps 20) deltax)) 0))(vlax-3d-point (list 0 (+ ymax (* (/ steps 10) deltax)) 0))))
  (setq yaxisarrow4 (vla-addline mspace (vlax-3d-point (list (+ 0 (* (/ steps 100) deltax)) (+ ymax (* (/ steps 20) deltax)) 0))(vlax-3d-point (list 0 (+ ymax (* (/ steps 10) deltax)) 0))))
  (setq xaxistext1 (vla-AddText mspace "+x" (vlax-3d-point (list (+ xmax (* (/ steps 8) deltax)) 0 0)) (* (/ steps 20) deltax)))
  (vlax-put-property xaxistext1 'alignment 9)
  (setq xaxistext2 (vla-AddText mspace "-x" (vlax-3d-point (list (- xmin (* (/ steps 8) deltax)) 0 0)) (* (/ steps 20) deltax)))
  (vlax-put-property xaxistext2 'alignment 11)
  (setq yaxistext1 (vla-AddText mspace "-y" (vlax-3d-point (list 0 (- ymin (* (/ steps 8) deltax)) 0)) (* (/ steps 20) deltax)))
  (vlax-put-property yaxistext1 'alignment 7)
  (setq yaxistext2 (vla-AddText mspace "+y" (vlax-3d-point (list 0 (+ ymax (* (/ steps 8) deltax)) 0)) (* (/ steps 20) deltax)))
  (vlax-put-property yaxistext2 'alignment 13)

  (setq bar (* (/ steps 10) deltax))
  (setq barlen (/ bar 6))

  (setq index2 0)
  (repeat 11
    (setq xbar (vla-addline mspace (vlax-3d-point (list (+ xmin (* bar index2)) barlen 0))(vlax-3d-point (list (+ xmin (* bar index2)) (* barlen -1) 0))))
    (setq xbartext (vla-AddText mspace (rtos (+ xmin (* bar index2)) 2 0) (vlax-3d-point (list (+ (+ xmin (* bar index2)) (/ barlen 2)) (- 0 (/ barlen 2)) 0)) (/ (* (/ steps 20) deltax) 2)))
    (vlax-put-property xbartext 'alignment 6)
    (setq index2 (+ index2 1))
  )

  (setq index2 0)
  (repeat 11
    (setq ybar (vla-addline mspace (vlax-3d-point (list barlen (+ ymin (* bar index2)) 0))(vlax-3d-point (list (* barlen -1) (+ ymin (* bar index2)) 0))))
    (setq ybartext (vla-AddText mspace (rtos (+ ymin (* bar index2)) 2 0) (vlax-3d-point (list (- 0 (/ barlen 2)) (+ (+ ymin (* bar index2)) (/ barlen 4)) 0)) (/ (* (/ steps 20) deltax) 2)))
    (vlax-put-property ybartext 'alignment 14)
    (setq index2 (+ index2 1))
  )



)

 

command list 

yx1 - linear equation  y = Ax + B

yx2 - quadratic equation y = Ax^2 + Bx + C

yx3 - cubic equation y = Ax^3 + Bx^2 + Cx + D

yx4 - biquadratic equation y = Ax^4 + Bx^3 + Cx^2 + Dx + E

yx5 - quintic equation y = Ax^5 + Bx^4 + Cx^3 + Dx^2 + Ex + F

yx6 - 6th y = Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G

y2x2 - circle equation (x-a)^2 + (y-b)^2 = c^2

 

 

 

This is a simple routine that just connects points with polylines and no curves. This is not an exact graph.

 

Edited by exceed
  • Like 4
Link to comment
Share on other sites

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

 Share

×
×
  • Create New...