# Improvement of PSTricks code for drawing of balls in a triangular box

Consider the following.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{auto-pst-pdf,pst-eucl}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\def\Angle[#1]#2#3#4{\pstMarkAngle{#2}{#3}{#4}{#1$60^{\circ}$}}

\begin{document}

\psset{unit=0.07cm}
\begin{pspicture}(\maksimumB,\maksimumH)
\pnodes{P}(0,0)%
(\maksimumB,0)%
(!\maksimumB 2 div \maksimumH)
\pspolygon(P0)(P1)(P2)
\multido{\iA=0+1,\iB=1+1,\r=210+120}{3}{%
\psdot(C\iA)
\uput[\r](C\iA){$C_{\iB}$}
}
\psset{%
linestyle=dotted,%
dotsep=1.5pt,%
LabelSep=9,%
}
\Angle[\footnotesize]{P2}{P1}{P0}
\Angle[\footnotesize]{P1}{P0}{P2}
\Angle[\footnotesize]{P0}{P2}{P1}
\psset{%
LabelSep=7,%
}
\pspolygon(C0)(C1)(C2)
\Angle[\scriptsize]{C1}{C0}{C2}
\Angle[\scriptsize]{C0}{C2}{C1}
\Angle[\scriptsize]{C2}{C1}{C0}
\end{pspicture}

\end{document}


Output

Question

I have the desired output but the code is somewhat messy, I think; can anyone help me simplify if?

• My opinion: I'm not entirely sure how helpful this question would be to a wider audience (including many of the others posed this way). Voting to close as TL. – Werner May 7 '13 at 6:27
• You've asked at least a dozen of these, all with the same boilerplate commentary. At some point, people will start to doubt the sincerity of the questions (i.e do you want to learn how to improve the code yourself, or do you just want someone else to do it for you). It looks like you're getting close to that point. I'm not expressing an opinion one way or the other, just rationalizing the close votes if you're confused by them. – Scott H. May 7 '13 at 7:47
• No need for name calling, you mentioned that you didn't understand the close votes (none of which were me) so I gave my take on it. I'm not sure what you find ignorant about that. – Scott H. May 7 '13 at 8:17
• Maybe I phrased my comment poorly, I'm sorry if it offended you. – Scott H. May 7 '13 at 8:41
• @SvendTveskæg: To add to this discussion I've posted a question on Meta: Tagging “Please improve my code” posts. Also, a question that was just posted here has a similar flavour to yours. However, in my opinion it has a reach to a wider audience, since it references styles for chapter pages: Code improvement and suggestions in template. Users can take the code and update it to their liking. Your post is particular to a triangle with 3 balls in it and doesn't scale to something larger/more abstract. – Werner May 8 '13 at 1:32

marking the angles is the same. Putting the origin in the middle of the base also simplifies the code:

\documentclass{article}
\usepackage{auto-pst-pdf,pst-eucl}

\begin{document}

\psset{unit=0.5mm}
\pstVerb{
/maxB 2 dup 3 sqrt mul add \radius mul 2 div def
\pnodes{P}(!maxB neg 0)(!maxB 0)(!0 maxH)
\pspolygon(P0)(P1)(P2)
\pspolygon[showpoints,linestyle=dotted](C0)(C1)(C2)
\uput[225](C0){C1}\uput[-45](C1){C2}\uput[90](C2){C3}
\end{pspicture}

\end{document}


with the current pstricks.tex you can also calculate coordinates as P(+{algebraic},{algebraic}). The + is the identifier for the special handling:

\psset{unit=0.5mm}
\pspolygon(P0)(P1)(P2)
\pspolygon[showpoints,linestyle=dotted](C0)(C1)(C2)
\uput[225](C0){C1}\uput[-45](C1){C2}\uput[90](C2){C3}
\end{pspicture}

• code does not work with pdflatex. Normally auto-pst-pdf by [pdf] option works right ? I know it works with xelatex and latex->dvips->ps2pdf – texenthusiast May 7 '13 at 7:11
• put the \pstVerb into the pspicture environment. I'll change my code, see above. – user2478 May 7 '13 at 7:12

Without TikZ.

\documentclass[pstricks,border=6pt]{standalone}
\SpecialCoor
\usepackage{fp}

\FPeval\XX{RR*root(2,3)}
\FPeval\RX{XX+RR}
\FPeval\Width{2*RX}
\FPeval\Height{RX*root(2,3)}

\def\Atom#1{%
\pscircle(\XX,\RR){\RR}
\psline(\RX;60)(0,0)(\RX,0)
\bgroup
\psset{linestyle=dashed}
\psarc(0,0){15pt}{0}{60}
\rput{*0}(25pt;30){$60^\circ$}
\rput(\XX,\RR){%
\psline(\RR;60)(0,0)(\RR,0)
\psarc(0,0){15pt}{0}{60}
\qdisk(0,0){3pt}
\rput{*0}(12pt;-150){$C_#1$}
\rput{*0}(25pt;30){$60^\circ$}}
\egroup
\ignorespaces
}

\begin{document}
\begin{pspicture}(\Width,\Height)
\Atom{1}
\rput{120}(\Width,0){\Atom{2}}
\rput{-120}(\RX,\Height){\Atom{3}}
\end{pspicture}
\end{document}


## How it works:

\documentclass{beamer}

\usepackage{pstricks}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt\relax

\SpecialCoor
\usepackage{fp}

\FPeval\XX{RR*root(2,3)}
\FPeval\RX{XX+RR}
\FPeval\Width{2*RX}
\FPeval\Height{RX*root(2,3)}

\def\Atom#1{%
\pscircle(\XX,\RR){\RR}\pause
\psline(\RX;60)(0,0)(\RX,0)\pause
\bgroup
\psset{linestyle=dashed}
\psarc(0,0){15pt}{0}{60}
\rput{*0}(25pt;30){$60^\circ$}\pause
\rput(\XX,\RR){%
\psline(\RR;60)(0,0)(\RR,0)\pause
\psarc(0,0){15pt}{0}{60}\pause
\qdisk(0,0){3pt}
\rput{*0}(12pt;-150){$C_#1$}
\rput{*0}(25pt;30){$60^\circ$}\pause}
\egroup
\ignorespaces
}

\begin{document}
\begin{frame}
\begin{pspicture}(\Width,\Height)
\Atom{1}
\rput{120}(\Width,0){\Atom{2}}
\rput{-120}(\RX,\Height){\Atom{3}}
\end{pspicture}
\end{frame}
\end{document}

• The 19th frame is identical to the 18th one. But I am too lazy to remove the 19th one. :-) – kiss my armpit May 7 '13 at 8:44
• This is a really elegant solution! I'll use this one instead of Herbert's, I think. – Svend Tveskæg May 7 '13 at 9:26
• Hmm. It is as if the circles are not touching each other; there seems to be a tiny gab, but I'm not sure. – Svend Tveskæg May 7 '13 at 11:01
• @SvendTveskæg: You can change the dimen from outer (default) to middle for \pscircle. – kiss my armpit May 7 '13 at 11:02

Just for fun with TikZ and the angles library from the CVS version.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{angles}
\begin{document}
\begin{tikzpicture}[x=7cm, y=7cm, thick, line join=round,
dots/.style={dash pattern=on 1\pgflinewidth off 2\pgflinewidth},
dot/.style={circle,fill,draw,inner sep=+0pt, minimum size=+2pt}]
\pgfmathsetmacro\r{.5/(1+1/tan 30)}
\pgfmathsetmacro\vB{2*\r}
\draw (0,0) coordinate (n-0) -- ++ (0:1) coordinate (n-1)
-- ++ (120:1) coordinate (n-2) -- cycle;
\foreach \a/\b/\c[count=\cnt from 0] in {1/\vB/\vB, \vB/1/\vB, \vB/\vB/1}
\draw (barycentric cs:n-0=\a,n-1=\b,n-2=\c) node[dot] (C-\cnt) {}
node[anchor=30+120*\cnt] {$C_{\pgfmathprint{int(\cnt+1)}}$} circle [radius=\r];
\draw[dots] (C-0) -- (C-1) -- (C-2) -- (C-0);
\foreach \cnt[evaluate={\pCnt=int(Mod(\cnt-1,3)); \nCnt=int(Mod(\cnt+1,3));}] in {0,...,2}
\path[nodes={draw, dots, angle radius=+10pt, angle eccentricity=1.8,
pic text=$\scriptstyle60^\circ$, pic text options={draw=none}}]
pic {angle=C-\nCnt--C-\cnt--C-\pCnt} pic {angle=n-\nCnt--n-\cnt--n-\pCnt};
\end{tikzpicture}
\end{document}


## Output

With tkz-euclide and no calculation (compass and ruler)

 \documentclass[a4paper]{scrartcl}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{tkz-euclide}
\usetkzobj{all}
\definecolor{fondpaille}{cmyk}{0,0,0.1,0}
\tkzSetUpColors[background=fondpaille,text=Maroon]

\begin{document}
\begin{tikzpicture}
\tkzDefPoint(0,0){A}
\tkzDefPoint(8,0){B}
\tkzDefEquilateral(A,B)\tkzGetPoint{C}
%first circle
\tkzDefPointBy[projection= onto A--B](C)  \tkzGetPoint{Hc}
\tkzDefLine[bisector](B,A,C) \tkzGetPoint{a}
\tkzDefLine[bisector](A,Hc,C) \tkzGetPoint{hc}
\tkzInterLL(A,a)(Hc,hc) \tkzGetPoint{Oa}
\tkzDefPointBy[projection= onto A--B](Oa)  \tkzGetPoint{I}
%second circle
\tkzDefLine[bisector](C,B,A) \tkzGetPoint{b}
\tkzDefLine[bisector](B,Hc,C) \tkzGetPoint{ha}
\tkzInterLL(B,b)(Hc,ha) \tkzGetPoint{Ob}
\tkzDefPointBy[projection= onto A--B](Ob)  \tkzGetPoint{J}
%third circle
\tkzDefPointBy[projection= onto A--C](B)  \tkzGetPoint{Hb}
\tkzDefLine[bisector](C,Hb,B) \tkzGetPoint{hb}
\tkzInterLL(C,Hc)(Hb,hb) \tkzGetPoint{Oc}
\tkzDefPointBy[projection= onto C--B](Oc)  \tkzGetPoint{K}
%drawing
\tkzDefMidPoint(C,B) \tkzGetPoint{E}
\tkzDrawPolygon[color=Maroon](A,B,C)
\tkzDrawPolygon[color=Maroon,dashed](Oa,Ob,Oc)
\tkzDrawCircle(Oa,I)
\tkzDrawCircle(Ob,J)
\tkzDrawCircle(Oc,K)
\tkzDrawPoints(Oa,Ob,Oc,A,B,C)
%labels
\tkzLabelPoints[left](Oa,A)
\tkzLabelPoints[right](Ob,B)
\tkzLabelPoints[above](Oc,C)  \end{tikzpicture}
\end{document}