给定两点 A 和 B,我想绘制直径为 AB 的圆以及它与 X 轴的交点。
我用代码得到了这个
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\rd}{(sqrt(2))}
\pgfmathsetmacro{\fs}{(1+sqrt(2))}
\pgfmathsetmacro{\ss}{(1-sqrt(2))}
\begin{axis}[
x=1cm,
y=1cm,
axis lines=middle,
xmin=-1,
xmax=3,
ymin=-2,
ymax=2
]
\node[right] (A) at (0,1) {A} ;
\node[right] (B) at (2,-1) {B} ;
\node (V) at (1,-1.2) {label} ;
\node[left] (C) at (\ss,0.2) {C} ;
\node[right] (D) at (\fs,0.2) {D} ;
\draw (1,0) circle[radius=\rd cm] ;
\addplot[mark=*,color=blue] coordinates {(0,1)} ;
\addplot[mark=*,color=blue] coordinates {(2,-1)};
\addplot[mark=*,color=yellow] coordinates {(\fs,0)};
\addplot[mark=*,color=yellow] coordinates {(\ss,0)};
\end{axis}
\end{tikzpicture}
\end{document}
尽管这种方法有效,但我并不满意。
我想要实现的是,我应该只提供 A 和 B 的坐标,以及给定圆的直径,然后其他一切都应该从中计算出来。
具体来说,我不应该计算圆心、半径和 C、D 的坐标。此外,点应该画在节点上,字母应该是标签。另外,圆的标签应该在圆内定义。
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{calc, intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x=1cm, y=1cm,
axis lines=middle,
xmin=-1, xmax=3,
ymin=-2, ymax=2,
x axis line style={name path=xaxis},
]
\coordinate (A) at (0,1);
\coordinate (B) at (2,-1);
\draw[name path=circle] let \p1=($ 0.5*(B)+0.5*(A) $), \p2=($ 0.5*(B)-0.5*(A) $), \n1={veclen(\x2,\y2)} in (\p1) circle[radius={\n1}];
\fill[fill=blue] (A) circle[radius=2pt] node[right]{A} (B) circle[radius=2pt] node[right]{B};
\fill[fill=yellow!80!orange, name intersections={of=xaxis and circle}]
(intersection-1) circle[radius=2pt] node[above left]{C}
(intersection-2) circle[radius=2pt] node[above right]{D};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
使用tkz-euclide package
我们有:
\documentclass{standalone}
\usepackage{tkz-euclide}
\usetikzlibrary{calc}
\tkzSetUpPoint[size=5]
\begin{document}
\begin{tikzpicture}[domain=-2:1.2]
\tkzInit[xmin=-1,xmax=3,ymin=-2,ymax=2,xstep=1,ystep=1] % limits the size of the axes
\tkzDrawX[>=stealth] % arrow of x-axis
\tkzDrawY[>=stealth] % arrow of y-axis
\foreach \x in {-1,1,2,3} \draw (\x,0.1) -- (\x,-0.1) node[below] {\x};% ticks on x-axix
\foreach \y in {-2,-1,1,2} \draw (0.1,\y) -- (-0.1,\y) node[left] {\y};% tikc on y-axis
\tkzDefPoints{0/1/A,2/-1/B,-1/0/X,0/0/O} %given points A,B,X(-1,0) and O(0,0)
\tkzDefMidPoint(A,B) %AB diameter-> midpoint K=center
\tkzGetPoint{K} %save coordinates of midpoint in name K
\tkzDrawCircle[thick,black](K,A) %draw circle with center K and R=KA
\tkzInterLC(O,X)(K,B) %find coordinates of intersection line OX (x-axis) and circle
\tkzGetPoints{D}{C} %save these coordinates with names D and C, points of intersection
\tkzDrawPoints[fill=yellow](C,D) %draw points C and D
\tkzDrawPoints[fill=blue](A,B) %fill points A and B with blue color
\tkzLabelPoints[right](A,B) % label points A and B with letters A and B
\tkzLabelPoint[above left](C){C}% labels point C
\tkzLabelPoint[above right](D){D}%labels point D
\node at (1,-1.2) {label};%labels the circle
\draw[color=blue,thick] plot (\x,{(\x)^2+\x}) node[right] {$f(x) = x^2+x$};% draw parabola, page 344 tikz manual.
\end{tikzpicture}
\end{document}
答案3
它pstricks
需要相对较短的代码:
\documentclass[svgnames]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(-1.25,-2.25)(3.25,2.25)
\psset{linewidth=0.5pt}
\psaxes[arrowinset=0.1, ticksize=2pt -2pt, showorigin = false]{->}(0,0)(-1,-2)(3.,2)
\pnodes(-1,0){X1}(2,0){X2}(1,0){I}(0,1){A}(2,-1){B}
\uput[r](A){$A$}\uput[r](B){$B$}
\pstCircleAB{A}{B}
\psdots[linecolor=RoyalBlue](A)(B)
\pstInterLC[PointName=none]{X1}{X2}{I}{A}{C}{D}
\psdots[linecolor=Yellow](C)(D)
\uput[ul](C){$C$}\uput[ur](D){$D$}
\end{pspicture}
\end{document}