How to calculate an area of a polygon?There is a formula for calculating the area of a triangle given its three vertices based on vector multiplication. It’s this: SABC = 1/2 * (xA(yB – yC) + xB(yC – yA) + xC(yA – yB)). Strictly speaking, this formula is for calculating oriented area, i.e. it will be positive if vertices A, B, C are located counterclockwise and negative if clockwise. If you don’t know their orientation and you don’t need to know it, just take an absolute value of this area. Using this formula we can obtain the area of any polygon considering it as a union of triangles. So SP1…PN = SP1P2P3 + SP1P3P4 + … + SP1PN-1PN. Note that this formula works correctly even with non-convex polygons, subtracting areas of some triangles automatically from a total sum.

How to check is a polygon convex?That’s performed using the same formula for area calculating. A polygon P1 P2 … PN is convex if all oriented areas SP1P2P3, SP2P3P4, …, SPN-1PNP1, SPNP1P2 are of the same sign, or, speaking more strictly, all nonnegative oriented areas among these are of the same sign. If we know that all vertices of the polygon are distinct, P0ero value of some area SP(i), P(i mod N + 1), P(i mod N + 2) tells us that an angle P(i mod N + 1) is equal to 180°.

How to check is a point inside of a polygon, outside of it, or on its boundary?First of all, to check is the point lie on the boundary of the polygon that’s sufficient to check consequently is it lie on any of segments forming the boundary. For this we refer to recipe about dealing with lines and segments.Second, if the polygon is convex, we can check if all triangles P0P1P2, …, P0PN-1PN, P0PNP1 have the same sign of oriented area. If it is true, this means the point P0 lies inside the polygon, if false – outside.And, third, in the case when we don’t know is the polygon convex, we should use a somewhat more complicated algorithm. Let’s consider a line parallel to OX axis, on which point P0 being under consideration lies. For every segment of the boundary, find a point of intersection of this segment and that line (if any) and count the number of these points lying to the right from point P0. If this number is odd, the point lies inside the polygon. But there are some disgusting cases, with which we should treat carefully. First, what if a segment lies on this line and lies to the right of the point? OK, just skip this segment. Second, what if this line goes through a vertex of a polygon? Well, here we should consider, for example, only upper ends of segments, but not lower – and for doing this we need a special test routine, because this discrimination is not the same as discrimination of “starting” and “ending” ends of segments in the order of a roundabout way of the polygon’s perimeter. On the other hand, we may get rid of taking into account these cases, if we can draw not an axis-parallel line, but a line that is guaranteed not to be parallel to any one of polygon’s sides. For example, if we deal with points with integer coordinates, we can draw a line through P0 and a point with coordinates (xP0 + c; yP0 + c +1) where c is a constant bigger than maximum difference of any coordinates between any two points given in the problem.

1. Do we really need to place the formula for triangle area here? It would be better to write it as half of cross product explicitly like [(b-a) x (c-a)] / 2. The formula of course does less arithmetic operations, but it is not a significant difference for algorithm competition. On the other hand, formula involving cross product is easier to remember and understand.

2. The example with determining the place for the point and polygon is awful. There is a lot of "redundant code" there that freaks people away from it. You can delete/inline manually 60% of all those tiny functions.Moreover, the algorithm you describe here is something I'm strongly against because it involves tricky special cases.There is another way: given point (X, Y) and a polygon create a segment with end points (X, Y) --- (X+10000, Y+10001). The second point is considered to be infinitely far away. Since vector has components with gcd=1, the segment cannot intersect any of the polygon vertices. So you can trace this segment without handling any special cases.

An example of checking is a point inside, outside or on a boundary of a polygon is PointInPolygon problem. Because all edges of the polygon here are parallel to coordinate axes, this problem can be solved in simpler way, but we forget about this favorable circumstance to demonstrate the general method. Also we demonstrate using of some geometrical routines. Take your attention that we don’t use a secure comparison of doubles in cases when we know in advance they differ more than our “epsilon”, our allowable absolute error. Here is the code:

1. People should learn an explicit vector multiplication formula from a recipe Working with Lines and Segments, so it doesn't matter here.

2. Yes, I've added a description of this alternative algorithm (which made me split the recipe).But you should remember that we don't teach people to solve exactly this problem, we teach them to solve geometrical problems in general, and we haven't enough recipes to show all these tiny functions directly. And I suppose that's better to carry these standard routines into separate functions than write these operations manually every time they encounter in the solution, especially speaking about really implementation-complicated problems.

The topcoder community gathers the world's experts in design, development and data science to work on interesting and challenging problems for fun and reward. We want to help topcoder members improve their skills, demonstrate and gain reward for their expertise, and provide the industry with objective insight on new and emerging technologies.