2017-06-20 138 views
1

正如我完全新Mathematica的我卡在以下问题:绘图的for循环的值(阵列)

我应该在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}}}}` 

我知道“为”不是在数学要做到这一点的最好办法,但我从其他程序语言习惯了。

我搜索了很多不同的方法(表,列表等),但没有解决,这是最接近我的解决方案,为我解决(如果我只是能够绘制它)。

编辑:

感谢您的解决方案。得到它的工作!

回答

1

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] 

enter image description here