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.
Make the vector [1 2 3 4 5 6 7 8 9 10]
43285 Solvers
Project Euler: Problem 2, Sum of even Fibonacci
742 Solvers
Calculate the area of a triangle between three points
457 Solvers
Find Index of maximum Value and maximum Value of a vector
141 Solvers
Square Digits Number Chain Terminal Value (Inspired by Project Euler Problem 92)
184 Solvers