A **vector** is a list of numbers. In Codea, a `vec2`

is a vector of length 2, whence a list of two numbers. Such a vector can be used to represent a point on the screen by using the first number to measure how far across and the second number how far up. Thus we can use `vec2`

s to represent points on the screen.

The next concept that is needed is that of **distance**. There are lots of ways to define a distance function, but the most common is the **Euclidean distance**. If we take two points, say `p`

and `q`

, then we want to measure the distance between them. Outside a program we would simply stick a ruler between them. Inside a program we need some way to compute it. Since each of these points can be represented by a vector (of length 2), we can take these two vectors and use some formula to compute the distance between them. That formula is the **Euclidean distance function**. If the first vector has components `x1`

and `y1`

and the second has components `x2`

and `y2`

then the Euclidean distance between `p`

and `q`

is given by the formula:

```
sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
```

where `a^2`

denotes the square of `a`

.

This is *just a formula*. What makes it the right formula is Pythagoras’ theorem.

Now we need the concept of a **circle**. A circle centred on a point `p`

of radius `r`

is *defined* to be the set of points which are exactly distance `r`

from `p`

. That is, it consists of all of those points `q`

with the property that `d(p,q) = r`

. From the formula, this means that `q`

is on the circle if and only if

```
sqrt( (x1 - x2)^2 + (y1 - y2)^2 ) = r
```

Now a circle divides the plane into two regions: an inside and an outside. These are characterised as follows: the inside consists of all of those points of distance to `p`

strictly less than `r`

and the outside of those points of distance strictly greater than `r`

. (The fact that the division is *clean* is due to something called the Intermediate Value Theorem.) So to test “Is `q`

inside the circle?” *programmatically* we ask “Is the distance from `p`

to `q`

less than `r`

?” (If we say “inside *or on* the circle” we ask “less than *or equal to*”; in practice the distinction is ignorable.)

So we might write:

```
if math.sqrt( (x1 - x2)^2 + (y1 - y2)^2 ) < r then
```

(using `math.sqrt`

as that is the name for the square root function in Codea)

However, square roots are expensive to compute so we use a trick to simplify this. The trick is that if `a`

and `b`

are **positive** numbers then `a < b`

if and only if `a^2 < b^2`

. Thus the above test is equivalent to the following (ie is true if and only if the following is true):

```
if (x1 - x2)^2 + (y1 - y2)^2 < r^2 then
```

and that’s almost the formula that @dave1707 has. There are two small differences. The first is that dave’s formula is for an ellipse, which is a distorted circle. The second is that his would be `if (x1 - x2)^2/r^2 + (y1 - y2)^2/r^2 < 1 then`

. This is equivalent, since it uses the fact that if `c`

is positive then `a < b`

if and only if `a/c < b/c`

. I’m not sure which is the more efficient.