Efficient Algorithms for Counting Line Crossings on a Grid

  • Thread starter Thread starter Bob Busby
  • Start date Start date
  • Tags Tags
    Algorithm Line
Click For Summary

Discussion Overview

The discussion revolves around the problem of counting line crossings on a 64x64 grid populated with 1 pixel wide vertical and horizontal lines of various colors. Participants explore different algorithmic approaches to efficiently determine the number of crossings for each color and identify which color has the fewest crossings.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant suggests that a brute-force approach would involve checking every pixel to identify line colors and orientations, which they find inefficient.
  • Another participant proposes using a masking technique to first identify the lines and then find intersections, suggesting this could lead to an O(2n) algorithm, although they express doubt about finding a faster solution.
  • A request for clarification on the masking technique is made, with a participant indicating their unfamiliarity with the concept beyond bit masking.
  • A reference to a specific Java method for line intersection is provided, along with a link to additional resources on computational geometry.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the most efficient method for counting line crossings, and multiple approaches are discussed without resolution.

Contextual Notes

There are assumptions about the simplicity of the problem, specifically regarding the presence of only vertical and horizontal lines and the limited number of colors. The efficiency of proposed methods may depend on these factors.

Who May Find This Useful

This discussion may be useful for individuals interested in algorithm design, computational geometry, or those working on similar grid-based problems in programming and mathematics.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K