Circle - Segment intersection
#2

pawn Код:
/*
pA,pB - line segments
pC - circle center
radius - well...radius
*/

stock LineIntersectCircle(Float:pA[2], Float:pB[2], Float:pC[2], Float:radius)
{
    new Float: a = (pB[0]-pA[0])*(pB[0]-pA[0])+(pB[1]-pA[1])*(pB[1]-pA[1]);
    new Float: b = 2*((pB[0]-pA[0])*(pA[0]-pC[0])+(pB[1]-pA[1])*(pA[1]-pC[1]));
    new Float: c = pC[0]*pC[0]+pC[1]*pC[1]+pA[0]*pA[0]+pA[1]*pA[1]-2*(pC[0]*pA[0]+pC[1]*pA[1]) - radius*radius;
    new Float: konst = b*b-4*a*c;
    if(konst <= 0) return 0;
    else
    {
        new Float: e = floatsqroot(konst);
        new Float: u1 = (-b+e)/(2*a);
        new Float: u2 = (-b-e)/(2*a);
        if((u1>=0 || u1>=1) && (u2<=0 || u2<=1)) return 1;
    }
    return 0;
}
This should do it.
Reply


Messages In This Thread
Circle - Segment intersection - by PeppeAC - 01.07.2013, 10:04
Re: Circle - Segment intersection - by Antonio144 - 01.07.2013, 15:11
Re: Circle - Segment intersection - by PeppeAC - 01.07.2013, 15:31
Re: Circle - Segment intersection - by Antonio144 - 01.07.2013, 17:34
Re: Circle - Segment intersection - by PeppeAC - 01.07.2013, 17:48
Re: Circle - Segment intersection - by Antonio144 - 01.07.2013, 18:01
Re: Circle - Segment intersection - by PeppeAC - 01.07.2013, 18:27

Forum Jump:


Users browsing this thread: 1 Guest(s)