Convex convexIntersection(Convex v1, Convex v2) {
    vector<Halfplane> h;
    for (int i = 0; i < v1.size(); i++) {
        h.push_back(Halfplane(v1[i], v1[(i+1) % v1.size()]));
    }
    for (int i = 0; i < v2.size(); i++) {
        h.push_back(Halfplane(v2[i], v2[(i+1) % v2.size()]));
    }
    return halfplaneIntersection(h);
}
double area(Convex a) {
    double sum = 0;
    a.push_back(a[0]);
    for (int i = 0; i < a.size() - 1; i++) {
        sum += cross(a[i], a[i+1]);
    }
    return sum / 2.0;
}
Convex convex_hull(Convex a) {
    Convex res(2 * a.size() + 5);
    sort(a.begin(), a.end(), comp_less);
    a.erase(unique(a.begin(), a.end()), a.end());
    int m = 0;
    for (int i = 0; i < a.size(); i++) {
        while (m > 1 && sgn(cross(res[m-1]-res[m-2], a[i]-res[m-2])) <= 0) {
            m--;
        }
        res[m++] = a[i];
    }
    int k = m;
    for (int i = a.size() - 2; i >= 0; i--) {
        while (m > k && sgn(cross(res[m-1]-res[m-2], a[i]-res[m-2])) <= 0) {
            m--;
        }
        res[m++] = a[i];
    }
    res.resize(m);
    if (a.size() > 1) {
        res.resize(m-1);
    }
    return res;
}
示例#4
0
int main(int argc, char const *argv[])
{
	Convex v1, v2;
	v1.push_back(Point(0,0));
	v1.push_back(Point(1,1));
	v1.push_back(Point(0,2));
	v1.push_back(Point(-1,1));
	v1.push_back(Point(-0.5,0));

	v2.push_back(Point(1,0));
	v2.push_back(Point(2,1));
	v2.push_back(Point(1,2));
	v2.push_back(Point(0,1));
	v2.push_back(Point(0.5,0));

	Convex v = convexIntersection(v1, v2);
	for (int i = 0; i < v.size(); ++i)
	{
		cout << v[i] << endl;
	}
	return 0;
}
示例#5
0
Convex convexIntersection(Convex v1, Convex v2)
{
	vector<Halfplane> h, h1, h2;
	for (int i = 0; i < v1.size(); ++i)
		h1.push_back(Halfplane(v1[i], v1[(i+1)%v1.size()]));
	for (int i = 0; i < v2.size(); ++i)
		h2.push_back(Halfplane(v2[i], v2[(i+1)%v2.size()]));
	int p1 = 0, p2 = 0;
	while(p1 < h1.size() && p2 < h2.size())
	{
		int res = sgn(arg(h1[p1].second - h1[p1].first) - arg(h2[p2].second - h2[p2].first));
		if (res < 0)
			h.push_back(h1[p1++]);
		else if (res > 0)
			h.push_back(h2[p2++]);
		else
			if (sgn(cross(h1[p1].first - h2[p2].first, h2[p2].second - h2[p2].first)) < 0)
			{
				h.push_back(h1[p1++]);
				p2++;
			}
			else
			{
				h.push_back(h2[p2++]);
				p1++;
			}
	}
	while(p1 < h1.size()) 
		h.push_back(h1[p1++]);
	while(p2 < h2.size()) 
		h.push_back(h2[p2++]);

	deque<Halfplane> q;
	deque<Point> ans;
	q.push_back(h[0]);
	for (int i = 1; i < int(h.size()); ++i)
	{
		if (sgn(arg(h[i].second - h[i].first) - arg(h[i-1].second - h[i-1].first)) == 0)
			continue;
		while (ans.size() > 0 && !satisfy(ans.back(), h[i]))
		{
			ans.pop_back();
			q.pop_back();
		}
		while (ans.size() > 0 && !satisfy(ans.front(), h[i]))
		{
			ans.pop_front();
			q.pop_front();
		}
		ans.push_back(crosspoint(q.back(), h[i]));
		q.push_back(h[i]);
	}
	while (ans.size() > 0 && !satisfy(ans.back(), q.front()))
	{
		ans.pop_back();
		q.pop_back();
	}
	while (ans.size() > 0 && !satisfy(ans.front(), q.back()))
	{
		ans.pop_front();
		q.pop_front();
	}
	ans.push_back(crosspoint(q.back(), q.front()));
	return vector<Point>(ans.begin(), ans.end());
}