5

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

Silvia
  • 27,556
  • 3
  • 84
  • 164
user8454
  • 353
  • 2
  • 9
  • 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 – Dr. belisarius Jan 26 '14 at 04:24

2 Answers2

8

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

Silvia
  • 27,556
  • 3
  • 84
  • 164
  • Nicely done - I tried SimplePolygonPartition but gave up after it just sat there thinking for ten minutes. – Simon Woods Jan 28 '14 at 09: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 '14 at 09:42
5

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

ciao
  • 25,774
  • 2
  • 58
  • 139
  • 2
    ...where yourImageHere is the OP's plot but where Polygon has been replaced by Line. – C. E. Jan 26 '14 at 04:40
  • @Pickett: Good idea, cleans up result nicely! – ciao Jan 26 '14 at 04: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 '14 at 04: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. – ciao Jan 26 '14 at 04:53
  • @rasher: Is there a way to avoid the colors with like neighbors in this, as well as using ColorFunction->"Rainbow"? – user8454 Jan 26 '14 at 05:07
  • @user8454: Yes, but you'll need to either write a custom color function using the palette you want, or use rules either for coloring or from getting component measures (see the documentation re: morphological processing, many examples). Probably some elegant way to use Rainbow but randomized, but I'll leave it to the graphics experts (perhaps Mr.W or Belisaurius will chime in, they cut a mean graph...) – ciao Jan 26 '14 at 07:34
  • 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 '14 at 11:52
  • @SimonWoods-- Excellent and thank you! I don't actually understand how Colorize uses that matrix. Do you see how ColorFunction->"VisibleSpectrum" could be implemented? ColorFunctionScaling->True doesn't help for some reason... – user8454 Jan 27 '14 at 00:45