# Creating Complex Polygons and Calculating Area in Three.js: Seeking Guidance

Hello everyone,

I’ve been using Three.js (fiber) for a little while now, and I’m posting this message because I’d like to create a feature, but I’m not sure how to go about it. Here it is:

I want to be able to create complex polygons by clicking at various points on a mesh that serves as my floor, and then calculate their area, like in Geogebra.

In a nutshell:

So, how do you think I should approach this? Should I use a triangulation method? Should I use something other than vertices?

Triangulation would be an overkill.

You can calculate the signed area and then just drop out the sign. You only need the vertices (ordered either clockwise or counterclockwise around the polygon perimeter).

2 Likes

This post was flagged by the community and is temporarily hidden.

First of all, thank you for your response, and for your second link ;), however, I need to know the area but also to build the shape as a mesh, and for that, I have to generate the faces of the triangles that will make up my polygon. Furthermore, for example, in GeoGebra, we can see the shape being filled in as we take the points, so maybe they have a way to create the polygon gradually. What do you think?

I assumed that the main question is how to calculate the area. The calculation of area does not need triangulation.

If, however, you have the polygon triangulated, you can calculate its area as the sum of areas of all triangles. So, yes, it is possible to do it this way, but this uses more calculations than the signed area.

If you want to create the intermediate shapes as a meshes, while the user still constructs the polygon, you can use THREE.ShapeGeometry which automatically triangulates a shape, so you do not have to do it by yourself. This somewhat contradicts with your first post, which says that the polygon is constructed at the end:

1 Like

Indeed, I didn’t specify it in the first message, and I apologize for that, but ultimately the purpose of the first message is still to create complex polygons. So, do I actually need to use triangulation to create my polygon as I described it?

You can use THREE.ShapeGeometry and it will automatically do triangulation, so you do not have to do it by yourself. Here it what it looks like when using a ShapeGeometry to define a polygon with mouse clicks:

2 Likes

Oh, that’s amazing! Could I have the source code? Is it therefore possible to calculate the area of the polygon since it’s automatically triangulated?

The source code is here: https://codepen.io/boytchev/full/GRPvaWL

The area can be calculated irrespective on whether the polygon is triangulated or not. If you insist on using triangulation for the area, then extract triangles from the geometry and compute the area based on their vertices.