Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Line crossing algorithm?

  1. Oct 1, 2011 #1
    Hello, there's this problem about line crossings I saw somewhere and was trying to solve.

    There's a 64x64 grid of 8 bit pixels and it has a bunch of 1 pixel wide vertical and horizontal lines of different colors on it. All parallel lines have at least one space between them. The problem is to find the number of line crossings that each set of colored lines makes (e.g. all green line crossings count towards one sum). And find which set of lines had the fewest amount of crossings. Also, all vertical lines of a color are the same size and all horizontal lines of the same color are the same size.

    I had a few ideas but they all seem pretty inefficient. It would involve going through every pixel in the grid, if you run into a color determine if it's a vertical or horizontal line, and then go in the direction of the line all the while checking adjacent sides for different colors.

    I'm trying to decide if first counting the length of the horizontal and vertical lines for each color would speed up the process. DO you guys have any brilliant ideas for how to do this?
  2. jcsd
  3. Oct 2, 2011 #2
    I would use a mask to first find out what the lines are, and then a second mask to find the intersections. That would give a O(2n) algorithm, which isn't nice, but at least linear. I doubt you'll find anything faster, though you can probably get rid of the 2 factor by merging the two approaches.

    (This is with the assumption that it is mostly a simple problem with vertical and horizontal lines and other space only.)
  4. Oct 2, 2011 #3
    Can you give me an example of these masks? The only kind of masking I know of is bit masking and I'm not following how to use them here. Thanks for the idea, though!

    EDIT: By the way, there are only 7 different colors.
    Last edited: Oct 2, 2011
  5. Oct 3, 2011 #4
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook