Login  Register

Re: draw lines in image

Posted by DwightBart on Jun 15, 2015; 8:11pm
URL: http://imagej.273.s1.nabble.com/draw-lines-in-image-tp5004213p5013174.html

Recently, I had to write code in an ImageJ Macro to draw a segmented line on an image.  The coordinates for the segments of the line were stored in two arrays (Segement_x[] and Segment_y[]) and there were NumberOfSegmentPoints in the arrays.

I wanted to use the macro command makeLine, put all of the segment points in a string, and execute it all at once.  After some experimentation, the following code did the trick:
//*******************************
// Draw a segmented line from arrays
if( NumberOfSegmentPoints>1 ) {
    AStr = "makeLine(" + Segment_x[0] + "," + Segment_y[0];
    for( i=1; i<NumberOfSegmentPoints; i++) {
        AStr = AStr + "," + Segment_x[i] + "," + Segment_y[i];
    }
    AStr = AStr + ")";
    eval(AStr);
}
//*******************************

The big trick was using the <b>eval macro command to interpret the string.