[Tex/LaTex] Tikz: Draw to the bounding box

tikz-pgf

Following my previous question (when I asked that one I thought the rest would be relatively easier):

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
    [plotmark/.style = {%
    solid,
    fill=red,
    circle,
    inner sep = 0pt,
    minimum size = 4pt}]

    \begin{scope}[rotate=-{30},local bounding box=a]
    \draw (0,0) |- (4,4) |- (6,2) |- (4,0) |- (2,-2) |- cycle;
    \draw (4, 0) -| (2,4)
          (0,2) -| (4,0);
    \coordinate (A) at (2,-2);
    \coordinate (B) at (2,4);
    \node[plotmark] at (A){};
    \node[plotmark] at (B){};
    \end{scope}
    \draw (a.south west) rectangle (a.north east);
\end{tikzpicture}
\end{document}

enter image description here

  1. What are my options to fill the bounding box but exclude the inner shape? (Torbjørn T. provided an answer with on background layer but advised to ask it as a separate question.) How can I do it with clip and scope?

  2. How can I draw a line which passes through the two red points and intersects with the bounding box?

Best Answer

UPDATE: Without backgrounds and clip.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}
    [plotmark/.style = {%
    solid,
    fill=red,
    circle,
    inner sep = 0pt,
    minimum size = 4pt}]

    \begin{scope}[rotate=-{30},local bounding box=a]
    \draw[fill=gray,even odd rule] (0,0) rectangle ++(2,2) (2,0) rectangle ++(2,2) (0,-2) rectangle ++(2,2) (2,-2) rectangle ++(2,2) (4,-2) rectangle ++(2,2) (2,-4) rectangle ++(2,2)
     (a.north west) -- (a.north east) -- (a.south east) -- (a.south west)
     --(a.north west);
    \coordinate (A) at (2,-4);
    \coordinate (B) at (2,2);
    \node[plotmark] at (A){};
    \node[plotmark] at (B){};
    \end{scope}
    \draw[name path global=bbox] (a.south west) rectangle 
    (a.north east);
    \path[overlay,name path=line] let \p1=(A),\p2=(B),\p3=(a.south),\p4=(a.north),\n1={((\y4-\y3)/(\y2-\y1))} in 
    ($(A)-\n1*(\x2-\x1,\y2-\y1)$)
    --
    ($(B)+\n1*(\x2-\x1,\y2-\y1)$);
    \draw[name intersections={of=bbox and line},fill=blue] 
    (intersection-1) node[plotmark]{} -- (intersection-2) node[plotmark]{};
\end{tikzpicture}
\end{document}

enter image description here

ORIGINAL ANSWER:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{backgrounds,intersections,calc}
\begin{document}
\begin{tikzpicture}
    [plotmark/.style = {%
    solid,
    fill=red,
    circle,
    inner sep = 0pt,
    minimum size = 4pt}]

    \begin{scope}[rotate=-{30},local bounding box=a]
    \draw[fill=white] (0,0) rectangle ++(2,2) (2,0) rectangle ++(2,2) (0,-2) rectangle ++(2,2) (2,-2) rectangle ++(2,2) (4,-2) rectangle ++(2,2) (2,-4) rectangle ++(2,2);
    \coordinate (A) at (2,-4);
    \coordinate (B) at (2,2);
    \node[plotmark] at (A){};
    \node[plotmark] at (B){};
    \end{scope}
    \begin{scope}[on background layer]
    \draw[fill=gray,name path global=bbox] (current bounding box.south west) rectangle 
    (current bounding box.north east);
    \end{scope}
    \path[overlay,name path=line] let \p1=(A),\p2=(B),\p3=(a.south),\p4=(a.north),\n1={((\y4-\y3)/(\y2-\y1))} in 
    ($(A)-\n1*(\x2-\x1,\y2-\y1)$)
    --
    ($(B)+\n1*(\x2-\x1,\y2-\y1)$);
    \draw[name intersections={of=bbox and line},fill=blue] 
    (intersection-1) circle (2pt) -- (intersection-2) circle (2pt);
\end{tikzpicture}
\end{document}

enter image description here