Algorithm one #include #include #include struct point {int x; int y;}; BOOL Islinesegmentcross (Point pFirst1, Spot PFirst2, points pSecond1, dot pSecond2) {////each segment's two dots are on the left and right sides of another segment, then it is possible to conclude that the segments intersect/ /formula for Vector (X1,Y1)-(X2,Y2), the Judgment Point (X3,y3) on the left side of the vector, on the right, or on the line. P=x1 (y3-y2) +x2 (y1-y3) +x3 (y2-y1). P<0 left, p=0 line, p>0 right long linep1,linep2; Determine if PSecond1 and pSecond2 are on pfirst1->pfirst2 sides LINEP1 = pfirst1.x * (PSECOND1.Y-PFIRST2.Y) + pfirst2.x * (pfirst1.y-pse COND1.Y) + psecond1.x * (PFIRST2.Y-PFIRST1.Y); LINEP2 = pfirst1.x * (PSECOND2.Y-PFIRST2.Y) + pfirst2.x * (PFIRST1.Y-PSECOND2.Y) + psecond2.x * (pfirst2.y-pfirst1.y ); if ((Linep1 ^ Linep2) >= 0) &&! ( Linep1==0 && linep2==0))//Sign bit XOR for 0:psecond1 and PSecond2 on Pfirst1->pfirst2 side {return false;}// Determine if PFirst1 and pFirst2 are on psecond1->psecond2 sides LINEP1 = psecond1.x * (pfirst1.y-psecond2.y) +psecond2.x * (PSECOND1.Y-PFI RST1.Y) + pfirst1.x * (PSECOND2.Y-PSECOND1.Y); LINEP2 = psecond1.x * (PFIRST2.Y-PSECOND2.Y) + psecond2.x * (PSECOND1.Y-PFIRST2.Y) + pfirst2.x * (PSECOND2.Y-PSECOND1.Y); if ((Linep1 ^ Linep2) >= 0) &&! ( Linep1==0 && linep2==0))//Sign bit XOR for 0:pfirst1 and PFirst2 on the same side of Psecond1->psecond2 {return false;}//Otherwise, the intersection return True } Point Getcrosspoint (Point P1, point P2, point Q1, point Q2) {//must intersect to find the intersection of the segment, but the following program segment is a generic assert (Islinesegmentcross (P1 , P2,Q1,Q2)); Point CrossPoint; Long templeft,tempright; X coordinate templeft = (q2.x-q1.x) * (P1.Y-P2.Y)-(p2.x-p1.x) * (Q1.Y-Q2.Y); Tempright = (p1.y-q1.y) * (p2.x-p1.x) * (q2.x-q1.x) + q1.x * (q2.y-q1.y) * (p2.x-p1.x)-p1.x * (P2.Y-P1.Y) * (q2.x-q1.x); Crosspoint.x = (int) ((double) tempright/(double) templeft); Y coordinate templeft = (p1.x-p2.x) * (Q2.Y-Q1.Y)-(P2.Y-P1.Y) * (q1.x-q2.x); Tempright = P2.y * (p1.x-p2.x) * (Q2.Y-Q1.Y) + (q2.x-p2.x) * (Q2.Y-Q1.Y) * (P1.Y-P2.Y)-q2.y * (q1.x-q2.x) * ( P2.Y-P1.Y); CROSSPOINT.Y = (int) ((double) tempright/(double) templeft); return crosspoint; } int main (void) {point pointa,poinTb,pointc,pointd; Point Pointcross; BOOL Bcross (FALSE); pointa.x = 400;pointa.y=440; pointb.x = 300;pointb.y = 440; pointc.x = 350;POINTC.Y = 500; pointd.x = 350;POINTD.Y = 400; Bcross = Islinesegmentcross (POINTA,POINTB,POINTC,POINTD); if (bcross) {Pointcross = Getcrosspoint (POINTA,POINTB,POINTC,POINTD); printf ("Intersection coordinate x=%d,y=%d\n", pointcross.x, POINTCROSS.Y); } else {printf ("They is not crossed!");} return 0; The algorithm two defines: three points on the plane P1 (x1,y1), P2 (X2,y2), P3 (x3,y3) area: |x1 x2 x3| S (P1,P2,P3) = |y1 y2 y3| = (x1-x3) * (y2-y3)-(Y1-y3) (x2-x3) | 1 1| It is known that the two endpoints of a line segment are a (x1,y1), B (X2,y2), and the other segment has two endpoints of C (X3,y3) and D (X4,Y4), to determine if there is an intersection between AB and CD, if any. First Judge d = (y2-y1) (x4-x3)-(Y4-y3) (x2-x1), if d=0, then the straight line AB and CD parallel or coincident, if d!=0, the line AB and CD intersection, set the intersection is E (x0,y0): ce/ed = S (a,c,b)/s (A, B, D) So: CE/CD = S (a,c,b)/(s (a,c,b) + S (a,b,d)) so x0 = c.x + (d.x-c.x) * s (a,c,b)/(s (a,c,b) + S (a,b,d)); y0 = C.y + (D.Y-C.Y) * s (a,c,b)/(s (a,c,b) + S (a,b,d)); You can also directly ask x0 = [(x2-x1) * (x4-x3) * (y3-y1) + (y2-y1) * (x4-x3) *x1-(y4-y3) * (x2-x1) *x3]/d y0 = [(y2-y1) *(y4-y3) * (x3-x1) + (x2-x1) * (y4-y3) *y1-(x4-x3) * (y2-y1) *y3]/(-D) after the intersection is determined to determine whether the intersection is on a line segment, the following is judged: (x0-x1) * (X0-X2) <=0 ( X0-X3) * (x0-x4) <=0 (y0-y1) * (y0-y2) <=0 (y0-y3) * (Y0-Y4) <=0 only the above four formulas can be determined (x0,y0) is the intersection of the line AB and the CD, otherwise two segments no intersection algorithm three// Linear equation for two points line Makeline (point p1,point p2) {lines tl; int sign = 1; tl.a=p2.y-p1.y; if (tl.a<0) {sign =-1; tl.a=sign* TL.A; } tl.b=sign* (p1.x-p2.x); tl.c=sign* (P1.Y*P2.X-P1.X*P2.Y); return TL; }//If two straight lines L1 (a1*x+b1*y+c1 = 0), L2 (a2*x+b2*y+c2 = 0) intersect, return True, and return the intersection of P BOOL Lineintersect (line L1,line l2,point &p) {/ /is L1,L2 double d=l1.a*l2.b-l2.a*l1.b; if (ABS (d) < p="">
Intersection of two segments to find intersections