Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Problem 2218. Wayfinding 1 - crossing

Created by J-G van der Toorn

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.

Problem Group

Solution Statistics

5 correct solutions 3 incorrect solutions
Last solution submitted on Mar 15, 2014