在一条线的中间画一个圆

在一条线的中间画一个圆

在下面的代码中,我通过反复试验将圆圈放在了线的中间。是否有代码可以自动执行此操作。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.9, transform shape]
\draw [thick,-latex](0,0) -- (4,0) node [black, xshift=.3cm, yshift=0cm] {$X$};
\draw [thick,-latex](0,0) -- (0,3) node [black, xshift=0cm, yshift=.3cm] {$P$};
\draw [thick, green!40!black] (.78,.47)  -- +(45:3.3cm) node [green!40!black, xshift=.2cm, yshift=.2cm] {$S$};
\draw[blue,fill=blue] (1.95,1.65) circle (2.5pt);
\end{tikzpicture}
\end{frame}
\end{document}

答案1

有很多方法可以做到这一点,但其中一种方法是定义坐标的名称,然后使用 TikZ 的calc库:($(A)!0.5!(S)$)它表示转到从到 的0.5路径上的坐标。坐标和定义为绘制线条:(A)(S)(A)(S)

\draw [thick, green!40!black] 
        (.78,.47) coordinate (A)  
    -- +(45:3.3cm) coordinate (S)

在此处输入图片描述

或者,您也可以将放置node在路径上的pos=0.5

\draw [thick, green!40!black] (.78,.47)  -- +(45:3.3cm) 
    node [midway, draw=red, fill=red, shape=circle, minimum size=5.0pt, inner sep=0pt] {}
    node [green!40!black, xshift=.2cm, yshift=.2cm] {$S$};
%\draw[blue,fill=blue] (1.95,1.65) circle (2.5pt);

代码:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.9, transform shape]
\draw [thick,-latex](0,0) -- (4,0) node [black, xshift=.3cm, yshift=0cm] {$X$};
\draw [thick,-latex](0,0) -- (0,3) node [black, xshift=0cm, yshift=.3cm] {$P$};
\draw [thick, green!40!black] 
    (.78,.47) coordinate (A)  
    -- +(45:3.3cm) coordinate (S)
    node [green!40!black, xshift=.2cm, yshift=.2cm] {$S$}
;
\draw[blue,fill=blue] ($(A)!0.5!(S)$) circle (2.5pt);
\end{tikzpicture}
\end{frame}
\end{document}

答案2

替代:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}

\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
    \begin{tikzpicture}[scale=.9, thick,
dot/.style = {circle, fill=blue, minimum size=6pt, inner sep=0pt, node contents={}}
                        ]
\draw [-latex](0,0) -- (4,0) node [right] {$X$};
\draw [-latex](0,0) -- (0,3) node [above] {$P$};
\draw [green!40!black]
    (.78,.47) -- node[dot] +(45:3.3cm) node [pos=1.1] {$S$};
    \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容