Is there any method for calculating the intersection point of 2 segments 2 segments. Let say if they intersect at the point I, how can I calculate the coordinate for the point I, given that I already had the coordinates of points A, B, C, D.
What I have tried:
I tried this method: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
Here is the result I got: my result. As you can see, there are some corners at which 2 endpoints do not match, then the method above cannot calculate the corners.
This is the equation I use to find line intersections. Note that these are lines, not line segments, so it'll connect even if the line segments don't extend all the way there.
This function is looking for two lines in point-slope form (a 2 element list of [slope, (x,y)]). It returns the intersection point [x,y] or None if the lines are parallel.
# calculates the intersection of two lines (in point-slope form) [slope, point]
# equation y = m(x - a) + b
# y = mx - ma + b
# where (a,b) is the point and m is the slope
def intersect(l1, l2):
# pull vars
m1 = l1[0];
m2 = l2[0];
a1, b1 = l1[1];
a2, b2 = l2[1];
# get 'x'
denom = m2 - m1;
if denom == 0:
return None; # lines are parallel
numer = m2*a2 - m1*a1 - b2 + b1;
x = numer / denom;
# get 'y'
y = m1 * (x - a1) + b1;
return [x, y];