# Thread: LISP to calculate area of rectangle

1. ## LISP to calculate area of rectangle

A colleague of mine, came to me requesting a lisp that calculate's the area of a rectangle. He wants to be able to run the routine, start the rectangle, be able to see the area of the rectangle before closing the rectangle.

Any suggestions?

2. ## Re: LISP to calculate area of rectangle

I think you will have to use (getpoint.... then use (grread... while you use (getcorner...
I don't know if that is possible, but that is what came to mind while reading your post.

3. ## Re: LISP to calculate area of rectangle

Originally Posted by john.nason
A colleague of mine, came to me requesting a lisp that calculate's the area of a rectangle. He wants to be able to run the routine, start the rectangle, be able to see the area of the rectangle before closing the rectangle.

Any suggestions?
Try this. You will have to wrap this in a (defun) if desired. While the command is running, the area will be displayed in the status bar.
Code:
```; --------------------------------
(setq r0 0.0
r45  (* 0.25 pi)
r90  (* 0.5 pi)
r135 (* 0.75 pi)
r180 pi
r225 (* 1.25 pi)
r270 (* 1.5 pi)
r315 (* 1.75 pi)
)
(setq pt1 (getpoint "\nSelect first point: ") time T)
(while time
(setq drag (grread t 1 1))
(cond ((= (car drag) 5) ;<- moving cursor
(setq pt2 (cadr drag))
(setq xdim (abs (- (car pt1)(car pt2))))
(setq ydim (abs (- (cadr pt1)(cadr pt2))))
(setq myarea (* xdim ydim))
(grtext -1 (strcat "Area = " (rtos myarea)))
(setq vec (angle pt1 pt2))
(cond ((and (>= vec 0.0) (< vec r90))
(setq x r0 y r90)
)
((and (>= vec r90) (< vec r180))
(setq x r180 y r90)
)
((and (>= vec r180) (< vec r270))
(setq x r180 y r270)
)
((>= vec r270)
(setq x r0 y r270)
)
)
(setq pt3 (polar pt1 x xdim) pt4 (polar pt1 y ydim))
(redraw)
(grvecs (list 7 pt1 pt3 7 pt3 pt2 7 pt2 pt4 7 pt4 pt1))
)
((= (car drag) 3) ;<- picked point
(setq time nil)
(command "._pline" pt1 pt3 pt2 pt4 "_C")
)
)
)```

4. ## Re: LISP to calculate area of rectangle

Thats awesome, thanks. Now I can't get it to work. :S

I'm just getting into writing Lisps ... and I'm not that dangerous yet.

5. ## Re: LISP to calculate area of rectangle

Originally Posted by john.nason
Now I can't get it to work.
What doesn't work about it?

6. ## Re: LISP to calculate area of rectangle

I put the code into a text file, added the defun d:rar to it, and I get the message below.

Command: _appload rar.lsp successfully loaded.

Command: ; error: An error has occurred inside the *error* functionbad
function: "malformed list on input"

7. ## Re: LISP to calculate area of rectangle

Originally Posted by rkmcswain
What doesn't work about it?
This is what I saved as rar.lsp

Code:
```(defun c:rar ()

; --------------------------------
(setq r0 0.0
r45  (* 0.25 pi)
r90  (* 0.5 pi)
r135 (* 0.75 pi)
r180 pi
r225 (* 1.25 pi)
r270 (* 1.5 pi)
r315 (* 1.75 pi)
)
(setq pt1 (getpoint "\nSelect first point: ") time T)
(while time
(setq drag (grread t 1 1))
(cond ((= (car drag) 5) ;<- moving cursor
(setq pt2 (cadr drag))
(setq xdim (abs (- (car pt1)(car pt2))))
(setq ydim (abs (- (cadr pt1)(cadr pt2))))
(setq myarea (* xdim ydim))
(grtext -1 (strcat "Area = " (rtos myarea)))
(setq vec (angle pt1 pt2))
(cond ((and (>= vec 0.0) (< vec r90))
(setq x r0 y r90)
)
((and (>= vec r90) (< vec r180))
(setq x r180 y r90)
)
((and (>= vec r180) (< vec r270))
(setq x r180 y r270)
)
((>= vec r270)
(setq x r0 y r270)
)
)
(setq pt3 (polar pt1 x xdim) pt4 (polar pt1 y ydim))
(redraw)
(grvecs (list 7 pt1 pt3 7 pt3 pt2 7 pt2 pt4 7 pt4 pt1))
)
((= (car drag) 3) ;<- picked point
(setq time nil)
(command "._pline" pt1 pt3 pt2 pt4 "_C")
)
)
)```

8. ## Re: LISP to calculate area of rectangle

John,

You are missing the closing parenthesis at the end of the file.

You may also want to make all the variables local by changing the defun line to:

Code:
`(defun c:rar (/ DRAG MYAREA PT1 PT2 PT3 PT4 R0 R135 R180 R225 R270 R315 R45 R90 TIME VEC X XDIM Y YDIM)`
Let's see if that fixes it.

FYI: The Visual Lisp editor would have helped you locate the error and compile the list of variables.

9. ## Re: LISP to calculate area of rectangle

Thats wonderful! Thanks man.

10. ## Re: LISP to calculate area of rectangle

Now to add another aspect to it ...

Can that translate the size of the rectangle as well??

And what about overiding the units to read back in metres to two decimal places.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•