2
$\begingroup$

In documentation of Polygon we can read:

Polygon[{poly_1,poly_2,…}] represents a collection of polygons poly_i.

But then there is no example how to style each polygon differently in such a collection.

Is there other method than this ridiculous manual method?

(I am not interested in methods that separate the collection into single polygons, I can do that myself.)

polycollection = Polygon[Append[ Table[RotationMatrix[n Pi/3] . # & /@ {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}}, {n, 0, 5}], {{1, 1}, {2, 1}, {2, 2}, {1, 2}}]]; Graphics[{EdgeForm[Black], Append[polycollection, VertexColors -> {{Red, Red, Red}, {Green, Green, Green}, {Blue, Blue, Blue}, {Yellow, Yellow, Yellow}, {Orange, Orange, Orange}, {Purple, Purple, Purple}, {Pink, Pink, Pink, Pink}}]}] 

enter image description here

$\endgroup$
10
  • $\begingroup$ Polygon will automatic union the polygons. I think there are no general way. Style[Polygon@#, RandomColor[]] & /@ polycollection[[1]] // Graphics $\endgroup$ Commented Nov 4 at 10:56
  • 1
    $\begingroup$ Why is it ridiculous? You have a "multi-shape specification" and in that context VertexColors affords you the ability to work with each shape independently. It seems to me that this is exactly what you wanted with your previous example with texture and cube, and there you were frustrated that you couldn't address each sub-shape independently. So, do you want fine level control or don't you? Maybe you should back up and explain what you're actually working on and what you're trying to achieve, and then we might be able to suggest more satisfying solutions. $\endgroup$ Commented Nov 4 at 16:24
  • 5
    $\begingroup$ I’m voting to close this question because it's not clear what answer would satisfy. The example shown demonstrates how to style each polygon separately, and that pattern of specification is consistent with other styling options, e.g. VertexTextureCoordinates, that OP is familiar with. Further, it's impossible to know what OP would considers ridiculous or not, so there's no way we can test our solutions for correctness. $\endgroup$ Commented Nov 4 at 18:26
  • $\begingroup$ The VertexColors method still needs to separate the polygons: How did you know we needs to insert exact 7 colors? $\endgroup$ Commented Nov 4 at 21:38
  • 1
    $\begingroup$ Er… by "I am not interested in methods that separate the collection into single polygons" do you mean you only want one single Polygon[…] rather than something like {…, Polygon[…], …, Polygon[…], …, Polygon[…], …} in your code? By "this ridiculous manual method" do you mean manually input an option like VertexColors -> {{Red, Red, Red}, {Green, Green, Green}, …, {Pink, Pink, Pink, Pink}} is too cumbersome? Can I recap the question as "if I only want to use one single Polygon[…] for multiple polygons, how can I style them separately in a simpler way"? $\endgroup$ Commented Nov 5 at 1:29

3 Answers 3

4
$\begingroup$

A third method is to use Table[#, n] where n is a number that is equal or greater than maximum vertex count of polygons. I used 10 but in this case it could be at lest 4 because we have at most quadrilateral.

So now you do not have to write ("ridiculously") that many color names ;-) Only as many as there are sub-polygons.

polycollection = Polygon[Append[ Table[RotationMatrix[n Pi/3] . # & /@ {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}}, {n, 0, 5}], {{1, 1}, {2, 1}, {2, 2}, {1, 2}}]]; Graphics[{EdgeForm[Black], Append[polycollection, VertexColors -> (Table[#, 10] & /@ {Red, Green, Blue, Yellow, Orange, Purple, Pink})]}] 

enter image description here

$\endgroup$
2
  • $\begingroup$ (+1) Great! Third out of "exactly" two. :-D $\endgroup$ Commented Nov 4 at 20:42
  • $\begingroup$ It is even simpler than separating polygons. I think I will accept it. It does not seem anybody is going to post anything better. $\endgroup$ Commented Nov 4 at 20:46
3
$\begingroup$

OK, since my rewording

If I only want to use one single Polygon[…] for multiple polygons, how can I style them separately in a simpler way?

has been approved by OP, here's my solution:

colored[p_Polygon, colorlst_ : ColorData[97, "ColorList"]] := With[{pointlst = p[[1]]}, Append[p, VertexColors -> MapThread[ Table, {PadRight[{}, Length@pointlst, colorlst], Length /@ pointlst}]]] 

Alternatively, use the new-in-14.3 Cyclic, which makes the code slightly shorter, but slightly slower:

colored[p_Polygon, colorlst_ : ColorData[97, "ColorList"]] := With[{pointlst = p[[1]]}, Append[p, VertexColors -> MapIndexed[Table[Cyclic[colorlst]@#2[[1]], Length@#] &, pointlst]]] 

Usage:

polycollection = Polygon[Append[ Table[RotationMatrix[n Pi/3] . # & /@ {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}}, {n, 0, 5}], {{1, 1}, {2, 1}, {2, 2}, {1, 2}}]]; colored@polycollection // Graphics 

enter image description here

polycollection~colored~{Orange, LightBlue, DarkGreen} // Graphics 

enter image description here

$\endgroup$
4
  • $\begingroup$ Since you answered the question maybe you think it is not off-topic (which to me it is evidently not off-topic) and probably can reopen it? $\endgroup$ Commented Nov 6 at 9:32
  • $\begingroup$ @azerbajdzan Well, as mentioned in my comments above, I think currently the question is still a bit ambiguous, and you should clarify the question a bit more (in the body of the question, of course)… $\endgroup$ Commented Nov 6 at 10:35
  • $\begingroup$ Thank you for your attention. I am not going to do that because the question is clearly stated as it is and meets all requirements. Anyway the decision is up to you and I respect it even if I do not agree with it. $\endgroup$ Commented Nov 6 at 10:43
  • $\begingroup$ @azerbajdzan You can still vote to reopen the question, and once it's approved by 3 members, the question will still be reopened. (Since the question will be reopened once I vote, and is at least in the gray zone in my view, this time I'd like to sit back and respect the decision of the community :) . ) $\endgroup$ Commented Nov 6 at 10:50
1
$\begingroup$

There are two solutions to this problem:

  1. Split the polygon into separate polygons
  2. Use VertexColors in the multi-polygon

Solution (1) is especially easy to use and a natural syntax.

$\endgroup$
4
  • 1
    $\begingroup$ What new information this answer brings? 1. It is already written in OP, that separating polygons is one possibility and I know how to do it but I am not interested in such answers . Also cvgmt's comment under OP mentions this method too. 2. Using VertexColors - this is the method already described in OP. $\endgroup$ Commented Nov 4 at 16:15
  • 3
    $\begingroup$ I answered your question, the answer is here for any future user that has the same question. The fact that you already know the answer and for some asinine reason don't like the answer is your own business. $\endgroup$ Commented Nov 4 at 16:57
  • 2
    $\begingroup$ (-1) Both of your methods are already described in my OP , no knew information reader gets by reading your "answer". It is a mere comment not an answer. $\endgroup$ Commented Nov 4 at 17:03
  • $\begingroup$ It looks like the community bot involved. Maybe just as well, it doesn’t seem like a hill worth dying on… $\endgroup$ Commented Nov 8 at 19:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.