# Polar Coordinate Graphing with Autocad

Polar coordinate graphing calculates position with a point format (Radius, Angle) rather than (X, Y). Radius is the distance from the origin, called the Pole, and Angle is measured from the horizontal X axis. The Formula is written with Angle as the independent variable. The programming loop increments the angle and calculates the Radius or R distance from the pole. For instance a circle is defined by the equation R = Sin (A) where A varies from 0 to 360. The start and end angle is taken from the user form, supplied by the user, along with the increment. The normal stop and start values are 0 to 360 with a step of 1 degree. However there are cases where many revolutions are used.

One line has two endpoints, two connected lines have 3 points. If you plot any sequence of connected lines, you always have one more point than lines. The graphing program takes the minimum and maximum Angle values, the increment Angle value, and calculates the number of points and lines to be drawn. It uses the calculated variable numpts to size the array for the point data, each point having two values X and Y. Even though we are polar graphing, the program still converts back to X and Y with some simple trigonometry to draw the graph.

Excel VBA wants trig function input to be Radians. We collect the input in degrees and convert to Radians in the program. Conversion of (R, A) to (X,Y) is through the simple Sin and Cos functions. X = R * Cos(A) and Y = R * Sin(A)

Thats the core of the program. There are a few enhancements, not shown below, such as an option to write the point data to an excel sheet.

```numpts = (Amax - Amin) / A_inc 'num of lines
numpts = numpts + 1
ReDim pt(1 To numpts * 2)

For i = 1 To numpts
A = Amin + ((i - 1) * A_inc)

'user works in degrees but trig functions use radians

pt(i * 2 - 1) = X: pt(i * 2) = Y

Next i
```

The function to evaluate the user typed in equation has an error handler to exit after 3 invalid results. I was a little surprised how robust the Evaluate function was in handling typed in equations. You must always indicate multiplication with the “*” and trig functions must always enclose data in parentheses without a space, but otherwise precise spacing between elements is not usually necessary in arithmetic operators.

```Function eval_polar_func(ByVal strfunc As String, A_rad As Double) As Double
On Error GoTo HandlError
Static errorcounter As Integer

strfunc = Replace(strfunc, "A", A_rad, , , vbTextCompare)
eval_polar_func = Evaluate(strfunc)

ExitHere:
Exit Function
'runtime error 13 type mismatch
HandlError:
If Err.number = 13 Then
MsgBox "type mismatch div by zero or bad equation"
eval_polar_func = ylim + 1
errorcounter = errorcounter + 1
If errorcounter = 3 Then
MsgBox "errorcounter at 3 - exiting"
End
End If

End If
End Function

```