# Creating new graphics primitive (EdgeForm, FaceForm)

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]


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]


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]


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

### 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, GraphicsMeshPolygonCombine 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]]}


And with GraphicsMeshPolygonCombine we get

-

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[GraphicsMeshPolygonCombine @ 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_] :=
TypesetMakeBoxes[p : patt, fmt_, Graphics] :=
TypesetMakeBoxes[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}


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}] *)

-
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

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

Boing2[] := GraphicsMeshPolygonCombine@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 GraphicsPolygonUtils. There might be some issues with self-intersecting polygons (I haven't looked into it), but this is a good start.

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

FilledCurve seems to do exactly what you want

-
Do you mind including in your answer how to plot the examples given in the question by using FilledCurve? – Dr. belisarius Dec 13 '13 at 9:00
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. – Dr. belisarius Dec 13 '13 at 17:08

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[GraphicsMeshPolygonCombine[poly], FaceForm[]]
}]


-