2
$\begingroup$

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]

enter image description here

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"}]

Triangulation mesh

meshes = MeshPrimitives[mesh, 2];
triangles = Map[Graphics[#] &, meshes]

enter image description here

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]

enter image description here

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.

enter image description here

$\endgroup$
5
  • $\begingroup$ This all stems from a triangulation mesh that was broken up into pieces. $\endgroup$ Oct 16, 2022 at 0:43
  • $\begingroup$ Please post triangles and what is wc2 $\endgroup$
    – cvgmt
    Oct 16, 2022 at 1:00
  • $\begingroup$ @cvgmt Just fixed it. wc2 was an old bit of code from the previous implementation sorry about that $\endgroup$ Oct 16, 2022 at 1:26
  • $\begingroup$ In this specific case you can certainly "cheat" a bit by matching matching triangle side lengths, but I assume you want a more general purpose packing than one matching this very specific triangles? (See mathematica.stackexchange.com/questions/123993/… ) $\endgroup$
    – kirma
    Oct 16, 2022 at 4:05
  • $\begingroup$ "This is working towards a program that turns any shape into a rectangle." Maybe you should explain your whole problem. Are you searching a rectangle with the same area? $\endgroup$ Oct 16, 2022 at 7:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.