# Voronoi

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## WHAT IS IT?

This model draws a Voronoi diagram of polygons around a set of points. These diagrams resemble many phenomena in the world including cells, forest canopies, territories of animals, fur and shell patterns, crystal growth and grain growth, cracks in dried mud and other geological phenomena, road networks, and so on. Voronoi diagrams are useful in computer graphics, vision and path planning for robots, marketing, and other applications.

## HOW IT WORKS

First the points are placed randomly. Then the polygons are drawn according to the following rules. Each point is enclosed inside exactly one polygon. All of the points inside the polygon are closer to that point than they are to any of the other points.

Instead of calculating the mathematically exact coordinates of the polygons, this model constructs an approximation using a grid. Each grid cell (each "patch", in NetLogo terminology) is colored according to which point it is closest to.

## HOW TO USE IT

Use the NUMBER slider to choose how many points you want, then press SETUP. The model will place the points and draw the polygons.

If you want to play with moving the points around yourself, press the GO button. Now you can drag the points around with the mouse. As you move a point, the model redraws the polygon. This takes time, so it redraws them starting near the mouse and proceeding outward. If you want to see the boundary of the updated region, turn on the SHOW-UPDATES? switch.

## THINGS TO NOTICE

The line segment separating two points is exactly midway between them.

How many sides do the polygons typically have? (You may want to ignore the polygons around the edges.)

Where different colors touch, there is usually a "Y". When do you get a "T" or an "X" instead?

## THINGS TO TRY

Experiment with the effect of moving the points around. Moving the points slowly is best. (If you move them too fast, the model will have trouble keeping up and it won't be easy to see what's going on.)

Align two points so they have the exact same x coordinate or y coordinate. Is the line between them always perfectly smooth? (To see the effect, you may have to move the points closer or farther away from each other. Look closely.) Also try putting two points exactly on top of each other. What happens? Both effects occur because when a grid square ("patch") is equally distant from two differently colored points, NetLogo resolves the tie randomly.

## EXTENDING THE MODEL

Instead of placing the points completely randomly, have them move away from each other until they are roughly equidistant from each other. This makes all the polygons roughly the same size.

Edit the view and turn wrapping on in both directions, and click SETUP. The model may seem to be working, but there is a problem. If you turn on SHOW-UPDATES?, you can see that the update rectangle keeps going forever, continually refreshing the grid colors. Fix the model to work with wrapping, so that update stops as soon as the whole screen has been redrawn.

Instead of using the patches to display Voronoi polygons, find the boundaries by using turtles. Create a large batch of turtles at each point (colored the same color as the point), each turtle facing a different angle. Have the turtles walk outward from their points at a uniform rate. Stop the turtles when they run into a turtle of a different color.

Instead of using a patch-based approximation, calculate the exact positions of the sides of the polygons. (There are numerous published algorithms for calculating this information.) Then display the polygons using turtles with the "line" shape.

## NETLOGO FEATURES

The core procedure for drawing the polygons is called `recolor`

. It is only one line long! It puts the `min-one-of`

and `distance`

reporters to good use.

The `mouse-down?`

, `mouse-xcor`

, and `mouse-ycor`

primitives are used so the user can interact with the model.

Because the number of patches is so large, it takes a while to update them all when a point moves. So we use moving turtles to recolor the patches; the moving turtles start where the mouse is, and move outwards in a square, since near the mouse is where the user will be looking first. See the Code tab for the details on how it works.

## RELATED MODELS

- MaterialSim Grain Growth
- Fur
- Honeycomb
- Scatter

## CREDITS AND REFERENCES

For more information on Voronoi diagrams, see http://en.wikipedia.org/wiki/Voronoi. (There are also many other sites on this topic on the web.)

Thanks to John Jungck from Beloit College for inspiring this model with his talk at Northwestern University about Voronoi structures in nature.

Thanks to Josh Unterman and Seth Tisue for their work on this model.

## HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. (2006). NetLogo Voronoi model. http://ccl.northwestern.edu/netlogo/models/Voronoi. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

## COPYRIGHT AND LICENSE

Copyright 2006 Uri Wilensky.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

## Comments and Questions

globals [ available-colors ;; list of colors that will be assigned to points current-point ;; the point the user is currently moving ] breed [points point] ;; these are the little circles in the middle of the polygons ;; The next two breeds are used only when we're updating the polygons ;; when the user moves the points with the mouse. See below for further ;; details. breed [spawners spawner] breed [updaters updater] ;;; ;;; CORE PROCEDURES ;;; These are the only procedures necessary to draw the diagram ;;; initially, without moving points. ;;; to setup clear-all ;; too dark and too light are hard to distinguish from each other, ;; so only use 13-17, 23-27, ..., 133-137 set available-colors shuffle filter [(? mod 10 >= 3) and (? mod 10 <= 7)] n-values 140 [?] set-default-shape points "circle 3" ask n-of number patches [ make-point ] ask patches [ recolor ] set current-point nobody end to make-point ; patch procedure sprout-points 1 [ set size 5 set color first available-colors set available-colors butfirst available-colors ] end to recolor ;; can be patch or turtle procedure set pcolor [color] of min-one-of points [distance myself] end ;;; ;;; OTHER PROCEDURES ;;; The rest of the procedures are used for efficiently updating ;;; the diagram when the user moves the points around. ;;; to go obey-mouse ask spawners [ spawn ] ask updaters [ update ] end to obey-mouse ;; first handle the case where the user has released the mouse button ;; (or hasn't pressed it yet) if not mouse-down? [ set current-point nobody stop ] ;; if the mouse button is down, get the mouse position let x round mouse-xcor let y round mouse-ycor ;; if we don't have a point yet, pick the closest one if current-point = nobody [ set current-point min-one-of points [distancexy x y] ] ;; check if the point needs to move if x != [xcor] of current-point or y != [ycor] of current-point [ ;; move the point ask current-point [ setxy x y ] ;; the point has moved, so we need to recolor all patches, so we kill off ;; the old turtles that were doing the recoloring and make new ones ask spawners [ die ] ask updaters [ die ] ask current-point [ ask patch-here [ make-spawners ] ] ] end ;; Here's how we use turtles to update the patches in a growing ;; square pattern. We use two breeds of turtles, spawners and updaters. ;; Spawners are at the corners of the square and move diagonally. Each ;; time a spawner moves, it spawns two new updaters. The updaters move ;; vertically or horizontally, and every time an updater lands on a patch, ;; it recolors it. When a spawner or an updater hits the edge of the world, ;; it dies. Together, these rules are enough to make the growing square! to make-spawners ;; patch procedure let counter 0 sprout-spawners 4 [ ;; give the four headings of NE, SE, SW, and NE set heading 45 + counter * 90 set counter counter + 1 set color gray if not show-updates? [ hide-turtle ] ] end to spawn ;; spawner procedure hatch-updaters 1 [ rt 45 ] hatch-updaters 1 [ lt 45 ] if not can-move? 1 [ die ] ;; Moving diagonally is a little tricky. Moving forward 1 isn't enough by ;; itself, since that doesn't take us all the way to the center of the ;; diagonally next patch. So after moving forward, we use SETXY to move ;; to the exact center of the new patch. fd 1 setxy pxcor pycor end to update ;; updater procedure recolor if not can-move? 1 [ die ] fd 1 end ; Copyright 2006 Uri Wilensky. ; See Info tab for full copyright and license.

There are 11 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Voronoi.png | preview | Preview | almost 11 years ago, by Reuven M. Lerner | Download |

This model does not have any ancestors.

This model does not have any descendants.

Uri Wilensky

## My learning sciences design class (Question)

I'm hoping this interface will work well in my class. Any thoughts on issues that might arise for my class? I'm thinking we might need pagination for the comments.

## Posted about 13 years ago

Forrest Stonedahl

## Re: learning sciences design class

Well, there is the issue that questions like this end up attached to models like "Voronoi", when I think the question is more broadly about the Modeling Commons platform... :-)

## Posted about 13 years ago

Michelle Wilkerson-Jerde

## Re: learning sciences design class

I remember liking to be able to look at code from the Commons to help people debug and think through their models. It helped me work harder to talk through the code and help people explain and interpret it, rather than jumping in and editing!

## Posted about 13 years ago