在椭圆中询问补丁
Ask patches in ellipse
你能创建一个带有参数(a、b、标题)的方法 "patches-in-ellipse" 吗? (类似于 "patches-in-radius")
或者我可以通过某种方式(轻松地)查询这些补丁吗?
这里有一个函数可以根据 xy 原点、到焦点的距离、角度(从 0 开始的度数)以及到焦点的最大距离来计算椭圆。我基于 wikipedia page and the answer by Jim on this thread。
to setup
ca
reset-ticks
end
to ellipse [ x y d angle maxdist ]
; origin x and y, distance to foci, angle in degrees, max distance from foci
ask patches [set pcolor black]
let f1x ( x + ( d * sin angle ) )
let f1y ( y + ( d * cos angle ) )
let f2x ( x - ( d * sin angle ) )
let f2y ( y - ( d * cos angle ) )
ask patches with [
( distancexy f1x f1y ) + ( distancexy f2x f2y ) <= maxdist ] [
set pcolor red
]
tick
end
实际上return这个椭圆包含的补丁集,你可以使用这个记者:
to-report patches-in-ellipse [ x y d angle maxdist ]
let f1x ( x + ( d * sin angle ) )
let f1y ( y + ( d * cos angle ) )
let f2x ( x - ( d * sin angle ) )
let f2y ( y - ( d * cos angle ) )
report patches with [ ( distancexy f1x f1y ) + ( distancexy f2x f2y ) <= maxdist ]
end
编辑:
如图所示使用 a 和 b 计算椭圆 here,查看此程序 - 将其复制并粘贴到新模型中并创建一个 setup
按钮和一个永久 go
观看乌龟带着它的椭圆四处游荡的按钮:
to setup
resize-world -50 50 -50 50
set-patch-size 5
ca
crt 1 [
set size 3
]
reset-ticks
end
to go
ask patches [ set pcolor black ]
ask turtles [
rt random 30 - 15
fd 1
]
draw-ellipse
tick
end
to-report ellipse-a-b-heading [ x y a b head ]
let c sqrt ( ( (a) ^ 2 ) - ( (b) ^ 2 ) )
let f1x ( x + ( c * sin head ) )
let f1y ( y + ( c * cos head ) )
let f2x ( x - ( c * sin head ) )
let f2y ( y - ( c * cos head ) )
ask patch f1x f1y [ set pcolor blue ]
ask patch f2x f2y [ set pcolor blue ]
print sqrt ( ( b ^ 2 ) + ( c ^ 2 ) )
report patches with [
( distancexy f1x f1y ) +
( distancexy f2x f2y ) <=
2 * ( sqrt ( ( b ^ 2 ) + ( c ^ 2 ) ) ) ]
end
to draw-ellipse
let a 10
let b 5
ask turtles [
ask ellipse-a-b-heading xcor ycor a b ( heading + 90 ) [
set pcolor red
]
]
end
请注意,函数 ellipse-a-b-heading
需要 5 个输入:一个 x、一个 y、半长轴、半短轴和航向。然而,乌龟提供了几乎所有这些值,如 draw-ellipse
过程所示。所以,你只需要选择你喜欢的 a 和 b 的值。
这更接近您想要的吗?
你能创建一个带有参数(a、b、标题)的方法 "patches-in-ellipse" 吗? (类似于 "patches-in-radius")
或者我可以通过某种方式(轻松地)查询这些补丁吗?
这里有一个函数可以根据 xy 原点、到焦点的距离、角度(从 0 开始的度数)以及到焦点的最大距离来计算椭圆。我基于 wikipedia page and the answer by Jim on this thread。
to setup
ca
reset-ticks
end
to ellipse [ x y d angle maxdist ]
; origin x and y, distance to foci, angle in degrees, max distance from foci
ask patches [set pcolor black]
let f1x ( x + ( d * sin angle ) )
let f1y ( y + ( d * cos angle ) )
let f2x ( x - ( d * sin angle ) )
let f2y ( y - ( d * cos angle ) )
ask patches with [
( distancexy f1x f1y ) + ( distancexy f2x f2y ) <= maxdist ] [
set pcolor red
]
tick
end
实际上return这个椭圆包含的补丁集,你可以使用这个记者:
to-report patches-in-ellipse [ x y d angle maxdist ]
let f1x ( x + ( d * sin angle ) )
let f1y ( y + ( d * cos angle ) )
let f2x ( x - ( d * sin angle ) )
let f2y ( y - ( d * cos angle ) )
report patches with [ ( distancexy f1x f1y ) + ( distancexy f2x f2y ) <= maxdist ]
end
编辑:
如图所示使用 a 和 b 计算椭圆 here,查看此程序 - 将其复制并粘贴到新模型中并创建一个 setup
按钮和一个永久 go
观看乌龟带着它的椭圆四处游荡的按钮:
to setup
resize-world -50 50 -50 50
set-patch-size 5
ca
crt 1 [
set size 3
]
reset-ticks
end
to go
ask patches [ set pcolor black ]
ask turtles [
rt random 30 - 15
fd 1
]
draw-ellipse
tick
end
to-report ellipse-a-b-heading [ x y a b head ]
let c sqrt ( ( (a) ^ 2 ) - ( (b) ^ 2 ) )
let f1x ( x + ( c * sin head ) )
let f1y ( y + ( c * cos head ) )
let f2x ( x - ( c * sin head ) )
let f2y ( y - ( c * cos head ) )
ask patch f1x f1y [ set pcolor blue ]
ask patch f2x f2y [ set pcolor blue ]
print sqrt ( ( b ^ 2 ) + ( c ^ 2 ) )
report patches with [
( distancexy f1x f1y ) +
( distancexy f2x f2y ) <=
2 * ( sqrt ( ( b ^ 2 ) + ( c ^ 2 ) ) ) ]
end
to draw-ellipse
let a 10
let b 5
ask turtles [
ask ellipse-a-b-heading xcor ycor a b ( heading + 90 ) [
set pcolor red
]
]
end
请注意,函数 ellipse-a-b-heading
需要 5 个输入:一个 x、一个 y、半长轴、半短轴和航向。然而,乌龟提供了几乎所有这些值,如 draw-ellipse
过程所示。所以,你只需要选择你喜欢的 a 和 b 的值。
这更接近您想要的吗?