Can you post the code you wrote so far?
Presuming that you know the length, respectively width of the rectangle, you should use Pythagoras formula:
Code:(setq sizeDiagonal (sqrt (+ (expt sizeLength 2.0) (expt sizeWidth 2.0))))
Registered forum members do not see this ad.
Hello! I was wondering how I would go about writing a function that would calculate the length of a diagonal of a rectangle using SQRT function? I've been trying but so far nothing!
Thanks!
Can you post the code you wrote so far?
Presuming that you know the length, respectively width of the rectangle, you should use Pythagoras formula:
Code:(setq sizeDiagonal (sqrt (+ (expt sizeLength 2.0) (expt sizeWidth 2.0))))
Regards,
Mircea
AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3
Thanks! I had a few simple lines, but I erased those as yours was basically what I needed. Would it be possible to write a function that would find the length & width itself? This one is out of curiosity.
Try this:
Yes, thanks to Lee Mac hahaCode:(defun LM:MAssoc ( key lst / item ) (if (setq item (assoc key lst)) (cons (cdr item) (LM:MAssoc key (cdr (member item lst)))) ) ) (defun c:getdist (/ s pt1 pt2 pt3 pt4 x y ent lst) (setq s (ssget "_+.:E:S" '((0 . "LWPOLYLINE")))) (if (= s nil) (alert "Select a rectangle.") (progn (setq ent (entget(ssname s 0))) (setq lst (LM:MAssoc 10 ent)) (princ) (setq pt1 (nth 0 lst)) (setq pt2 (nth 1 lst)) (setq pt3 (nth 2 lst)) (setq pt4 (nth 3 lst)) (setq x(distance pt1 pt2)) (setq y(distance pt1 pt4)) (princ (strcat "\nHeight: " (rtos y) "\nWidth: " (rtos x))) (princ) ) ) )
Edit: But there is some problem with this code, if you rotate your rectangle by 90º the height will become width and vice versa.
Nothing that a if can't solve hehe
Last edited by fabriciorby; 18th Apr 2013 at 04:34 pm.
Registered forum members do not see this ad.
Here is another method:
Code:(defun c:test ( / lst sel ) (if (setq sel (ssget "_+.:E:S" '((0 . "LWPOLYLINE")))) (progn (setq lst (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget (ssname sel 0)) ) ) ) (apply 'mapcar (cons '- (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(max min) ) ) ) ) ) )
Lee Mac Programming • Twitter • Exchange App Store • Dropbox (500MB free)
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Bookmarks