//  This is a tool macro similar to the spray can tool in NIH Image.
//  Double-click on the tool icon to set the spray width, dot size
//  and flow rate.

  var sprayCanWidth=75, sprayCanDotSize=1, sprayCanRate=6;

   macro 'Spray Can Tool-C037D20D22D24D41D43D62D82Da2L93b3D84Dc4L75d5L757f Ld5dfLa7d7LabdbLa9d9LacdcLa7ac' {
         setLineWidth(sprayCanDotSize);
         radius=sprayCanWidth/2; radius2=radius*radius;
         start = getTime();
         autoUpdate(false);
         n = 5*exp(0.9*(10-sprayCanRate));
         if (n<=5) n = 0;
         while (true) {
            getCursorLoc(x, y, z, flags);
            if (flags&16==0) exit();
            x2 = (random()-0.5)*sprayCanWidth;
            y2 = (random()-0.5)*sprayCanWidth;
            if (x2*x2+y2*y2<radius2) {
                x += x2;
                y += y2;
                drawLine(x, y, x, y);
                if (getTime()>start+50) {
                    updateDisplay();
                    start = getTime();
                 }
            }
          
            for (i=0; i<n; i++);
        }
   }

  // ImageJ runs this macro when user double-clicks on the spay can tool icon
  macro 'Spray Can Tool Options' {
    sprayCanWidth = getNumber("Spray Width (pixels):", sprayCanWidth);
    sprayCanDotSize = getNumber("Dot Size (pixels):", sprayCanDotSize);
    sprayCanRate = getNumber("Flow Rate (1-10):", sprayCanRate);
    if (sprayCanRate<1) sprayCanRate = 1;
    if (sprayCanRate>10) sprayCanRate = 10;
 }

