万花筒模型与代码(二)

发布时间:2024年01月04日

?这个模型使用海龟来重复绘制圆圈和其他几何形状,同时定期转动,形成复杂的图案,看起来像万花筒和风车。它展示了即使简单的规则也能创造复杂而美丽的图案。

它是如何工作的

图案-1

在这个图案中,每10只海龟向前移动0.5步,向右转动DIRECTION度。每次调用图案都会孵化一只海龟,它执行RIGHT-CIRCLE函数,然后执行LEFT-CIRCLE函数,然后死亡。通过增加DIRECTION,海龟转动的角度也会增加。这意味着每次向前移动,海龟都会在一个中心点周围转动得更紧密。因此,当DIRECTION设置为0时,海龟只是向前移动。当它增加时,比如从0到1再到10,海龟的运动就会变得更加圆形。虽然图案会改变,但必须明白正在创建的基本形状是一个圆。

图案-2

每个偶数编号的海龟向前移动1步,并向右转1度。然后孵化一只执行HEXAGON函数的海龟,然后死亡。每个奇数编号的海龟向前移动1步,并向左转1度。然后孵化一只执行OCTAGON函数的海龟,然后死亡。有趣的是,当NUM-TURTLES滑块调到一个很高的数字,比如大于或等于20时,较小的数字中的图案变得难以辨认。然而,必须明白,尽管新图案可能看起来不同,但正在制作的基本形状仍然是六边形和八边形。

图案-3

每5只海龟向前移动0.5步,并向右转动DIRECTION度。然后孵化一只执行PENTAGON函数的海龟,然后死亡。图案-3和图案-5可能是这个模型中最简单的图案,因为它们的行为解释起来最简单。基本上,图案-3创建五边形,但是因为海龟向前移动0.5步,并向右转动DIRECTION度,这就导致图案发生了显著的变化。看上去好像孵化的海龟在创造不同的形状,但事实上,它们正在制作五边形,而且永远都是五边形。只有外部影响的轻微变化才会导致图案的变化。

图案-4

每3只海龟向右转1度,然后孵化一只执行NINE-GON函数的海龟,然后死亡。所有其他海龟都向左转1度,然后孵化一只执行LEFT-CIRCLE函数的海龟,然后死亡。

图案-5

每只海龟都孵化一只执行LEFT-SQUARE函数的海龟,然后死亡。

图案-6

每只海龟向前移动DIRECTION步,然后孵化一只按照以下顺序执行以下函数的海龟:NINE-GON、OCTAGON、RIGHT-CIRCLE、PENTAGON、HEXAGON和LEFT-CIRCLE。执行完这些函数后,每只孵化的海龟都死亡。

代码

globals [
  current-color-sep        ; spread of the colors in the kaleidoscope
  counter
]

turtles-own [
  new?                 ; was the turtle just created?
  type-a?              ; used when turtles with different behaviors are hatched
]

to setup
  clear-all
  ; some of the patterns assume
  ; evenly spaced turtles
  create-ordered-turtles num-turtles
  ask turtles [
    pen-down
    set new? false
    set type-a? false
  ]
  set current-color-sep color-sep
  set counter 0
  reset-ticks
end

to lift-pen
  ask turtles [
    if who >= num-turtles [ die ]
     pen-up
    ]
  clear-patches
end


to restore
  ask turtles [
    if who >= num-turtles [ die ]
     pen-down
    ]
  clear-patches
end

to color-shift
  ifelse shift-direction = "increment"
    [ set current-color-sep (current-color-sep + (random-float 0.001)) ]
    [ set current-color-sep (current-color-sep - (random-float 0.001)) ]
end

; turtles draw circles creating an overall circular design.
to pattern-1    ; turtle procedure
  if new? [
    set color (who / current-color-sep)
     if follow-turtle? [
       if who = (50 + num-turtles) [ pen-down ]
     ]
     right-circle
     left-circle
     die
  ]
  if (who mod 10) = 0 [
    rt direction
    fd 0.5
    if (count turtles + 1) <= max-num [
      hatch 1 [set new? true]
    ]
  ]
end


; Turtles draw a combination of hexagons and octagons, Overall shape determined by num-turtles.
to pattern-2    ; turtle procedure
  if new? [
    ifelse type-a? [
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (60 + num-turtles) [ pen-down ]
      ]
      hexagon
      die
    ][
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (50 + num-turtles) [ pen-down ]
      ]
      octagon
      die
    ]
  ]
  ifelse (who mod 2) = 0 [
    rt 1
    fd 1
    if (count turtles + 1) <= max-num [
      hatch 1 [
        set new? true
        set type-a? true
      ]
    ]
  ][
    lt 1
    fd 1
    if (count turtles + 1) <= max-num [
      hatch 1 [
        set new? true
        set type-a? false
      ]
    ]
  ]
end

; Turtles create only pentagons, slight variations in their origin create the overall effect.
to pattern-3    ; turtle procedure
  if new? [
    set color (who / current-color-sep)
    if follow-turtle? [
      if who = (60 + num-turtles) [ pen-down ]
    ]
    pentagon
    die
  ]
  if (who mod 5) = 0 [
    rt direction
    fd 0.5
    if (count turtles + 1) <= max-num [
      hatch 1 [ set new? true ]
    ]
  ]
end

; Turtles draw ninegons and left circles creating an overall circular pattern.
to pattern-4    ; turtle procedure
  if new? [
    ifelse type-a? [
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (1583 + num-turtles) [ pen-down ]
        if who = (1087 + num-turtles) [ pen-down ]
      ]
      nine-gon
      die
    ][
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (214 + num-turtles) [ pen-down ]
      ]
      left-circle
      die
    ]
  ]
  ifelse (who mod 3) = 0 [
    rt 1
    if (count turtles + 1) <= max-num [
      hatch 1 [
        set new? true
        set type-a? true
      ]
    ]
  ][
    lt 1
    if (count turtles + 1) <= max-num [
    hatch 1 [
      set new? true
      set type-a? false
    ]
  ]
]
end

; Turtles draw a left square and then die.
to pattern-5    ; turtle procedure
    if new? [
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (80 + num-turtles) [ pen-down ]
      ]
      left-square
      die
    ]
    if (count turtles + 1) <= max-num [
      hatch 1 [ set new? true ]
    ]
end

; Turtles draw several shapes, however overall design remains circular.
to pattern-6    ; turtle procedure
  if count turtles > max-num [
    if who > max-num [ die ]
     stop
    ]

  if new? [
    ifelse type-a? [
      set color (who / current-color-sep)
      if follow-turtle? [
        if who = (60 + num-turtles) [ pen-down ]
      ]
      pentagon
      hexagon
      left-circle
      die
    ][ set color (who / current-color-sep)
      if follow-turtle? [
        if who = (60 + num-turtles) [ pen-down ]
      ]
      nine-gon
      octagon
      right-circle
      die
    ]
  ]

  if (count turtles + 1) <= max-num [
    hatch 1 [
      set new? true
      set type-a? true
    ]
  ]
  if (count turtles + 1) <= max-num [
    hatch 1 [
      set new? true
      set type-a? false
    ]
  ]
end

; Performs the following procedure 180 times:
; Move forward 1.5 steps and turn right by 2 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a circle heading in the right direction.
to right-circle    ; turtle procedure
  repeat 180 [
    fd 1.5
    rt 2
  ]
end

; Performs the following procedure 180 times:
; Move forward 1.5 steps and turn left by 2 degrees.
; To see the shape that this function creates,
; try calling it in the command center with 0one turtle with the pen down.
; A turtle will create a circle heading in the left direction.
to left-circle    ; turtle procedure
  repeat 180 [
    fd 1.5
    lt 2
  ]
end

; Performs the following procedure 4 times:
; Move forward EXPANDER steps and turn left by 90 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a square heading in the left direction.
to left-square    ; turtle procedure
  repeat 4 [
    fd expander
    lt 90
  ]
end

; Performs the following procedure 3 times:
; Move forward 35 steps and turn right by 120 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a triangle heading in the right direction.
to right-triangle    ; turtle procedure
  repeat 3 [
    fd expander
    rt 120
  ]
end

; Performs the following procedure 8 times:
; Move forward 30 steps and turn right by 45 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create an octagon heading in the right direction.
to octagon    ;;turtle procedure
  repeat 8 [
    fd 30
    lt 45
  ]
end

; Performs the following procedure 5 times:
; Move forward 35 steps and turn right by 72 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a pentagon heading in the right direction.
to pentagon    ; turtle procedure
  repeat 5 [
    fd 35
    rt 72
  ]
end

; Performs the following procedure 6 times:
; Move forward 30 steps and turn right by 60 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a hexagon heading in the right direction.
to hexagon    ; turtle procedure
  repeat 6 [
    fd 30
    rt 60
  ]
end

; Performs the following procedure 9 times:
; Move forward 35 steps and turn right by 40 degrees.
; To see the shape that this function creates,
; try calling it in the command center with one turtle with the pen down.
; A turtle will create a nine-gon heading in the right direction.
to nine-gon    ; turtle procedure
  repeat 9 [
    fd 35
    lt 40
  ]
end


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

文章来源:https://blog.csdn.net/weixin_44956825/article/details/135331303
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。