This page shows a powerful trick on how to compute the area defined by any polygon with integer vertices.
Namely, the area of such a polygon is :
(number of point inside of it)
+ (number of points on the frontier) / 2 - 1, this is known as Pick's Theorem.
(The proper statement of the result and a proof sketch are given below).
Note: may not work for flat polygons
`A = i + b/2 - 1.`
`A: P \mapsto i(P) +(b(P))/2 - 1`
where, as above, `i(P)` is the number of points interior to `P` and `b(P)` the number of point on its border.`A(P) = (m-1)(n-1) + (2(m+n))/2 - 1 = mn.`
Thus the formula holds for rectangles.
`A(T) = A(R) - A(U) - A(V) - A(W).`
We have:`b(R) = b(U) + b(V) + b(W) - b(T)`
and`i(R) = i(A) + i(B) + i(C) + i(T) + (b(A) + b(B) + b(C) - b(R)) - 3.`
Using these equations we show that Pick's formula holds for `T`.While working on this simulation, I realized that there are several problems of 2D geometry that arise when studying non-self-intersecting polygons, and that the algorithms used to solve them (polygon generation from points, point-in-polygon algorithm, Ray Tracing, ...) are quite interesting in themselves. I also thought that they deserve their own small developments (if not their own page on this website).
Given an arbitrary finite set of points `S = (x_i, y_i)_{1\leqi\leqn}` of `\mathbb{R}^2`, we want to build a non self-intersecting polygon
`P` which has `S` as the set of its summits. The approach chosen in this simulation is to pick
a special point `(x_0, y_0)` of `\mathbb{Z}^2`, which we use as a reference to convert our vertices to polar coordinates.
Here, I chose for this point the barycenter of the polygon, with the above notations
for the `n` vertices of `S` :
`x_0 = \frac{1}{n}\sum_{i=1}^n x_i`, `y_0 = \frac{1}{n}\sum_{i=1}^n y_i.`
While counting the number of points inside or on the frontier of a polygon `P` is a very straightforward
task for a human being, automating it to illustrate Pick's Theorem is more complex.
The idea is to scan a "window" `W` of `\mathbb{Z}^2` and check wether each point lies
on the frontier or inside the polygon.
For `W`, we take here the integer points standing between the minimum and maximum coordinates of `P`
which yields a rectangular domain.
Then take a point `X_0 = (x_0, y_0)` outside of `W` and for each point `X = (x, y)` of `W`,
count how many times the segment `[X_0,X]` intersects the frontier of the polygon.
This process of selecting an outer point and tracing segments to detect relative positions of objects is
the basis of ray tracing.
Ray tracing has become somewhat famous thanks to modern visual rendering techniques in video games, which
allow for the creation of stunning images, but of course this comes at an increased cost in terms
of computing power. The principle of ray tracing is not new, however, and has been used since the 16th
century as a drawing technique.
The first use on a computer is more recent and was first accomplished by
A. Appel in 1968.
A scene generated using the POV-Ray software
Ray casting with one object
The concept of integer polygons is related to a whole geometric and algebraic theory (Polytop Theory among others), which provides a whole lot of results dealing with the same kind of problems as the one presented on this page. For instance the Ehrhart theorem is a very nice generalization of Pick's theorem to higher dimension. This course by O. Debarre provides a very clear and concise introduction to Polytope theory, requiring only a very basic knowledge of linear algebra.
Sources :
Images : Morn, CC0, via Wikimedia Commons, Render glass scene; Wikimedia commons, Ray tracing
Autoencoders and ray tracing denoising
A paper on hardware considerations