Problem 2218. Wayfinding 1 - crossing
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.
Solution Stats
Problem Comments
-
1 Comment
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.
Solution Comments
Show commentsProblem Recent Solvers22
Suggested Problems
-
Project Euler: Problem 7, Nth prime
1428 Solvers
-
Project Euler: Problem 9, Pythagorean numbers
1151 Solvers
-
"Low : High - Low : High - Turn around " -- Create a subindices vector
540 Solvers
-
361 Solvers
-
5522 Solvers
More from this Author31
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!