如何使用两条相交线的概念在 Netlogo 中实现避障(海龟航向与由补丁组成的墙) [英] How to implement obstacle avoidance in Netlogo using the concept of two intersecting lines ( turtle heading vs wall made of patches)

查看:55
本文介绍了如何使用两条相交线的概念在 Netlogo 中实现避障(海龟航向与由补丁组成的墙)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何将 Netlogo 海龟的航向转换为线方程 (y = mx + c ),以便将其与另一个线方程(例如代表墙的补丁的线方程)进行比较?

How can we convert the heading, of a Netlogo turtle, into a line equation (y = mx + c ) so that it can be compared to another line equation (eg that of patches representing a wall) ?

我需要将海龟的航向转换为直线方程.然后将航向线方程与墙的线方程进行比较(这将有一个固定的 x 或一个固定的 y - 取决于墙是垂直的还是水平的)库中有一个拦截线示例代码(我不太了解),它使用移动段.我认为我所追求的要简单一些.也许更好的起点是我在其中一个论坛中找到的以下代码.

I need to convert the heading of a turtle into a line equation. Then compare the heading line equation with the line equation of a wall (which will have either a fixed x or a fixed y – depending on whether the wall is vertical or horizontal) There is an interception lines example code in the library (that I don’t understand that well) which is using moving segments. I think what I am after is a bit simpler. Perhaps a better starting point would be the code below that I found in one of the forums.

to-report计算线[x y角]让 m tan 角让我让 B -1让 C ( - m * x + y )报告(列表 A B C)结束

为了读取"当前海龟航向的 x 和 y 值并将它们输入到方程中,必须做什么?类似地,如何将一组已着色以表示墙壁的补丁转换为一个线方程,用于检查可能的交叉点(在航向线方程和墙壁线方程之间)?例如,我使用下面的代码创建了一条灰色补丁的线/段来表示墙并且鉴于它是直的,必须有一种方法可以将其转换为直线方程(它将具有一个固定的 y 值 - 这样我就只需要根据乌龟航向的线方程测试"x 值.(在模拟环境中总共有 4 面墙 - 2 面垂直和 2 面水平面)

What must be done in order to 'read' the x and y values of the current turtle's heading and input those into the equations? Similarly, how can a set of patches that have been coloured to represent a wall, can be converted into a line equation that will be used to check possible intersection (between the heading line equation and wall line equation) ? For example I have create a line/segment of grey patches using the below code to represent the wall and given it is straight, there must be a way to convert this into a line equation (which will have a fixed y value - so that I will only have to 'test' the x value against the line equation of the turtle's heading. (In the simulated environment there are 4 walls in total - 2 vertical and 2 horizontal)

使用 [abs pycor = 10] [set pcolor grey] 询问补丁使用 [abs pycor = 11] [set pcolor grey]

推荐答案

calculate-line 的参数中的 xy 参考到海龟的当前位置,以及 angle 海龟当前航向和 x 轴之间的角度,逆时针测量,东"为零.我不知道为什么 B 在那里,它只是设置并返回为 -1,所以一个更简单的版本是

The x and y in the arguments of calculate-line refer to the current position of the turtle, and angle the angle between the turtle's current heading and the x axis, measured counter-clockwise with zero "east". I have no idea why the B is there, it is simply set and returned as -1, so a simpler version would be

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 度,顺时针旋转所有东西,并且,因为这会使海龟旅行(例如)NE 旅行 SE,从而反转斜率的符号,我们需要取负结果.最后,如果我们让海龟自己确定线,它可以简单地填写自己的位置和航向.

and it would be called by a turtle. However, NetLogo measures a turtle's heading clockwise from the vertical axis, with zero "north", so we need to adjust the heading before taking its angle. The easiest way of doing this is to add 90 degrees to the heading, rotating everything clockwise, and, since that would make a turtle traveling (say) NE travel SE, thus reversing the sign of the slope, we need to take the negative of the result. Finally, if we let the turtle itself determine the line, it can simply fill in its own location and heading.

然后我们以

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]

这样,海龟就填充了自己的坐标和航向.请注意,我们将沿轴的航向视为特殊情况,并使无限"(垂直)斜率成为一个非常大的数字.

This way, the turtle fills in its own coordinates and heading. Note that we treat headings along the axes as special cases, and make and "infinite" (vertical) slope a very large number.

如果您有四面墙,则需要小心地测试该线与一堵墙的相交是否超出了与其垂直的一堵墙的界限.另外,如果您使用墙的 pxcor 和 pycor 来定义其位置,请记住,交叉点将发生在墙的中间,即补丁的中间.

If you have four walls, you will need to be careful to test that the line does not intersect one wall beyond the limits of one perpendicular to it. Also, if you are using the pxcor and pycor of the wall to define its position, remember that the intersections will occur in the middle of the wall, i.e., the middle of the patch.

这篇关于如何使用两条相交线的概念在 Netlogo 中实现避障(海龟航向与由补丁组成的墙)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆