-1

I have a math puzzle/question

In case of 3 x 3 gird, how can I determine which lines will intersect the center cell if we to connect two random points within two neighboring cells. Neighbor is defined by (grid_size - 1) / 2 = 1 where gird size is 3 or sqrt(cell count). For example, cells 1 and 3 are neighbors and the line will intersect center cell. Cells 0 and 8 are not as it is 2 cells away.

So, in case of 3 x 3 the lines drawn between points from the following cells will intersect center cell: 1-3, 1-5, 7-3, 7-5

3 x 3 grid

Now with 5x5 grid things get more complicated. Neighbor is considered 2 steps from center cell since (grid_size - 1) / 2 = 2 where gird size is 5 or sqrt(cell count). Lines from the following cells will intersect center cell:

2-11, 2-13, 7-10, 7-11, 7-13, 7-14, 17-10, 17-11, 17-13, 17-14, 22,11, 22-13, 6-13, 6-17, 6-18, 7-16, 7-17, 7-18, 8-11, 8-16, 8-17, 11-13, 11-18, 16-13, 1-13, 3-11, 5-17, 7-15, 7-19, 9-17, 11-23, 13-21

5 x 5 grid

edit for clarification:

While in a cell, I'm drawing lines from a point within the current cell to points in surrounding neighboring cells of 5 x 5 grid (pink). While in the cell I also need to account for any intersecting lines drawn between the neighboring cells (green).

enter image description here

I could just iterate over all cells and lines while in a cell (gray) but this would be inefficient.

enter image description here

Ideally, I would only draw/compute the lines that potentially intersect the cell.

part of fragment shader:

#define C 25 // cells in 5x5 grid
#define T 2  // shift size sqrt(C)-1
#define X 12 // center cell (C-1)/2
#define NP (2,11, 2,13, 7,10, 7,11, 7,13, 7,14, 17,10, 17,11, 17,13, 17,14, 22,11, 22,13, 6,13, 6,17, 6,18, 7,16, 7,17, 7,18, 8,11, 8,16, 8,17, 11,13, 11,18, 16,13, 1,13, 3,11, 5,17, 7,15, 7,19,  9,17, 11,23, 13,21)

// Cells
vec2 id = floor(st)+n;
st = fract(st)-.5;
vec2 p[C];
int i=0;
for(float y=-T; y<=T; y++) {
    for(float x=-T; x<=T; x++) {
        p[i++] = GetPos(id, vec2(x,y));
    }
}

// Lines
const int[] np = int[]NP;
for(int i=0; i<(C+np.length()); i++) {
    // draw grid center to neigboring cells
    if( i < C){
        m += line(p[X], p[i], st);
    // intersecting lines
    }else{
        int e = (i - C) * 2;
        m += line(p[np[e]], p[np[e+1]], st);
    }

}
shaderology
  • 599
  • 3
  • 7
  • 1
    In your first example, it is possible to pick points in cells `1` and `3` such that the line will intersect the center cell, but it is also possible to pick such points such that the line would not intersect. How do you make the difference ? – m.raynal Jun 21 '19 at 07:17
  • You really need to clarify your question. Are you asking if *there exist* points in the two cells where the line segment between them intersects *any point* in the central cell? Is the routine given the grid size and two cell numbers then returns True/False depending on this, or is it given only the cell size and it returns an array of all such pairs of cell numbers, or something else? Are you asking for an idea, an algorithm, or code? And so on. – Rory Daulton Jun 21 '19 at 15:48
  • Where does the lines start? – Luis Gurmendez Jun 21 '19 at 16:37
  • Hi Rory, edited original post for clarification – shaderology Jun 24 '19 at 05:14

1 Answers1

1

Just a quick thought: if the x coordinates of your two points both lie on the left side of the central square, the line does not intersect it. If both x-coordinates lie on the right side, they don't either, neither do they if both y coordinates are above the top or below the bottom. In all other cases, they intersect (?).

  • Ah yes,. I think I need to look at the cells as XY coordinates instead of a count number, then a rule/pattern seem to emerge. Thanks! – shaderology Jun 24 '19 at 09:39