<p>There is no general algorithm for packing problems. Only some of the special cases have known, and optimal, solutions. If you are packing one shape then finding a reasonable solution is possible. Like the known cases of hexagonal packing etc.</p>

<p>However, if you have multiple diffenently sized objects then easy just flew out of the door. Some heurestics have been developed, since there is a very big demand for such algorithms in manufacturing. But they are not usually very fast (performant?) or simple to implement. Many of them use a genetic algorithm. A free one that you may test can be found at<a href="http://svgnest.com/" rel="nofollow noreferrer">svgnest.com</a> but faster ones and ones working on shaking and collision also exist.</p>

<p>Anyway your question is sufficiently vague, so i dont actually know if
 you want to do packing. I mean it is perfectly possible you mean tiling, which is pretty trivial.</p>

CC BY-SA 3.0
2017-10-04T13:31:46.130Z
2017-10-04T13:31:46.130Z
joojaa
<p>I am creating a 2D CAD drawing generator in C# using .netDXF library. Given a area I want to fill it with rectangles/circles (convex). For example, the regions marked with numbers in below image. This is just an example, the pattern may change.</p>

<p>I'am not an expert in the area of graphics. What should be my approach to this problem? Is there any algorithm that I can follow? What would be a generalized (not specific to .netDXF) way to fill an irregular region with 2D shapes.</p>

<p>Any help is appreciated!</p>

<p><a href="https://i.sstatic.net/gSzNA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gSzNA.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-10-04T03:18:11.033Z
So you want to place a bunch of small shapes of one type (same size?) inside a larger shape? Its not possible to fill the shapes in the images with circles completely with no overlap.
Andrew Wilson
CC BY-SA 3.0
2017-10-04T03:49:54.190Z
Same sized polygon/circle, there'd be a fixed distance between them, and it's fine to have partial shapes near the boundary.
user7413
CC BY-SA 3.0
2017-10-04T03:57:30.467Z
For circles you'd hexagonally pack them to get them as tight as can be. Then clip ones that lie on boundaries. This wouldn't vary with regions or circle size. If all you want to do is fill these shapes with color then forget the rectangles/circles and just scanline them in.
Andrew Wilson
<vector-graphics><polygon><2d-graphics><cad>
Fill an irregular region with 2D shapes
user7413
# Fill an irregular region with 2D shapes

(Asked by: username_0 on 2017-10-03)

I am creating a 2D CAD drawing generator in C# using .netDXF library. Given a area I want to fill it with rectangles/circles (convex). For example, the regions marked with numbers in below image. This is just an example, the pattern may change.

I'am not an expert in the area of graphics. What should be my approach to this problem? Is there any algorithm that I can follow? What would be a generalized (not specific to .netDXF) way to fill an irregular region with 2D shapes.

Any help is appreciated!

[![enter image description here](https://i.sstatic.net/gSzNA.png)](https://i.sstatic.net/gSzNA.png)

**Tags:** vector-graphics, polygon, 2d-graphics, cad

> **Comments:**
> **username_1**: So you want to place a bunch of small shapes of one type (same size?) inside a larger shape? Its not possible to fill the shapes in the images with circles completely with no overlap.  
> **username_0**: Same sized polygon/circle, there'd be a fixed distance between them, and it's fine to have partial shapes near the boundary.  
> **username_1**: For circles you'd hexagonally pack them to get them as tight as can be. Then clip ones that lie on boundaries. This wouldn't vary with regions or circle size. If all you want to do is fill these shapes with color then forget the rectangles/circles and just scanline them in.  

---

## 1 answers

### Answer by username_2 on 2017-10-04. Score: 1

There is no general algorithm for packing problems. Only some of the special cases have known, and optimal, solutions. If you are packing one shape then finding a reasonable solution is possible. Like the known cases of hexagonal packing etc.

However, if you have multiple diffenently sized objects then easy just flew out of the door. Some heurestics have been developed, since there is a very big demand for such algorithms in manufacturing. But they are not usually very fast (performant?) or simple to implement. Many of them use a genetic algorithm. A free one that you may test can be found at[svgnest.com](http://svgnest.com/) but faster ones and ones working on shaking and collision also exist.

Anyway your question is sufficiently vague, so i dont actually know if
you want to do packing. I mean it is perfectly possible you mean tiling, which is pretty trivial.


---

