Parable of the Polygons
This exploration is based on the delightful active essay "Parable of the Polygons" by Vi Hart and Nicky Case. The essay makes it possible to experience the effect of individiual choices to a society. It was motivated by Thomas Schelling's Dynamic Models of Segregation.
In this version we give an example how a simple JavaScript/Lively version can be built.
Meet the actors
Below are our friends, the rectangles and triangles. Try dragging them around. Depending on how far away from each other they are they will find their neighbors.
Neighbors: 0 triangles 1 rectangles.
Neighbors: 1 triangles 0 rectangles.
Neighbors: 1 triangles 1 rectangles.
Neighbors: 0 triangles 1 rectangles.
Neighbors: 1 triangles 0 rectangles.
Neighbors: 2 triangles 0 rectangles.
How does this work? Each polygon has a findNeighbors method. Try changing it. For example, give "maxNeighborDist" a fixed number or multiply it with something.
if (!this.owner) return [];
var maxNeighborDist = Math.ceil(this.getExtent().r()),
    center = this.bounds().center();
return this.owner.submorphs.filter(function(m) {
  if (m === this || (!m.isTriangle && !m.isSquare)) return false;
  return center.dist(m.bounds().center()) < maxNeighborDist;
}, this);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Save
Each rectangle and triangle can be in a happy, sad, and neutral ("meh") mood. When you click into the drop down list above you will see the methods "beHappy", "beSad", "beMeh". Select them to see what's happening.
Happiness
Be happy!
Be sad!
Meh!!!
unhappy: only 1 out of 6 neighbors are like me. less than 1/3.
happy: 2 out of 6 neighbors are like me. exactly 1/3.
meh: all neighbors are like me. (also meh if i've got no neighbors)
Below, the grouping of the polygons affect the mood of each other: A polygon is not happy if less than 1/3 of its neighbors are like it or if there are no neighbors.
var counted = this.countNeighbors(),
    same = counted.same,
    other = counted.other,
    total = same + other;
if (total > 0 && (same / total) < 0.33)
  this.beSad();
else if (total == 0 || (same / total) > 0.99)
  this.beMeh();
else
  this.beHappy();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Save
How does the code for this rule look like? See the "checkMood" method below. It is an individual decision made by each polygon based on what neighbors it sees.

Restart
Make 'em happy!
What effect does this individual behavior have on a society of polygons? See what happens when we move the sad polygons closer to their peers...