Draw Curve From Equation in Autocad

A polynom of degree no greater then 30 (or so) can be
represented exactly in AutoCAD as a Bezier spline.

We need to translate the equation (polynom coefficients)
and range into the NURBS data - specifically, control points.
The knot values are implied since it's a Bezier curve.

Parabola is a simple special case for which a program can
be developed empirically, look into my post in c.c.a.

{{ Vladimir Nesterovsky


comp.cad.autocad on Wednesday, October 03, 2001 4:58 AM
message news:9pdrkf$i6btn$1@ID-91128.news.dfncis.de }}

Here follows a program to do that. To be perfect, for higher degrees
it has to have a precise arithmetics. You would need to add that
yourself; it is doable with (ineffective but) simple representation
of an integer as a list of its decimal digits.

If someone's interested in explanations about this code, let me know
and I'll post them (please e-mail me, I don't check here frequently
enough).

E.g. to draw a pnom y=2x^2-3x+7 from x=-3 to x=1.5, you'd call

(pnom-bezier '(2 -3 7) -3 1.5)

;; (C.) 2002 by Vladimir Nesterovsky
;; this code is not free and can be used only with author's permission

(defun pnom-eval (pnom x / res)
(setq res (car pnom)
pnom (cdr pnom))
(repeat (length pnom)
(setq res (+ (* res x) (car pnom))
pnom (cdr pnom)))
res)

(defun pnom-degree (pnom)
(1- (length (member-if-not 'zerop pnom))))

;; pnoms expected in descending power order
(defun pnom2-bezier (pnx pny v1 v2 /
n d b cx cy px py)
(setq n (max (length pnx) (length pny))
d (1- n))
(if (< (length pnx) n)
(repeat (- n (length pnx))
(setq pnx (cons 0.0 pnx))))
(if (< (length pny) n)
(repeat (- n (length pny))
(setq pny (cons 0.0 pny))))
(setq cx (reparam pnx v1 v2)
cy (reparam pny v1 v2) ; reparameterize to u=[0 1]
b (b-matrix d) ; list of *columns*
px (solve-bezier cx b)
py (solve-bezier cy b))
(and (create-bezier px py)))

;; pnom expected in descending power order
(defun pnom-bezier (pnom x1 x2)
(pnom2-bezier '(1 0) pnom x1 x2))

(defun make-list (elm len / ls)
(repeat len
(setq ls (cons elm ls)))
ls)

(defun create-bezier (px py / n)
(setq n (length px))
(entmake
(append
'( (0 . "SPLINE") (100 . "AcDbEntity") (100 . "AcDbSpline") )
(list (cons 70 :smiling_face_with_sunglasses: ; 1-closed, 2-periodic, 4-rational,
8-planar,16-linear
(cons 71 (1- n))) ; degree
(make-list '(40 . 0.0) n)
(make-list '(40 . 1.0) n)
(mapcar '(lambda(x y)(list 10 x y 0.0))
px py))))

;; find P: P * B = CF --
;; p0*b00 = c0
;; p0*b01 + p1*b11 = c1
;; p0*b02 + p1*b12 + p2*b22 = c2

(defun solve-bezier (cf b ; curve-function (pn-descnd) and b-mtx
/ ps)
(mapcar
'(lambda(bcol c / pn)
(setq pn (/ (- c (apply '+ (mapcar '* (reverse ps) bcol))) (last
bcol)))
(setq ps (cons pn ps))
pn)
b (reverse cf)))

;; (r + su)^i = Sum

K r^(i-j) s^j u^j

;; Q(x) = Sum Q x^i = Sum Q (r + su)^i =
;; = Sum Q (r + su)^i =
;; = Sum Q Sum K r^(i-j) s^j u^j
;; = Sum (Sum Q K r^(i-j) s^j) u^j

;; K = (i+1)!/(i+1-j)!j! = i!(i+1)/(i-j)!j!(i-j+1) = K
(i+1)/(i-j+1)
;; K = i!/(i-j)!j!
(defun reparam ( pn x1 x2 / ; pn's in descending powers order
r s n res j xpsj i xprij kaij)
(setq r (float x1)
s (float (- x2 x1)) ; [ r r+s] = [x1 x2]
n (length pn)
pn (reverse pn) ; make it ascending
res ()
j 0
xpsj 1) ; (expt s j)
(repeat n
(setq i j)
(setq xprij xpsj ; 1 ; (expt r (- i j))
kaij 1)
(setq res (cons
(apply '+ (mapcar '(lambda (q / rs)
(setq rs (* q kaij xprij) ; xpsj )
xprij (* xprij r)
i (+ i 1)
kaij (/ (* kaij i) (- i j)))
rs)
pn))
res))
(setq pn (cdr pn)
xpsj (* xpsj s)
j (1+ j)))
res) ; ret res in descending powers order

;; d is pnom's degree
;; generate it by *columns*
;; b[i,j] = (-1)^(j-i) K

K , ,
;; = 0 , i > j --- imply it, don't generate.
;; K = K (d-j)/(j+1)
;; K==1
;; K = K (j-i)/(i+1)

(defun b-matrix (d / i j col res kadj kaji)
(setq j 0
res ()
kadj 1.0) ; (ka d j)
(repeat (1+ d)
(setq i 0
col ()
kaji 1.0) ; (ka j i)
(repeat (1+ j)
(setq col (cons (* kadj kaji) col)
kaji (- (/ (* kaji (- j i)) (+ i 1)))
i (1+ i)))
(setq res (cons (reverse col) res)
kadj (- (/ (* kadj (- d j)) (+ j 1)))
j (1+ j)))
(reverse res))

Jon Fleming

wrote in message
news:VA.0000111f.04d694b8@fleming-group.com...
> You need to do a little simple programming. AutoCAD cannot represent
> arbitrary curves exactly, but it can represent any curve arbitrarily
> close to exactly. For example, the following draws a sine curve:
> [.....]
>
> In article , Ndaoli wrote:
> > I have a polinomial equation and would like to draw and plot it. Is
> > it possible feed Acad with such equation and draw the curve? How can
> > I do it?
> > Thanks in advance.
> >
>
>

reaganwheive.blogspot.com

Source: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/drawing-curves-from-equation/td-p/880632

0 Response to "Draw Curve From Equation in Autocad"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel