How to fit ellipse into another object - using intersections and calc library

I would like to ask, how to place ellipse into any object, so that the length of the main arm is defined by line1 (point A, B) in my code:

• calculate the coordinates of the ellipse midpoint
• rotation angle of the ellipse
• length of the main arm of the ellipse.

Code is:

    \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}


Picture still looks as follows:

-

Does something like the following solve your problem?

\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}


-
Thanks, can be used variables from the previous ('fill' ... ´let \p1, \p2´ ) command in the following command? – jafan Sep 28 '12 at 8:22
@jafan Unfortunately no unless you save them into some variable. But for this particular example you can combine both with \draw[red,line width=1mm] let \p1=(A),\p2=(B), \n1={atan2(\y2-\y1,\x2-\x1)},\n2={veclen(\y2-\y1,\x2-\x1)} in (A) -- ++(90-\n1:\n2) [fill=cyan,draw=none,opacity=0.25,rotate=90-\n1] ($(A)!0.5!(B)$) ellipse (\n2/2 and 1cm); – percusse Sep 28 '12 at 8:40
What does mean the keyword in in the \draw command? – jafan Sep 29 '12 at 11:04
@jafan Sorry somehow I've missed your comment. in actually terminates the reading of declarations after let operation. So it's almost as if you had \begin{let} ... \end{let} but TikZ does it for you. – percusse Oct 13 '12 at 11:44