# Keep playing with HANA: Any polygon area.

Surely you remember you geometry classes. Don’t you?

When you want to calculate the area of a polygon, there already exist formulas for the regular ones: the square, the triangle, pentagon… but, what about those non-regular polygons? What about the non-convex polygons? Well, actually we have a method that works in any case.

This method works with the vertices coordinates.

What you have to do, is to take all the vertices and numerate them counterclockwise. Then, we put it in an array and repeat the first point For example, imagine the traingle ABC with vertices in (1,0), (3,2) and (2,5).

So, we have:

Point | Id |
x1 |
x2 |
---|---|---|---|

A | 1 | 1 | 0 |

B | 2 | 3 | 2 |

C | 3 | 2 | 5 |

~A | 4 | 1 | 0 |

Then, we define **D** and **I **like

D = (1*2)+(3*5)+(2*0) = 17

I = (0*3) + (2*2) + (5*1) = 9

Thus, the area of that polygon is

A = 0.5 * | D – I |

A = 0.5 * | 17 – 9 |

A = 0.5 * | 8 |

A = 0.5 * 8

A = 4

In a more general way, **D** is the sum of the low-right-diagonal multiplications of the elements in the array (just *x1* and *x2*). And **I** is the sum of the low-left-diagonal multiplications of the elements of the same array. And this works for any number of vertices, and no matter if the polygon is not even convex!

(This works because of the polygon triangulation and the areas with positive and negative orientation. If you want more mathematical details, send me a message, please)

Let’s do it with HANA.

In your schema (mine is JC), we do:

```
set schema JC;
drop table COORDINATES;
create column table COORDINATES(
v INTEGER,
x1 FLOAT,
x2 FLOAT
);
insert into COORDINATES values(1,1,0);
insert into COORDINATES values(2,3,2);
insert into COORDINATES values(3,2,5);
insert into COORDINATES values(4,1,0);
DROP TABLE SUMA;
CREATE GLOBAL TEMPORARY TABLE SUMA(
COL VARCHAR(2),
X FLOAT
);
DROP TABLE MULTIPLICA;
CREATE GLOBAL TEMPORARY TABLE MULTIPLICA (
v INTEGER,
x1 FLOAT,
x2 FLOAT
);
CALL "_SYS_BIC"."area.poligon/area"(?)
```

You can get the *area.procedure* and the *poligonoArea.sql* in this Github repostory.

Thanks José for sharing this. Liked the idea of sharing a geometric problem using HANA.

Cheers!!

Durga

Yes, I also think that it's interesting.