Explaining IM1's script:
The node ID is firstly obtained and put into a local variable $3 (we could also directly use $$.2 in the computation in the computation). We then check if the location for this node (node $3) is 0,0 -- the initial position before the first polling result comes back, in which case we simply return without further calculation. Otherwise we continue our calculation.
In the next step, we calculate (x-x1)^2 +(y-x1)^2 and put the result into $1, where x and y represent the coordinates of the node of concern, and x1, y1 represent the coordinates of node1 (IM1). We will use x2 and y2 for the coordinates for IM2 (node 2). We will follow this convention throughout this explanation.
The calculation proceeds according to different situations:
IM2's differences from IM1:
While IM1 is responsible for initiating a split, IM2 is responsible for initiating a join by calling the join function cell. The threshold distance for controlling the join condition is slightly different from that for split. This threshold is controlled by the split threshold minus a smaller gap. This design is to prevent the hysterisis situation where the distance between IM1 and IM2 ossilate around the split threshold.
Scripts for Calculation function:
on:
action:
$3 = $$.2;
if ([$3:1].1 == 0
|| [$3:1].2 == 0) then
break; // don't use initialization values.
endif;
$1 = ([$3:1].1 - [1:1].1)*([$3:1].1
- [1:1].1)
+ ([$3:1].2 - [1:1].2)*([$3:1].2 - [1:1].2);
if $3 == 1 then
break;
else if $3 == 2 then
if [6:0] == 1
&& $1 > 100 * 100 then
exec [0:10];
$$ = 2;
endif;
else if [6:0] ==
2 then
$2 = ([$3:1].1 - [2:1].1)*([$3:1].1 - [2:1].1)
+ ([$3:1].2 - [2:1].2)*([$3:1].2 - [2:1].2);
if $1 > 80 * 80 && $1 > $2 then
[2:10].1 = $3;
exec [2:10];
$$ = 2;
endif;
endif;
endif;
endif;
IM2's calculation function cell (cell [0:9] at IM2):
init: // ********** Most important calculation function
$$.1 = 2;
$$.2 = 0;
on:
action:
$3 = $$.2;
if ([$3:1].1 == 0
|| [$3:1].2 == 0) then
break; // initialization values are not valid. Wait
until real poll.
endif;
$1 = ([$3:1].1 - [1:1].1)*([$3:1].1
- [1:1].1)
+ ([$3:1].2 - [1:1].2)*([$3:1].2 - [1:1].2);
if $3 == 1 then
break;
else if $3 == 2 then
if $1 <= (100 - 20) * (100 - 20) then
$$ = 1;
exec [1:10];
endif;
else
$2 = ([$3:1].1 - [2:1].1)*([$3:1].1 - [2:1].1)
+ ([$3:1].2 - [2:1].2)*([$3:1].2 - [2:1].2);
if $2 > 80 * 80 && $2 > $1 then
[2:10].1 = $3;
exec [2:10];
$$ = 1;
endif;
endif;
endif;