I am emulating an old arcade game: I think it was called Qix. The player gradually “fills in” the game screen. I have instances of both Polygon and Area that encapsulate the area that the player has filled in, but I can’t figure out how to get an int or a double that represent the actual number of pixels contained by an Area or Polygon instance. Any pointers?
Once you know how to compute the area of a triangle, you can subdivide your polygon to find the total area.
Basically, take one vertex A, and for each edge B->C in the polygon, compute the area of the triangle formed by ABC, and sum the results.
This, however, will only work for convex polygons. To handle concave polygons you need to do a bit more work. For each edge B->C, you check which side A falls on, and either add or subtract the area of the triangle to the total.
For instance, assuming you are iterating through the edges in a counter-clockwise direction, if A lies on the left hand side of B->C, you add the area of the triangle ABC, if on the right hand side, you subtract.
Excellent. Thanks. The link to the Line2D static method is a nice added bonus: didn’t know about that.