TransWikia.com

How to outline the union of an annulus and a rectangle in TikZ?

TeX - LaTeX Asked on April 28, 2021

My Goal

I’m trying to make a shape in TikZ like the one depicted in the following sketch:

Annulus with rectangle at base, as described in following paragraph, but with dashed arrows and text annotations for dimensions

It’s the union of an annulus and a rectangle, with the region shaded and outlined. The annulus has a given inner radius and outer radius. The rectangle, located at the bottom of the annulus and with its left side aligned with the center of the annulus, has a height equal to the width of the annulus and some length that is some value up to the outer radius of the annulus).

My Work So Far

documentclass[tikz]{standalone}

% Definitions
newcommand*{innerRadius}{3cm}
newcommand*{outerRadius}{4cm}
newcommand*{footLength}{2.4cm}

begin{document}
begin{tikzpicture}

% Draw the ring
draw[fill=gray!30,even odd rule]  (0,0) circle (outerRadius)
                                   (0,0) circle (innerRadius);
% Draw the rectangle
draw[fill=gray!30] (0,-outerRadius) rectangle (footLength,-innerRadius);

end{tikzpicture}
end{document}

This produces the following:

Similar to above picture, minus arrows and annotations, and the rectangle outline covers the annulus outline

The problem with my code is that the rectangle’s outline covers the annulus.

My Question

How can I make the desired figure in TikZ such that the outline is around the entire region rather than having the rectangle’s outline be separate from the annulus’s outline?

Bonus Points (not necessary!)

If the outer radius is large and the inner radius of the annulus is only slightly less, and if the rectangle is wide enough (e.g. 5 cm, 4 cm and 3.5 cm, respectively) then the outer edge of the annulus will intersect the top of the rectangle rather than its right side, as shown below. In this case, I don’t like that the right side of the foot does not reach the annulus. Modeling the intended figure with an annulus and a rectangle is insufficient in this special case.

Almost looks like a letter Q. See description of this figure above.

If possible, I’d love to have a solution that raises the right side of the rectangle up to meet the outer edge of the annulus. In this case, a rectangle would no longer be sufficient: a rectangle should not reach higher than the inner radius of the annulus on the left, but the right side needs to reach from the bottom to the outer radius on the right. This would give me more flexibility with my diagram’s dimensions and would give me more insight into how TikZ works. However, I do not expect to need this, so this would be for my curiosity. I would be grateful for any solution, with or without this extension!

One Answer

The standard case is very simple.

documentclass[tikz]{standalone}

% Definitions
newcommand*{innerRadius}{3cm}
newcommand*{outerRadius}{4cm}
newcommand*{footLength}{2.4cm}
begin{document}
begin{tikzpicture}
% Draw the thing
draw[fill=gray!30,even odd rule]  
    (0,-outerRadius) arc[start angle=270,end angle={-90+asin(footLength/outerRadius)},radius=outerRadius]
    |- cycle                                  
    (0,0) circle[radius=innerRadius];
% double-check that it works
% draw[dashed] (0,-outerRadius) rectangle (footLength,-innerRadius);
end{tikzpicture}
end{document}

For the general case, i.e. to include the "bonus" situation I'd use a pic.

documentclass[tikz]{standalone}
newcommand*{innerRadius}{3cm}
newcommand*{outerRadius}{4cm}
newcommand*{footLength}{2.4cm}
tikzset{pics/my pic/.style={code={
 pgfmathtruncatemacro{itest}{footLength>outerRadius}
 ifnumitest=0
  pgfmathsetmacro{myangle}{asin(footLength/outerRadius)}
  pgfmathtruncatemacro{itest}{(1-cos(myangle))*outerRadius>outerRadius-innerRadius}
 fi
 ifnumitest=0
   draw[fill=gray!30,even odd rule]  
    (0,-outerRadius) arc[start angle=270,end angle={-90+myangle},radius=outerRadius]
    |- cycle                                  
    (0,0) circle[radius=innerRadius];
 else
  pgfmathsetmacro{myangle}{acos(innerRadius/outerRadius)}
   draw[fill=gray!30,even odd rule]  
    (0,-outerRadius) arc[start angle=270,end angle={-90+myangle},radius=outerRadius]
    -- (footLength,-innerRadius)
    |- cycle                                  
    (0,0) circle[radius=innerRadius];
 fi    
}}}
begin{document}
begin{tikzpicture}
 pic{my pic};
end{tikzpicture}
renewcommand*{footLength}{3.5cm}
begin{tikzpicture}
 pic{my pic};
end{tikzpicture}
end{document}

enter image description here

Correct answer by user237299 on April 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP