Efficient Algorithms for Counting Line Crossings on a Grid

  • Thread starter Thread starter Bob Busby
  • Start date Start date
  • Tags Tags
    Algorithm Line
AI Thread Summary
The discussion focuses on an algorithmic problem involving counting line crossings on a 64x64 grid populated with 1-pixel wide vertical and horizontal lines of seven different colors. The goal is to determine the number of crossings for each color set and identify which set has the fewest crossings. The initial approach considered is inefficient, involving pixel-by-pixel checks, but there is a suggestion to first count the lengths of the lines by color to potentially streamline the process. The use of masks to identify lines and intersections is proposed, aiming for a linear time complexity. Resources such as a Java method for line intersection and a book on computational geometry are shared for further reference.
Bob Busby
Messages
44
Reaction score
0
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?
 
Technology news on Phys.org
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.)
 
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top