示例#1
0
文件: 137-old.c 项目: noodles-v6/ACM
/* 0: lines don't intersect, 1:lines intersect, 2:lines are colinear */
int linesintersect(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,
   double *x,double *y)
{
   int a1,a2,b1,b2,c1,c2;
   int r1,r2,r3,r4;
   int denom,num;

   a1=y2-y1; b1=x1-x2; c1=x2*y1-x1*y2;
   r3=a1*x3+b1*y3+c1;
   r4=a1*x4+b1*y4+c1;
   if(r3!=0 && r4!=0 && SAME_SIGNS(r3,r4)) return 0;
   a2=y4-y3;
   b2=x3-x4;
   c2=x4*y3-x3*y4;
   r1=a2*x1+b2*y1+c2;
   r2=a2*x2+b2*y2+c2;
   if(r1!=0 && r2!=0 && SAME_SIGNS(r1,r2)) return 0;
   denom=a1*b2-a2*b1;
   if(denom==0) return 2;
   num=b1*c2-b2*c1;
   *x=(double)num/denom;
   num=a2*c1-a1*c2;
   *y=(double)num/denom;
   return 1;
}
示例#2
0
int intersection_test(point A, point B, point C, point D,
		      point *INTERSECTION)
{
    long a1, b1, c1;
    long a2, b2, c2;
    long r1, r2, r3, r4;
    long denom, offset, num;


    /* Compute a1, b1, c1, where line joining points 1 and 2
     * is "a1 x  +  b1 y  +  c1  =  0".
     */

    a1 = B.y - A.y;
    b1 = A.x - B.x;
    c1 = B.x * A.y - A.x * B.y;

    /* Compute r3 and r4.
     */
    r3 = a1 * C.x + b1 * C.y + c1;
    r4 = a1 * D.x + b1 * D.y + c1;

    /* Check signs of r3 and r4.  If both point 3 and point 4 lie on
     * same side of line 1, the line segments do not intersect.
     */

    if ( r3 != 0 &&
         r4 != 0 &&
         SAME_SIGNS( r3, r4 ))
        return 0;

    /* Compute a2, b2, c2 */

    a2 = D.y - C.y;
    b2 = C.x - D.x;
    c2 = D.x * C.y - C.x * D.y;

    /* Compute r1 and r2 */

    r1 = a2 * A.x + b2 * A.y + c2;
    r2 = a2 * B.x + b2 * B.y + c2;

    /* Check signs of r1 and r2.  If both point 1 and point 2 lie
     * on same side of second line segment, the line segments do
     * not intersect.
     */

    if ( r1 != 0 &&
         r2 != 0 &&
         SAME_SIGNS( r1, r2 ))
        return 0;

    /* Line segments intersect: compute intersection point.
     */

    denom = a1 * b2 - a2 * b1;
    if ( denom == 0 )
        return 0;
    offset = denom < 0 ? - denom / 2 : denom / 2;

    /* The denom/2 is to get rounding instead of truncating.  It
     * is added or subtracted to the numerator, depending upon the
     * sign of the numerator.
     */

    num = b1 * c2 - b2 * c1;
    INTERSECTION->x = ( num < 0 ? num - offset : num + offset ) / denom;

    num = a2 * c1 - a1 * c2;
    INTERSECTION->y = ( num < 0 ? num - offset : num + offset ) / denom;

    return 1;
}
示例#3
0
/*!
   \brief Line intersect

   Author: Mukesh Prasad
   Modified for floating point: Bill Brown

   This function computes whether two line segments,
   respectively joining the input points (x1,y1) -- (x2,y2)
   and the input points (x3,y3) -- (x4,y4) intersect.
   If the lines intersect, the output variables x, y are
   set to coordinates of the point of intersection.

   \param x1,y1,x2,y2 coordinates of endpoints of one segment
   \param x3,y3,x4,y4 coordinates of endpoints of other segment
   \param[out] x,y coordinates of intersection point

   \return 0 no intersection
   \return 1 intersect
   \return 2 collinear
 */
int segs_intersect(float x1, float y1, float x2, float y2, float x3, float y3,
		   float x4, float y4, float *x, float *y)
{
    float a1, a2, b1, b2, c1, c2;	/* Coefficients of line eqns. */
    float r1, r2, r3, r4;	/* 'Sign' values */
    float denom, offset, num;	/* Intermediate values */

    /* Compute a1, b1, c1, where line joining points 1 and 2
     * is "a1 x  +  b1 y  +  c1  =  0".
     */
    a1 = y2 - y1;
    b1 = x1 - x2;
    c1 = x2 * y1 - x1 * y2;

    /* Compute r3 and r4.
     */
    r3 = a1 * x3 + b1 * y3 + c1;
    r4 = a1 * x4 + b1 * y4 + c1;

    /* Check signs of r3 and r4.  If both point 3 and point 4 lie on
     * same side of line 1, the line segments do not intersect.
     */

    if (!EQUAL(r3, 0.0) && !EQUAL(r4, 0.0) && SAME_SIGNS(r3, r4)) {
	return (DONT_INTERSECT);
    }

    /* Compute a2, b2, c2 */
    a2 = y4 - y3;
    b2 = x3 - x4;
    c2 = x4 * y3 - x3 * y4;

    /* Compute r1 and r2 */
    r1 = a2 * x1 + b2 * y1 + c2;
    r2 = a2 * x2 + b2 * y2 + c2;

    /* Check signs of r1 and r2.  If both point 1 and point 2 lie
     * on same side of second line segment, the line segments do
     * not intersect.
     */

    if (!EQUAL(r1, 0.0) && !EQUAL(r2, 0.0) && SAME_SIGNS(r1, r2)) {
	return (DONT_INTERSECT);
    }

    /* Line segments intersect: compute intersection point. 
     */
    denom = a1 * b2 - a2 * b1;

    if (denom == 0) {
	return (COLLINEAR);
    }

    offset = denom < 0 ? -denom / 2 : denom / 2;

    /* The denom/2 is to get rounding instead of truncating.  It
     * is added or subtracted to the numerator, depending upon the
     * sign of the numerator.
     */
    num = b1 * c2 - b2 * c1;

    *x = num / denom;

    num = a2 * c1 - a1 * c2;
    *y = num / denom;

    return (DO_INTERSECT);
}
示例#4
0
int lines_intersect( 
	CPoint one, CPoint two,				  /* First line segment */
	CPoint three, CPoint four,			  /* Second line segment */
	CPoint &result)
{
    long a1, a2, b1, b2, c1, c2; /* Coefficients of line eqns. */
    long r1, r2, r3, r4;         /* 'Sign' values */
    long denom, offset, num;     /* Intermediate values */

    /* Compute a1, b1, c1, where line joining points 1 and 2
     * is "a1 x  +  b1 y  +  c1  =  0".
     */

    a1 = two.y - one.y; //y2 - y1;
    b1 = one.x - two.x; //x1 - x2;
    c1 = two.x * one.y - one.x * two.y; //x2 * y1 - x1 * y2;

    /* Compute r3 and r4.
     */


    r3 = a1 * three.x + b1 * three.y + c1;  //a1 * x3 + b1 * y3 + c1;
    r4 = a1 * four.x + b1 * four.y + c1;			//a1 * x4 + b1 * y4 + c1;

    /* Check signs of r3 and r4.  If both point 3 and point 4 lie on
     * same side of line 1, the line segments do not intersect.
     */

    if ( r3 != 0 &&
         r4 != 0 &&
         SAME_SIGNS( r3, r4 ))
        return ( DONT_INTERSECT );

    /* Compute a2, b2, c2 */

    a2 = four.y - three.y; //y4 - y3;
    b2 = three.x - four.x; //x3 - x4;
    c2 = four.x * three.y - three.x * four.y; //x4 * y3 - x3 * y4;

    /* Compute r1 and r2 */

    r1 = a2 * one.x + b2 * one.y + c2; //a2 * x1 + b2 * y1 + c2;
    r2 = a2 * two.x + b2 * two.y + c2; //a2 * x2 + b2 * y2 + c2;

    /* Check signs of r1 and r2.  If both point 1 and point 2 lie
     * on same side of second line segment, the line segments do
     * not intersect.
     */

    if ( r1 != 0 &&
         r2 != 0 &&
         SAME_SIGNS( r1, r2 ))
        return ( DONT_INTERSECT );

    /* Line segments intersect: compute intersection point. 
     */

    denom = a1 * b2 - a2 * b1;
    if ( denom == 0 )
        return ( COLLINEAR );
    offset = denom < 0 ? - denom / 2 : denom / 2;

    /* The denom/2 is to get rounding instead of truncating.  It
     * is added or subtracted to the numerator, depending upon the
     * sign of the numerator.
     */

    num = b1 * c2 - b2 * c1;
    result.x = ( num < 0 ? num - offset : num + offset ) / denom;

    num = a2 * c1 - a1 * c2;
    result.y = ( num < 0 ? num - offset : num + offset ) / denom;

    return ( DO_INTERSECT );
    } /* lines_intersect */