如何制作有向圆弧(或有向曲线)?

发布于 2024-11-02 06:10:20 字数 1890 浏览 0 评论 0原文

我想在 Mathematica 中显示有向圆弧,使用像 Arrow 这样简单的东西。我能想到的最好的例子就是将一个箭头钉在圆弧一端的示例。但我怀疑有一种更直接的方法可以达到同样的效果。

start=\[Pi];
Manipulate[
Graphics[{
   Arrow[{{Cos[\[Theta] + If[\[Theta] < start, .01, -.01]], 
           Sin[\[Theta] + If[\[Theta] < start, .01, -.01]]}, 
          {Cos[\[Theta]], Sin[\[Theta]]}}],
   Circle[{0, 0}, 1, {start, \[Theta]}]},
PlotRange -> 2], 
{{\[Theta], .7 start}, 0, 2 start} 
           ]

arrow

Arrow 接受 BSplineCurvesBezierCurves >但我无法让它们保持在圆弧上。 Tube 接受 3D 曲线的公式,但我不知道如何让它在 2D 中工作。

欢迎所有建议。如果您的解决方案适用于任何二维曲线,那就更好了!


结语:

我从这些建议中学到了很多东西: Mark McClure 表明,当给定点列表时,Arrow 本身可以处理需求。

yoda 使用 ParametricPlot 给出了一个相当通用的解决方案。

我最终发现贝利萨留的建议最有帮助。他的方法是对熟悉的图形对象进行微小的变化。最后,我谨慎地选择定义一个新对象arcArrow,它使用Circle的参数:center、radius、{start、finish}。 取消保护仍然让我害怕!无论如何,这就是我解决的问题。我还顽固地坚持了我原来方法的一些特点。

Manipulate[
 Graphics[{  
   arcArrow[center, radius, {start, end}],
   PointSize[Large], Blue, If[showCenter, Point[center]]},
  PlotRange -> p, ImageSize -> 250],
 {{start, \[Pi]/2}, -2 \[Pi], 2 \[Pi], ImageSize -> Small},
 {{end, 0}, -2 \[Pi], 2 \[Pi], ImageSize -> Small},
 {{radius, 1}, 1/2, 4, ImageSize -> Small},
 {{center, {0, 0}}, {-p, -p}, {p, p}, Slider2D},
 {showCenter, {True, False}},

 Initialization :> {p = 3;
   arcArrow[a_, r_, {start_, end_}] :=
    {Circle[a, r, {start, end}], 
     Arrowheads[Medium],
     Arrow[{a + r {Cos[end + If[end < start, .01, -.01]], 
                   Sin[end + If[end < start, .01, -.01]]}, 
            a + r {Cos[end], Sin[end]}}]} }]

最终版本


I'd like to display a directed circular arc in Mathematica, using something as simple as Arrow. The best I have been able to come up with is this example that nails an Arrow onto one end of a circular arc. But I suspect there is a more direct way to achieve the same effect.

start=\[Pi];
Manipulate[
Graphics[{
   Arrow[{{Cos[\[Theta] + If[\[Theta] < start, .01, -.01]], 
           Sin[\[Theta] + If[\[Theta] < start, .01, -.01]]}, 
          {Cos[\[Theta]], Sin[\[Theta]]}}],
   Circle[{0, 0}, 1, {start, \[Theta]}]},
PlotRange -> 2], 
{{\[Theta], .7 start}, 0, 2 start} 
           ]

arrow

Arrow accepts BSplineCurves and BezierCurvesbut I can't get them to stay on a circular arc. Tube accepts the formula for a curve in 3D but I can't figure out how to get it to work in 2D.

All suggestions are welcome. If your solution works for any 2D curve, all the better!


Epilogue:

I learned quite a bit from the suggestions:
Mark McClure showed that Arrow itself can handle the demands when given a list of points.

yoda gave a fairly general solution using ParametricPlot.

I ended up finding belisarius' suggestions the most helpful. His approach was to work on minor variations of familiar graphical objects. In the end, I cautiously chose to define a new object, arcArrow, that employs the parameters of Circle: center, radius, {start,finish}. Unprotect still scares me! Anyway, here's what I settled with. I also stubbornly held on to some features of my original approach.

Manipulate[
 Graphics[{  
   arcArrow[center, radius, {start, end}],
   PointSize[Large], Blue, If[showCenter, Point[center]]},
  PlotRange -> p, ImageSize -> 250],
 {{start, \[Pi]/2}, -2 \[Pi], 2 \[Pi], ImageSize -> Small},
 {{end, 0}, -2 \[Pi], 2 \[Pi], ImageSize -> Small},
 {{radius, 1}, 1/2, 4, ImageSize -> Small},
 {{center, {0, 0}}, {-p, -p}, {p, p}, Slider2D},
 {showCenter, {True, False}},

 Initialization :> {p = 3;
   arcArrow[a_, r_, {start_, end_}] :=
    {Circle[a, r, {start, end}], 
     Arrowheads[Medium],
     Arrow[{a + r {Cos[end + If[end < start, .01, -.01]], 
                   Sin[end + If[end < start, .01, -.01]]}, 
            a + r {Cos[end], Sin[end]}}]} }]

final version


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

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

发布评论

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

评论(3

彼岸花似海 2024-11-09 06:10:20

您还可以向圆弧的终点添加箭头,如下所示:

circle[x_] = {Cos[x], Sin[x]};
ParametricPlot[{0.9 circle[x], 0.7 circle[x + Pi/3], 0.4 circle[-x]}, 
 {x, Pi/4, Pi/2}, PlotRange -> {-1, 1}, Axes -> False] 
/.Line[x__] :> Sequence[Arrowheads[.03], Arrow[x]]

在此处输入图像描述

这可能更容易控制,因为您可以以编程方式设置半径和弧长,只需替换最后的箭头即可。

您还应该看看关于 在边缘添加箭头的讨论一条曲线。那里有很多好的方法,也许比这个更好。我个人找到了西蒙对他自己的回答问题是我在我的“来自互联网的函数”集合中拥有的一个非常漂亮的小函数,并且不止一次地使用它来将箭头放置在继续绘图之外的图表上。

编辑

按照我上面定义的方式,它在行尾放置了一个箭头。例如,如果您绘制一条从 L 到 R 的线,则会将其放置在右侧和左侧。因此,在示例中,x 的正增加是逆时针方向的,因此箭头朝该方向。增加负方向将产生顺时针箭头。为了避免第二个绘图命令,我只是将列表中第三个函数中的 x 更改为 -x,这在金色曲线中产生了相同的效果。

更一般地说,您可以通过更改 Arrowheads 的输入来更改箭头方向,如下所示:

箭头朝相反方向

在此处输入图像描述

两端都有箭头

在此处输入图像描述

倒箭头

在此处输入图像描述

You can also add an arrow to the end point of an arc like so:

circle[x_] = {Cos[x], Sin[x]};
ParametricPlot[{0.9 circle[x], 0.7 circle[x + Pi/3], 0.4 circle[-x]}, 
 {x, Pi/4, Pi/2}, PlotRange -> {-1, 1}, Axes -> False] 
/.Line[x__] :> Sequence[Arrowheads[.03], Arrow[x]]

enter image description here

This is probably much easier to control, as you can set the radius and arc length programmatically and just replace the arrows in the end.

You should also take a look at the discussion on adding arrows at the edges of a curve. There are a lot of good approaches there, perhaps better than this one. I personally found Simon's answer to his own question to be a pretty nifty little function that I have in my little 'functions from the internet' collection, and have used it more than once to place arrows on graphs that continue outside the plot.

EDIT

The way I've defined it above, it places an arrow at the end of the line. For e.g., if you plot a line from L to R, it places it on the right and left otherwise. So in the example, positive increase in x is counter-clockwise and hence the arrows in that direction. Increase in the negative dir will produce a clockwise arrow. To avoid a second plot command, I simply changed x to -x in the third function in the list, which produced the same effect in the golden colored curve.

More generally, you can change orientation of the arrows by changing the inputs to Arrowheads as follows:

Arrows going the other way round

enter image description here

Arrows on both ends

enter image description here

Inverted arrowheads

enter image description here

杯别 2024-11-09 06:10:20

使用圆形[]

f[s_Circle] := 
 s /. Circle[a_, r_, {start_, end_}] :>
      ({s,Arrow[{# - r/10^6 {-Sin@end, Cos@end}, #}]} &[a+r {Cos@end, Sin@end}]) 

Graphics@f[Circle[{0, 0}, 1, {4 Pi/3, 2 Pi}]]

在此处输入图像描述

编辑

重新定义默认 Circle[ ] 行为:

Unprotect[Circle];
Circle[a_: {0, 0}, r_: 1, {start_, end_}] :=
  Block[{$inMsg = True},
    {Circle[a, r, {start, end}],
     Circle[a, r, {start, end}] /. 
      Circle[aa_, 
        ar_, {astart_, aend_}] :> (Arrow[If[start < end, #, Reverse@#]] &@
             {# - r/10^6 {-Sin@end, Cos@end}, #} &
               [aa + ar {Cos@aend, Sin@aend}])}
    ] /; ! TrueQ[$inMsg];
Protect[Circle];

在此处输入图像描述

Using Circle[ ]:

f[s_Circle] := 
 s /. Circle[a_, r_, {start_, end_}] :>
      ({s,Arrow[{# - r/10^6 {-Sin@end, Cos@end}, #}]} &[a+r {Cos@end, Sin@end}]) 

Graphics@f[Circle[{0, 0}, 1, {4 Pi/3, 2 Pi}]]

enter image description here

Edit

Redefining the default Circle[ ] behavior:

Unprotect[Circle];
Circle[a_: {0, 0}, r_: 1, {start_, end_}] :=
  Block[{$inMsg = True},
    {Circle[a, r, {start, end}],
     Circle[a, r, {start, end}] /. 
      Circle[aa_, 
        ar_, {astart_, aend_}] :> (Arrow[If[start < end, #, Reverse@#]] &@
             {# - r/10^6 {-Sin@end, Cos@end}, #} &
               [aa + ar {Cos@aend, Sin@aend}])}
    ] /; ! TrueQ[$inMsg];
Protect[Circle];

enter image description here

红焚 2024-11-09 06:10:20

Arrow 接受任意长度的点列表。因此,也许是这样的。

Manipulate[Graphics[
  Arrow[Table[{Cos[t], Sin[t]}, {t, 0, T, Sign[T] Pi/100}]],
  PlotRange -> 1.1], {{T, 0}, -2 Pi, 2 Pi}]

Arrow accepts a list of points of arbitrary length. Thus, perhaps something like so.

Manipulate[Graphics[
  Arrow[Table[{Cos[t], Sin[t]}, {t, 0, T, Sign[T] Pi/100}]],
  PlotRange -> 1.1], {{T, 0}, -2 Pi, 2 Pi}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文