以下问题基于我为学生写的一份作业,要求确定以原点为中心、半径为 r 的圆的切点。
我已成功使用以下代码获得我想要的图表:
\begin{tikzpicture}
\pgfmathsetmacro{\tangentpoint}{sqrt(19)}%
\draw[-stealth] (-2,0)--(2,0) node [right]{$x$};
\draw[-stealth] (0,-2)--(0,2) node [above]{$y$};
\draw[thick,Cerulean] (0,0) circle (1cm);
\draw (0,0)--(0.9,\tangentpoint/10);
\draw[domain=0.25:1.5,smooth,variable=\x,OrangeRed,thick] plot ({\x},{(-9*\tangentpoint)*\x/19+(10*\tangentpoint)/19});
\node[fill, inner sep=0.75pt, circle, draw] at (0,0) {};
\node[fill, inner sep=0.75pt, circle, draw] at (0.9,\tangentpoint/10) {};
\end{tikzpicture}
输出符合要求。当然,现在我正试图开发一种仅基于给定 x 坐标来确定切点和切线的一般形式。以下在区间(-0.99999,-0.00001)
和之间执行此操作(0.00001,0.99999)
。它在x=0
和处失败,x= +- 1
如下面的代码所示:
\begin{tikzpicture}
\newcommand{\Xtangent}{-0.4}
\newcommand{\Radius}{1}
\pgfmathsetmacro{\Ytangentcalc}{sqrt(pow(\Radius,2)- pow(\Xtangent,2))}%
\pgfmathsetmacro{\Yintercepttangentlinecalc}{pow(\Radius,2)/\Ytangentcalc}%
\pgfmathsetmacro{\mtangentlinecalc}{(\Ytangentcalc-\Yintercepttangentlinecalc)/\Xtangent}%
%\clip (-2,-2) rectangle (2,2);
\draw[-stealth] (-2,0)--(2,0) node [right]{$x$};
\draw[-stealth] (0,-2)--(0,2) node [above]{$y$};
\draw[thick,Cerulean] (0,0) circle (\Radius);
\draw (0,0)--(\Xtangent,\Ytangentcalc);
\draw[domain=-2:2,smooth,variable=\x,OrangeRed,thick] plot ({\x},{\mtangentlinecalc*\x+\Yintercepttangentlinecalc});
\node[fill, inner sep=0.75pt, circle, draw] at (0,0) {};
\node[fill, inner sep=0.75pt, circle, draw] at (\Xtangent,\Ytangentcalc) {};
\end{tikzpicture}
问题是:我该如何解决失败以及如何确定切点是否位于象限中的任一象限,尤其是 3 和 4?
以下是完整的 MWE:
\documentclass[letterpaper,dvipsnames]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\newcommand{\Xtangent}{-0.4}
\newcommand{\Radius}{1}
\pgfmathsetmacro{\Ytangentcalc}{sqrt(pow(\Radius,2)- pow(\Xtangent,2))}%
\pgfmathsetmacro{\Yintercepttangentlinecalc}{pow(\Radius,2)/\Ytangentcalc}%
\pgfmathsetmacro{\mtangentlinecalc}{(\Ytangentcalc-\Yintercepttangentlinecalc)/\Xtangent}%
%\clip (-2,-2) rectangle (2,2);
\draw[-stealth] (-2,0)--(2,0) node [right]{$x$};
\draw[-stealth] (0,-2)--(0,2) node [above]{$y$};
\draw[thick,Cerulean] (0,0) circle (\Radius);
\draw (0,0)--(\Xtangent,\Ytangentcalc);
\draw[domain=-2:2,smooth,variable=\x,OrangeRed,thick] plot ({\x},{\mtangentlinecalc*\x+\Yintercepttangentlinecalc});
\node[fill, inner sep=0.75pt, circle, draw] at (0,0) {};
\node[fill, inner sep=0.75pt, circle, draw] at (\Xtangent,\Ytangentcalc) {};
\end{tikzpicture}
\end{document}
答案1
只是为了好玩,因此没有确切的答案。tkz-euclide
毫不费力地做到这一点。
\documentclass[dvipsnames]{article}
\usepackage{tkz-euclide,tikz}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
\newcommand{\myangle}{120}
\tkzInit[xmin=-2,xmax=2,xstep=1,ymin=-2,ymax=2,ystep=1]
\tkzDrawX \tkzDrawY
\tkzDefPoint(0,0){c}
\tkzDefPoint(1,1){a0}
\tkzRadius=1 cm
\tkzDrawCircle[R,thick,color=Cerulean](c,\tkzRadius)
\tkzDefPointBy[rotation=center c angle \myangle](a0)
\tkzGetPoint{a}
\tkzTangent[from with R = a](c,\tkzRadius)
\tkzGetPoints{e}{f}
\tkzDrawLine[add = 1 and 2,color=OrangeRed,thick](a,f)
\tkzDrawSegment(c,f)
\tkzDrawPoints[size=3,fill](f,c)
\end{tikzpicture}
\end{document}
和\newcommand{\myangle}{180}
和\newcommand{\myangle}{0}
答案2
这是另一个 Metapost 版本,它定义了一个绘制切线的函数。这只是普通的 Metapost,因此您可以mpost xxx.mp
像往常一样对其进行编译。
该函数的参数应该是一条圆形路径,x 坐标应为 -1 到 1 之间的数字。相应的 y 坐标使用方便的“毕达哥拉斯减法”运算符计算,该运算符记录在Metafont 书籍(本质上a +-+ b
等同于sqrt(a**2-b**2)
,但效率更高)。切线利用 的事实进行(x,y) rotated 90
旋转(-y,x)
。
prologues := 3;
outputtemplate := "%j%c.eps";
vardef mark_upper_tangent(expr circle, dx) =
save r, p, dy; pair p; numeric r, dy;
if abs(dx) <= 1:
dy = 1 +-+ dx;
r = xpart (point 0 of circle - center circle);
p = (dx,dy) scaled r shifted center circle;
draw center circle -- p withcolor .67 white;
draw (left--right) scaled r rotated angle (-dy,dx) shifted p withcolor .67 red;
fill fullcircle scaled dotlabeldiam shifted p;
fi
enddef;
beginfig(1);
u := 1cm;
path xx, yy, C;
xx = (left--right) scaled 2u;
yy = (down-- up ) scaled 2u;
C = fullcircle scaled 3u;
drawarrow xx; label.rt (btex $x$ etex, point 1 of xx);
drawarrow yy; label.top(btex $y$ etex, point 1 of yy);
draw C withcolor .67 blue;
mark_upper_tangent(C, 0.4);
endfig;
end.
答案3
我不确定应该朝哪个方向实现自动化。也许你可以通过使事情变得更加参数化来构建它。
基本思路是pos
使用选项将节点放置在 360 度弧上sloped
。然后使用该节点的锚点绕过角度计算(实际上是所有计算)。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
place tannode/.style={insert path={
node[pos=#1,sloped,fill, circle,inner sep=0.75pt] (tannode){}}},
place tannode/.default=0.5,
draw tannode/.style={insert path={
($(tannode.center)!1cm!(tannode.west)$)--($(tannode.center)!1cm!(tannode.east)$)
}}
]
\def\XR{10mm}
\draw (\XR,0) arc(0:360:\XR) [place tannode=7/8];% You can use acos for the value
\draw[red,thick,draw tannode];
\end{tikzpicture}
\end{document}
答案4
这里有一个解决方案,允许您使用tangent over={.5 r 2}
将切线放在半径和中心x=.5
为的圆上的样式:2
(0,0)
\documentclass[varwidth,border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
tangent at/.style = {
insert path={
let \p1=(#1) in {
(\p1) node[scale=2]{.} -- +(\y1,-\x1) -- +(-\y1,\x1)
}
}
},
tangent over/.code args={#1 r #2}{
\pgfmathparse{sqrt((#2)^2-(#1)^2)}
\pgfkeysalso{tangent at={#1,\pgfmathresult}}
},
tangent under/.code args={#1 r #2}{
\pgfmathparse{sqrt((#2)^2-(#1)^2)}
\pgfkeysalso{tangent at={#1,-\pgfmathresult}}
}
}
\begin{document}
\begin{tikzpicture}
\draw circle(1) circle(2) [tangent under={.5 r 1}];
\draw[red,tangent under={1 r 1}, tangent over={-1 r 1}];
\draw[green,tangent over={-.5 r 2}];
\end{tikzpicture}
\end{document}
笔记:
- 如果您想绘制不在中心的圆的切线,
(0,0)
可以使用shift
。 - 为了简单起见,我没有添加切线长度的参数,但您可以轻松地做到这一点。