main() { tPointi a,b,c,d; tPointd p; char code; char ans; do { printf("\na,b,c,d=\n"); scanf("%d %d", &a[X],&a[Y]); scanf("%d %d", &b[X],&b[Y]); scanf("%d %d", &c[X],&c[Y]); scanf("%d %d", &d[X],&d[Y]); printf("(%d,%d) ", a[X],a[Y]); printf("(%d,%d) ", b[X],b[Y]); printf("(%d,%d) ", c[X],c[Y]); printf("(%d,%d) ", d[X],d[Y]); putchar('\n'); code = SegSegInt(a,b,c,d,p); printf("code=%c; p=(%lf,%lf)\n", code, p[X], p[Y]); printf("Continue(y/n)? "); ans = getchar(); ans = getchar(); } while (ans == 'y'); }
void merge2Holes(c_diagonal& cut,vector<c_diagonal>& allCuts, vector<c_diagonal>::iterator tmpIte, c_ply* result_ply, bool& p1hasmorecut,bool& p2hasmorecut) { ply_vertex * v1, *v2; v1 = cut.getV1(); v2 = cut.getV2(); ply_vertex* v1n = v1->getNext(); ply_vertex* v2n = v2->getNext(); addDiagnal(v1,v2); ply_vertex* fakeV1 = v2->getNext(); ply_vertex* fakeV2 = v1->getNext(); c_polygon* initPolygon = v1->getExtra().parentPolygon; fakeV1->getExtra().parentPly = result_ply; fakeV2->getExtra().parentPly = result_ply; fakeV1->getExtra().parentPolygon = v1->getExtra().parentPolygon; fakeV2->getExtra().parentPolygon = v1->getExtra().parentPolygon; v1->getExtra().parentPly = result_ply; v2->getExtra().parentPly = result_ply; result_ply->set(result_ply->getType(), v1); result_ply->doInit(); result_ply->re_triangulate(); markParentPly(result_ply, initPolygon); //fakeV1->getExtra().parentPly = v1-> //update the rest diagonals vector<c_diagonal>::iterator dIte = tmpIte; dIte++; for (;dIte!=allCuts.end();++dIte) { c_diagonal& tmpDiag = *dIte; ply_vertex* s = tmpDiag.getV1(); ply_vertex* e = tmpDiag.getV2(); if(s->getExtra().parentPolygon != initPolygon) { assert(e->getExtra().parentPolygon != initPolygon); continue; } if(tmpDiag.getV1()==v1) { ply_vertex* otherVert = tmpDiag.getV2(); //if (v1,otherVert) intersects with (v2,v1n) if(SegSegInt(v1->getPos().get(),otherVert->getPos().get(), v2->getPos().get(),v1n->getPos().get())) { (*dIte).setEndPoints(fakeV1,otherVert); } } if(tmpDiag.getV2()==v1) { ply_vertex* otherVert = tmpDiag.getV1(); //if (v1,otherVert) intersects with (v2,v1n) if(SegSegInt(v1->getPos().get(),otherVert->getPos().get(), v2->getPos().get(),v1n->getPos().get())) { (*dIte).setEndPoints(otherVert, fakeV1); } } if(tmpDiag.getV1()==v2) { ply_vertex* otherVert = tmpDiag.getV2(); //if (v2,otherVert) intersects with (v1,v2n) if (SegSegInt(v2->getPos().get(),otherVert->getPos().get(), v1->getPos().get(),v2n->getPos().get())) { (*dIte).setEndPoints(fakeV2,otherVert); } } if(tmpDiag.getV2()==v2) { ply_vertex* otherVert = tmpDiag.getV1(); //if ((v2,otherVert) intersects with (v1,v2n) if (SegSegInt(v2->getPos().get(),otherVert->getPos().get(), v1->getPos().get(),v2n->getPos().get())) { (*dIte).setEndPoints(otherVert, fakeV2); } } assert(tmpDiag.getV1()->getExtra().parentPolygon == tmpDiag.getV2()->getExtra().parentPolygon); } }
/*--------------------------------------------------------------------- ---------------------------------------------------------------------*/ void ConvexIntersect( tPolygoni P, tPolygoni Q, int n, int m ) /* P has n vertices, Q has m vertices. */ { int a, b; /* indices on P and Q (resp.) */ int a1, b1; /* a-1, b-1 (resp.) */ tPointi A, B; /* directed edges on P and Q (resp.) */ int cross; /* sign of z-component of A x B */ int bHA, aHB; /* b in H(A); a in H(b). */ tPointi Origin = {0,0}; /* (0,0) */ tPointd p; /* double point of intersection */ tPointd q; /* second point of intersection */ tInFlag inflag; /* {Pin, Qin, Unknown}: which inside */ int aa, ba; /* # advances on a & b indices (after 1st inter.) */ bool FirstPoint; /* Is this the first point? (used to initialize).*/ tPointd p0; /* The first point. */ int code; /* SegSegInt return code. */ /* Initialize variables. */ a = 0; b = 0; aa = 0; ba = 0; inflag = Unknown; FirstPoint = TRUE; do { /*printf("%%Before Advances:a=%d, b=%d; aa=%d, ba=%d; inflag=%d\n", a, b, aa, ba, inflag);*/ /* Computations of key variables. */ a1 = (a + n - 1) % n; b1 = (b + m - 1) % m; SubVec( P[a], P[a1], A ); SubVec( Q[b], Q[b1], B ); cross = AreaSign( Origin, A, B ); aHB = AreaSign( Q[b1], Q[b], P[a] ); bHA = AreaSign( P[a1], P[a], Q[b] ); printf("%%cross=%d, aHB=%d, bHA=%d\n", cross, aHB, bHA ); /* If A & B intersect, update inflag. */ code = SegSegInt( P[a1], P[a], Q[b1], Q[b], p, q ); printf("%%SegSegInt: code = %c\n", code ); if ( code == '1' || code == 'v' ) { if ( inflag == Unknown && FirstPoint ) { aa = ba = 0; FirstPoint = FALSE; p0[X] = p[X]; p0[Y] = p[Y]; printf("%8.2lf %8.2lf moveto\n", p0[X], p0[Y] ); } inflag = InOut( p, inflag, aHB, bHA ); printf("%%InOut sets inflag=%d\n", inflag); } /*-----Advance rules-----*/ /* Special case: A & B overlap and oppositely oriented. */ if ( ( code == 'e' ) && (Dot( A, B ) < 0) ) PrintSharedSeg( p, q ), exit(EXIT_SUCCESS); /* Special case: A & B parallel and separated. */ if ( (cross == 0) && ( aHB < 0) && ( bHA < 0 ) ) printf("%%P and Q are disjoint.\n"), exit(EXIT_SUCCESS); /* Special case: A & B collinear. */ else if ( (cross == 0) && ( aHB == 0) && ( bHA == 0 ) ) { /* Advance but do not output point. */ if ( inflag == Pin ) b = Advance( b, &ba, m, inflag == Qin, Q[b] ); else a = Advance( a, &aa, n, inflag == Pin, P[a] ); } /* Generic cases. */ else if ( cross >= 0 ) { if ( bHA > 0) a = Advance( a, &aa, n, inflag == Pin, P[a] ); else b = Advance( b, &ba, m, inflag == Qin, Q[b] ); } else /* if ( cross < 0 ) */{ if ( aHB > 0) b = Advance( b, &ba, m, inflag == Qin, Q[b] ); else a = Advance( a, &aa, n, inflag == Pin, P[a] ); } printf("%%After advances:a=%d, b=%d; aa=%d, ba=%d; inflag=%d\n", a, b, aa, ba, inflag); /* Quit when both adv. indices have cycled, or one has cycled twice. */ } while ( ((aa < n) || (ba < m)) && (aa < 2*n) && (ba < 2*m) ); if ( !FirstPoint ) /* If at least one point output, close up. */ printf("%8.2lf %8.2lf lineto\n", p0[X], p0[Y] ); /* Deal with special cases: not implemented. */ if ( inflag == Unknown) printf("%%The boundaries of P and Q do not cross.\n"); }