This chapter shows how we can calculate the intersection of two crossing lines.
Step 01: The function intersection_line_line
The function takes as arguments two arrays, with the start and endpoints of the two lines. For example:
argument 1: line1 = newArray(start_x, start_y, end_x, end_y);
argument 2: line2 = newArray(start_x, start_y, end_x, end_y);
First the function checks whether the two lines are parallel (no intersection) or if the lines are on top of each other (infinite amount of intersection points). This is done by comparing the gradient of the first line with the gradient of the second line. And we also compare the negative gradient in case the line is 180° turned. In case the gradients match each other the function will return “NULL” as a string argument. Otherwise the function will calculate the X- and Y-value for the intersection point. The point will be returned in a simple array. The first entry in the array will be the X-value (x = point_intersection[0]) and the second entry will be the Y-value (y = point_intersection[1]).
// Funktion zur Berechnung eines Schnittpunktes zweier Linien
function intersection_line_line(line1, line2){
b_line1 = (line1[3]-line1[1])/(line1[2]-line1[0]); //Berechnen der Steigung Linie 1: b = (y2-y1)/(x2-x1)
b_line2 = (line2[3]-line2[1])/(line2[2]-line2[0]); //Berechnen der Steigung Linie 2: b = (y2-y1)/(x2-x1)
a_line1 = line1[1]-b_line1*line1[0]; //Berechnen des Achsenabschnittes a = y-bx
a_line2 = line2[1]-b_line2*line2[0]; //Berechnen des Achsenabschnittes a = y-bx
point_intersection = newArray(2); //Variable für den Schnittpunkt
if ((b_line1 == b_line2) || (b_line1 == b_line2*-1)){ //check for parallel lines. Both lines have the same absolut value for b(Steigung).
point_intersection[0] = "NULL"
point_intersection[1] = "NULL"
}
else {
point_intersection[0] = (a_line1-a_line2)/(b_line2-b_line1); //Berechnen X-Wert Schnittpunkt
point_intersection[1] = a_line1 +b_line1*point_intersection[0]; //Berechnen Y-Wert Schnittpunkt
}
return point_intersection;
}
Step 02: Example Macro to show the function intersection_line_line
Copy the code into a new ImageJ text window and run the macro by pressing the “Run Macro” command.
//===START MACRO====================================================================================
//------Funktion zur Berechnung eines Schnittpunktes zweier Linien-------------------------------------------------------------------------------------------
function intersection_line_line(line1, line2){
b_line1 = (line1[3]-line1[1])/(line1[2]-line1[0]); //Berechnen der Steigung Linie 1: b = (y2-y1)/(x2-x1)
b_line2 = (line2[3]-line2[1])/(line2[2]-line2[0]); //Berechnen der Steigung Linie 2: b = (y2-y1)/(x2-x1)
a_line1 = line1[1]-b_line1*line1[0]; //Berechnen des Achsenabschnittes a = y-bx
a_line2 = line2[1]-b_line2*line2[0]; //Berechnen des Achsenabschnittes a = y-bx
point_intersection = newArray(2); //Variable für den Schnittpunkt
if ((b_line1 == b_line2) || (b_line1 == b_line2*-1)){ //check for parallel lines. Both lines have the same absolut value for b(Steigung).
point_intersection[0] = "NULL"
point_intersection[1] = "NULL"
}
else {
point_intersection[0] = (a_line1-a_line2)/(b_line2-b_line1);
point_intersection[1] = a_line1 +b_line1*point_intersection[0];
}
return point_intersection;
}
//------End function------------------------------------------------------------------------------------------------------------------------------------------------------------
newImage("test function_intersection_lineline", "RGB", 400,400,1);
line1 = newArray(20, 20, 350, 350);
line2 = newArray(380,50,40,360);
point = intersection_line_line(line1, line2); //Run the function and catch the return values
// Show the Results in the image:
setColor(0,255,0); //Grün als Vordergrundfarbe
setLineWidth(1); //Linienbreite auf 1 einstellen
drawLine(20, 20, 350, 350); //Eingabelinie zeichnen
drawLine(380,50,40,360); //Eingabelinie zeichnen
//Schnittpunkt anzeigen
// to make it more fancy:
for (i=0; i<7; i++){
makePoint(point[0],point[1]);
wait(200);
run("Invert");
wait(200);
run("Invert");
}
makePoint(point[0],point[1]);
//===END MACRO=====================================================================================
Step 03: Show the Result: