How to resolve the algorithm Find the intersection of two lines step by step in the Processing programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Find the intersection of two lines step by step in the Processing programming language
Table of Contents
Problem Statement
Find the point of intersection of two lines in 2D.
The 1st line passes though (4,0) and (6,10) . The 2nd line passes though (0,3) and (10,7) .
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the Processing programming language
Source code in the processing programming language
void setup() {
// test lineIntersect() with visual and textual output
float lineA[] = {4, 0, 6, 10}; // try 4, 0, 6, 4
float lineB[] = {0, 3, 10, 7}; // for non intersecting test
PVector pt = lineInstersect(lineA[0], lineA[1], lineA[2], lineA[3],
lineB[0], lineB[1], lineB[2], lineB[3]);
scale(9);
line(lineA[0], lineA[1], lineA[2], lineA[3]);
line(lineB[0], lineB[1], lineB[2], lineB[3]);
if (pt != null) {
stroke(255);
point(pt.x, pt.y);
println(pt.x, pt.y);
} else {
println("No point");
}
}
PVector lineInstersect(float Ax1, float Ay1, float Ax2, float Ay2,
float Bx1, float By1, float Bx2, float By2) {
// returns null if there is no intersection
float uA, uB;
float d = ((By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1));
if (d != 0) {
uA = ((Bx2 - Bx1) * (Ay1 - By1) - (By2 - By1) * (Ax1 - Bx1)) / d;
uB = ((Ax2 - Ax1) * (Ay1 - By1) - (Ay2 - Ay1) * (Ax1 - Bx1)) / d;
} else {
return null;
}
if (0 > uA || uA > 1 || 0 > uB || uB > 1) {
return null;
}
float x = Ax1 + uA * (Ax2 - Ax1);
float y = Ay1 + uA * (Ay2 - Ay1);
return new PVector(x, y);
}
from __future__ import division
def setup():
""" test line_intersect() with visual and textual output """
(a, b), (c, d) = (4, 0), (6, 10) # try (4, 0), (6, 4)
(e, f), (g, h) = (0, 3), (10, 7) # for non intersecting test
pt = line_instersect(a, b, c, d, e, f, g, h)
scale(9)
line(a, b, c, d)
line(e, f, g, h)
if pt:
x, y = pt
stroke(255)
point(x, y)
println(pt) # prints x, y coordinates or 'None'
def line_instersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
""" returns a (x, y) tuple or None if there is no intersection """
d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
if d:
uA = ((Bx2 - Bx1) * (Ay1 - By1) - (By2 - By1) * (Ax1 - Bx1)) / d
uB = ((Ax2 - Ax1) * (Ay1 - By1) - (Ay2 - Ay1) * (Ax1 - Bx1)) / d
else:
return
if not(0 <= uA <= 1 and 0 <= uB <= 1):
return
x = Ax1 + uA * (Ax2 - Ax1)
y = Ay1 + uA * (Ay2 - Ay1)
return x, y
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Ursala programming language
You may also check:How to resolve the algorithm HTTP step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the BCPL programming language
You may also check:How to resolve the algorithm Smarandache-Wellin primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the SAS programming language