7
$\begingroup$

I have a (vaguely) complex curve with some self-intersections, and I would like to add a filling to the interior. However, the recommended method (of turning Lines into FilledCurves), i.e. by doing something like

ParametricPlot[
  {Cos[t] + 2 Cos[2 t], Sin[t] - 2 Sin[2 t]}
  , {t, 0, 2 π}
  , Frame -> True
  ] /. {Line[pts_] :> {FilledCurve[{Line[pts]}]}}

is unsatisfactory, since, as the documentation puts it,

Filled curves can be non-convex and intersect themselves. Self-intersecting curves are filled according to an even-odd rule that alternates between filling and not at each crossing.

This puts a big hole in the middle of my figure that I would also like to fill:

Mathematica graphics

How can I control the filling in that hole?

$\endgroup$
4
  • $\begingroup$ ParametricPlot[ r {Cos[t] + 2 Cos[2 t], Sin[t] - 2 Sin[2 t]}, {t, 0, 2 Pi}, {r, 0, 1}, Frame -> True] $\endgroup$
    – Bob Hanlon
    Jun 5, 2017 at 0:43
  • $\begingroup$ @Bob That's nowhere near sufficient - try e.g. changing the weight between the two components. $\endgroup$ Jun 5, 2017 at 0:49
  • $\begingroup$ Related: (9406), (11517) $\endgroup$
    – Mr.Wizard
    Jun 5, 2017 at 0:52
  • 2
    $\begingroup$ That Mathematica does not have an option to disable this behavior is a pretty significant limitation compared to, say, SVG. $\endgroup$
    – user484
    Jun 5, 2017 at 1:22

3 Answers 3

4
$\begingroup$
pp = ParametricPlot[{Cos[t] + 2 Cos[2 t], Sin[t] - 2 Sin[2 t]}, {t, 0, 2 π} , 
        Frame -> True ] /. {Line[pts_] :> {FilledCurve[Line@pts]}};

bdg = BoundaryDiscretizeGraphics[pp[[1]], Frame->True]

Mathematica graphics

 BoundaryDiscretizeGraphics[pp[[1]], Frame->True, 
   MeshCellStyle -> {{1, All} -> Directive[Thick, Blue], {2} -> Yellow}]

Mathematica graphics

Or, use bdg in RegionPlot (thanks: @Mr.Wizard):

 RegionPlot @ bdg

Mathematica graphics

Note: In version 11, bdg = BoundaryDiscretizeGraphics @@ pp also works.

$\endgroup$
5
  • $\begingroup$ In v10.1 I need bdg = BoundaryDiscretizeGraphics @@ pp[[1]]; -- if that doesn't break code in other versions you might make that change. $\endgroup$
    – Mr.Wizard
    Jun 5, 2017 at 0:48
  • $\begingroup$ That's some pretty interesting tooling. $\endgroup$ Jun 5, 2017 at 0:49
  • $\begingroup$ @Mr.Wizard, i made the change, $\endgroup$
    – kglr
    Jun 5, 2017 at 0:54
  • $\begingroup$ Thanks. Depending on the OP's needs RegionPlot[bdg] may be sufficient and significantly cleaner. $\endgroup$
    – Mr.Wizard
    Jun 5, 2017 at 1:00
  • $\begingroup$ @Mr.Wizard, thank you again. Added the RegionPlot alternative. $\endgroup$
    – kglr
    Jun 5, 2017 at 1:05
4
$\begingroup$

Now we can use WindingPolygon since v12.

plot = ParametricPlot[{Cos[t] + 2 Cos[2 t], Sin[t] - 2 Sin[2 t]}, {t, 
    0, 2 π}, Frame -> True];
plot /. Line[pts_] :> 
   WindingPolygon[pts, "NonzeroRule"] // BoundaryDiscretizeGraphics

enter image description here

Or

Clear["Global`*"];
Needs["NDSolve`FEM`"]
plot = ParametricPlot[{Cos[t] + 2  Cos[2  t], 
   Sin[t] - 2  Sin[2  t]}, {t, 0, 2  π}, Frame -> True];
m = 
 ToElementMesh@
  DiscretizeGraphics[plot]@"MakeRepresentation"@"ElementMesh"
m // MeshRegion // BoundaryMesh
$\endgroup$
1
  • $\begingroup$ plot = ParametricPlot[{Cos[t] + 2 Cos[2 t], Sin[t] - 2 Sin[2 t]}, {t, 0, 2 π}, Frame -> True]; NDSolve`FEM`ToElementMesh[ DiscretizeGraphics[plot]["MakeRepresentation"["ElementMesh"]]] // MeshRegion // BoundaryMesh $\endgroup$
    – cvgmt
    Jul 7, 2023 at 12:39
0
$\begingroup$

In:

xss = Table[{Cos[t] + 2 Cos[2 t] + 2, Sin[t] - 2 Sin[2 t]}, {t, 0, 
    2 \[Pi], 1/100}];
regionPlot[xss_] := ListLinePlot[
  xss, 
  Filling -> {Axis}, 
  FillingStyle -> LightBlue, 
  AspectRatio -> 1,
   Frame -> True, 
  Axes -> False,
   PlotStyle -> Transparent]
regionPlot@xss   

Out:

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Some notes: my region need not be centered at the origin, and I would like to explicitly avoid double-filling. $\endgroup$ Jun 4, 2017 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.