5
$\begingroup$

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

$\endgroup$
1
  • $\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$ Commented Jan 26, 2014 at 4:24

2 Answers 2

5
$\begingroup$

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

$\endgroup$
8
  • 2
    $\begingroup$ ...where yourImageHere is the OP's plot but where Polygon has been replaced by Line. $\endgroup$ Commented Jan 26, 2014 at 4:40
  • $\begingroup$ @Pickett: Good idea, cleans up result nicely! $\endgroup$ Commented 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$ Commented 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$ Commented 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$ Commented Jan 26, 2014 at 11:52
8
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Nicely done - I tried SimplePolygonPartition but gave up after it just sat there thinking for ten minutes. $\endgroup$ Commented 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$ Commented Jan 28, 2014 at 9:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.