9

My goal in trying to learn tkz-euclide is so that I can be able to get an arbitrary point from a circle and define the other elements of my figure from that. So far, from what I can understand from the examples given in the manual (I don't know any French), I came up with the following code.

\documentclass[tikz]{standalone}

\usepackage{tkz-euclide}
\usetkzobj{all} 

\begin{document}
\begin{tikzpicture}
    \tkzDefPoint(0,0){O} % Defines a point
    \tkzGetRandPointOn[circle=center O radius 1.5cm]{A} % Gets random point of the circle with center at O and radius 1.5cm
    \tkzDrawCircle(O,A) % Draws the circle
    \tkzDefPointBy[rotation=center O angle 100](A) % Defines a point...
        \tkzGetPoint{C} % named C
    \tkzDefPointBy[rotation=center O angle 78](A) % Defines a point...
        \tkzGetPoint{B} % named B
    \tkzDrawPoints(O,A,B,C) % Draws dots
    \tkzDrawSegments(C,B B,A A,O O,C) % Draws the segments
    \tkzLabelPoints(O,A,B,C)    % Labels the points
    %\node [below] at (O) {$O$};
    %\node [above] at (A) {$A$};
    %\node [left] at (B) {$B$};
    %\node [left] at (C) {$C$};
\end{tikzpicture}
\end{document}

This is nice. But the random positioning of the points screws up the positioning of the labels. (Try compiling several times to see.) I have tried setting up the nodes through the usual \node commands but I still end up with the same problem.How can I control the behavior of the labels so that they would not overlap with other elements of the figure?

Edit

I read this mod comment on my list of questions: Have you considered accepting an answer or starting a bounty for this question? Indeed, I have considered accepting an answer since the solution now looks trivial with tikz but I would still like to see a tkz-euclide-based solution, too. So I thought I would prolong accepting an answer for a short while. So how about starting a bounty instead? Well, why not?

mforbes
  • 5,571
hpesoj626
  • 17,282

2 Answers2

11

You could compute a point radially outside of the circle at each point to place the node:

enter image description here enter image description here enter image description here enter image description here

The label for the origin is placed at a point opposite of the (B) node. A slightly better solution would be to compute the angle midway between (OA) and (OC) and place the origin label at a 180 degree offset from that.

Code:

\documentclass[tikz,border=2pt]{standalone}

\usepackage{tkz-euclide} \usetkzobj{all}

\begin{document} \begin{tikzpicture} \tkzDefPoint(2,1){O} % Defines the point at which the origin is located \tkzGetRandPointOn[circle=center O radius 1.5cm]{A} % Gets random point of the circle with center at O and radius 1.5cm \tkzDrawCircle(O,A) % Draws the circle \tkzDefPointByrotation=center O angle 100 % Defines a point... \tkzGetPoint{C} % named C \tkzDefPointByrotation=center O angle 78 % Defines a point... \tkzGetPoint{B} % named B \tkzDrawPoints(O,A,B,C) % Draws dots \tkzDrawSegments(C,B B,A A,O O,C) % Draws the segments

% compute vectors of each point form the origin
\coordinate (OA) at ($(A)-(O)$);
\coordinate (OB) at ($(B)-(O)$);
\coordinate (OC) at ($(C)-(O)$);

\node [red] at ($(O)+1.15*(OA)$)  {A};
\node [red] at ($(O)+1.15*(OB)$)  {B};
\node [red] at ($(O)+1.15*(OC)$)  {C};
\node [red] at ($(B)-1.15*(OB)$)  {O};

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288
  • Put each label outside the circle but on the corresponding bisector will be more beautiful (in my opinion). – kiss my armpit Sep 11 '12 at 17:51
  • 1
    @GarbageCollector: Not sure I understand. To me, the correct place to put the label is along the radial line from the origin, which is what is done here. – Peter Grill Sep 11 '12 at 18:01
  • OK. For this special case (as opposed to arbitrary polygon), putting the labels radially outward makes sense! – kiss my armpit Sep 11 '12 at 18:08
  • There is still the old trouble with that center. I tried moving it to another point with nonzero coordinates but the result was a mess. – hpesoj626 Sep 12 '12 at 01:58
  • @hpesoj626: Have corrected for an arbitrary origin. – Peter Grill Sep 12 '12 at 05:06
  • This already very good. But see that second figure on the top figures? How can we get that label to not overlap with any of the segments? It seems like the only recourse is to run several compiles to get no overlaps. – hpesoj626 Sep 18 '12 at 01:42
  • @hpesoj626: As mentioned in the Notes section, I was only addressing the points around the circle. But a simple way to place the origin label would be opposite of (OB) -- I will update the solution for that. – Peter Grill Sep 18 '12 at 02:11
  • more concise : \path (O) -- ($ (B) + 0.15*($(B)-(O)$) $) node{B}; – Alain Matthes Dec 28 '19 at 06:33
3

I added a part of my answer in the next version of tkz-euclide.

\tkzAutoLabelPoints needs a point called **center** and a coefficient called **with**, this number represents  a percentage of the distance between the point and the center. By default it's 0.15

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all} 

\makeatletter
\pgfkeys{/@tkzautolab/.cd,
    center/.store in    = \tkz@center,
    with/.store in      = \tkz@with,
    with                = 0.15,
   /@tkzautolab/.search also   = {/tikz},
}
\def\tkzAutoLabelPoints{\pgfutil@ifnextchar[{\tkz@AutoLabelPoints}{%
                                         \tkz@AutoLabelPoints[]}}% 
\def\tkz@AutoLabelPoints[#1](#2){%
\begingroup 
\pgfqkeys{/@tkzautolab}{#1} 
 \foreach \point in {#2}{
 \path (\tkz@center) -- ($ (\point) + \tkz@with*($(\point)-(\tkz@center)$) $) node{$\point$};}
\endgroup
}%
\makeatother

\begin{document}
\begin{tikzpicture}
    \tkzDefPoint(2,1){O} % Defines the point at which the origin is located
    \tkzGetRandPointOn[circle=center O radius 1.5cm]{A} % Gets random point of the circle with center at O and radius 1.5cm
    \tkzDrawCircle(O,A) % Draws the circle
    \tkzDefPointBy[rotation=center O angle 100](A) % Defines a point...
    \tkzGetPoint{C} % named C
    \tkzDefPointBy[rotation=center O angle 78](A) % Defines a point...
    \tkzGetPoint{B} % named B
    \tkzDrawPoints(O,A,B,C) % Draws dots
    \tkzDrawSegments(C,B B,A A,O O,C) % Draws the segments
    \tkzAutoLabelPoints[center=O,red](A,B,C)
\end{tikzpicture}
\end{document}

enter image description here

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tkz-euclide}

\begin{document}
\begin{tikzpicture}
    \tkzDefPoint(2,1){O} % Defines the point at which the origin is located
    \tkzGetRandPointOn[circle=center O radius 1.5cm]{A} % Gets random point of the circle with center at O and radius 1.5cm
    \tkzDrawCircle(O,A) % Draws the circle
    \tkzDefPointBy[rotation=center O angle 100](A) % Defines a point...
    \tkzGetPoint{C} % named C
    \tkzDefPointBy[rotation=center O angle 78](A) % Defines a point...
    \tkzGetPoint{B} % named B
    \tkzDrawPoints(O,A,B,C) % Draws dots
    \tkzDrawSegments(C,B B,A A,O O,C) % Draws the segments
    \tkzDefBarycentricPoint(O=1,A=1,B=1,C=1)
     \tkzAutoLabelPoints[center=tkzPointResult,with=.3,red](O,A,B,C)
\end{tikzpicture}
\end{document}

enter image description here

Alain Matthes
  • 95,075