Random Box Array
The macro generates an array of circle. The circles are scrambled and check for overlapping with other circles.
Download: randomCircleArray.layout
1 #!/usr/bin/layout
2 #name=random array circle
3 #help=generates a random array of circle
4
5 int main(){
6 //++++++++++++++++++++++++++++++++++++++
7 // enter values here:
8 int sizeX=10000; //array width
9 int sizeY=10000; //array height
10 int diameter=100; //circle diameter
11 int count=2000; //number boxes
12 int layer =9; //layer for array
13 int space=100; //minimum circle to circle distance
14 //++++++++++++++++++++++++++++++++++++++
15 // the macro requires a empty cell to find the nearest element correctly
16 layout->newCell();
17
18 int ds=diameter+space;
19 int i;
20 for ( i=count;i>0;i--){
21 bool found=false;
22 int x;
23 int y;
24 int try=0;
25 while (!found){
26 try++;
27 //use this variant, if the random generator is limited to 16 bit
28 //x=(stdlib::rand()%1000+stdlib::rand()*1000)%sizeX;
29 //y=(stdlib::rand()%1000+stdlib::rand()*1000)%sizeY;
30 //use this variant, if the random generator is 32 bit
31 x=stdlib::rand()%sizeX;
32 y=stdlib::rand()%sizeY;
33 point p; p.set(x,y);
34 elementList *e=layout->drawing->currentCell->nearestElement(p);
35 if (e==NULL) found=true;
36 else {
37 // checks, if conflicts with existing circles
38 point p2;
39 int radius;
40 if (e->thisElement->isCircle(&p2,&radius)){
41 if (p2.x()>(x+ds)) found=true;
42 else if (p2.x()<(x-ds)) found=true;
43 else if (p2.y()<(y-ds)) found=true;
44 else if (p2.y()>(y+ds)) found=true;
45 }
46 }
47 // no free space found
48 if (try>1000) return 1;
49 }
50 point p;
51 p.set(x,y);
52 layout->drawing->currentCell->addCircle(layer,p,diameter/2);
53 }
54 layout->drawing->scaleFull();
55 }