![使用 Tikz 绘制斜椭圆](https://linux22.com/image/337174/%E4%BD%BF%E7%94%A8%20Tikz%20%E7%BB%98%E5%88%B6%E6%96%9C%E6%A4%AD%E5%9C%86.png)
几个小时前我发现了 tikz 包。我想画一个斜椭圆。我该怎么做?
这是我的代码:
\documentclass[tikz,border]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[gray, thick] (-3,2) -- (3,2);
\draw[gray, thick, dotted] (-3.5, 2) -- (-3,2);
\draw[gray, thick, dotted] (3, 2) -- (3.5,2);
\draw[gray, thick] (-3,0) -- (3,0);
\draw[gray, thick, dotted] (-3.5, 0) -- (-3,0);
\draw[gray, thick, dotted] (3, 0) -- (3.5,0);
\filldraw[black] (-2,0) circle (1.5pt) node[anchor=north] {$(p, 0)$};
\filldraw[black] (-2,2) circle (1.5pt) node[anchor=south] {$(q, 0)$};
\filldraw[black] (2,2) circle (1.5pt) node[anchor=south] {$(q, t)$};
\draw[gray, thick] (-2,1) ellipse (0.5 and 1);
\end{tikzpicture}
\end{document}
我需要用椭圆将点 (p, 0) 与点 (q, t) 连接起来。
谢谢你!
答案1
使用 TikZ,椭圆的轴始终是水平和垂直的。要旋转椭圆,您可以使用scope
更改坐标系并calc
使用库来计算新的x
和y
向量。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[gray, thick] (-3,2) -- (3,2);
\draw[gray, thick, dotted] (-3.5, 2) -- (-3,2);
\draw[gray, thick, dotted] (3, 2) -- (3.5,2);
\draw[gray, thick] (-3,0) -- (3,0);
\draw[gray, thick, dotted] (-3.5, 0) -- (-3,0);
\draw[gray, thick, dotted] (3, 0) -- (3.5,0);
\filldraw[black] (-2,0) coordinate (p0) circle (1.5pt) node[anchor=north] {$(p, 0)$};
\filldraw[black] (-2,2) coordinate (q0) circle (1.5pt) node[anchor=south] {$(q, 0)$};
\filldraw[black] (2,2) coordinate (qt) circle (1.5pt) node[anchor=south] {$(q, t)$};
\draw[gray, thick] (-2,1) ellipse (0.5 and 1);
\begin{scope}[shift={(p0))},x={(qt)},y={($(p0)!1!90:(qt)$)}]
\draw[red] (.5,0) ellipse (.5 and .25);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
另一种解决方案是使用calc
库和绘制的椭圆nodes
。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.geometric, calc}
\begin{document}
\begin{tikzpicture}[
dot/.style={minimum size=3pt, inner sep=0pt, fill, draw, circle}]
\draw[gray, thick] (-3,2) -- (3,2);
\draw[gray, thick, dotted] (-3.5, 2) -- (-3,2);
\draw[gray, thick, dotted] (3, 2) -- (3.5,2);
\draw[gray, thick] (-3,0) -- (3,0);
\draw[gray, thick, dotted] (-3.5, 0) -- (-3,0);
\draw[gray, thick, dotted] (3, 0) -- (3.5,0);
\node[dot, label={below:$(p,0)$}] (p0) at (-2,0) {};
\node[dot, label={$(q,0)$}] (q0) at (-2,2) {};
\node[dot, label={$(q,t)$}] (qt) at (2,2) {};
\path (p0.center) let \p1=($(q0.center)-(p0.center)$) in node[ellipse, minimum height={veclen(\x1,\y1)}, minimum width=1cm, anchor=south, thick, gray, draw] {};
\path (p0.center) let \p1=($(qt.center)-(p0.center)$) in node[ellipse, minimum height={veclen(\x1,\y1)}, minimum width=1cm, anchor=south, thick, draw, gray, rotate={-atan2(\x1,\y1)}] {};
\end{tikzpicture}
\end{document}
答案3
另一种解决方案是使用优雅transform
的元帖子。
要定义变换,您可以给出三个非共线点的隐式方程。符号point x of y
允许您挑选路径的点;对于fullcircle
,因此e
本例中的路径,有 8 个点从“3 点”开始逆时针等距分布。因此点 2 在顶部,点 6 在底部。
prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);
path e, e', hi_bar, lo_bar;
e = fullcircle xscaled 1cm yscaled 2cm;
hi_bar = (left -- 5 right) scaled 1cm shifted point 2 of e;
lo_bar = (left -- 5 right) scaled 1cm shifted point 6 of e;
transform s;
point 6 of e transformed s = point 6 of e;
point 2 of e transformed s = point 2 of e shifted (3.8cm,0);
point 0 of e transformed s = point 6 of e shifted (2.4cm,0);
e' = e transformed s;
drawoptions(withcolor .5 white);
draw e; r = 1/16;
draw subpath (0, r) of lo_bar dashed withdots scaled 1/2;
draw subpath (r,1-r) of lo_bar;
draw subpath (1-r,1) of lo_bar dashed withdots scaled 1/2;
draw subpath (0, r) of hi_bar dashed withdots scaled 1/2;
draw subpath (r,1-r) of hi_bar;
draw subpath (1-r,1) of hi_bar dashed withdots scaled 1/2;
drawoptions(withcolor red);
draw e';
drawoptions();
dotlabel.top (btex $(q,0)$ etex, point 2 of e);
dotlabel.urt (btex $(q,t)$ etex, point 2 of e');
dotlabel.llft(btex $(p,0)$ etex, point 6 of e);
endfig;
end