在Netlogo中画一个半圆

时间:2019-04-23 18:40:06

标签: netlogo

我想创建一个半圆,我的半圆的中心在世界的尽头,例如,我希望我的半圆的中心在50,20的位置,并且从20变为-20。 我尝试了一下,但是有一个例外:

  

[60,20]点在世界范围之外,   不允许在一个方向或两个方向都进行包装。

to mysemicircle
  let cx 50                ;; x coordinate of patch you want to circle
  let cy 20                ;; y coordinate of patch you want to circle
  let r 10                ;; radius of the circle you want
  let p2r ( 2 * pi * r )  ;; get circumference of the circle
  let step p2r / 360      ;; make step lengths 1/360th of the circumference
  crt 1 [                 ;; create a single drawing turtle
    setxy cx + r cy       ;; move it to the highlight patch + the radius
    pd                    ;; put the pen down
    set heading 0         ;; make it face along the tangent
    while [ p2r > 0 ] [   ;; make the turtle continue to move until the circle is drawn
      lt 1                
      fd step            
      set p2r p2r - step 
      set color white   
    ]
  ]

end

如果我使用cx 40和cy 20,则圆不是从y -20到y 20,而是仅从y 0到y 20。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

也许带有参数的过程对此更好,例如:

to setup
  ca  
  resize-world -20 20 -20 20
  draw-semi-circle 0 0 15 45 blue
  draw-semi-circle 5 5 5 270 red
  draw-semi-circle -10 -10 8 135 green
  reset-ticks
end

to draw-semi-circle [ x y r a col ] 
  ; Give arguments for x, y, radius, and angle of the semicircle
  let half-p ( pi * r ) 
  let step half-p / 180
  ask patch x y  [
    set pcolor col
  ]
  crt 1 [  
    setxy x y
    set heading a   
    lt 90
    set color col
    back r 
    pd                    
    fd 2 * r
    rt 90
    while [ half-p > step ] [  
      rt 1                
      fd step            
      set half-p half-p - step  
    ]
  ]
end

要获得如下输出:

enter image description here