I'm working on packing a set of triangles into a rectangle but I'm unable to get them to pack dense enough or even touch. I found a post from 3 years ago called "Packing arbitrary shapes" which was great and I've built half my program off of that. However, it doesn't work for graphics and I've been unable to get past that.
This is working towards a program that turns any shape into a rectangle of the same area.
n = 98;
BlockRandom[glyphs = RandomChoice[triangles, n];
scales = RandomReal[5, n], RandomSeeding -> 1234]
wc = WordCloud[AssociationThread[glyphs, scales], WordSpacings -> 0,
WordOrientation -> "Random", RandomSeeding -> 1234]
insetToReg[mr_, c_, p_, s_] :=
BoundaryMeshRegion[
TransformedRegion[#,
TranslationTransform[c - RegionCentroid[BoundingRegion[#]]]],
MeshCellStyle -> {1 -> Black, 2 -> RandomColor[Hue[_]]}] &@
RegionResize[mr[[1]], s]
Triangles are made from the following code
img = https://i.sstatic.net/zM2Hd.png ;
mesh = TriangulateMesh[ImageMesh[ColorNegate[img]],
MeshQualityGoal -> 0.001,
MaxCellMeasure -> \[Infinity],
MeshCellLabel -> {2 -> "Index"}]
meshes = MeshPrimitives[mesh, 2];
triangles = Map[Graphics[#] &, meshes]
UPDATE:
I am currently working on packing the triangles closer but have run into an issue with mesh regions as illustrated below.
meshes = MeshPrimitives[mesh, 2]; triangles = Map[Graphics[#] &, meshes]
n = 20;
BlockRandom[glyphs = RandomChoice[meshes[[1 ;; 20]], n];
scales = RandomReal[5, n], RandomSeeding -> 1234]
wc = WordCloud[AssociationThread[glyphs, scales], WordSpacings -> 0,
WordOrientation -> "Random", RandomSeeding -> 1234]
insetToReg[mr_, c_, p_, s_] :=
BoundaryMeshRegion[
TransformedRegion[#,
TranslationTransform[c - RegionCentroid[BoundingRegion[#]]]],
MeshCellStyle -> {1 -> Black, 2 -> RandomColor[Hue[_]]}] &@
RegionResize[mr[[1]], s]
UPDATE AGAIN:
I've tried going in a new direction as illustrated below but I'm experiencing errors with RegionMeasure.
(*Import the RegionMeasure function from the Region \
package*)Needs["Region'"]
(*Import the vector image of the clothing pattern*)
pattern = Import["/Users/jonathanvardy/triangle pattern filled.png"];
(*Cut the pattern into triangles*)
triangles = Tessellate[pattern];
(*Convert the triangles into regios using the Polygon function*)
triangleRegions = Polygon /@ triangles;
(*Compute the total area of the triangles*)
triangleArea = Total[RegionMeasure /@ triangles];
(*Compute the side length of the rectangle that has the same area as \
the triangles*)
rectSideLength = Sqrt[triangleArea];
(*Pack the triangles into the rectangle without gaps*)
packedTriangles =
Graphics[{EdgeForm[Black], FaceForm[White], triangles},
ImageSize -> {rectSideLength, rectSideLength}];
(*Print the final result*)
packedTriangles
And I get the following error.
triangles
and what iswc2
$\endgroup$