Ejemplo n.º 1
0
    VP solve(vector<Line> line) {
        sort(line.begin(), line.end(), cmp());
        int n = unique(line.begin(), line.end(), kequal) - line.begin();
        assert(n > 2);

        int head = 0, tail = 1;
        que[0] = line[0]; que[1] = line[1];
        VP ret;
        for (int i = 2; i < n; i++) {
            if (fabs((que[tail].e - que[tail].s).det(que[tail - 1].e - que[tail - 1].s)) < eps ||
                fabs((que[head].e - que[head].s).det(que[head + 1].e - que[head + 1].s)) < eps) {
                return ret;
            }
            while (head < tail && ((isLL(que[tail], que[tail - 1])[0] - line[i].s)
                .det(line[i].e - line[i].s)) > eps) tail--;
            while (head < tail && ((isLL(que[head], que[head + 1])[0] - line[i].s)
                .det(line[i].e - line[i].s)) > eps) head++;
            que[++tail] = line[i];
        }
        while (head < tail && ((isLL(que[tail], que[tail - 1])[0] - que[head].s)
            .det(que[head].e - que[head].s)) > eps) tail--;
        while (head < tail && ((isLL(que[head], que[head + 1])[0] - que[tail].s)
            .det(que[tail].e - que[tail].s)) > eps) head++;

        if (tail <= head + 1)
            return ret;
        for (int i = head; i < tail; i++) {
            ret.push_back(isLL(que[i], que[i + 1])[0]);
        }
        if (head < tail + 1)
            ret.push_back(isLL(que[head], que[tail])[0]);
        return ret;
    }
void cut(point_t p1, point_t p2) {
	int tcnt = 0;
	static point_t tp[maxn];
	p[cnt + 1] = p[1];
	for (int i = 1; i <= cnt; ++i) {
		double v1 = cross(p2 - p1, p[i] - p1);
		double v2 = cross(p2 - p1, p[i + 1] - p1);
		if (dblcmp(v1) >= 0) tp[++tcnt] = p[i]; // <= if right side
		if (dblcmp(v1) * dblcmp(v2) < 0) tp[++tcnt] = isLL(p1, p2, p[i], p[i + 1]);		
	}
	cnt = tcnt;
	for (int i = 1; i <= cnt; ++i) p[i] = tp[i];
}
Ejemplo n.º 3
0
 VP isLL(Line l1, Line l2) {
     return isLL(l1.s, l1.e, l2.s, l2.e);
 }