Bryan Head
Bryan Head

Reputation: 12580

Export part of the view in NetLogo

I want to, given a list of turtles, export the part of the view that contains those turtles as an image. Being able to export part of the view specified by a set of boundaries would solve this problem. That is, a function like export-view-of-turtles list-of-turtles or export-view-rectangle min-xcor max-xcor min-ycor max-ycor would be ideal.

Obviously, a solution that works entirely in NetLogo would be best, but I find that unlikely: export-view is the only function I know of that exports an image of the view at all, and that only does the whole view. However, if there's a plugin for this, that would be awesome.

My last resort is to just export the view and then run a script that clips it accordingly. If there isn't a better solution, I'll do this, and post the script.

Upvotes: 2

Views: 443

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

Alright, this is kind of dirty, but it seems to work. Basically, the below exports the world state in a temp file, records the data about the turtles in question, resizes the view based on the distance of those turtles from the center, recreates just those turtles from the recorded data, exports the view, then restores the original world state. Here's the code:

to export-view-of-turtles [ filename the-turtles ]
  let center-patch min-one-of patches [ sum [ (distance myself ^ 2) ] of the-turtles ]
  let turtle-props [ (list
       (- distance center-patch * sin towards center-patch) ; xcor relative to center patch
       (- distance center-patch * cos towards center-patch) ; ycor relative to center patch
       heading size shape label color
  ) ] of the-turtles
  let max-x max map [ first ? + item 3 ? ] turtle-props
  let min-x min map [ first ? - item 3 ? ] turtle-props
  let max-y max map [ item 1 ? + item 3 ? ] turtle-props
  let min-y min map [ item 1 ? - item 3 ? ] turtle-props
  let world-state-backup (word "temp-world-" date-and-time ".csv")
  export-world world-state-backup
  resize-world min-x max-x min-y max-y
  foreach turtle-props [
    crt 1 [
      setxy first ? (item 1 ?)
      set heading (item 2 ?)
      set size (item 3 ?)
      set shape (item 4 ?)
      set label (item 5 ?)
      set color (item 6 ?)
    ]
  ]
  export-view filename
  import-world world-state-backup
  file-delete world-state-backup
end

Example of use. Given:

enter image description here

Calling export-view-of-turtles "test.png" [ turtles in-radius 5 ] of turtle 85 gives:

enter image description here

Notes:

  • This perfectly supports world wrapping.
  • It will ONLY show the given turtles. Patches, drawing layer, and other turtles will not be shown. That said, it could easily be modified so that they patches and other turtles are shown.
  • As with any code that uses import and export world (not recommended for this kind of thing), this will likely break in many corner cases.

Upvotes: 2

Related Questions