• Home
  • Training Books
  • Subscribe to Our Email Newsletter
  • About
    • Contributors
    • Feedback
    • Contact
    • Privacy policy
    • Cookie Policy

CADnotes

CAD Tutorials and Best Practices for professionals and students

  • Featured
  • AutoCAD
    • AutoLISP
  • Revit
    • Revit Architecture Basic
    • Revit MEP Basic Tutorial
  • Inventor
  • MicroStation
    • MicroStation Basic Tutorial
You are here: Home / AutoCAD / AutoLISP / AutoLISP Exercise : Create Regular Polygon by Defining Area

AutoLISP Exercise : Create Regular Polygon by Defining Area

December 1, 2010 by Edwin Prakoso 3 Comments

In this Article...

  • writing Calculation in AutoLISP
  • Drawing Polygon Method
  • Calculating Apothem Length
  • Asking For User Input
  • Writing the Complete Code

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.

calculating_polygon_area

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.

apothem

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:

Formula_for_apothem

Functions in Our Formula

We will use these function in our formula.

  1. square root can be written as (sqrt a),
  2. multiplication as (* a b),
  3. division as (/ a b),
  4. cosine of an angle as (cos ang),
  5. 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.

  1. 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.
  2. Area can have decimal numbers, so it is a real number. We can use GETREAL for this purpose.
  3. 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.2

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

  1. You need to ask the user the number of polygon sides, the expected polygon area.
  2. Then you calculate the value.
  3. You need to ask one more input: the polygon center.
  4. 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.

About Edwin Prakoso

I work as a Sr. Consultant in PT Cipta Satria Informatika. I've been using AutoCAD since R14 and Revit since Revit Building 9. I occasionally write for AUGIWorld magazine and I am also active in Autodesk discussion forum. I'm a member of Autodesk Expert Elite, an appreciation for individuals who give contributions to the Autodesk community.
Connect with me on twitter or LinkedIn.

Filed Under: AutoLISP Tagged With: autolisp tutorial, calculation, equation, formula

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Inline Feedbacks
View all comments
johny
johny
14 years ago

Mr. Prakoso

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

0
Reply
Edwin Prakoso
Edwin Prakoso
Author
Reply to  johny
14 years ago

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

0
Reply
Moataz Abbas
Moataz Abbas
14 years ago

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

0
Reply
wpdiscuz   wpDiscuz

Featured

What’s New in Revit 2021 – part 1: General Enhancements

Revit 2021 has been released! See what’s new in part 1. More to come!

Recent Articles

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available

Advertisement

New on CADnotes

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available
  • Autodesk Forma Design Contest
  • Revit 2025: Toposolid Enhancements

Meet the Authors

avatar for
avatar for
avatar for
avatar for
avatar for
avatar for

Get Connected

CADnotes on FacebookCADnotes on InstagramCADnotes on TwitterCADnotes on YouTube

© 2009 – 2025 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz