# Clip GridLines outside of a circle

Graphics[Circle[], Frame -> True, GridLines -> Automatic]


This puts grids across a 2D graphic (a circle here).

Is there any way to specify the range of values of GridLines? More specifically I want the part of GridLines that are inside the circle. That is the part of them that is outside the circle should be removed.

Thanks a lot!

You can use a FilledCurve to create a graphics primitive with a hole in it. For example:

Graphics[
{
White,
FilledCurve[{
{Line[{Scaled[{0,0}],Scaled[{1,0}],Scaled[{1,1}],Scaled[{0,1}],Scaled[{0,0}]}]},
{Line@CirclePoints[.5, 100]}
}],
Blue,
Circle[{0,0},.5]
},
Frame -> True,
GridLines -> Automatic
]


Update

My previous answer let the grid lines from the underlying Graphics option show through the hole in the FilledCurve. If you want the grid lines to be rotated, then another approach using Texture and FilledCurve would work better. FilledCurve supports Line, BezierCurve and BSplineCurve segments. The documentation provides an example for creating a circle from a BSplineCurve, so I will use that:

bSplineCircle[c_, r_] := Module[{pts=TranslationTransform[c][r $$CirclePoints]}, BSplineCurve[ pts, SplineDegree->2, SplineKnots->$$CircleKnots,
SplineWeights->$CircleWeights ] ] $$CircleKnots = {0, 0, 0, 1/4, 1/2, 1/2, 3/4, 1, 1, 1};$$CircleWeights = {1, .5, .5, 1, .5, .5, 1}; $$CirclePoints = {{0,-1},{1,-1},{1,1},{0,1},{-1,1},{-1,-1},{0,-1}};$$CircleTextureCoordinates = {{.5,0},{1,0},{1,1},{.5,1},{0,1},{0,0},{.5,0}};  Example: Graphics[{bSplineCircle[{-1,1},1], bSplineCircle[{1,-1},.5]}, Axes->True]  Using the above bSplineCircle function we can create a texturedCircle function: texturedCircle[c_, r_, texture_, rot_:0] :={ Texture[texture], FilledCurve[ {bSplineCircle[c, r]}, VertexTextureCoordinates -> RotationTransform[rot, {.5, .5}] @$CircleTextureCoordinates
]
}


Here is an example using texturedCircle:

Graphics[
{
EdgeForm[Blue],
texturedCircle[{1, 2}, 1, Graphics[{}, GridLines->Automatic], Pi/8],
EdgeForm[Green],
texturedCircle[{3, 4}, .5, Graphics[{}, GridLines->Automatic]],
EdgeForm[None],
texturedCircle[{3, 1}, 1, ExampleData[{"TestImage","Lena"}], Pi/4]
},
Axes->True
]


• This is perfect! Thank you very much. Is is possible also to modify the orientation of these grid lines (for instance by π/4 rad)? – Dimitris Apr 23 at 16:30

You may also use parametric plot which can give more flexibility (like the pi/4 rotation you want):

pt = {Table[
ParametricPlot[{x, x + a}, {x, 1/2 (-a - Sqrt[2 - a^2]),
1/2 (-a + Sqrt[2 - a^2])}], {a, -1, 1, .5}],
Table[ParametricPlot[{x, -x + a}, {x, 1/2 (a - Sqrt[2 - a^2]),
1/2 (a + Sqrt[2 - a^2])}], {a, -1, 1, .5}]} // Flatten;

Show[{pt, Graphics[Circle[]]}, PlotRange -> All, Frame -> True]


where the x range for the gridlines are from

Solve[x + a == Sqrt[1 - x^2], x]
Solve[-x + a == Sqrt[1 - x^2], x]

• Thanks! How can we modify the code in order to have the center of circle (and the grids translated) to (0.5,0.5)? – Dimitris Apr 23 at 19:52