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));
  }
}

2009年7月10日 星期五

dictionary appendix to MMSEG


appendix to the mmseg Chinese word segmentation tool
http://sites.google.com/site/sekewei/home/download/dictionary.zip
---
readme.txt

filelist:
getdic.c source to retrieve dictionary in use
getdic.exe binary to retrieve dictionary in use
gcc -o getdic -I../src getdic.c ../src/lexicon.c

mmsegmnt.c source to segment a chunk in simple mode
getchunk.c source to get a chunk for segmenting
mmseg.exe binary to segment words
gcc -o mmseg -I. mmseg.c search.c
segment.c b5char.c iofiles.c
lexicon.c message.c
..\dictionary\getchunk.c
..\dictionary\mmsegmnt.c
mmseg inp.txt out.txt ../lexicon/ complex verbose
usage: mmseg in out lexicon [complex|simple*] [verbose|standard|quiet*]
default options are simple and quiet

mklex1.c source to build dictionary tables CHR?.TXT
mklex1.exe binary to build dictionary tables CHR?.TXT
gcc -o mklex1 -I. ..\dictionary\mklex1.c
mklex1 TSAIWORD.TXT

mklex2.c source to build dictionary tables CHR?.LEX
mklex2.exe binary to build dictionary tables CHR?.LEX
gcc -o mklex2 -I. ..\dictionary\mklex2.c
mklex2 .

mklex3.c source to build dictionary tables CHR?.INX
mklex3.exe binary to build dictionary tables CHR?.INX
gcc -o mklex3 -I. ..\dictionary\mklex3.c
mklex3 .

mmsegwords_raw.txt retrieved dictionary in use (137546 items)
mmsegwords.txt revised dictionary (137555 items)
mmsegwords_sort.txt revised dictionary sorted (137555 items)
TSAIWORD.TXT separate sorted dictionary by Tsai (137425 items)
diff.txt difference between mmsegwords_sort.txt
and TSAIWORD.TXT