## Posts Tagged ‘**vector graphics**’

## c++ check if 2d line segments cross

very simple, posting it here is more or less needless. you can check which side of an infinite line a point is on using the cross product. so check to see if the points of segment A are on different sides of the line defined by segment B. if they are, check to see if the points of segment B are on different sides of the line defined by segment A.

this alg isn’t particularly concerned about if the segments are on the same line 🙂

bool segmentscross(point2 point00, point2 point01, point2 point10, point2 point11) {

bool check0 = 0;

bool check1 = 0;

point2 param00 = point00 – point10; point2 param01 = point01 – point10; point2 param11 = point11 – point10;

if (param00.cross(param11) > 0) check0 = 1;

if (param01.cross(param11) > 0) check1 = 1;

if (check0 == check1) return 0;

check0 = check1 = 0;

param00 = point10 – point00; param01 = point11 – point00; param11 = point01 – point00;

if (param00.cross(param11) > 0) check0 = 1;

if (param01.cross(param11) > 0) check1 = 1;

if (check0 == check1) return 0;

return 1;

}