Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]}
Graphics[Polygon[
   Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 1000}]], 
  AspectRatio -> 1] // Colorize

enter image description here

share|improve this question
    
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 –  belisarius Jan 26 at 4:24

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
2  
...where yourImageHere is the OP's plot but where Polygon has been replaced by Line. –  Pickett Jan 26 at 4:40
    
@Pickett: Good idea, cleans up result nicely! –  rasher Jan 26 at 4:44
    
Brilliant! Is it possible to change the color palette (ColorFunction(?))? Note that Colorize[yourImageHere,ColorFunction->"Rainbow"] ruins the stained glass randomness. –  user8454 Jan 26 at 4:50
    
@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. –  rasher Jan 26 at 4:53
1  
@rasher, it's probably easier to modify the component matrix than the color function. e.g. Colorize[Mod[863 #, 231], ColorFunction -> "Rainbow"] & –  Simon Woods Jan 26 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;

Graphics`Region`RegionInit[];

simplePolySet = SimplePolygonPartition[poly];

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

sunflower

share|improve this answer
    
Nicely done - I tried SimplePolygonPartition but gave up after it just sat there thinking for ten minutes. –  Simon Woods Jan 28 at 9:27
    
@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. –  Silvia Jan 28 at 9:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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