# Defining Rectangles on the fly

Hi everyone,

I asked this question a while back: http://answers.unity3d.com/questions/479616/box-mechanic-is-it-possible-to-know-when-the-box-i.html but it still bugs me to not be 100% certain of it.

So I’d like to pick your brain about a couple of possibilities for this type of mechanic to work:

1. Qix/Volfied system – filling an area “drawn” by the user then calculating that area’s size → does anyone have any good examples for this type of mechanic
2. Dynamic Rectangles – i’m not sure if it’s possible but it would mean defining a Rect after each instantiation (or replacing one)

Any other ideas would be appreciated. Let me know if I can provide more info.

Thanks!

Qix appear to do more than just simple rectangle shapes as demonstrated in this clip. However it is feasible to define the shape as a composite of several rectangles. Care has to be taken to generate multiple rectangles for certain cases where the area becomes concave etc.

Img: A few possible rectangulariations for some sample area. More solutions are possible.

I don’t have a ready algorithm for you and there are more solutions that could be generated than described in the example above. But to start somewhere, think of starting at two adjacent corners, then extending a rect inward from those two corners until you hit an edge. That would be one rect. Now you can adjust the hull by removing the area you just created. This can in turn modify the existing hull or spawn separate, isolated hulls. It depends on the shape you are rectangularizing.

Consider the first example in the image above. If you started with the top corners, you’d create the green tall rect. The hull should then have been modified to an L shape.

Img: After figuring out one rect, the hull is modified and the process starts again.

Img: In some cases you could end up dividing the hull in two or more hulls, like in this simple case.

Another representation could be generating triangles using ear clipping algorithm.

With either representation (a collection of rects vs a collection of triangles) you can then render or calculate the area. Just sum the area of all rects or triangles to get the total area. To draw the area it sounds like you want to perform two actions. Drawing the fill and the border. The border can be found by traversing the hull of your collection of primitives if you want to draw it as a mesh etc.

Not sure what you mean by dynamic rectangles. You can certainly change the fields of a Rect after it’s been instantiated.