Exemplo n.º 1
0
int judge(Circle E,Segment S)//判断圆与线的交点
{
    double a,b,c,d,e,f,g,h,lex,ley,rix,riy;
    int i,j;
    i=0;
    j=0;
    a=sub(S.p1,S.p2);
    b = 2*((S.p1.x-E.p.x)*(S.p2.x-S.p1.x) + (S.p1.y-E.p.y)*(S.p2.y-S.p1.y));
    c = (S.p1.x-E.p.x)*(S.p1.x-E.p.x) + (S.p1.y-E.p.y)*(S.p1.y-E.p.y) - E.r*E.r;
    d = b*b - 4*a*c;
    //printf("d=%lf\n",d);
    if (d < 0)return 0;
    else if(a==0)return 0;
    else
    {
        lex= ((-b+sqrt(d)) / (2*a));
        rix= ((-b-sqrt(d)) / (2*a));
        if(lex==rix)
        {
            e=S.p1.x+(S.p2.x-S.p1.x)*lex;
            I0->x=e;
            f=S.p1.y+(S.p2.y-S.p1.y)*lex;
            I0->y=f;
            if(on_segment(S,*I0))return 1;
            else return 0;
        }
        else
        {
            e=S.p1.x+(S.p2.x-S.p1.x)*lex;
            I0->x=e;
            f=S.p1.y+(S.p2.y-S.p1.y)*lex;
            I0->y=f;
            g=S.p1.x+(S.p2.x-S.p1.x)*rix;
            I1->x=g;
            h=S.p1.y+(S.p2.y-S.p1.y)*rix;
            I1->y=h;
            if(on_segment(S,*I0))i=1;
            if(on_segment(S,*I1))j=1;
            if(i&&j)return 2;
            if(i&&!j)return 1;
            if(!i&&j)
            {
                *I0=*I1;
                return 1;
            }
            return 0;
        }
    }

}
Exemplo n.º 2
0
/* true se os segmentos ab e cd intercedem */
bool segments_intersect(point a, point b, point c, point d) {
	int d1 = direction(c, d, a);
	int d2 = direction(c, d, b);
	int d3 = direction(a, b, c);
	int d4 = direction(a, b, d);

	if(((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0))
			&& ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) return true;
	if(d1 == 0 && on_segment(c, d, a)) return true;
	if(d2 == 0 && on_segment(c, d, b)) return true;
	if(d3 == 0 && on_segment(a, b, c)) return true;
	if(d4 == 0 && on_segment(a, b, d)) return true;

	return false;
}
Exemplo n.º 3
0
bool intersect2D_2Segments(Segment s1,Segment s2)//求线的交点
{
    Vector  u,v,w;
    u.x= s1.p2.x-s1.p1.x;
    u.y= s1.p2.y-s1.p1.y;
    v.x= s2.p2.x-s2.p1.x;
    v.y= s2.p2.y-s2.p1.y;
    w.x= s1.p1.x-s2.p1.x;
    w.y= s1.p1.y-s2.p1.y;
    double  D = perp(v,u);
    if (fabs(D) < SMALL_NUM)// S1 and S2 are parallel
            return 0;               // they are NOT collinear
    double sI=perp(w,v)/D;
    I0->x=s1.p1.x+u.x*sI;
    I0->y=s1.p1.y+u.y*sI;
    if(on_segment(s1,*I0)&&on_segment(s2,*I0)) return 1;//判断交点是否在两条线上
    else return 0;
}
Exemplo n.º 4
0
bool intersect(Segment const & s1, Segment const & s2) {
	int d1 = direct(s2, s1.p1);
	int d2 = direct(s2, s1.p2);
	int d3 = direct(s1, s2.p1);
	int d4 = direct(s1, s2.p2);
	if (d1 * d2 == -1 && d3 * d4 == -1) {
		return true;
	} else if (d1 == 0 && on_segment(s2, s1.p1)) {
		return true;
	} else if (d2 == 0 && on_segment(s2, s1.p2)) {
		return true;
	} else if (d3 == 0 && on_segment(s1, s2.p1)) {
		return true;
	} else if (d4 == 0 && on_segment(s1, s2.p2)) {
		return true;
	} else {
		return false;
	}
}