Suddivide surfaces in triangles and rectangles

Hello,
I hope I didn’t get the wrong category for this topic. This is not a job for someone, I’m just trying to confront myself with other minds about my topic. I’m trying to create a pyRevit plugin. I would like to create a python script where I can take a surface (from areas/rooms) and split its area in rectangles and triangle (if necessary i.e. curve/inclinated wall/room boundary/area boundary, ecc..). Is there some formulas or criteria/ articles/ sites who can help me to create this program concept?
Thanks

Much depends on what your final desired geometry is and what it will be used for. For general tessellation (triangles) there are python libraries like PyVista and many others.

But sounds like you are after something other than tessellation(?)

Imagine we have a room/area with irregular form (not curve lines). I would like to divide this macro area in smaller rectangles and triangles (for a matter of optimization, the lesser number of forms there are, the better it is).

For a simple example: a trapezoid should be divided in a rectangular and a triangle.

Maybe look at the Shapely library.
If I had to code it from scratch, I’d probably apply a grid over the form and then reassemble the boxes to create the largest rectangle(s) I could find. The leftover bits should be triangles - except for curves that would need to be triangulated.

Get’s a bit tricky when your room has orthogonal rectangles and angled rectangles. Then I would need to do some parallel lines testing to find those angled rectangles. I’m doing something similar with another script, where I group parallel lines and then project end points to each paired lines. Might be another approach.


A lot of possible end results from a complex space. What are the shapes to be used for?

…and are you looking for biggest orthogonal? Biggest nonorthogonal? Fewest shapes? Etc…

I am looking for fewest shapes + filling the more as possible with rectangles (not too small, for a matter of dimensions/annotations).

With the script i would like to “extract” some urbanistic data (es. Apartmenr Total area, external surface, property surface, ecc.) filling the rectangles/triangles with adaptive generic models.

Looking at your last image im thinking about an orientation similar to the second one.

…just search for find biggest rectangle in shape. Plenty of math out there on this with multiple approaches. The two biggest issues will be concave polygons and holes in a polygon.

You could probably simplify the code if the user picks and edge or direction. In the examples above, you could select the top edge and use point projection to find the rectangle. Gather all vertices, project them to the top selected line. Find the two points furthest apart on the line. Some refinement to see if the line’s endpoints should be used or not (does perpendicular ray cross polygon? …a concave edge complicates things, so you would need to do a few loops to check for that. How many times does a ray cross another edge?) Use that to then create the rectangle. From there it should be pretty easy.