我们如何将 Netlogo 海龟的方向转换为直线方程 (y = mx + c ),以便可以将其与另一个直线方程(例如代表墙的补丁)进行比较?
我需要将乌龟的航向转换为直线方程。然后将标题线方程与墙的线方程进行比较(墙的线方程有固定的 x 或固定的 y – 取决于墙是垂直的还是水平的)
库中有一个截取线示例代码(我不太理解),它使用移动段。我认为我追求的东西更简单一些。也许更好的起点是我在其中一个论坛中找到的下面的代码。
to-report calculate-line [ x y angle]
let m tan angle
let A m
let B -1
let C ( - m * x + y )
report (list A B C)
end
为了“读取”当前乌龟航向的 x 和 y 值并将其输入方程中,必须做什么?类似地,如何将一组已着色以代表墙的面片转换为线方程,用于检查可能的交点(标题线方程和墙线方程之间)?
例如,我使用下面的代码创建了一条灰色补丁的线/段来代表墙
考虑到它是直的,必须有一种方法将其转换为直线方程(这将有
固定的 y 值 - 这样我只需根据海龟航向的线方程“测试”x 值。 (在模拟环境中总共有 4 面墙 - 2 面垂直,2 面水平)
ask patches with [abs pycor = 10] [set pcolor grey]
ask patches with [abs pycor = 11] [set pcolor grey]
The x
and y
在论据中calculate-line
参考海龟的当前位置,并且angle
海龟当前航向与 x 轴之间的角度,以零“东”逆时针测量。我不知道为什么B
在那里,它被简单地设置并返回为-1,所以一个更简单的版本是
to-report calculate-line [x y angle]
let m tan angle ; the slope
let c y - m * x ; the constant
report (list m c)
end
乌龟会叫它。然而,NetLogo 测量海龟的heading
从垂直轴顺时针方向,零“北”,所以我们需要在获取角度之前调整航向。最简单的方法是向航向添加 90 度,顺时针旋转所有东西,并且,因为这会使乌龟向东北方向行驶(例如)向东南方向行驶,从而反转坡度的符号,我们需要取负值结果。最后,如果我们让乌龟自己确定路线,它可以简单地填写自己的位置和航向。
然后我们最终得到
to-report calculate-line
; We need to remember that NetLogo measures
; heading from the vertical axis, while the
; tan function assumes an angle measured from
; the horizontal axis. Thus we rotate the
; heading 90 degrees before taking the tan.
; also we deal separately with headings of
; 0 and 180, where the slope is "infinite",
; and headings of 90 and 270, where the slope
; is zero.
let m 0
(ifelse (heading mod 180 = 0) [set m 10e16] ; m is "infinite"
(heading mod 90 = 0) [set m 0]
[set m (- tan (heading + 90))])
let c ycor - m * xcor
report (list m c)
end
ask one-of turtles [show calculate-line]
这样,乌龟就会填写自己的坐标和航向。请注意,我们将沿轴的方向视为特殊情况,并使“无限”(垂直)斜率成为一个非常大的数字。
如果您有四堵墙,则需要小心测试该线与一堵墙的相交是否超出了与其垂直的墙的限制。另外,如果您使用墙的 pxcor 和 pycor 来定义其位置,请记住交叉点将出现在墙的中间,即面片的中间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)