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.
WordCloud
using random weights andWordSpacings -> 0
. If you need them touching, perhaps you could iterate over some force field until they're close enough. $\endgroup$toPack
. I'm not sure why.SeedRandom[1234]; WordCloud[ AssociationThread[Rasterize[#, Background -> None] & /@ Graphics /@ toPack, RandomReal[5, Length[toPack]]], WordSpacings -> 0]
$\endgroup$