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
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
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).
I could just iterate over all cells and lines while in a cell (gray) but this would be inefficient.
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);
}
}