0

I found this answer which says how to check if a circle intersects a line segment (https://math.stackexchange.com/a/103592/565258)

After some algebra I came up with that equation

$t^2 ((x_0-x_1)^2 + (y_0 - y_1)^2) + 2t((x_0-x_1)(x_1-c_x) + (y_0-y_1)(y_1-c_y)) + (x_1-c_x)^2 + (y_1-c_y)^2 - R^2 = 0$

where $(x_0, y_0)$ and $(x_1, y_1)$ are my line segment points, $(c_x, c_y)$ is the circle center point and $R$ is the circle radius

I have that input:
$L_1 = (-0.728324413, 0.380718231)$ -> (x0, y0)
$L_2 = (-0.953550815, 0.620288848)$ -> (x1, y1)
$C = (-0.129390717, 0.420000076)$
$R = 0.5$

Here is a graphical representation I did

enter image description here

Obviously there is no intersection so the $t$ in the equation must fall out of $[0; 1]$ range but actual values of $t$ I get are:
$t_0 = 0.0159721095;$
$t_1 = 0.00928559806;$

Something is wrong with my equation?

UPD
Here is the function I use for calculation. (I've hardcoded the values from the question for simplicity)

bool test()
{
    float x0 = -0.728324413f;
    float y0 = 0.380718231f;
    float x1 = -0.953550815f;
    float y1 = 0.620288848f;
    float cx = -0.129390717f;
    float cy = 0.420000076f;
    float radius = 0.5f;
    float radius_sqr = 0.5f * 0.5f;
float a = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);
float b = (x0 - x1) * (x1 - cx) + (y0 - y1) * (y1 - cy);
float c = (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy) - radius_sqr;
float d = b * b - 4.0f * a * c;

if (d >= 0.0f)
{
    float sqrt_d = sqrtf(d);

    float t1 = (-b + sqrt_d) / 2 * a;
    float t2 = (-b - sqrt_d) / 2 * a;

    if (t1 >= 0.0f && t1 <= 1.0f)
        return true;
    if (t2 >= 0.0f && t2 <= 1.0f)
        return true;
}
return false;

}

UPD #2

enter image description here

bool test()
{
    float x0 = 0.0993375778;
    float y0 = 0.255897522;
    float x1 = -0.294656277;
    float y1 = 0.880306243;
    float cx = -0.123999998;
    float cy = 0.414999962;
    float radius = 0.25;
    float radius_sqr = radius * radius;
float a = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);
float b = (x0 - x1) * (x1 - cx) + (y0 - y1) * (y1 - cy);
float c = (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy) - radius_sqr;
float d = b * b - 4.0f * a * c;

if (d >= 0.0f)
{
    float sqrt_d = sqrtf(d);

    float t1 = (-b + sqrt_d) / 2 * a;
    float t2 = (-b - sqrt_d) / 2 * a;

    printf("sqrt_d = %f, t1 = %f, t2 = %f\n", sqrt_d, t1, t2);

    if (t1 >= 0.0f && t1 <= 1.0f)
        return true;
    if (t2 >= 0.0f && t2 <= 1.0f)
        return true;
}

printf("d = %f\n", d);

return false;

}

result of d = -0.271311

IC_
  • 145
  • 1
    Calculate the points $,\left(tx_0+(1-t)x_1, ty_0+(1-t)y_1\right),$ for those two values of $,t,$. If they are not at distance $,R,$ from the center, then you made some mistake in the calculations you haven't shown. – dxiv Jun 17 '22 at 21:14
  • @dxiv calculations are done in the program, i just took the results out of there – IC_ Jun 17 '22 at 21:17
  • 3
    If the end result is wrong, then the calculation must go wrong somewhere, whether you do it by hand or in the code. You are the only one who can see all the calculations from beginning to end, so you are the only one who can debug this. – dxiv Jun 17 '22 at 21:19
  • If you provide your code, someone of leisure may check it. – aerile Jun 17 '22 at 21:31
  • @aerile i've updated the question – IC_ Jun 18 '22 at 08:09
  • @IC_ When I tried the code, I obtained $d=-0.148417$. – aerile Jun 18 '22 at 09:27
  • @aerile yeah, i'm sorry, the previous case started working after i made some fixes, but the opposite case stopped working as expected. Intersecting line segment leaves out a negative discriminant – IC_ Jun 18 '22 at 10:51
  • 2
    So, I found out that my code has two issues. b term has a multiplier of 2 which I've forgotten to add and expressions 2 / a should be enclosed in brackets. Thanks everyone for your help – IC_ Jun 18 '22 at 13:48

0 Answers0