In this Article...

I hope you are having fun with our AutoLISP exercises. Last time, we were introduced to use AutoLISP variables and asking users for input. This time we will learn about asking for more user input, then using the input in mathematical equation. The calculation result result will be used to draw an object.

Our challenge now is to create a program that can draw a regular polygon by defining number of sides and the area. Interesting isn’t it?

### writing Calculation in AutoLISP

Writing calculation in AutoLISP is quite different. But should not be difficult to understand.

If we want to calculate 2+5, then we write:

(+ 2 5)

Let us try it in AutoCAD command line. Type in AutoCAD the line above. Yes you can use AutoLISP command in AutoCAD. You need to type it exactly like above.

Now let’s try another one. Type each line then press [enter].

`(setq a 2)`

(setq b 5)

(+ a b)

What we did is set variable a value to 2, b to 5, then calculate a+b.

`(setq c (+ a b)`

This one means c = a + b.

Not so difficult, isn’t it? Refer to other calculation function in AfraLISP site here. Now let us see the polygon formula.

### Drawing Polygon Method

How can we draw polygon by defining the area? I did some searches and find this formula.

source: Math Open Reference page.

If we know the area and number of sides, we can calculate the apothem. What is apothem? See image below.

How can we draw a polygon when we know the apothem and number of sides? Using polygon tool of course. We create it using circumscribed method.

### Calculating Apothem Length

We need to change the formula to get the apothem like below:

#### Functions in Our Formula

We will use these function in our formula.

- square root can be written as (sqrt a),
- multiplication as (* a b),
- division as (/ a b),
- cosine of an angle as (cos ang),
- sine of an angle as (sin ang).

With a, b, and ang are variables.

The bad news is AutoLISP doesn’t have tan function. But there is work around. Tangen is sin/cos. Tan (pi/N) can be written as:

`(setq ang (/ pi N))`

(/ (sin ang) (cos ang))

Pi is built in variable, so we will just use it.

Besides the sequence, you should be familiar with the command. If you want to try writing the equation by yourself, go ahead. It can be a good exercise to get familiar with mathematical function in AutoLISP. You can find the equation below later.

The complete equation can be written. I use variable a for area and n for number of sides. I also use apt to keep the equation value (the apothem length), and ang to keep (pi/n).

`(setq ang (/ pi n))`

(setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n)))

Or you can write in a single line, which looks more confusing.

`(setq apt (sqrt (/ (/ a (/ (sin(/ pi n)) (cos(/ pi n)))) n)))`

### Asking For User Input

We already use getpoint to ask user to pick a point. Or they can type the coordinate. Now we need three user input: number of sides, area, and center point.

- Number of sides is an integer. You don’t accept 2.5 as number of sides, don’t you? We can use GETINT to ask for integer.
- Area can have decimal numbers, so it is a real number. We can use GETREAL for this purpose.
- You already use GETPOINT to ask for a point right?

Let us try. In AutoCAD command line, type

(GETINT)

type integer number. It should return the number you entered. Try again. This time type a decimal number.Let’s say 3.2. What will AutoCAD say?

Command: (getint)

3.2Requires an integer value.

Using the right user input function will also reduce probability of errors.

### Writing the Complete Code

Now we can write the complete code.

- You need to ask the user the number of polygon sides, the expected polygon area.
- Then you calculate the value.
- You need to ask one more input: the polygon center.
- Finally you can write the polygon command.

Now we have complete data, what we will put in our program. I strongly suggest you to try writing the program first. You can check and compare the code later.

Below is the complete code I created. If you have problem, you can copy the code below.

`; This LISP will create regular polygon by defining polygon area and number of sides`

; Created by Edwin Prakoso

; Website: https://www.cad-notes.com

` `

`(defun c:pba (/ a n apt ptloc)`

(setq n (getint "

Number of Polygon Sides: "))

(setq a (getreal "

Expected Polygon Area: "))

(setq ang (/ pi n))

(setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n))) ;calculating apothem for circumscribed polygon

(setq ptloc (getpoint "

Pick Location: "))

(command "_POLYGON" n ptloc "C" apt)

)

How are you doing so far? I would like to know what do you think after you have done the exercises.

Next, we are going to create an AutoLISP program to label coordinate.

Mr. Prakoso

Can you help me do a program that aligns all entities circle from the drawing after( x, y) axis?

Is this trick by Orhan Toker helps? Aligning Text Using Quick Properties Window. You can do it by using properties or quick properties.

i am from cairo egypt how i can get this book ?