Re: Straight line with preset length macro?

Posted by Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Straight-line-with-preset-length-macro-tp5014526p5014543.html

Hi Alvin and everyone,

Wayne has improved the macro and I have added a few more bells & whistles.
Now one can also have a line where only the maximum length is fixed or a
centered line (see the options dialog).

Beware of possible additional line breaks introduced by the mailer.

Michael
__________________________________________________________________________

// Fixed Length Line Tool
// This ImageJ tool macro allows for drawing a line with a given
// fixed length or maximum length.
// One can also draw a line centered at the point where the
// mouse button was pressed.
// Hold the shift key to constrain the direction.
//
// Authors: Wayne Rasband & Michael Schmid, 2015-Oct-04

var desiredLength = 500; //in scaled units
var isMaxlength = false; //not fixed, only maximum line length
var isCentered = false;

macro "Fixed Length Line Tool - C00bL2ae4L2be5C888L083eB10Lc1f7" {
   leftClick = 16;
   shift = 1;
   getPixelSize(unit, pixelWidth, pixelHeight);
   getCursorLoc(x0, y0, z, flags);
   lastX = x0;
   lastY = y0;
   while (true) {
      getCursorLoc(x, y, z, flags);
      if (flags&leftClick == 0) exit();
      if (x==lastX && y==lastY)    //no change, don't update live profile
etc.
         wait(10);
      else {
         lastX = x;
         lastY = y;
         dx = (x - x0);
         dy = (y - y0);
         if (flags&shift != 0) {
            angle = atan2(y - y0, x - x0);
            angle = PI/4*round(4*angle/PI);
            len = maxOf(abs(dx), abs(dy));
            dx = len*cos(angle);
            dy = len*sin(angle);
         }
         dxs = dx * pixelWidth;    //in scaled units
         dys = dy * pixelHeight;
         length = sqrt(dxs*dxs + dys*dys);
         if (isCentered)
            length = 2*length;
         enlageFactor = desiredLength/length;
         if (isMaxlength && enlageFactor > 1)
            enlageFactor = 1;
         if (isCentered) {
            xS = x0 - dx*enlageFactor;
            yS = y0 - dy*enlageFactor;
         } else {
            xS = x0;
            yS = y0;
         }
         x = x0 + dx*enlageFactor;
         y = y0 + dy*enlageFactor;
         makeLine(xS, yS, x, y);
      }
   }
}

macro "Fixed Length Line Tool Options" {
   getPixelSize(unit, pixelWidth, pixelHeight);
   if (unit=="pixel") unit="pixels";
   Dialog.create("Fixed Length Line Tool Options");
   Dialog.addNumber("Desired line length", desiredLength, 2, 10, unit);
   Dialog.addCheckbox("This length is maximum", isMaxlength);
   Dialog.addCheckbox("Centered", isCentered);
   Dialog.addMessage("Hold <shift> key to constrain directions");
   Dialog.show();
   desiredLength = Dialog.getNumber();
   isMaxlength = Dialog.getCheckbox();
   isCentered = Dialog.getCheckbox();
}


__________________________________________________________________________

On Fri, October 2, 2015 19:24, AlvinH wrote:

> Thank you Michael!
>
> That did the trick. I can draw anywhere on the image towards any direction
> and it's always 500 um in length.
>
> On another note, do you mind clarifying "*don't recalculate x, y if
> 'enlargeFactor' is more than 1*". What would I do for that? Do I delete
> certain part of the macro? I have little knowledge in coding.
>
> Thanks again.
>
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/Straight-line-with-preset-length-macro-tp5014526p5014533.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html