[Tex/LaTex] How to draw circles on a line and rotate them

rotatingtikz-pgf

Two questions about the following MWE:

\documentclass[12pt,a4paper]{scrartcl}  %%KOMA class
\setkomafont{sectioning}{\rmfamily\bfseries\boldmath}  %%

 \usepackage{tikz}
 \usetikzlibrary{rulercompass}
 \usetikzlibrary{intersections,quotes,angles}
 \usetikzlibrary{calc}

 \begin{document}
 \begin{tikzpicture}
 \draw [color=black!5] (0,0) grid (14,10);
 \draw (14,0) coordinate (a) node[right, below] {$x$}
       -- (0,0) coordinate (b) node[left] {(0,0)}
       -- (0,10) coordinate (c) node[left] {$y$};
 \draw [->, ultra thick] (0,2) coordinate (ad) node[left] {(0,2)}  -- (30:15cm) coordinate (dd)  node[above] {$l$};
 \draw (ad) -- (14,2)  coordinate (l);

 \path (ad) -- (dd) coordinate[pos=0.355](c1) coordinate[pos=0.692](c2);
 %circle A
 \draw [fill=red!15] (c1) circle [radius=2.365];
 %circle B
 \draw [fill=green!15] (c2) circle [radius=2.365];
 % centre  circles
 \draw (ad) -- (c1) node{$\bullet$} -- (c2) node {$\bullet$}--(dd)
 pic["$\alpha$", draw=red, <<-, angle eccentricity=1.1, angle radius=5cm]{angle=l--ad--dd}
 pic["$\alpha$", draw=red, <<-, angle eccentricity=1.1, angle radius=9.8cm]{angle=l--ad--dd};
 \end{tikzpicture}
 \end{document}
  1. How do I calculate the second coordinate[pos=0.692] in terms of
    the circle radius [radius=2.365]?
  2. How do I draw the rotation of the oblique line l as to center the two circles on the y=2 line at the correct position?
    enter image description here

The following almost fixes it (code to be cleaned):

 \coordinate
 let
 \p1=(ad),\p2=(c1),\p3=(c2),\n1={veclen(\x2-\x1,\y2-\y1)},\n2={veclen(\x3-\x1,\y3-\y1)}
 in
 node (c1n) at (\n1,2) node (c2n) at (\n2,2);
 \draw [fill=red!25] (c1n) circle [radius=2.365];
 \draw [fill=green!25] (c2n) circle [radius=2.365];

 \draw
 let
 \p1=(ad),\p2=(c1),\p3=(c2),\n1={veclen(\x2-\x1,\y2-\y1)},\n2={veclen(\x3-\x1,\y3-\y1)}
 in
 (ad) -- (c1n) node{$\bullet$} -- (c2n) node{$\bullet$} {}--(l)
 pic["$\alpha$", draw=red, <<-, angle eccentricity=1.05, angle radius=\n1]{angle=l--ad--dd}
 pic["$\alpha$", draw=red, <<-, angle eccentricity=1.02, angle radius=\n2]{angle=l--ad--dd};
 \draw (ad) -- (14,2)  coordinate (l);

Enter image description here

Best Answer

You can use the features of the calc library for both problems. For 1., define the second coordinate as \coordinate (c2) at ($(c1)!2*2.365 cm!(dd)$);, i.e. the point that is twice the radius away from c1, towards dd.

For the second you can use the let syntax to calculate the distance from dd to each of the circle centers, and use that as the angle radius, and to define the center points of the two circles on the horizontal line.

I also added a second possible method for making the circles, by using nodes with an appropriate anchor set.

Small note: I wouldn't use \node {$\bullet$} in the circle centers, as that is positioned a bit wrong. I used a filled, circular node instead, another option would be e.g. \fill (c1) circle[radius=2pt];

\documentclass[12pt,a4paper]{scrartcl}  %%KOMA class
\setkomafont{sectioning}{\rmfamily\bfseries\boldmath}  %% 

\usepackage{tikz}
\usetikzlibrary{rulercompass}
\usetikzlibrary{intersections,quotes,angles}
\usetikzlibrary{calc}
\tikzset{bullet/.style={circle,inner sep=0pt,minimum size=4pt,fill,draw}}
\begin{document}
\begin{tikzpicture}
 \draw [color=black!5] (0,0) grid (14,10);  
 \draw (14,0) coordinate (a) node[right, below] {$x$}
       -- (0,0) coordinate (b) node[left] {(0,0)} 
       -- (0,10) coordinate (c) node[left] {$y$};
 \draw [->, ultra thick] (0,2) coordinate (ad) node[left] {(0,2)}  -- (30:15cm) coordinate (dd)  node[above] {$l$};

 \path (ad) -- (dd) coordinate[pos=0.355](c1);
 \coordinate (c2) at ($(c1)!2*2.365 cm!(dd)$); 
 %circle A
 \draw [fill=red!15] (c1) circle [radius=2.365]; 
 %circle B
 \draw [fill=green!15] (c2) circle [radius=2.365]; 

 \path
 let
  \p1=(ad),\p2=(c1),\p3=(c2),\n1={veclen(\x2-\x1,\y2-\y1)},\n2={veclen(\x3-\x1,\y3-\y1)}
 in
  (ad) ++(\n1,0) coordinate (c1n)
  (ad) ++(\n2,0) coordinate (c2n);

 \draw [fill=red!15] (c1n) circle [radius=2.365]; 
 %circle B
 \draw [fill=green!15] (c2n) circle [radius=2.365]; 


 \draw (ad) -- (14,2)  coordinate (l);
 % draw angles
 \draw 
  let
  \p1=(ad),\p2=(c1),\p3=(c2),\n1={veclen(\x2-\x1,\y2-\y1)},\n2={veclen(\x3-\x1,\y3-\y1)}
   in
  (ad) -- (c1) node[bullet]{} -- (c2) node[bullet] {}--(dd)
   pic["$\alpha$", draw=red, <<-, angle eccentricity=1.05, angle radius=\n1]{angle=l--ad--dd}
   pic["$\alpha$", draw=red, <<-, angle eccentricity=1.02, angle radius=\n2]{angle=l--ad--dd};
\end{tikzpicture}

\begin{tikzpicture}[
  declare function={
   alpha=25; % angle
   R=2.365cm; % radius
   L=14cm; % length of ray
   circpos=0.5; % position of circle tangent along ray
  },
  mycirc/.style={
   circle,
   draw=black,
   thin,
   fill=#1,
   minimum size=R*2,
   outer sep=0pt,
   label={[bullet]center:}
  }
]

 \draw [color=black!5] (0,0) grid (L,10);  
 \draw (L,0) coordinate (a) node[right, below] {$x$}
       -- (0,0) coordinate (b) node[left] {(0,0)} 
       -- (0,10) coordinate (c) node[left] {$y$};

 \draw [->, ultra thick] (0,2) coordinate (ad) node[left] {(0,2)}  -- ++(alpha:L) coordinate (dd)
  node[above] {$l$}
  node[mycirc=red!15,anchor=alpha,pos=circpos] (c1) {}
  node[mycirc=green!15,anchor=alpha+180,pos=circpos] (c2) {};

% draw second set of circles
 \path (ad) -- ++(0:L)
  node[mycirc=red!15,anchor=0,pos=circpos] {}
  node[mycirc=green!15,anchor=180,pos=circpos]  {};

 \draw (ad) -- (L,2)  coordinate (l);

 \draw (c1.alpha+180) -- (c2.alpha);

 \draw 
  let
  \p1=(ad),\p2=(c1.center),\p3=(c2.center),\n1={veclen(\x2-\x1,\y2-\y1)},\n2={veclen(\x3-\x1,\y3-\y1)}
   in
   pic["$\alpha$", draw=red, <<-, angle eccentricity=1.05, angle radius=\n1]{angle=l--ad--dd}
   pic["$\alpha$", draw=red, <<-, angle eccentricity=1.02, angle radius=\n2]{angle=l--ad--dd};\end{tikzpicture}
\end{document}

enter image description here