Friday, 11 September 2015

Line-Line intersection test

I once did some programming that required mathematical line-line intersections, and it's really something that can't always be easily found ready-made. The mathematical formulae and "theory" are readily available, but are not easily converted to practical situations.

Below I have a Processing sketch that includes the function intersection which compares two lines. Copy/paste the text below into a processing sketch window and go. Everything else outside the function is just to facilitate the demo sketch. Moving the mouse pointer around moves the other line, whereas the other line moves randomly.

It's based on a very old program so it might not be up to scratch. My previous work also returned the intersection coordinate (what I was really after), so this is clearly a simpler case.

There's always the problem that two parallel lines, even if they overlap visibly, coordinate-wise they may not intersect mathematically. For example, lines 0,0-100,0 and 50,0-150,0 do not intersect. The program below does not solve this case. I've supposed that making multiple intersection tests with suitably "jiggled" coordinates might fix this problem for some practical purposes.

Not intersecting!


float [] xp=new float[8];
float [] yp=new float[8];
float [] xv=new float[8];
float [] yv=new float[8];

boolean intersection(float X1,float Y1,float X2,float Y2,float X3,float Y3,float X4,float Y4)

//Tests intersection between lines x1,y1-x2,y2 and x3,y3-x4,y4
//Not sure where the original formulas are from.

//The function has the practical problem that parallel lines do not intersect.
//This may be circumvented by "jiggling" the line coordinates just a bit and making two intersection tests, not implemented here

//My original function returned the intersection coordinate, this is a simplification

 float ua,ub,denom,numer;

//not sure if the following are needed really

 if(X3>X2&&X4>X2&&X3>X1&&X4>X1){return false;}
 if(X3<X2&&X4<X2&&X3<X1&&X4<X1){return false;}
 if(Y3>Y2&&Y4>Y2&&Y3>Y1&&Y4>Y1){return false;}
 if(Y3<Y2&&Y4<Y2&&Y3<Y1&&Y4<Y1){return false;}

// the proper calculations


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


 if(ua==0&&ub==0){return true;}
 if(ub>0&&ub<=1&&ua>0&&ua<=1){return true;}

 return false;

void draw_lines(float xa1,float ya1,float xa2,float ya2,float xb1,float yb1,float xb2,float yb2)


void setup()

void mousePressed()

void draw()
  for(int i=0;i<=7;i++){


No comments:

Post a Comment