Area of polygons

An example showing limits of this two methods, result is false for any fill-rule!

Two methods

1°) Using triangles from any point, and with
points pair as vertices.

Area is calculated with vectorial product, result is
positive or negative.

2°) Using trapeze, with base on x-axis and
points pair as vertices.

Area is calculated by base*(y1+y2)/2, result is positive or negative

Final result is positive or negative, it depends of order for points.

On SVG picture, positive area is yellow and negative area is red.

Code

Get coordinates of points:

svgdoc=evt.getTarget().getOwnerDocument();obj=svgdoc.getElementById('pol1');

points=obj.getAttribute("points");coordo=points.split(" ");

In array coordo, you can have empty values (
CR, two or more spaces ), you must eliminate them.

p0=coordo[j].split(",");x1=p0[0];y1=p0[1]; give
coordinates xi and yi

Calculation

First method

area=area+(x1*y2-x2*y1)/2; if common vertice is (
0 ; 0 )

Second method

area=area+(x2-x1)*(parseFloat(y2)+parseFloat(y1))/2;

Don't forget to take again first point to close