如何在 PGF/TikZ 中查找与椭圆的交点

发布于 2024-09-01 02:42:54 字数 1231 浏览 13 评论 0原文

我试图在 PGF/TikZ 中显示一个球体来说明大圆的概念。

我当前结果的代码是:

\begin{tikzpicture}

\tikzfading[name=fade right,
left color=transparent!20,
right color=transparent!90]

\tikzfading[name=fade out,
inner color=transparent!100,
outer color=transparent!10]

\tikzfading[name=fade right gc,
left color=transparent!0,
right color=transparent!70]

\draw [<->, dashed] (0,-5) -- (0,5); % y-axis
\draw [->, dashed] (0, 0) -- (20:5); % x-axis
\draw [->, dashed] (0, 0) -- (200:5); % x-axis
\draw [->, dashed] (0, 0) -- (340:5); % z-axis
\draw [->, dashed] (0, 0) -- (160:5); % z-axis

\fill [color=cyan, opacity=0.15, path fading=fade out] (0,0) circle (4cm); % bounding circle
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90] (0,0) ellipse (4cm and 1cm); % x-y-axis area

% great circle 1
\draw [rotate=-40, color=red, path fading=fade right gc, fading angle=40] (0,0) ellipse (4cm and 1cm);

% great circle 2
\draw[rotate=5, color=red, path fading=fade right gc, fading angle=5] (0,0) ellipse (1.5cm and 4cm);

\end{tikzpicture}

如何

  1. 找到两个红色椭圆(注释为大圆1和2)的两个交点,
  2. 找到一条线的交点(始于中心(0,0) )和一个椭圆,并
  3. 在那里放置一个小圆形或矩形?

放置一个小圆圈或矩形没有问题。 非常感谢!

I am trying to display a sphere in PGF/TikZ to illustrate the idea of great circles.

The code for my current result is:

\begin{tikzpicture}

\tikzfading[name=fade right,
left color=transparent!20,
right color=transparent!90]

\tikzfading[name=fade out,
inner color=transparent!100,
outer color=transparent!10]

\tikzfading[name=fade right gc,
left color=transparent!0,
right color=transparent!70]

\draw [<->, dashed] (0,-5) -- (0,5); % y-axis
\draw [->, dashed] (0, 0) -- (20:5); % x-axis
\draw [->, dashed] (0, 0) -- (200:5); % x-axis
\draw [->, dashed] (0, 0) -- (340:5); % z-axis
\draw [->, dashed] (0, 0) -- (160:5); % z-axis

\fill [color=cyan, opacity=0.15, path fading=fade out] (0,0) circle (4cm); % bounding circle
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90] (0,0) ellipse (4cm and 1cm); % x-y-axis area

% great circle 1
\draw [rotate=-40, color=red, path fading=fade right gc, fading angle=40] (0,0) ellipse (4cm and 1cm);

% great circle 2
\draw[rotate=5, color=red, path fading=fade right gc, fading angle=5] (0,0) ellipse (1.5cm and 4cm);

\end{tikzpicture}

How do I

  1. find the two points of intersection of the two red ellipses (commented as great circle 1 and 2),
  2. find the point of intersection of a line (originating at the center (0,0)) with a ellipse, and
  3. place a little circle or rectangle there?

Placing a little circle or rectangle there is not an issue.
Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-09-08 02:42:54

查看第 4.1.4 节。 TikZ 和 PGF 手册,标题为“圆的交点”。您需要使用 intersections 库,它允许您使用 name junctions 键,如 \path [name junctions={of=path 1 和 path 2}];。要使用此功能,您需要使用 name path 键,如 \draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y 轴。不同版本的交叉路口访问方式似乎有所不同;我本地的手册副本与我链接到的手册的说明不同。但是,至少在我的版本中,您可以使用 (intersection-1)(intersection-2) 等访问交叉点。要在每个交叉点处获取圆圈,例如,那么,我会将您的代码更改为如下所示:

\begin{tikzpicture}
  \tikzfading[ name        = fade right
             , left color  = transparent!20
             , right color = transparent!90 ]

  \tikzfading[name         = fade out
             , inner color = transparent!100
             , outer color = transparent!10 ]

  \tikzfading[name         = fade right gc
             , left color  = transparent!0
             , right color = transparent!70]

  \draw [name path = y  axis, <->, dashed] (0,-5) -- (0,5)   ; % y-axis
  \draw [name path = x- axis,  ->, dashed] (0, 0) -- (20:5)  ; % x-axis
  \draw [name path = x+ axis,  ->, dashed] (0, 0) -- (200:5) ; % x-axis
  \draw [name path = z+ axis,  ->, dashed] (0, 0) -- (340:5) ; % z-axis
  \draw [name path = z- axis,  ->, dashed] (0, 0) -- (160:5) ; % z-axis

  % bounding circle
  \fill [color=cyan, opacity=0.15, path fading=fade out]
        (0,0) circle (4cm) ;

  % x-y-axis area
  \fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90]
        (0,0) ellipse (4cm and 1cm);

  % great circle 1
  \draw [ name path    = great circle 1
        , rotate       = -40
        , color        = red
        , path fading  = fade right gc
        , fading angle = 40]
        (0,0) ellipse (4cm and 1cm);

  % great circle 2
  \draw [ name path    = great circle 2
        , rotate       = 5
        , color        = red
        , path fading  = fade right gc
        , fading angle = 5]
        (0,0) ellipse (1.5cm and 4cm);

  % Intersections
  \path [name intersections={of=great circle 1 and great circle 2}] ;
  \foreach \i in {1,...,4}
    \fill [color=red] (intersection-\i) circle (2pt) ;

  \path [name intersections={of=y axis and great circle 1}] ;
  \fill (intersection-1) circle (2pt) ;
  \fill (intersection-2) circle (2pt) ;
  \path [name intersections={of=y axis and great circle 2}] ;
  \fill (intersection-1) circle (2pt) ;
  \fill (intersection-2) circle (2pt) ;

  \foreach \a in {x,z} {
    \foreach \ss in {+,-} {
      \def\s.{\ss} % Otherwise the space in `\a\s axis` would get gobbled.
      \path [name intersections={of=\a\s. axis and great circle 1}] ;
      \fill (intersection-1) circle (2pt) ;
      \path [name intersections={of=\a\s. axis and great circle 2}] ;
      \fill (intersection-1) circle (2pt) ;
    }
  }
\end{tikzpicture}

除了重新格式化(以避免水平滚动条)之外,我对现有代码所做的所有更改都是添加 name path 键到你的轴和大圆。然后我添加了交叉点代码,这应该是相对不言自明的。请记住首先使用 \usetikzlibrary{intersections} ,一切都应该正常。

Check out section 4.1.4. of the TikZ and PGF manual, titled "The Intersection of the Circles." You need to use the intersections library, which allows you to use the name intersections key, as in \path [name intersections={of=path 1 and path 2}] ;. To use this, you'll need to use the name path key, as in \draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y-axis. Accessing the intersections seems to vary between versions; my local copy of the manual has different instructions from the one I linked you to. However, at least on my version, you then access the intersections with (intersection-1), (intersection-2), etc. To get circles at each intersection in your example, then, I would change your code to look like the following:

\begin{tikzpicture}
  \tikzfading[ name        = fade right
             , left color  = transparent!20
             , right color = transparent!90 ]

  \tikzfading[name         = fade out
             , inner color = transparent!100
             , outer color = transparent!10 ]

  \tikzfading[name         = fade right gc
             , left color  = transparent!0
             , right color = transparent!70]

  \draw [name path = y  axis, <->, dashed] (0,-5) -- (0,5)   ; % y-axis
  \draw [name path = x- axis,  ->, dashed] (0, 0) -- (20:5)  ; % x-axis
  \draw [name path = x+ axis,  ->, dashed] (0, 0) -- (200:5) ; % x-axis
  \draw [name path = z+ axis,  ->, dashed] (0, 0) -- (340:5) ; % z-axis
  \draw [name path = z- axis,  ->, dashed] (0, 0) -- (160:5) ; % z-axis

  % bounding circle
  \fill [color=cyan, opacity=0.15, path fading=fade out]
        (0,0) circle (4cm) ;

  % x-y-axis area
  \fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90]
        (0,0) ellipse (4cm and 1cm);

  % great circle 1
  \draw [ name path    = great circle 1
        , rotate       = -40
        , color        = red
        , path fading  = fade right gc
        , fading angle = 40]
        (0,0) ellipse (4cm and 1cm);

  % great circle 2
  \draw [ name path    = great circle 2
        , rotate       = 5
        , color        = red
        , path fading  = fade right gc
        , fading angle = 5]
        (0,0) ellipse (1.5cm and 4cm);

  % Intersections
  \path [name intersections={of=great circle 1 and great circle 2}] ;
  \foreach \i in {1,...,4}
    \fill [color=red] (intersection-\i) circle (2pt) ;

  \path [name intersections={of=y axis and great circle 1}] ;
  \fill (intersection-1) circle (2pt) ;
  \fill (intersection-2) circle (2pt) ;
  \path [name intersections={of=y axis and great circle 2}] ;
  \fill (intersection-1) circle (2pt) ;
  \fill (intersection-2) circle (2pt) ;

  \foreach \a in {x,z} {
    \foreach \ss in {+,-} {
      \def\s.{\ss} % Otherwise the space in `\a\s axis` would get gobbled.
      \path [name intersections={of=\a\s. axis and great circle 1}] ;
      \fill (intersection-1) circle (2pt) ;
      \path [name intersections={of=\a\s. axis and great circle 2}] ;
      \fill (intersection-1) circle (2pt) ;
    }
  }
\end{tikzpicture}

Other than the reformatting (to avoid the horizontal scroll bar), all I have changed of your existing code is to add the name path key to your axes and great circles. I then added the intersections code, which should be relatively self-explanatory. Remember to \usetikzlibrary{intersections} first, and everything should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文