5

How do you duplicate a path in tikz and rotate the duplicate about its midpoint?

I want to make a copy of the line inside the circle (imaged below with MWE) and draw it through the original line perpendicularly (or any angle I want for that matter).

Thank you very much for your assistance, Pseudonym123

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\path[name path=circle,draw] (0,0) circle [radius=5cm];
\path[name path=line127] (3,0) -- ++(127:10);
\path[name intersections={of=circle and line127}]
    (intersection-1);
\path[save path=\name](3,0) -- (intersection-1);
\path[draw,rotate=90,use path=\name];%<--- this part isn't rotating
\end{tikzpicture}
\end{document}

4 Answers4

4

If all you need is to rotate straight lines between coordinates (not nodes), I suggest a custom to path with the real start points calculated. That way you can still place nodes along the path.

I put overlay on the invisible line127 so that the bounding box doesn't get updated and named the intersection via a \tikzset since there's no need to use a \path.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections, calc}
\tikzset{
  rotate straight line/.default=90,
  rotate straight line/.style={
    to path={
      coordinate (@middle@) at ($(\tikztostart)!.5!(\tikztotarget)$)
      ($(@middle@)!1!#1:(\tikztostart)$)--($(@middle@)!1!#1:(\tikztotarget)$)
      \tikztonodes}}}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\path[name path=circle,  draw]    (0,0) circle [radius=5cm];
\path[name path=line127, overlay] (3,0) -- ++(127:10);
\tikzset{name intersections={of=circle and line127}}

\draw[help lines] (3,0) -- (intersection-1); \draw (3,0) to[rotate straight line] (intersection-1); \draw[red] (3,0) to[rotate straight line=45] (intersection-1); \end{tikzpicture} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
4

I did not understand the question as Qrrbrbirlbel. With the tkz-euclide package (I'm a beginner), I come up with this.

enter image description here

    \documentclass[border=3mm]{standalone}
    \usepackage{tkz-euclide}
    \begin{document}
    \begin{tikzpicture}[scale=0.3]
        \tkzDefPoints{0/0/P,3/0/A}
        \coordinate (B) at ($(A)+(127:10)$);% we can use tikz
    %       (AB)  is  your line127
    \tkzDefCircle[R](P,5) \tkzGetPoint{C}
    \tkzDrawCircles(P,C)

    \tkzInterLC[near](A,B)(P,C) \tkzGetPoints{d}{D}
    \tkzDrawSegment(A,D)% D is the intersection point

    \tkzDefMidPoint(A,D) \tkzGetPoint{M}
    %\tkzDrawPoints(A,D,M)

    \tkzDefPointBy[rotation in rad= center M angle -pi/2](A) \tkzGetPoint{A'}
    \tkzDefPointBy[rotation in rad= center M angle -pi/2](D) \tkzGetPoint{D'}
    %\tkzDrawPoints(A',D')
    \tkzDrawSegment(A',D')
    %\tkzLabelPoints(A,D,M,A',D')
    \end{tikzpicture}
\end{document}

pascal974
  • 4,652
4

Here's a solution using the spath3 library (it's recently updated on CTAN so you might need to update your distribution). This library allows you to save a path and then manipulate it, including applying transformations. The transform option on the use key allows you to specify a transformation to be applied to the path before it is re-used, and the spath cs coordinate system allows you to specify a coordinate along a path. So with the rotate around key it's not too hard to do what you want.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/664167/86}
\usepackage{tikz}
\usetikzlibrary{
  intersections,
  spath3
}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\path[name path=circle,draw] (0,0) circle [radius=5cm];
\path[name path=line127] (3,0) -- ++(127:10);
\path[name intersections={of=circle and line127}]
    (intersection-1);
\path[draw,red,spath/save=name](3,0) -- (intersection-1);
\path[draw,blue,
  spath/use={
    name,
    transform={
      rotate around={90:(spath cs:name .5)}
    }
  }
];%<--- this part isn't rotating
\end{tikzpicture}
\end{document}

I've drawn the original path as well so that you can see that it does what you asked for:

rotated path

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 1
    Wow, thank you very much. Was reading the manual yesterday and was having some trouble, so this is really nice to see. By the way, always appreciate it when bigger fishes such as yourself take the time to help little guys like me. Cheers –  Nov 05 '22 at 20:14
  • 1
    This is the sort of thing I wrote spath3 for so it's nice to have a question where I can demonstrate how straightforward it is to use. Which manual were you reading that you were having trouble with? If it's the spath3 manual then do let me know as I want it to be useable. I'm definitely not a "big fish" - I've just been on this site for a long time! – Andrew Stacey Nov 06 '22 at 15:29
  • 1
    Ah, did a little more reading while more awake and noticed the "this document is the code, there’s another which focusses on usage," my apologies for the confusion. Was able to locate the correct documentation. Thanks again for the help. –  Nov 06 '22 at 17:31
3

This is rather a comment than an answer.

For a path, duplicating/copying, rotating about some point, etc. is usual path operations; and can be done in plain Asymptote with the slogan "let it be as it is", see the simple code below. One can read and understand it even not knowing Asymptote programming before.

The spath3 library enhances TikZ's path operations (see Andrew's answer). Without using this library, the task still can be done in plain TikZ with a bit tricky code. In my opinion, it's not really defective of TikZ; it's just how TikZ simplify a way of drawing.

enter image description here

% https://www.overleaf.com/read/pjqpgyztmwcf
\documentclass{standalone}
\usepackage{asymptote}
\begin{document}
\begin{asy}
// http://asymptote.ualberta.ca/
size(6cm);
path cir=circle((0,0),5);
pair A=(3,0), B=A+10dir(127);
path line1=A--B;
pair C=intersectionpoint(cir,line1); 
pair M=(A+C)/2;
dot(M,blue);
// choose 90 for perpendicular one
real al=90, be=45;    
path line2=rotate(al,M)*(A--C);
path line3=rotate(be,M)*(A--C);

draw(cir); draw(line1,lightblue+.8pt+opacity(.5)); draw(A--C,blue); draw(line2,red); draw(line3,orange); \end{asy} \end{document}

Black Mild
  • 17,569