So we have a polygon with N vertices located on grid. All vertices are located at the intersection of cells (so their coordinates are integers).

The objective is to calculate the total length of line segments within given polygon.
There is the simple way to do this when given polygon is [right triangle](http://s7.postimg.org/tmaljyw5n/image.png).

We can just find the length of lines inside rectangle which contains our triangle

```
xSegments = x(y - 1)
ySegments = y(x - 1)
rectangleSegments = xSegments + ySegments = 2 x y - (x + y)
triangleSegments = totalSegments / 2 = x y - (x + y)/2

```

But what should we do with other types of polygon like this:

```
Graphics[
    {
    EdgeForm[Black], FaceForm[None],
    Polygon[{{0,0}, {4,-1}, {4,1}, {2,1}, {0,3}}],
    },
    GridLines->Automatic,
    PlotRange->{{-1,5}, {-2,4}}
]

```

![enter image description here](https://i.stack.imgur.com/Wo65I.png)

Any ideas please?