so I have x circles, given by X,Y coordinates + radius.
I'm unsuccessfully trying to figure out, how to make an algorithm for counting how many circles will line segment cross. For illustration: 1 (Green line crossed 2 circles, red line crossed 1)
I know the starting and ending points of line segments + circles as stated before. Tried to solve it with vector distance equation. The code i wrote was like this (Already googled answers):
for(int i = 0; i<d;i++)
{
for (int j = 0; j<s; j++)
{
int x1 = lines[i][0];
int x2 = lines[i][2];
int y1 = lines[i][1];
int y2 = lines[i][3];
double x21 = x2-x1;
double y12 = y1-y2;
double x12 = x1-x2;
double y21 = y2-y1;
double x0 = rooms[j][0];
double y0 = rooms[j][1];
double numerator = abs((x21*x0)+(y12*y0)+(x12*y1)+(y21*x1));
double enumerator = sqrt(pow(x21,2)+pow(y12,2));
double s = numerator /enumerator ;
double r = rooms[j][2];
if (s<=r) {
count++;
}
}
}
, but the code didn't work.
If we have a circle with centre at $C(x,y)$, radius $r$ and line determined by two points $A(x_1, y_1)$ and $B(x_2, y_2)$ we can determine if the line cross the circle using the following algorithm:
Python script: