This is the first part of a series of assignments about wayfinding. The final goal is to be able to calculate the fastest route through a terrain of areas with different properties. The assignments will build on top of each other, gradually increasing the complexity, but guiding you stepwise towards the final goal. You can re-use code from preceding assignments to save some work.
How many times does AB cross another line?
The first assignment deals with the problem of finding the lines we cross while going from A to B. The answer will be the number of times the segment AB intersects with the other lines. The other lines are isolated (or intersecting) line segments of two nodes each.
The inputs of the function WayfindingIntersections(AB,L) are a matrix AB of two columns, each with x-y coordinates, of our straight path from A (1st column) to B (2nd column), and a 3-dimensional matrix L of columns with x- and y-coordinates, each column either the start or the end of a line, and with all individual lines concatenated in the 3rd dimension.
AB = [ xA xB yA yB ]
L = cat(3,... [ x1_start x1_end y1_start y1_end ] ... ,... [ x2_start x2_end y2_start y2_end ] ... ,... [ x3_start x3_end y3_start y3_end ] ... % etc. )
Your output n will be the number of times the line AB intersects with any of the other lines. The lines will not 'just touch' AB with their begin or end.
p.s. I noticed later on that there is another Cody problem 1720 that is somewhat similar. But this was a logical start for the series.
The problem's figure is missing. We can use our solution for problem 1720 (https://nl.mathworks.com/matlabcentral/cody/problems/1720-do-the-lines-intersect). However, line matrices are transposed in this problem. And for the picture, we can imagine an instance of Buffon's needle problem without horizontal lines.
647 Solvers
Find common elements in matrix rows
1175 Solvers
Back to basics 8 - Matrix Diagonals
784 Solvers
Numbers with prime factors 2, 3 and 5.
161 Solvers
166 Solvers