Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Line intersection (See related posts)

the function gives the point where 2 lines intersect

float[] checkIntersection(float lineAx1,float lineAy1,float lineAx2,float lineAy2,float lineBx1,float lineBy1,float lineBx2,float lineBy2){
  float aM, bM, aB, bB, isX=0, isY=0;
  if((lineAx2-lineAx1)==0){
    isX=lineAx1;
    bM=(lineBy2-lineBy1)/(lineBx2-lineBx1);
    bB=lineBy2-bM*lineBx2;
    isY=bM*isX+bB;
  }
  else if((lineBx2-lineBx1)==0){
    isX=lineBx1;
    aM=(lineAy2-lineAy1)/(lineAx2-lineAx1);
    aB=lineAy2-aM*lineAx2;
    isY=aM*isX+aB;
  }
  else{
    aM=(lineAy2-lineAy1)/(lineAx2-lineAx1);
    bM=(lineBy2-lineBy1)/(lineBx2-lineBx1);
    aB=lineAy2-aM*lineAx2;
    bB=lineBy2-bM*lineBx2;
    isX=max(((bB-aB)/(aM-bM)),0);
    isY=aM*isX+aB;
  }
  float[] r={
    isX,isY  };
  return r; 
}

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts