在椭圆中询问补丁

时间:2017-10-22 10:45:47

标签: netlogo

你能用参数(a,b,标题)创建一个方法"在椭圆中补丁" ? (类似于"半径补丁")

或者我可以(轻松)查询这些补丁吗?

1 个答案:

答案 0 :(得分:1)

这是一个可以根据xy原点,到焦点的距离,角度(从0开始的度数)以及与焦点的最大距离来计算椭圆的函数。我基于wikipedia page以及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

要实际返回此椭圆包含的补丁集,您可以使用此记者:

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选择值,但是你喜欢。

那是否更接近你所追求的目标?