如何控制 tikz-euclide 中的标签位置

如何控制 tikz-euclide 中的标签位置

我学习的目的tkz-euclide是为了能够从圆中获取任意点,并以此定义图形的其他元素。到目前为止,根据我对手册中给出的示例的理解(我不懂法语),我得出了以下代码。

\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}

这很好。但是点的随机定位会搞乱标签的定位。(尝试编译几次以查看。)我尝试通过常用\node命令设置节点,但仍然遇到相同的问题。我如何控制标签的行为,以便它们不会与图形的其他元素重叠?

编辑

我在我的问题列表中看到了这个 mod 评论:您是否考虑过接受答案或为这个问题开始悬赏?确实,我考虑过接受答案,因为现在解决方案看起来很简单,tikz但我仍然希望看到tkz-euclide基于的解决方案。所以我想我会暂时延长接受答案的时间。那么,开始悬赏怎么样?好吧,为什么不呢?

答案1

您可以在每个点处计算圆外径向的一个点来放置节点:

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

原点标签放置在(B)节点对面的点。一个稍微好一点的解决方案是计算和之间的中点角度(OA)(OC)并将原点标签放置在与该点偏移 180 度的位置。

代码:

\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
    \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

    % 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}

答案2

我在下一版中添加了我的部分答案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}

在此处输入图片描述

\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}

在此处输入图片描述

相关内容