# How to arbitrarily center these drawings?

I have adapted the following to draw lines from the center of a circle out to n equally spaced points on a circle (the n th roots of unity, or the points of an n-gon, or a star graph).

I need to be able to freely manipulate EDIT:four parameters:

1) the radius of the circle

2) the number n

3) the center of the circle.

EDIT:

4) the angle of inclination of the star (i.e. ability to rotate star)

With the LaTeX below I am able to do the first two, but I cannot center the circles. I need to be able to have the second circle centered at one of the points of the first circle, and I need to be able to do this generally (i.e. recursively, n-gon after n-gon, basically building rooted trees).

(I also need to be able to specify the colors of arbitrary edges and points, as the LaTeX below shows -- I assume that any technique that solves my problem with 3) will not prevent me from doing this).

Thanks in advance for your help.

\documentclass[portrait]{article}
\usepackage{tikz}
\usepackage{animate}
\usepackage[top=1in,bottom=1in,right=1in,left=1in]{geometry}
\begin{document}

\begin{tikzpicture}[scale=5.3,cap=round,>=latex]

\foreach \x in {0,60,...,360} {
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:.1cm);
% dots at each point
\filldraw[black] (\x:.1cm) circle(0.2pt);
}

\filldraw[black] (0,0) circle(0.2pt);
\filldraw[red] (0:.1cm) circle(0.2pt);
\draw[red] (0cm,0cm) -- (0:.1cm);

\end{tikzpicture}

\begin{tikzpicture}[scale=5.3,cap=round,>=latex]

\foreach \x in {0,120,...,360} {
% lines from center to point
\draw[gray] (0,0cm) -- (\x:.1cm);
% dots at each point
\filldraw[black] (\x:.1cm) circle(0.2pt);
}

\filldraw[black] (0,0) circle(0.2pt);
\filldraw[red] (0:.1cm) circle(0.2pt);
\draw[red] (0cm,0cm) -- (0:.1cm);

\end{tikzpicture}
\end{document}

-
Welcome to TeX.SX! – Adam Liter Mar 31 '14 at 0:13
thank you sir . – Wapiti Mar 31 '14 at 18:19

## 3 Answers

Just for typing exercise with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\def\f(#1)#2#3#4{%
% #1: center
% #2: radius
% #3: point name prefix
% #4: number of points
\rput(#1){\curvepnodes[plotpoints=\numexpr#4+1]{0}{360}{#2 t PtoC}{#3}}%
\multido{\i=0+1}{#4}{\psline{-*}(#1)(#3\i)}%
}

\begin{document}
\begin{pspicture}(-3,-3)(7,7)
\f(1,1){3.5}{A}{10}
\f(A1){2}{B}{6}
\f(B1){.5}{C}{12}
\end{pspicture}
\end{document}


## Edit 3.1415926535

New features as follows are added

1. Rotation
2. Line color
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\def\f(#1)#2#3#4#5#6{%
% #1: center
% #2: radius
% #3: point name prefix
% #4: number of points
% #5: rotation
% #6: color
\rput(#1){\curvepnodes[plotpoints=\numexpr#4+1]{0}{360}{#2 t #5 add PtoC}{#3}}%
\multido{\i=0+1}{#4}{\psline[linecolor=#6]{-*}(#1)(#3\i)}%
}

\begin{document}
\begin{pspicture}(-3,-3)(7,7)
\f(1,1){3.5}{A}{10}{0}{red}
\f(A1){2}{B}{6}{30}{green}
\f(B1){.5}{C}{12}{90}{blue}
\end{pspicture}
\end{document}


## Edit 3.14159265358979

New features as follows are added

1. set a branch color individually
2. set a branch's end color individually
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\def\f(#1)#2#3#4#5#6{%
% #1: center
% #2: radius
% #3: point name prefix
% #4: number of points
% #5: rotation
% #6: color
\rput(#1){\curvepnodes[plotpoints=\numexpr#4+1]{0}{360}{#2 t #5 add PtoC}{#3}}%
\multido{\i=0+1}{#4}{\psline[linecolor=#6]{-*}(#1)(#3\i)}%
}

\begin{document}
\begin{pspicture}(-3,-3)(7,7)
\f(1,1){3.5}{A}{10}{0}{lightgray}
\f(A1){2}{B}{6}{30}{green}
\f(B1){.5}{C}{12}{90}{blue}
\psline[linecolor=orange]{-*}(1,1)(A3)
\psline[linecolor=magenta]{*-*}(B1)(C6)
\end{pspicture}
\end{document}


-
Good answer (!) but can't you just write "a PSTricks solution" or similar instead of "typing exercise"; the latter sounds like it's almost too easy for you and thereby implicitly saying that others are 'stupid' since that haven't done it themselves. – Svend Tveskæg Mar 31 '14 at 7:10
@SvendTveskæg: It is just an opening greeting that is not necessarily important. :-) – kiss my armpit Mar 31 '14 at 7:16
You make it look so simple! I don't know PSTricks at all -- looks like it's well named though. I wasn't able to reproduce your example, my interpreter crashed at \f(1,1){3.5}{A}{10} saying Undefined control sequence. I have the packages... don't know what's going on here. Also, I need the ability to color branches (possible?) and nodes selectively. (Perhaps by point name as you have defined it?) Also, how would one rotate the star through an arbitrary angle? I realized after posting the question that I probably need this. – Wapiti Mar 31 '14 at 18:12
Is there any way to color the branches and nodes selectively? It looks like the stars are being colored globally here. – Wapiti Mar 31 '14 at 21:17
@Wapiti: It is always possible with PSTricks. – kiss my armpit Apr 1 '14 at 0:51

As a starting point, you can use tikzlibray shapes.geometric. Here is a minimal example how to use it:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}
\def\r{2}%the radius of the circle
\def\n{6}%the number n
\def\x{0}%x coordinate of the center of the circle
\def\y{0}%y coordinate of the center of the circle
% create the node
\node at (\x,\y) [draw=none,minimum size=\r cm,regular polygon,regular polygon sides=\n] (a) {};

% draw a black dot in each vertex
\foreach \t in {1,2,...,\n}{
\fill (a.corner \t) circle[radius=\r pt];
\draw (a.center)--(a.corner \t);}
\fill (a.center) circle[radius=\r pt];
\draw [red] (a.center)--(a.corner \n);
\filldraw [red] (a.corner \n) circle[radius=\r pt];
\end{tikzpicture}
\end{document}


Output:

-
Very cool. I (kind of) know tikz, which is one thing I like about this answer. A question: Is there any way to alter the ORIENTATION of the regular polygon? For example, rotate the red arm / node above by some degree? I ask because the shapes.geometric package seems to give a default orientation that is not necessarily what I would want. – Wapiti Mar 31 '14 at 17:53
Sure, use, for example rotate=30 in \node parameter. \node at (\x,\y) [draw=none,minimum size=\r cm,regular polygon,regular polygon sides=\n,rotate=10] (a) {}; – ferahfeza Mar 31 '14 at 20:24
I really like this. It seems however that rooting new stars at arbitrary locations might be difficult. Say I have a star rooted at the third point (some arbitrary point) on a 17 pointed polygon . It looks like I need to know the actual coordinate for this point, right? I need to say ' \node at (\x,\y) ' to center a star there. – Wapiti Mar 31 '14 at 21:22

In the function \myfunct, there are three arguments. The first argument is the angular increment between arms, 360/n if you will. The second argument is the radius. The third argument is the starting angle (where the red arm will lie). Placement is accomplished by placing an instance of \myfunct at a specified nodal coordinate.

\documentclass[portrait]{article}
\usepackage{tikz}
\usepackage{animate}
\usepackage[top=1in,bottom=1in,right=1in,left=1in]{geometry}
\newcounter{tmpcount}
\newcommand\myfunct[3]{%
\begin{tikzpicture}[scale=5.3,cap=round,>=latex]
\def\donefirst{F}
\setcounter{tmpcount}{#1}
\addtocounter{tmpcount}{#3}
\foreach \x in {#3, \thetmpcount,...,\numexpr 359 + #3\relax} {
\if F\donefirst
\filldraw[red] (\x:#2) circle(0.2pt);
\draw[red] (0cm,0cm) -- (\x:#2);
\gdef\donefirst{T}
\else
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:#2);
% dots at each point
\filldraw[black] (\x:#2) circle(0.2pt);
\fi
}
\filldraw[black] (0,0) circle(0.2pt);
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\draw(0cm,0cm) node [anchor = center] {\myfunct{15}{.3cm}{97}};
\draw(-.19cm,1.59cm) node [anchor = center] {\myfunct{60}{.2cm}{12}};
\draw(0.83cm,1.96cm) node [anchor = center] {\myfunct{120}{.13cm}{87}};
\end{tikzpicture}
\end{document}


ADDITIONAL THOUGHTS:

I apologize for not knowing tikz other than by trial and error, but my intent to allow you to transfer from one figure to the next automatically is sketched in this code below. If I could somehow turn what I call \dx and \dy into actual lengths, I could increment the "working center" of the figure with each call to \myfunct. As you see in the final calls, I can (for a single call only), invoke \dx and \dy to serve as the new "working center" of my figure. But my skills are limited at this point to proceed further. Someone knowing tikz could probably click a few keys and bring it to life.

The other quirk with these additional thoughts is that in order for the center of the star to always lie at the center node (it won't normally if there are an odd number of spokes), I drew a thin white circle around the perimeter of the star. This, of course, overwrites any underlying figure slightly, but I don't know a way to draw a "clear" circle, but I'm sure tikz can do so.

\documentclass[portrait]{article}
\usepackage{tikz}
\usepackage{animate}
\usepackage[top=1in,bottom=1in,right=1in,left=1in]{geometry}
\newcounter{tmpcount}
\newcommand\myfunct[3]{%
\pgfmathsetmacro\cs{cos{#3}}%
\gdef\dx{#2*cos(#3)}% NOT SURE HOW TO INCORPORATE THESE INTO LENGTHS
\gdef\dy{#2*sin(#3)}% NOT SURE HOW TO INCORPORATE THESE INTO LENGTHS
\begin{tikzpicture}[scale=1,cap=round,>=latex]
\def\donefirst{F}
\setcounter{tmpcount}{#1}
\addtocounter{tmpcount}{#3}
\draw[white](0:0) circle(#2);
\foreach \x in {#3, \thetmpcount,...,\numexpr 359 + #3\relax} {
\if F\donefirst
\filldraw[red] (\x:#2) circle(1pt);
\draw[red] (0cm,0cm) -- (\x:#2);
\gdef\donefirst{T}
\else
% lines from center to point
\draw[gray] (0cm,0cm) -- (\x:#2);
% dots at each point
\filldraw[black] (\x:#2) circle(1pt);
\fi
}
\filldraw[black] (0,0) circle(1pt);
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\draw(0cm,0cm) node [anchor = center] {\myfunct{15}{1.59cm}{97}};
\draw({\dx},{\dy}) node [anchor = center] {\myfunct{60}{1.06cm}{12}};
\draw(0.85cm,1.82cm) node [anchor = center] {\myfunct{120}{.72cm}{94}};
\end{tikzpicture}
\end{document}

-
It's very helpful to see this code, since I need to learn this sort of stuff in tikz (I'm making a bunch of really complicated designs). Thanks for your comments on my question! – Wapiti Apr 1 '14 at 15:16