Chapter 02-2: Calculate the distance between a point and a line |
Step 01: The function distance_line_point(line, point) |
function distance_line_point(line, point) {
// check for vertical line
if(line[0]==line[2]) { // both x-values are the same --> vertical line
distance = line[0]- point[0];
}
// check for horizontal line
if(line[1]==line[3]) { // both y-values are the same --> horizontal line
distance = line[1] - point[1];
}
// check for tilted line
if((line[0]!=line[2]) && (line[1]!=line[3])) { // tilted line --> x and y values of the start and endpoint are different
// calculate the gradient b = (y2-y1)/(x2-x1)
b = (line[3]-line[1])/(line[2]-line[0]);
// calculate the centre distance a=y-bx
a = line[1] - ( b * line[0]);
// calculate the angle of the 90° crossing line
angle_line90deg = (atan(b)*180/PI) + 90;
// calculate the gradient b2 of the 90° intersection line
angle_line90rad = angle_line90deg / (180/PI); // angle in rad
b2 = (tan(angle_line90rad));
// calculate the centre distance a2 of the 90° intersection line
// a2 = y_point - b2 * x_point;
a2 = point[1] - b2 * point[0];
// calculate the intersection of the two lines
ptx_intersection = (a2-a) / (b - b2);
pty_intersection = a + b * ptx_intersection;
// calculate the distance between the point and the intersection of the two lines
dx = point[0] - ptx_intersection;
dy = point[1] - pty_intersection;
distance = sqrt(dx*dx + dy*dy);
if(distance<0.00001) distance = 0;
}
return distance;
}
Step 02: Macro example with the distance_line_point function |
Some simple code sample for the imagej macro language. Open any image, copy the code into a new imagej text window and run the macro.
//+++++++++++ STEP 01 DECLARATIONEN +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function distance_line_point(line, point);
//+++++++++++ STEP 02 MAIN PROGRAM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
line = newArray(50,50,300,150);
makeLine(line[0],line[1],line[2],line[3]);
roiManager("Add");
point = newArray(150,185);
makePoint(point[0],point[1]);
roiManager("Add");
setOption("Show All", true);
dist_line_point = distance_line_point(line,point);
print(dist_line_point);
//+++++++++++ STEP 03 Function: distance_line_point ++++++++++++++++++++++++++++++++++++++++++++++
function distance_line_point(line, point) {
// check for vertical line
if(line[0]==line[2]) { // both x-values are the same --> vertical line
distance = line[0]- point[0];
}
// check for horizontal line
if(line[1]==line[3]) { // both y-values are the same --> horizontal line
distance = line[1] - point[1];
}
// else tilted line
if((line[0]!=line[2]) && (line[1]!=line[3])) { // tilted line --> x and y values of the start and endpoint are different
// calculate the gradient b = (y2-y1)/(x2-x1)
b = (line[3]-line[1])/(line[2]-line[0]);
// calculate the centre distance a=y-bx
a = line[1] - ( b * line[0]);
// calculate the angle of the 90° crossing line
angle_line90deg = (atan(b)*180/PI) + 90;
// calculate the gradient b2 of the 90° intersection line
angle_line90rad = angle_line90deg / (180/PI); // angle in rad
b2 = (tan(angle_line90rad));
// calculate the centre distance a2 of the 90° intersection line
// a2 = y_point - b2 * x_point;
a2 = point[1] - b2 * point[0];
// calculate the intersection of the two lines
ptx_intersection = (a2-a) / (b - b2);
pty_intersection = a + b * ptx_intersection;
// calculate the distance between the point and the intersection of the two lines
dx = point[0] - ptx_intersection;
dy = point[1] - pty_intersection;
distance = sqrt(dx*dx + dy*dy);
if(distance<0.00001) distance = 0;
}
return distance;
}
Step 03: Enjoy the results |
The Log window shows you the distance in pixels. For realworld measurements you have to calibrate the pixelsize. For that it is important to know what format your pixel on the sensor has. You have to find out, whether it is square or rectangular shaped. The function can be implemented very easy in measuring programms.