2009年7月21日 星期二

code for testing if two line segments are intersecting


/*
   Line.java

     Test if two line segments are intersecting or not
     Line segment 1 is between endpoints (x1,y1) and (x2,y2)
     Line segment 2 is between endpoints (u1,v1) and (u2,v2)

   Usage: java Line x1 y1 x2 y2 u1 v1 u2 v2

   > java Line
   java Line 0 1 1 0 0 0 1 1

   > java Line 0 1 1 0 0 0 1 1
    t * 1.000000 = 0.000000 + u * 1.000000
    t * -1.000000 = -1.000000 + u * 1.000000
    ---
    t * 1.000000 = 0.000000 + u * 1.000000
    t * -1.000000 = -1.000000 + u * 1.000000
    ---
    t = nom(1.000000) / denom(2.000000) = 0.500000
    u = nom(-1.000000) / denom(-2.000000) = 0.500000
   true
*/
public class Line
{
  //  line1:  0 <= t <= 1
  // x = ax1 + t * (ax2 - ax1)
  //    y = ay1 + t * (ay2 - ay1)

  //  line2:  0 <= u <= 1
  // x = bx1 + u * (bx2 - bx1)
  //    y = by1 + u * (by2 - by1)

  //  returns true if two lines intersect; and false otherwise

  public static boolean isTwoLinesIntersecting(
    double ax1, double ay1, double ax2, double ay2,
    double bx1, double by1, double bx2, double by2)
  {
    double nom, denom;
    double ax21 = ax2 - ax1;
    double bax1 = bx1 - ax1;
    double bx21 = bx2 - bx1;
    double ay21 = ay2 - ay1;
    double bay1 = by1 - ay1;
    double by21 = by2 - by1;

    // t * (ax2 - ax1) = (bx1 - ax1) + u * (bx2 - bx1)
    // t * (ay2 - ay1) = (by1 - ay1) + u * (by2 - by1)
     System.out.printf(" t * %f = %f + u * %f\n", ax21, bax1, bx21);
     System.out.printf(" t * %f = %f + u * %f\n", ay21, bay1, by21);
     System.out.printf(" ---\n");

    // ==>
    // t * (ax2 - ax1) * (by2 - by1) = (bx1 - ax1) * (by2 - by1) + u * (bx2 - bx1) * (by2 - by1)
    // t * (ay2 - ay1) * (bx2 - bx1) = (by1 - ay1) * (bx2 - bx1) + u * (by2 - by1) * (bx2 - bx1)
     System.out.printf(" t * %f = %f + u * %f\n", ax21*by21, bax1*by21, bx21*by21);
     System.out.printf(" t * %f = %f + u * %f\n", ay21*bx21, bay1*bx21, by21*bx21);
     System.out.printf(" ---\n");

    // ==>
    // t = (bx1 - ax1) * (by2 - by1) - (by1 - ay1) * (bx2 - bx1)
    //    / [ (ax2 - ax1) * (by2 - by1) - (ay2 - ay1) * (bx2 - bx1)]
    // u = (ax1 - bx1) * (ay2 - ay1) - (ay1 - by1) * (ax2 - ax1)
    //    / [ (bx2 - bx1) * (ay2 - ay1) - (by2 - by1) * (ax2 - ax1)]
    nom = (bx1 - ax1) * (by2 - by1) - (by1 - ay1) * (bx2 - bx1);
    denom = (ax2 - ax1) * (by2 - by1) - (ay2 - ay1) * (bx2 - bx1);
    double t = nom / denom;
     System.out.printf(" t = nom(%f) / denom(%f) = %f\n", nom, denom, t);

    nom = (ax1 - bx1) * (ay2 - ay1) - (ay1 - by1) * (ax2 - ax1);
    denom = (bx2 - bx1) * (ay2 - ay1) - (by2 - by1) * (ax2 - ax1);
    double u = nom / denom;
     System.out.printf(" u = nom(%f) / denom(%f) = %f\n", nom, denom, u);

    if(t>=0 && t <=1 && u>=0 && u<=1)  return true;
    else return false;
  }

  public static void main(String[] args)
  {
    if(args.length != 8)
    {
      System.out.println("java line 0 1 1 0 0 0 1 1");
      System.exit(0);
    }

    double ax1 = Double.parseDouble(args[0]);
    double ay1 = Double.parseDouble(args[1]);
    double ax2 = Double.parseDouble(args[2]);
    double ay2 = Double.parseDouble(args[3]);
    double bx1 = Double.parseDouble(args[4]);
    double by1 = Double.parseDouble(args[5]);
    double bx2 = Double.parseDouble(args[6]);
    double by2 = Double.parseDouble(args[7]);

    System.out.println(Line.isTwoLinesIntersecting(
      ax1,ay1,ax2,ay2,bx1,by1,bx2,by2));
  }
}

沒有留言: