这是一道比较简单的判断线是否相交的题。Line A 的起始点 (x1, y1) & 终点 (x2, y2) Line B 的起始点 (x1, y1) & 终点 (x2, y2) Line C 的起始点 (x1, y1) & 终点 (x2, y2) 有五种可能的关系:1. 三条线都不想gddbg/pbzdzmp>2.AB相交,C与它们稳重的毛巾3.BC相交,A与它们稳重的毛巾4.AC相交,B与它们稳重的毛巾5.ABC都相gddbg/pbzdzmp动听的自行车brbzdzm/pbzdzmp>在下手写之前,先理解一下可以用的方法请参考下面的文档,我觉得写得特别的好:http://hsfzxjy.github.io/the-simplest-way-to-find-out-if-two-segments-are-intersected/bool checkCross(line1,line2) { if(两条线平行且重叠) { return true; } if(两线相交) { // direct(AC, AD) != direct(BC, BD) 且 direct(CA, CB) != direct(DA, DB) return true; } return false;}具体的判断是判断方向,判断方向是点和线当向量叉积为0是判断是否重叠// if direct(a,b) is clockwise, 180<=<a,b><=360, sin<a,b> <= 0// while anticlockwise 0<=<a,b><=180, sin<a,b> >=0int checkDirect(Line l, Point p) { Vect v1 = makeVect(p,l.start); Vect v2 = makeVect(p,l.end); int direct = v1.x*v2.y - v2.x*v1.y; // cout the direct of v1 and v2 vector if(direct == 0) { if(checkInside(l,p)) { return 2; } else { return 0; } } else { return direct>0?1:-1; }}
重叠的判断方法:bool checkInside(Line l, Point p) { // check whether point inside the lineint lX = getMin(l.start.x, l.end.x);int rX = getMax(l.start.x, l.end.x);int lY = getMin(l.start.y, l.end.y);int rY = getMax(l.start.y, l.end.y);if(lX<=p.x && p.x<=rX && lY<=p.y && p.y<=rY) {return true;} else {return false;}}