TikZ 中橢球體的正交投影

TikZ 中橢球體的正交投影

在 TikZ 中,我想繪製從一個點到一個(旋轉和移動)橢圓的正交投影。作為一個特定的範例,我想繪製從圖片中的點到橢圓的最短線,並且最好也在橢圓上標記該點:

在此輸入影像描述

我已經成功地用一個圓做到了這一點(因為該點只是由與圓的交點以及通過該點本身和圓心的線給出)。但是對於橢圓,我似乎無法讓它在 TikZ 中工作。

上圖的範例程式碼如下:

\documentclass{standalone}
\usepackage{tikz,tkz-euclide}

\begin{document}

\newcommand{\boundellipse}[3]% center, xdim, ydim
{(#1) ellipse (#2 and #3)
}

\begin{tikzpicture}

\draw[shift={(-0.875,0)},rotate=25] \boundellipse{0,0}{1}{3};%left

\node at (0,4)[circle,fill,inner sep=1.5pt]{};

\end{tikzpicture}

\end{document}

答案1

我建議 TikZ + 梯度下降

\documentclass[tikz]{standalone}
\usepackage{tikz,tkz-euclide}

\begin{document}

\newcommand{\boundellipse}[3]% center, xdim, ydim
    {(#1) ellipse (#2 and #3)}

\makeatletter
\xdef\sx{-0.875} % shift x
\xdef\sy{0} % shift y
\xdef\ra{1} % radius a
\xdef\rb{3} % radius b
\xdef\ro{25} % rotation
\pgfpointxy{0}{4}
\xdef\Px{\the\pgf@x}\xdef\Py{\the\pgf@y}

% let \ang ("angle") be a free variable and run gradient descent
\def\ang{234} % choose your favorite initial value
\foreach\iterationcounter in{1,...,20}{
    \begin{tikzpicture}
        \draw(-5,-3)rectangle(1,5);
        \draw[shift={(-0.875,0)},rotate=25] \boundellipse{0,0}{1}{3};
        \node at (0,4)[circle,fill,inner sep=1.5pt]{};
        % evaluate Ellipse(\ang)
        \pgfpointxy{\sx + \rb*cos(\ang)*sin(\ro) + \ra*sin(\ang)*cos(\ro)}
                    {\sy - \rb*cos(\ang)*cos(\ro) + \ra*sin(\ang)*sin(\ro)}
        \xdef\Qx{\the\pgf@x}\xdef\Qy{\the\pgf@y}
        \draw(\Qx,\Qy)circle(.1);
        % evaluate diff vector to target point
        \xdef\Dx{\the\dimexpr\Px-\Qx}
        \xdef\Dy{\the\dimexpr\Py-\Qy}
        \draw[red,->](\Qx,\Qy)--+(\Dx,\Dy);
        % evaluate tangent line = d Ellipse(\ang) / d\ang 
        \pgfpointxy{- \rb*sin(\ang)*sin(\ro) + \ra*cos(\ang)*cos(\ro)}
                    {+ \rb*sin(\ang)*cos(\ro) + \ra*cos(\ang)*sin(\ro)}
        \xdef\Tx{\the\pgf@x}
        \xdef\Ty{\the\pgf@y}
        \draw[blue,->](\Qx,\Qy)--+(\Tx,\Ty);
        % inner product
        \pgfmathsetmacro\Inn{\Dx*\Tx + \Dy*\Ty}
        % rescale inner product
        \pgfmathsetmacro\inn{\Inn / sqrt(\Tx*\Tx+\Ty*\Ty)}
        \message{^^J thinbold: \inn ^^J}
        % update angle
        \pgfmathsetmacro\ang{\ang + \inn/10} % /10 is the step length
        \xdef\ang{\ang}
    \end{tikzpicture}
}

\end{document}

答案2

數學問題和演算法方法

正如@Thruston 所建議的,需要數學來解決這個問題。無論如何,這會導致一個不平凡的(四次)方程,很難以解析方式求解(讓我們看看類似的問題或者點到橢圓和點到橢球距離方程式分析)。所以我們的想法是用數值方法來解這個方程式。在https://wet-robots.ghost.io/simple-method-for-distance-to-ellipse/我找到了一種幾何穩定的演算法,它透過最小化與原始點的距離來找到橢圓上的點(正交投影)。

演算法

以下步驟和圖像將暗示這個想法。

  1. 連接為了得到開始(這允許在橢圓的“右側”運行演算法)。
  2. 畫一個圓(藍色)並取得藍色圓和橢圓的兩個交點的中點。
  3. 使用中點繪製一個新的較小圓圈(紫色)並迭代該過程(即紅色、橙色、粉紅色…)

在此輸入影像描述

程式碼

該代碼需要包tikztkz-euclide特別\usetikzlibrary{intersections}是交叉點。我使用它是tkz-euclide因為我對這些命令感覺很好。無論如何,你可以在純 tikz 中得到相同的結果。

\begin{tikzpicture}

% INITIAL DATA %
% the arbitrary point P
\tkzDefPoint(3,2){P}
% the center of the ellipse
\tkzDefPoint(0,0){O}
% use rotate=angle to set the desired orientation
\path[draw,name path=theellipse,rotate=20] (O) ellipse (2cm and 1cm);
\tkzLabelPoints[above right](P)
\tkzLabelPoints[below left](O)

% STARTING POINT OF ALGORITHM %
\path[name path=OP] (O)--(P);
\path[name intersections={of=OP and theellipse,by={Aone}}];
% comment/erase if need next three code lines
\tkzLabelPoint[above left](Aone){$A_{\textrm{start}}$}
\tkzDrawCircle[help lines](P,Aone)
\tkzDrawPoints(Aone)

% ALGORITHM TO FIND THE ORTHOGONAL PROJECTION %
% set up a different number of steps if needed
% (algorithm converges relatively fast)
\foreach \i in {1,...,3}
{
% define a circle with center P through Aone
% (Astart for the first step)
\tkzDefCircle[radius](P,Aone)
\tkzGetLength{dPAone}
\path[name path=circle] (P) circle (\dPAone pt);

% find intersections of circle with ellipse (Aone, Atwo)
\path[name intersections={of=circle and theellipse,by={Atwo,Aone}}];

% find a "proper" midpoint of Aone -- Atwo on the ellipse
\tkzDefMidPoint(Aone,Atwo)\tkzGetPoint{Aone}
\path[name path=PAone] (P)--(Aone);
\path[name intersections={of=PAone and theellipse,by={Aone}}];
}


% GET AND PRINT OUT THE DISTANCE
\tkzDrawPoints(O,P,Aone)
\tkzDrawSegment[red](P,Aone)
\end{tikzpicture}

在此輸入影像描述

答案3

只是為了比較,您可以非常簡單地執行此操作梅塔普斯特使用solve巨集和合適的輔助函數。

在此輸入影像描述

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

    path e; pair p; numeric k;
    e = fullcircle xscaled 233 yscaled 144 rotated 10;
    p = 160 dir 142;

    vardef acute(expr t) =
        direction t of e dotprod (p - point t of e) > 0
    enddef;

    k = solve acute(0, 4);

    drawarrow p -- point k of e withcolor red;
    draw e; 
    dotlabel.top(btex $p$ etex, p);

endfig;
\end{mplibcode}
\end{document}

這是包含在中的luamplib,因此您可以使用lualatex.

筆記

  • solve的第 176-177 頁有解釋元字體書

  • 這個想法是您定義宏,foo使得foo(x)或。然後你呼叫where和are 值,使得true 和false 。 使用二分查找來尋找和之間的邊緣值。truefalsesolve foo(a, b)abfoo(a)foo(b)solveab

  • 在本例中,我定義了一個名為 的宏acute,它使用dotprod運算子來告訴我們橢圓點處的切線是否與橢圓點到點的t直線形成銳角。pt

  • solve求角度不再為銳角的點,因此該點是線p與 切線正交的點,因此最接近p

  • 需要一些技巧和判斷力來為 的不同位置選擇正確的初始值p

正如你所看到的,我的解釋比代碼更長...

相關內容