Advertisement
Guest User

ColorLabeler1.pde

a guest
Mar 18th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. //Compatible with Processing 3.
  2. PImage map0;
  3. PGraphics offscreen;
  4. int countDistricts=3;//Must >= 2, obviously.
  5. District[] districts;
  6. Dot[] dots;
  7. Boolean done;
  8. int round=0,frameNum=0;
  9. int lastImageTime;
  10.  
  11. void setup(){
  12.   map0=loadImage("map.png");//Black shape on whatever background; data/map.png
  13.   map0.loadPixels();
  14.   offscreen=createGraphics(map0.width,map0.height);
  15.   offscreen.noSmooth();
  16.   offscreen.beginDraw();
  17.   offscreen.colorMode(HSB,1);
  18.   offscreen.ellipseMode(RADIUS);
  19.   offscreen.endDraw();
  20. }
  21.  
  22. void draw(){
  23.   //Initialize districts
  24.   districts=new District[countDistricts];
  25.   for(int district=0;district<countDistricts;district++){
  26.     districts[district]=new District();
  27.   }
  28.   //Initialize dots
  29.   int countDotsImportant=0;
  30.   for(int dot=0;dot<map0.pixels.length;dot++){
  31.     if(map0.pixels[dot]==color(0,0,0)){
  32.       countDotsImportant++;
  33.     }
  34.   }
  35.   dots=new Dot[0];
  36.   for(int dot=0;dot<map0.pixels.length;dot++){
  37.     if(map0.pixels[dot]==color(0,0,0)){
  38.       //dots=(Dot[])append(dots,new Dot(dot%map0.width,dot/map0.width,dots.length%countDistricts));
  39.       if(dots.length<countDotsImportant/countDistricts*countDistricts){
  40.         dots=(Dot[])append(dots,new Dot(dot%map0.width,dot/map0.width,dots.length/(countDotsImportant/countDistricts)));
  41.       }else{
  42.         dots=(Dot[])append(dots,new Dot(dot%map0.width,dot/map0.width,dots.length%countDistricts));
  43.       }
  44.     }
  45.   }
  46.   for(int district=0;district<districts.length;district++){
  47.     println("District "+district+" contains "+districts[district].countDots+" dots.");
  48.   }
  49.   recalc();
  50.   imageDraw();
  51.   //Main loop
  52.   int dot1,dot2;
  53.   int fails=0;
  54.   do{
  55.     round++;
  56.     do{
  57.       dot1=int(random(dots.length));
  58.       dot2=int(random(dots.length));
  59.     }while(dots[dot1].district==dots[dot2].district);
  60.     if(/*dots[dot1].r>districts[dots[dot1].district].r&&dots[dot2].r>districts[dots[dot2].district].r&&*/(dots[dot1].r+dots[dot2].r)/(dots[dot1].rMaybe(dots[dot2].district)+dots[dot2].rMaybe(dots[dot1].district))>1){
  61.       dots[dot1].flipWith(dot2);
  62.       recalc();
  63.       fails=0;
  64.     }else{
  65.       fails++;
  66.     }
  67.     if(millis()-lastImageTime>10*1000||fails>pow(dots.length,1)*pow(districts.length,2)){
  68.       imageDraw();
  69.     }
  70.     if(fails>pow(dots.length,1)*pow(districts.length,2)){
  71.       break;
  72.     }
  73.   }while(true);
  74.   exit();
  75. }
  76.  
  77. class Dot{
  78.   int x,y;//Coordinates
  79.   int district;//To which this dot belongs
  80.   float r;//From center of district
  81.   Dot(int input0,int input1,int input2){
  82.     x=input0;
  83.     y=input1;
  84.     district=input2;
  85.     r=-1;
  86.     districts[input2].countDots++;
  87.   }
  88.   float rMaybe(int input0){
  89.     return dist(x,y,districts[input0].x,districts[input0].y);
  90.   }
  91.   void flipWith(int input0){
  92.     int temp=dots[input0].district;
  93.     dots[input0].district=district;
  94.     district=temp;
  95.   }
  96. }
  97.  
  98. class District{
  99.   float x,y;//Coordinates of center
  100.   float r;//Average for all dots
  101.   int countDots;//Number of dots belonging to this district
  102.   District(){
  103.     x=0;
  104.     y=0;
  105.     r=0;
  106.     countDots=0;
  107.   }
  108. }
  109. void recalc(){
  110.   //Calculate district centers, then dot radii, and finally district radii
  111.   for(int dot=0;dot<dots.length;dot++){
  112.     districts[dots[dot].district].x+=dots[dot].x;
  113.     districts[dots[dot].district].y+=dots[dot].y;
  114.   }
  115.   for(int district=0;district<districts.length;district++){
  116.     districts[district].x/=(float)districts[district].countDots;
  117.     districts[district].y/=(float)districts[district].countDots;
  118.   }
  119.   for(int dot=0;dot<dots.length;dot++){
  120.     dots[dot].r=dist(dots[dot].x,dots[dot].y,districts[dots[dot].district].x,districts[dots[dot].district].y);
  121.     districts[dots[dot].district].r+=dots[dot].r;
  122.   }
  123.   for(int district=0;district<districts.length;district++){
  124.     districts[district].r/=(float)districts[district].countDots;
  125.   }
  126. }
  127. void imageDraw(){
  128.   offscreen.beginDraw();
  129.   offscreen.noStroke();
  130.   offscreen.fill(0,0,0);
  131.   offscreen.rect(0,0,offscreen.width,offscreen.height);
  132.   for(int dot=0;dot<dots.length;dot++){
  133.     offscreen.fill((float)dots[dot].district/districts.length,1,1);
  134.     offscreen.rect(dots[dot].x,dots[dot].y,1,1);
  135.   }
  136.   offscreen.stroke(0,0,1);
  137.   offscreen.strokeWeight(1);
  138.   offscreen.noFill();
  139.   for(int district=0;district<districts.length;district++){
  140.     offscreen.ellipse(districts[district].x,districts[district].y,districts[district].r,districts[district].r);
  141.     offscreen.point(round(districts[district].x),round(districts[district].y));
  142.   }
  143.   offscreen.save("output/"+frameNum+".png");
  144.   frameNum++;
  145.   offscreen.endDraw();
  146.   lastImageTime=millis();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement