如何将椭圆拟合到另一个对象中 - 使用交叉点和计算库

如何将椭圆拟合到另一个对象中 - 使用交叉点和计算库

我想问一下,如何将椭圆放置到任何物体中,以便主臂的长度由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}

在此处输入图片描述

相关内容