How to calculate the areas of irregular shapes?
Hey, I am pretty new to Unity and game development. I am trying to make a game where a player will draw lines and create some random irregular shapes. How can I calculate these irregular areas when the player draws a line? I am not sure how to approach this problem. Currently, I am using line renderer to draw lines. So, I know where the lines begin and end. But, I don't know how to calculate the area automatically. Can someone give me some directions?
Answer by JVene · Nov 12, 2018 at 12:50 PM
There is a large body of math related to the subject, but the basic approach to what you've illustrated is to break the information into triangles (which are easy to calculate). You'll discover that the vertices that define such shapes can be traversed so as to identify multiple triangles that comprise the shape. This simplified approach relies on the notion that straight lines are used.
To be clear, examine the yellow region and notice it is a pair of triangles back to back. Given 3 sides, you can find the common method for area. The green region is comprised of 3 triangles, the white is 4.
You may also want to know that the GPU handles everything (in models) as triangles, and must store the vertices to make it trivial to "loop through" the triangles as connected areas for painting with textures. There are also algorithms you can find (Google, etc) which orient the vertices for such regions in order for triangle strips or fans, specifically for OpenGL or DirectX models (optimizing models for triangle strips). You might find them useful here.
In case it isn't obvious, once you have vertices ordered so you can "loop through" the triangles that comprise a region, you simply use the distance formulate to measure each side, from which you can obtain the area (use trig for area of non-right triangles to generalize).
Apparently, it is not a straightforward simple thing to do but I think I got the necessary keywords. Thanks :)
Your answer
Follow this Question
Related Questions
How to calculate which tiles to affect in tile based game? 1 Answer
Longest distance between straight line and a curve. 1 Answer
How to get a drawn line to snap into a shape? 0 Answers
Help with arrival time calculations,Arrival time calculation? 2 Answers
How can I transition between areas on the tilemap in a top-down RPG type of game? 0 Answers