I have an image of polygons built from a polar function (see below). I'd like to color each triangle or quadrilateral a random color.

Firstly, I'm not sure how to separate the simple shapes.

Secondly, I can't think of a simple way to color each a random color (only 3 different colors result with Colorize).

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
   Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 1000}]], 
  AspectRatio -> 1] // Colorize

enter image description here

  • $\begingroup$ A simple first approach could be p = Graphics[ Polygon[Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 500}]], ImageSize -> 1000]; MorphologicalComponents[ColorNegate@Dilation[p, 2], CornerNeighbors -> False] // Colorize $\endgroup$ Jan 26, 2014 at 4:24

2 Answers 2


Something like this (there's many ways to skin this cat) will do it. Play with parameters to your liking:

SelectComponents[MorphologicalComponents[yourImageHere, .8], "Area", 
  10^9] // Colorize

Putting this with the excellent linearization idea of Pickett in the comments, we can get this pleasing result:

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
p = Graphics[
   Line[Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 1000}]], 
   AspectRatio -> 1];

SelectComponents[MorphologicalComponents[p, .89], "Area", 
  10^3] // Colorize

enter image description here

  • 2
    $\begingroup$ ...where yourImageHere is the OP's plot but where Polygon has been replaced by Line. $\endgroup$
    – C. E.
    Jan 26, 2014 at 4:40
  • $\begingroup$ @Pickett: Good idea, cleans up result nicely! $\endgroup$
    – ciao
    Jan 26, 2014 at 4:44
  • $\begingroup$ Brilliant! Is it possible to change the color palette (ColorFunction(?))? Note that Colorize[yourImageHere,ColorFunction->"Rainbow"] ruins the stained glass randomness. $\endgroup$
    – user8454
    Jan 26, 2014 at 4:50
  • $\begingroup$ @user8454: Sure, you can just replace the Colorize above with something like Colorize[#, ColorFunction -> "Rainbow"] &. You'll probably want to play around with parameters, and perhaps implement your own ColorFunction to get the results you want. $\endgroup$
    – ciao
    Jan 26, 2014 at 4:53
  • 1
    $\begingroup$ @rasher, it's probably easier to modify the component matrix than the color function. e.g. Colorize[Mod[863 #, 231], ColorFunction -> "Rainbow"] & $\endgroup$ Jan 26, 2014 at 11:52

This method can be very time-consuming, and the scale of the original graphics seems need be small (thus i^10/10^30), but yes you can do it in vectorgraph way, with the help of Region` functions described here.

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
poly = Polygon[Table[PolarCoordinate[i^10/10^30, i*sunflower], {i, 900, 1000}]] // N;


simplePolySet = SimplePolygonPartition[poly];

         {EdgeForm[White], ColorData["DarkRainbow"][RandomReal[]], #} & /@ 
             simplePolySet (*uncomment to manipulate them:*)(* /.
                  Polygon[pts__] :>
                              TranslationTransform[Norm[Mean[pts]]^5 Normalize[Mean[pts]]]


  • $\begingroup$ Nicely done - I tried SimplePolygonPartition but gave up after it just sat there thinking for ten minutes. $\endgroup$ Jan 28, 2014 at 9:27
  • $\begingroup$ @SimonWoods Thanks :) I guess when the scale is too large (here $10^{30}$), this function will stuck because of some inner parts (in fact, IntersectQ, who used something like Graphics`Mesh`Developer`CreateMesh ) using machine number. $\endgroup$
    – Silvia
    Jan 28, 2014 at 9:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.