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.

My question is strongly related to this question, nevertheless I would like to bring it to every ones attention. Lets say I want to create a new graphics primitive Boing which should look like this

Boing[] := 
 Polygon[Join[#1, Reverse[#2]] & @@@ 
   Partition[
    Table[{2 x/Pi, y/2*Cos[x]}, {x, -Pi/2, Pi/2, 
      Pi/10}, {y, {-1, 1}}], 2, 1]]

Graphics[{Opacity[0.3, Blue], Boing[]}, 
 AspectRatio -> Automatic]

Mathematica graphics

As new primitive, it should clearly work like other primitives like Disk, Rectangle, etc. When I define a FaceForm and EdgeForm one sees that this does not work because the settings are applied to the underlying Polygons

Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3, Blue]], 
  Boing[]}, AspectRatio -> Automatic]

Mathematica graphics

In this case one could slightly re-arrange the points and create one overall polygon to circumvent the issue

Boing[] := 
 Polygon[Join[#1, Reverse[#2]] & @@ 
   Transpose[
    Table[{2 x/Pi, y/2*Cos[x]}, {x, -Pi/2, Pi/2, 
      Pi/10}, {y, {-1, 1}}]]]
Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3, Blue]], Boing[]}, 
 AspectRatio -> Automatic]

Mathematica graphics

When I am not mistaken, then this is in general not possible without having access to the current values of FaceForm and EdgeForm. Consider the following example where I don't see a direct way

Mathematica graphics

Question:

What is the most simple solution to create a new graphics primitive which works like e.g. a polygon? To clarify: I want to be able to manually construct a new primitive and define how the settings of the current graphics state like FaceForm or EdgeForm (Texture, Specularity or Lighting are further examples which change the state of the graphics engine) are handled.

Update:

The answer of rm-rf was promising but unfortunately, Graphics`Mesh`PolygonCombine seems to make more or less the same what I have already shown above. This means, that for my target application it will not work. Consider the following example

points[r1_, r2_, n_] :=
  With[{dphi = 2.0 Pi/(n - 1)},
   Table[r {Cos[phi], Sin[phi]}, {phi, 0, 2 Pi, dphi}, {r, {r1, r2}}]
   ];

Graphics@{EdgeForm[{Black, Thick}], FaceForm[Opacity[0.3, Red]],
  Polygon[
   Function[{p1, p2}, Join[p1, Reverse[p2]]] @@@ 
    Partition[points[0.3, 0.7, 30], 2, 1]]}

Mathematica graphics

And with Graphics`Mesh`PolygonCombine we get

Mathematica graphics

share|improve this question
add comment

4 Answers

up vote 18 down vote accepted

I think you will need to use FilledCurve to create objects with holes in.

For example:

points[r1_, r2_, n_] := With[{dphi = 2.0 Pi/(n - 1)}, 
   Table[r {Cos[phi], Sin[phi]}, {phi, 0, 2 Pi, dphi}, {r, {r1, r2}}]];

poly = Polygon[Function[{p1, p2}, Join[p1, Reverse[p2]]] @@@ 
    Partition[points[0.3, 0.7, 30], 2, 1]];

prim = FilledCurve[Thread[Graphics`Mesh`PolygonCombine @ poly] /. 
    Polygon[data_] :> {Line[data]}];

To make something that behaves more like a graphics primitive I will use my answer from here

SetAttributes[createPrimitive, HoldAll];
createPrimitive[patt_, expr_] := 
 Typeset`MakeBoxes[p : patt, fmt_, Graphics] := 
  Typeset`MakeBoxes[Interpretation[expr, p], fmt, Graphics]

createPrimitive[donut, Evaluate@prim]

Now you can use donut in Graphics:

Graphics@{EdgeForm[{Black, Thick}], FaceForm[Opacity[0.3, Red]], donut}

enter image description here

Because donut has no downvalues, it remains unevaluated in the graphics expression:

InputForm[%]
(* Graphics[{EdgeForm[{GrayLevel[0], Thickness[Large]}], 
    FaceForm[Opacity[0.3, RGBColor[1, 0, 0]]], donut}] *)
share|improve this answer
    
I haven't considered FilledCurve because I was disappointed that there seems to be no way to access the current state of a Graphics in between the drawing steps. I was particularly interested in this because in OpenGL this is possible for textures, colors, etc. I know hacks around this, but I was really curious that no one seems to miss this. Your answer is nevertheless the most direct way in this case. +1 –  halirutan Dec 13 '13 at 16:14
    
@halirutan Not being able to access it might turn out to be a more serious problem when trying to write an exporter to another format which doesn't directly map to Mma's graphics representation. –  Szabolcs Dec 13 '13 at 20:31
add comment

You can use the undocumented PolygonCombine to create a single polygon which behaves well with EdgeForm and FaceForm:

Boing2[] := Graphics`Mesh`PolygonCombine@Boing[]
Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3, Blue]], Boing2[]}, AspectRatio -> Automatic]

In Mathematica 10/Wolfram Language/Mathematica-RPi, the function can be found under Graphics`PolygonUtils`. There might be some issues with self-intersecting polygons (I haven't looked into it), but this is a good start.

share|improve this answer
    
It is indeed a good start. Thanks for this answer! –  halirutan Dec 13 '13 at 1:55
5  
Unfortunately, for things with holes it doesn't work. –  halirutan Dec 13 '13 at 2:26
add comment

FilledCurve seems to do exactly what you want

share|improve this answer
    
Do you mind including in your answer how to plot the examples given in the question by using FilledCurve? –  belisarius Dec 13 '13 at 9:00
3  
Sorry , didn't included it because the example in the first page of the reference documentation is really close to the requirement. Just needs a few adjustments for the shape. –  Nicolas Venuti Dec 13 '13 at 10:31
    
Don't bother now. @SimonWoods did it. –  belisarius Dec 13 '13 at 17:08
add comment

Posting a separate answer, as this is really a completely different approach. While it seems impossible to access the current setting for FaceForm and EdgeForm in between the drawing steps, it is possible to temporarily change them for a particular primitive with Style.

So you can show the original polygon with no edges, and the combined polygon with no faces:

points[r1_, r2_, n_] := With[{dphi = 2.0 Pi/(n - 1)}, 
   Table[r {Cos[phi], Sin[phi]}, {phi, 0, 2 Pi, dphi}, {r, {r1, r2}}]];

poly = Polygon[Function[{p1, p2}, Join[p1, Reverse[p2]]] @@@ 
    Partition[points[0.3, 0.7, 30], 2, 1]];

Graphics[{EdgeForm[{Black, Thick}], FaceForm[Opacity[0.3, Red]], 
  Style[poly, EdgeForm[]], 
  Style[Graphics`Mesh`PolygonCombine[poly], FaceForm[]]
  }]

enter image description here

share|improve this answer
add comment

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.