Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a random shape (blob) of a given area in NetLogo

Tags:

netlogo

Is it possible to create random shapes (see below for example) of a given area in NetLogo?

enter image description here

like image 973
user2359494 Avatar asked Jan 08 '14 14:01

user2359494


2 Answers

A first stab at Seth's suggestion #1. It creates a neat visual too!

patches-own [ height ]

to blobbify
  clear-all
  repeat (ceiling 2 * (ln (world-height * world-width))) [
    ask patch (round (random (world-width / 2)) - world-width / 4)
              (round (random (world-height / 2)) - world-height / 4)
              [ set height world-width * world-height ] ]
  while [ count patches with [ height > 1 ] < (world-width * world-height / 4)] 
        [ diffuse height 1
          ask patches with [ height > 0 ] [ set pcolor height ]
        ]
   ask patches with [ height > 1 ] [ set pcolor white ]
end
like image 106
Frank Duncan Avatar answered Nov 10 '22 17:11

Frank Duncan


I found a really simple approach that produces pretty nice results.

Create a turtle. The turtle performs a random walk. After each step, they set the uncolored patch closest to them to the desired color. The turtle does this a number of times equal to the desired area.

Here's the code:

to make-blob [ area ]
  let blob-maker nobody
  crt 1 [ set blob-maker self ]
  repeat area [
    ask blob-maker [
      ask min-one-of patches with [ pcolor = black ] [ distance myself ] [ set pcolor blue ]
      rt random 360
      fd 1
    ]
  ]
  ask blob-maker [ die ]
end

This naturally produces nicely curved blobs.

Making the turtle's step size smaller makes the blob more circle-y. Making it bigger results in thinner, more sporadic blobs (though you run the risk of getting disconnected patches).

Edit:

I noticed that my answer runs quite slowly when you have a gigantic number of patches. Here's a much faster version:

to make-blob [ area x y ]
  let blob-maker nobody
  crt 1 [ set blob-maker self setxy x y ]
  let border patch-set [ patch-here ] of blob-maker
  repeat area [
    ask blob-maker [
      ask min-one-of border [ distance myself ] [
        set pcolor green
        set border (patch-set border neighbors4) with [ pcolor = black ]
      ]
      rt random 360
      fd .8
    ]
  ]
  ask blob-maker [ die ]
end
like image 36
Bryan Head Avatar answered Nov 10 '22 18:11

Bryan Head