因为我对Mathematica完全陌生,所以我遇到了以下问题:
我应该在y方向(向上)“飞”一架无人机,在它撞上放置在{0,5}处的“障碍物”之前,它应该在x方向上移动。这是可行的,但是现在我应该绘制无人机的“飞行路径”。我用一个数组试了一下,但是我不能画出它。有谁能帮帮我吗?
Reap[For[it = 1, it < 11, it++, drone = {0, it}; Sow[drone] If[obstacle == drone + {0, 1}, For[i = 1, i < 11, i++, drone = {i, it}; Sow[drone]]]]]
`{Null, {{{0, 1}, {0, 2}, {0, 3}, {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4,
4}, {5, 4}, {6, 4}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {0, 5}, {0,
6}, {0, 7}, {0, 8}, {0, 9}, {0, 10}}}}`
我知道"For“在Mathematica中不是最好的方式,但我在其他编程语言中已经习惯了。
我在谷歌上搜索了很多不同的方法(表格、列表等)。但都没有成功,这是我最接近的解决方案了(如果我能够画出它的话)。
编辑:
感谢您的解决方案。让它起作用了!
发布于 2017-06-21 16:25:32
在Sow[drone]
和If[obstacle == drone + {0, 1}
之间应该有一个分号,尽管在本例中它仍然有效。这里有一些策划建议。
obstacles = {{0, 5}, {3, 12}};
i = 0;
path = Reap[For[it = 1, it < 21, it++,
drone = {i, it};
Sow[drone];
If[MemberQ[obstacles, drone + {0, 1}],
Do[drone = {i++, it};
Sow[drone], 3]]]][[2, 1]];
plot = Show[ListLinePlot[path, PlotMarkers -> Automatic],
ListPlot[obstacles, PlotStyle -> Red, PlotMarkers -> {Automatic, 12}],
Frame -> True, PlotRangePadding -> {0.6, {1, 2}}, Axes -> False]
https://stackoverflow.com/questions/44664610
复制相似问题