![如何将椭圆拟合到另一个对象中 - 使用交叉点和计算库](https://linux22.com/image/256130/%E5%A6%82%E4%BD%95%E5%B0%86%E6%A4%AD%E5%9C%86%E6%8B%9F%E5%90%88%E5%88%B0%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1%E4%B8%AD%20-%20%E4%BD%BF%E7%94%A8%E4%BA%A4%E5%8F%89%E7%82%B9%E5%92%8C%E8%AE%A1%E7%AE%97%E5%BA%93.png)
我想问一下,如何将椭圆放置到任何物体中,以便主臂的长度由line1
我的代码中的(点 A,B)定义:
- 计算椭圆中点的坐标
- 椭圆的旋转角度
- 椭圆主臂的长度。
代码为:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections, calc}
\begin{document}
\begin{figure}[hb!]
\centering
\begin{tikzpicture}
\draw[xstep=0.5, ystep=0.5, dashed, color=gray] (-1,-1) grid (5,5);
\draw[->] (0,0) -- (7,0) node[right] {$x$};
\draw[->] (0,0) -- (0,7) node[above] {$y$};
\coordinate (dS) at (2,0.5);
\shade[name path=teleso,ball color=blue!10!white,opacity=0.50,line width=1,draw=black] plot [smooth cycle] coordinates
{(0,0) (1,-1) (3,0) (2.5,2) (3,3) (3,4) (2,5) (-1,3) };
\draw (3,4) node[right]{S};
% ploska dS
\shade[bottom color=black,top color=black!50!blue!35](dS) node[below]{$dS$} rectangle +(0.5,0.5);
\draw[->] (dS) ++ (0.25,0.25) -- +(1,0.5) node[right]{$\vec{n}$};
\draw[->] (dS) ++ (0.25,0.25) -- +(2,1.5) node[right]{$\vec{h}$};
\draw[fill=black] (2,3) circle (0.05) node[above, black]{$objem$};
\draw[name path=line1](-1.5,3.5) -- (3,1.5);
% Intersections
\path [name intersections={of=teleso and line1, name=cross}] ;
\foreach \i in {1,2}
\fill [color=red] (cross-\i) circle (2pt) ;
% ellipse
\coordinate [label=left:$A$] (A) at (cross-1);
\coordinate [label=right:$B$] (B) at (cross-2);
\draw[red,line width=1mm]
let \p1 = ($(B)-(A)$) in (A) -- ++(45:({veclen(\x1,\y1)}););
\fill [color=cyan, opacity=0.25]
($ (A)!0.5!(B) $) ellipse (2cm and 1cm);
\end{tikzpicture}
\end{figure}
\end{document}
图像仍然如下:
答案1
下面的方法能解决你的问题吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections, calc}
\begin{document}
\begin{figure}[hb!]
\centering
\begin{tikzpicture}
\draw[xstep=0.5, ystep=0.5, dashed, color=gray] (-1,-1) grid (5,5);
\draw[->] (0,0) -- (7,0) node[right] {$x$};
\draw[->] (0,0) -- (0,7) node[above] {$y$};
\coordinate (dS) at (2,0.5);
\shade[name path=teleso,ball color=blue!10!white,opacity=0.50,line width=1,draw=black] plot [smooth cycle] coordinates
{(0,0) (1,-1) (3,0) (2.5,2) (3,3) (3,4) (2,5) (-1,3) };
\draw (3,4) node[right]{S};
% ploska dS
\shade[bottom color=black,top color=black!50!blue!35](dS) node[below]{$dS$} rectangle +(0.5,0.5);
\draw[->] (dS) ++ (0.25,0.25) -- +(1,0.5) node[right]{$\vec{n}$};
\draw[->] (dS) ++ (0.25,0.25) -- +(2,1.5) node[right]{$\vec{h}$};
\draw[fill=black] (2,3) circle (0.05) node[above, black]{$objem$};
\draw[name path=line1](-1.5,3.5) -- (3,1.5);
% Intersections
\path [name intersections={of=teleso and line1, name=cross}] ;
\foreach \i in {1,2}
\fill [color=red] (cross-\i) circle (2pt) ;
% ellipse
\coordinate [label=left:$A$] (A) at (cross-1);
\coordinate [label=right:$B$] (B) at (cross-2);
\draw[red,line width=1mm]
let \p1 = ($(B)-(A)$) in (A) -- ++(45:({veclen(\x1,\y1)}););
\fill [color=cyan, opacity=0.25] let \p1=(A),\p2=(B),
\n1={atan2(\y2-\y1,\x2-\x1)},\n2={veclen(\y2-\y1,\x2-\x1)} in
[rotate=90-\n1] ($ (A)!0.5!(B) $) ellipse (\n2/2 and 1cm);
\end{tikzpicture}
\end{figure}
\end{document}