Triangle Containment (102)
In Problem 102, we're given a list of vertices of triangles. For each triangle, we're supposed to check whether the coordinate origin is contained within the triangle and count these.
There are many different ways to check whether a point is contained in a triangle. We will use cross products. Let
Without loss of generality, let us assume that the vertices are arranged in a counterclockwise fashion. If the origin is within the triangle, the connections from one vertex to the origin will always be left of the connection to the next vertex. This holds for all three vertices. If the origin is outside, some of these origin connections will be on the left, others on the right.
We can express this “left or right” relation with a cross product. Since we're in two dimensions only, we only need to look at the third component of the cross product. For two vectors
where
In our concrete case we're interested in
Setting