12

4

I'm looking for a general method of packing any set of 2D glyphs. For example, say I had 30 randomly transformed english characters:

```
$letters =
Table[First[
First[ImportString[
ExportString[
Style[c, Italic, FontSize -> 24, FontFamily -> "Times"],
"PDF"], "PDF", "TextMode" -> "Outlines"]]], {c, Alphabet[]}];
n = 30;
toPack = Table[{Hue[RandomReal[]],
Translate[
Rotate[Scale[RandomChoice[$letters], RandomReal[5]],
RandomReal[2 Pi]], RandomReal[20, {2}]]}, {n}];
Graphics@toPack
```

How would I pack them so they are touching on their edges? Doesn't have to be optimal, just a random layout where they are touching compactly. Basically, assume they are all magnets that attract each other in 2D.

For example, with 5 shapes, here's an output I would expect:

**Update:**

I played with @ChipHurst's idea of using `WordCloud`

, but it doesn't make valid tight packings, most of them aren't touching and some of them even overlap.

2I assume your picture is an infeasible packing? – MikeY – 2019-03-28T23:23:50.190

Sure, just starter code for generating random shapes that I'd like to layout in a packing – M.R. – 2019-03-30T02:48:08.547

1You can get close to what you want with

`WordCloud`

using random weights and`WordSpacings -> 0`

. If you need them touching, perhaps you could iterate over some force field until they're close enough. – Chip Hurst – 2019-04-10T21:08:59.707Though to avoid overlap, I had to rasterize your

`toPack`

. I'm not sure why.`SeedRandom[1234]; WordCloud[ AssociationThread[Rasterize[#, Background -> None] & /@ Graphics /@ toPack, RandomReal[5, Length[toPack]]], WordSpacings -> 0]`

– Chip Hurst – 2019-04-10T21:12:41.820Ah nice trick, but the shapes might not be just letters (I picked letters arbitrarily) but arbitrary polygon shapes – M.R. – 2019-04-10T21:22:51.933

@M.R. Need to solve the problem of packing arbitrary polygons? – Alex Trounev – 2019-04-10T23:55:43.293

@AlexTrounev chip solved it – M.R. – 2019-04-12T03:17:58.130