Under construction

Still being built. Things may change

Area of Polygons

Time limit1sMemory limit128 MB

Summary
Given a simple integer-coordinate polygon, count how many unit grid squares have nonzero area of intersection with it.
Level

Medium5 of 10

Topics
Geometry, Simulation, Implementation
Solved
No attempts yet

Problem

Yoko's math homework today was to calculate the areas of polygons in the xy-plane. The vertices are all aligned to grid points (that is, they have integer coordinates).

Your job is to help Yoko — who is not good at math or at computer programming — finish her homework. A polygon is given by listing the coordinates of its vertices. Your program should approximate its area by counting the number of unit squares (whose vertices are also grid points, i.e. 1×11 \times 1 squares) that intersect the polygon. Precisely, a unit square "intersects the polygon" if and only if the intersection of the two has non-zero area. In the figure below, the dashed horizontal and vertical lines are grid lines, and the solid lines are edges of the polygon. The shaded unit squares are considered to intersect the polygon. For this polygon your program should output 55 (as you can see, the number of shaded unit squares is 55).

Figure 1: A polygon and the unit squares intersecting it

Input

The input describes polygons one after another, followed by a terminating line that contains only a single zero.

A description of a polygon begins with a line containing a single integer mm (m≥3m \ge 3) that gives the number of its vertices. It is followed by mm lines, each containing two integers xx and yy, the coordinates of a vertex, separated by a single space. The ii-th of these mm lines gives the coordinates of the ii-th vertex (i=1,⋯ ,mi = 1, \cdots, m). For each i=1,⋯ ,m−1i = 1, \cdots, m-1, the ii-th vertex and the (i+1)(i+1)-th vertex are connected by an edge. The mm-th vertex and the first vertex are also connected by an edge (that is, the curve is closed). Edges intersect only at vertices. No three edges share a single vertex (that is, the curve is simple). The number of polygons is no more than 100100. For each polygon, the number of vertices mm is no more than 100100. All coordinates xx and yy satisfy −2000≤x≤2000-2000 \le x \le 2000 and −2000≤y≤2000-2000 \le y \le 2000.

Output

The output should consist of as many lines as the number of polygons. The kk-th output line should print an integer that is the area of the kk-th polygon, approximated as described above. No other characters, including whitespace, should be printed.

Examples1

  1. Example 1

    Input
    4
    5 -3
    1 0
    1 7
    -7 -1
    3
    5 5
    18 5
    5 10
    3
    -5 -5
    -5 -10
    -18 -10
    5
    0 0
    20 2
    11 1
    21 2
    2 0
    0
    
    Expected output
    55
    41
    41
    23