Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I draw a line of length l and at an angle of a from a circular node N in TikZ?

share|improve this question

2 Answers 2

up vote 13 down vote accepted

You can access the points around a node with <node name.angle> for example, a.50 will give you a point on the border of node a at an angle of 50 degrees. Further, you can draw a line of length lcm elevated at an angle of t degrees by (t:lcm).

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}
  \begin{tikzpicture}
      \node[draw,circle]  at (0,0) (a) {N};
      %% draw a line of 2cm length from the border of node a at an angle 50. The line is having a slope of tan(50).
      \draw (a.50) -- (50:2cm);
  \end{tikzpicture}
\end{document}

enter image description here

Putting many lines and some make-up ;-)

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}
  \begin{tikzpicture}
      \node[circle]  at (0,0) (a) {N};
      \foreach \angle in {5,10,...,360}{%
      \draw (a.\angle) -- (\angle:2cm);
      }
  \end{tikzpicture}
\end{document}

enter image description here

And some animation:

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}
  \foreach \angle in {1,2,...,360}{%
    \begin{tikzpicture}
      \path (-2,-2.3) rectangle (2,2.3);  %% pad some bounding box
      \node[circle]  at (0,0) (a) {N};      
      \draw (a.-\angle) -- (-\angle:2cm);
    \end{tikzpicture}
  }
\end{document}

enter image description here

share|improve this answer
    
Hope I understood the question :-) –  Harish Kumar Jan 3 at 7:18
    
Yes, this is exactly and much more than I was looking for! Thanks. –  Neil G Jan 3 at 18:26
    
Ah, am I right that to make it relative to the node a, you should add ++ in front of the (50:2cm)? –  Neil G Jan 3 at 18:35
    
What I mean is that if the node a were not at the origin, I think your solution doesn't work anymore? –  Neil G Jan 3 at 22:49
2  
@NeilG: + also specifies a relative coordinate, but it does not update the current point for subsequent usages of relative coordinates. You can use this notation to specify numerous points, all relative to the same initial point, i.e., (0,0). ++ changes the initial point for subsequent paths in the same \draw command. –  Harish Kumar Jan 3 at 23:41

You could also use pins:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \node [circle,draw,pin={[pin distance=2cm,pin edge={blue,thick}]330:$P_1$},
                     pin=60:$P_2$] at (0,0) {N};
\end{tikzpicture}
\end{document}

Pin P1 represents the default behaviour: gray line of a certain length, while pin P2 is customized. Length is set with pin distance, while pin edge lets you define the colour, the thickness, lets you add an arrowhead or even decorate the path (e.g. a snake path). In both cases what precedes the colon : is the angle that defines the slope of the line.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.