代码之家  ›  专栏  ›  技术社区  ›  Liz Lamperouge

在Netlogo中画一个半圆

  •  0
  • Liz Lamperouge  · 技术社区  · 7 年前

    我想创建一个半圆,半圆的中心在世界的尽头,例如,我想半圆的中心在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
    

    我该怎么做?

    0 回复  |  直到 7 年前
        1
  •  3
  •   Luke C    7 年前

    也许一个带参数的过程会更好,比如:

    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

    推荐文章