You are currently browsing the category archive for the ‘graphing’ category.

**The Shoelace Algorithm to find areas of polygons**

This is a nice algorithm, formally known as Gauss’s Area formula, which allows you to work out the area of any polygon as long as you know the Cartesian coordinates of the vertices. The case can be shown to work for all triangles, and then can be extended to all polygons by first splitting them into triangles and following the same approach.

Let’s see if we can work out the algorithm ourselves using the construction at the top of the page. We want the area of the triangle (4), and we can see that this will be equivalent to the area of the rectangle minus the area of the 3 triangles (1) (2) (3).

Let’s start by adding some other coordinate points for the rectangle:

Therefore the area of the rectangle will be:

(1) + (2) +(3) +(4): (x_{3}-x_{2})(y_{1}-y_{3})

And the area of triangles will be:

(1): 0.5(x_{3}-x_{2})(y_{2}-y_{3})

(2): 0.5(x_{1}-x_{2})(y_{1}-y_{2})

(3): 0.5(x_{3}-x_{1})(y_{1}-y_{3})

Therefore the area of triangle (4) will be:

Area = (x_{3}-x_{2})(y_{1}-y_{3}) – 0.5(x_{3}-x_{2})(y_{2}-y_{3}) – 0.5(x_{1}-x_{2})(y_{1}-y_{2}) – 0.5(x_{3}-x_{1})(y_{1}-y_{3})

Therefore we have our algorithm! Let’s see if it works with the following coordinates added:

x_{1 } = 2 x_{2 } = 1 x_{3 } = 3

y_{1 } = 3 y_{2 } = 2 y_{3 } = 1

Area = (x_{3}-x_{2})(y_{1}-y_{3}) – 0.5(x_{3}-x_{2})(y_{2}-y_{3}) – 0.5(x_{1}-x_{2})(y_{1}-y_{2}) – 0.5(x_{3}-x_{1})(y_{1}-y_{3})

Area = (3-1)(3-1) – 0.5(3-1)(2-1) – 0.5(2-1)(3-2) – 0.5(3-2)(3-1)

Area = 4 – 1 – 0.5 – 1 = 1.5 units squared

We could check this using Pythagoras to find all 3 sides of the triangle, followed by the Cosine rule to find an angle, followed by the Sine area of triangle formula, but let’s take an easier route and ask Wolfram Alpha (simply type “area of a triangle with coordinates (1,2) (2,3) (3,1)). This does indeed confirm an area of 1.5 units squared. Our algorithm works. We can of course simplify the area formula by expanding brackets and simplifying. If we were to do this we would get the commonly used version of the area formula for triangles.

**The general case for finding areas of polygons**

The general formula for the area of an n-sided polygon is given above.

For a triangle this gives:

For a quadrilateral this gives:

For a pentagon this gives:

You might notice a nice shoelace like pattern (hence the name) where x coordinates criss cross with the next y coordinate along. To finish off let’s see if it works for an irregular pentagon.

If we arbitrarily assign our (x_{1}, y_{1}) as (1,1) and then (x_{2}, y_{2}) as (3,2), and continue in a clockwise direction we will get the following:

area = absolute of 0.5( 1×2 + 3×4 + 3×1 + 4×0 + 2×1 – 3×1 – 3×2 – 4×4 – 2×1 – 1×0)

area = 4.

Let’s check again with Wolfram Alpha – and yes it does indeed have an area of 4.

It could be a nice exploration task to take this further and to explore how many different methods there are to find the area of polygons – and compare their ease of use, level of mathematics required and aesthetic appeal.

**The Van Eck Sequence**

This is a nice sequence as discussed in the Numberphile video above. There are only 2 rules:

- If you have not seen the number in the sequence before, add a 0 to the sequence.
- If you have seen the number in the sequence before, count how long since you last saw it.

You start with a 0.

0

You have never seen a 0 before, so the next number is 0.

00

You have seen a 0 before, and it was 1 step ago, so the next number is 1.

001

You have never seen a 1 before, so the next number is 0.

0010

You have seen a 0 before, it was 2 steps ago, so the next number is 2.

00102.

etc.

I can run a quick Python program (adapted from the entry in the Online Encyclopedia of Integer Sequences here) to find the first 100 terms.

```
```A181391 = [0, 0]

for n in range(1, 10**2):

for m in range(n-1, -1, -1):

if A181391[m] == A181391[n]:

A181391.append(n-m)

break

else:

A181391.append(0)

print(A181391)

This returns:

[0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1, 46, 0, 6, 23]

I then assigned each term an x coordinate value, i.e.:

0 , 0

1 , 0

2 , 1

3 , 0

4 , 2

5 , 0

6 , 2

7 , 2

8 , 1

9 , 6

10 , 0

11 , 5

12 , 0

13 , 2

14 , 6

15 , 5

16 , 4

17 , 0

18 , 5

19 , 3

20 , 0

etc.

This means that you can then plot the sequence as a line graph, with the y values corresponding to the sequence terms. As you can see, every time we hit a new peak the following value is 0, leading to the peaks and troughs seen below:

Let’s extend the sequence to the first 1000 terms:

We can see that the line y = x provides a reasonably good upper bound for this data:

But it is not known if every number would actually appear in the sequence somewhere – so this bound may not hold for larger values.

**Length of steps before new numbers appear.**

We can also investigate how long we have to wait to see each number for the first time by running the following Python code:

```
```A181391 = [0, 0]

for n in range(1, 10**3):

for m in range(n-1, -1, -1):

if A181391[m] == A181391[n]:

A181391.append(n-m)

break

else:

A181391.append(0)

for m in range(1,50):

if A181391[n]==m:

print(m, ",", n+1)

break

This returns the following data:

1 , 3

2 , 5

6 , 10

5 , 12

4 , 17

3 , 20

9 , 24

14 , 30

15 , 35

17 , 41

11 , 44

8 , 47

42 , 52

20 , 56

32 , 59

18 , 63

7 , 66

31 , 72

33 , 81

19 , 89

etc.

The first coordinate tells us the number we are interested in, and the second number tells us how long we have to wait in the sequence until it appears. So (1 , 3) means that we have to wait until 3 terms in the sequence to see the number 1 for the first time.

Plotting this for numbers 1-50 on a graph returns the following:

So, we can see (for example that we wait 66 terms to first see a 7, and 173 terms to first see a 12. There seems to be a general trend that as the numbers get larger we have to wait longer to see them. Testing this with a linear regression we can see a weak to moderate correlation:

Checking for the numbers up to 300 we get the following:

For example this shows that we have to wait 9700 terms until we see the number 254 for the first time. Testing this with a linear correlation we have a weaker positive correlation than previously.

So, a nice and quick investigation using a combination of sequences, coding, graphing and regression, with lots of areas this could be developed further.