# Loops, Lines and the Parabola

The ADDLINE method in Autocad VBA requires an array of 3 doubles for each endpoint of the line. The code for drawing a line from 2,3 to 4,5 looks like this.

```Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double
pt1(0) = 2: pt1(1) = 3: pt1(2) = 0
pt2(0) = 4: pt2(1) = 5: pt2(2) = 0
```

By contrast the same thing can be done in autolisp with just 3 lines of code.
```(setq pt1 (list 2 3) pt2 (list 4 5)) (command "line" pt1 pt2 "") ```

You can even dispense with the variable names altogether in lisp
`(command "line" (list 2 3)  (list 4 5) "")`

As far as I know, Autocad VBA does not allow any shortcuts. Every line drawn has to use a dimensioned named assigned array of 3 doubles. This is less of a problem in practice than it looks at first.  In a loop when we calculate the point values, we can re-use the variables and re-assign values. Later we will create wrapper functions so we can draw a line the same way we used the last lisp version above.

The long goal is to create a form so the user can enter parameters. Right now we want to establish methods and write simple code with all input in the parameter list, no global variables (except AcadDoc), and no error checking. The called program is not responsible for bad data. Later we will have to add something, for instance when the calculated y value becomes too large.

To plot an equation, we draw short straight line increments from plotted point to plotted point. We will use the form PT1(x1,y1) to PT2(x2,y2). We will refer to our maximum x-range as min_x and max_x. With min_x, max_x and x_inc, we calculate the number of points and use an integer counter for the for-next loop. To minimize double floating point errors, we do not add x_inc to x each time through the loop, but we get the next x by multiplying the counter by x_inc and adding that to min_x.

```Sub testparabola()
Call parabola(-3, 3, 2, 3, 4, 0.1)
'Call parabola(-3, 3, 1, 0, 0, 0.1)
End Sub

Sub parabola(min_x As Double, max_x As Double, a As Double, b As Double, c As Double, x_inc As Double)
'y = ax^2 + bx + c
Dim x1 As Double, x2 As Double
Dim y1 As Double, y2 As Double

Dim lineobj As AcadLine
Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double

Dim i As Integer, numpts As Integer

numpts = (max_x - min_x) / x_inc 'this is the number of line segments
numpts = numpts + 1  'there is always one more pt than line segment

For i = 1 To numpts
x1 = min_x + ((i - 1) * x_inc)
x2 = x1 + x_inc
'x2 = x1 + min_x  fun mistake
y1 = a * x1 ^ 2 + b * x1 + c
y2 = a * x2 ^ 2 + b * x2 + c

pt1(0) = x1: pt1(1) = y1: pt1(2) = 0
pt2(0) = x2: pt2(1) = y2: pt2(2) = 0