Intersecting Lines



If you want to find the point of intersection of two lines
you set their equations to be equal and solve for X.

Below I have shared my step by step code for doing this in Processing.

The illustration is interactive, you can drag the endpoints..

NB: Interaction not supported on Internet Explorer!





// Calculating and illustrating the point of intersection between to lines
					
// Initial random coordinates for endpoints of the two lines AB and CD

	float xA = random(20, 940);
	float yA = random(20, 700);
	float xB = random(20, 940);
	float yB = random(20, 700);
	float xC = random(20, 940);
	float yC = random(20, 700);
	float xD = random(20, 940);
	float yD = random(20, 700);
						
// The intesection is calculated by setting the equation Y=n+dX of the two lines to be equal and isolate X
// In the following each step is solved separately and stored in a variable
// Inclination (delta) of the two lines
						
	float dAB;
	float dCD;

// Y-offset of the two lines

	float nAB;
	float nCD;

// Coordinates of point of intersection

	float xP;
	float yP;

// Crosshair parameters

	float crossSize = 20;
	float crossSpace = 10;

// Is an endpoint currently being moved

	boolean moveA = false;
	boolean moveB = false;
	boolean moveC = false;
	boolean moveD = false;

// Initialization

void setup() {
	size(960, 720);
	background(64);
}

// Main loop

void draw() {
	
	background(64);

	//If an endpoint is currently being moved follow the mouse

	if (moveA) {
		xA = mouseX;
		yA = mouseY;
	}

	if (moveB) {
		xB = mouseX;
		yB = mouseY;
	}

	if (moveC) {
		xC = mouseX;
		yC = mouseY;
	}

	if (moveD) {
		xD = mouseX;
		yD = mouseY;
	}

	// Inclination d is calculated by dividing the difference in Y by the differnce in X

	dAB = (yB-yA)/(xB-xA);
	dCD = (yD-yC)/(xD-xC);

	// Offset n is calculated by subtracting inclination multiplied by X from Y
	
	nAB = yA-dAB*xA;
	nCD = yC-dCD*xC;

	// Intersection xP is calculated by setting the two equations equal and isolate x (yCD=yAB <-> nCD+dCD*xCD = nAB+dAB*xAB)

	xP = (nCD-nAB) / (dAB-dCD);

	// Intersection yP is then calculated by solving either of the original equations

	yP = dAB*xP + nAB;

	// Graphics

	noFill();
	stroke(255, 64);
	line(xA, yA, xB, yB);
	stroke(255, 128);
	ellipse(xA, yA, 10, 10);
	ellipse(xB, yB, 10, 10);
	stroke(255, 64);
	line(xC, yC, xD, yD);
	stroke(255, 128);
	ellipse(xC, yC, 10, 10);
	ellipse(xD, yD, 10, 10);
	stroke(0, 64);
	line (xP, 0, xP, 1000);
	line (0, yP, 1000, yP);
	stroke(250, 128);
	ellipse(xP, yP, 20, 20);

	// Coordinate tags
	
	fill(180);
	String coordA = round(xA) + " / " + round(yA);
	text(coordA, xA+10, yA-10);
	String coordB = round(xB) + " / " + round(yB);
	text(coordB, xB+10, yB-10);
	String coordC = round(xC) + " / " + round(yC);
	text(coordC, xC+10, yC-10);
	String coordD = round(xD) + " / " + round(yD);
	text(coordD, xD+10, yD-10);
	fill(250);
	String coordP = round(xP) + " / " + round(yP);
	text(coordP, xP+20, yP-10);
	noFill();

	// Crosshair

	//  stroke(0, 128);
	//  line (mouseX-crossSize, mouseY, mouseX-crossSpace, mouseY);
	//  line (mouseX, mouseY-crossSize, mouseX, mouseY-crossSpace);
	//  line (mouseX+crossSpace, mouseY, mouseX+crossSize, mouseY);
	//  line (mouseX, mouseY+crossSpace, mouseX, mouseY+crossSize);
	
}

// End of main loop

// Input handling

void mousePressed() {

	// Checking if an endpoint is being clicked
	
	if (abs(mouseX-xA)<10 && abs(mouseY-yA)<10) {
		moveA = true;
	} 
	
	else if (abs(mouseX-xB)<10 && abs(mouseY-yB)<10) {
		moveB = true;
	}
													 
	else if (abs(mouseX-xC)<10 && abs(mouseY-yC)<10) {
		moveC = true;
	} 
	
	else if (abs(mouseX-xD)<10 && abs(mouseY-yD)<10) {
		moveD = true;
	}

}

void mouseReleased() {

	//Resetting the move state

	moveA = false;
	moveB = false;
	moveC = false;
	moveD = false;
}

// End of Code



< top

< back

next > Redshift