Convex hull area of intersection, area and.

Source: Internet
Author: User
Tags min
Title: Nbut 1640: The public part of the polygon. Test instructions is to ask: whether two simple polygons have intersect. Idea: Calculate the area intersection or area of two convex hull, can solve this problem.
Because the topic is a simple polygon, and the output is counter-clockwise, all do not need to require convex package. To find the code of the area intersection:
#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include <vector > #include <iostream> #include <complex> #include <string> #include <set> #include <map
> #include <algorithm> using namespace std; #pragma COMMENT (linker, "/uack:1024000000,1024000000") #define LL Long long #define ULL unsiged Long long #define PB push
_back #define MP Make_pair #define MOD 1000003 #define EPS 0.00000001 typedef pair<int, int> PII;
CONST LL INF = 1ll<<50;
const int NN = 100100;

Const double PI = ACOs (-1.0);
	int dcmp (double x) {if (Fabs (x) < EPS) return 0; return x > 0?
1:-1;
	} struct Point {double x, y;
Point () {}-point (double _x, double _y): X (_x), Y (_y) {}};
Point operator + (point A, point B) {return point (a.x + b.x, A.Y + b.y);}
Point operator-(point A, point B) {return point (a.x-b.x, a.y-b.y);}
Point operator * (Point A, double b) {return point (a.x*b,a.y*b);} Point operator/(POint A, double b) {return point (a.x/b, a.y/b);} double Dot (point A, point B) {return a.x*b.x + a.y*b.y;} double Len (Point a) {return sqrt (Dot (A, a)), double Cross (point A, point B) {return a.x*b.y-a.y*b.x;} double Area (vector
	&LT;POINT&GT;P)//Calculate the area of the convex hull {double ans = 0;
	int sz = P.size ();
	for (int i = 1; i < sz-1; i++) ans + = Cross (P[i]-p[0], P[i + 1]-p[0]);
return ans/2.0;
	} Double seg (Point O, point A, point B) {if (dcmp (b.x-a.x) = = 0) return (O.Y-A.Y)/(B.Y-A.Y);
Return (o.x-a.x)/(b.x-a.x);
} vector<point> pp[110];
Pair<double, int> s[2000200];
	Double polyunion (vector<point>*p, int n)//the area intersection of n multiple convex packets {DOUBLE ret = 0;
		for (int i = 0; i < n; i++) {int sz = p[i].size ();
			for (int j = 0; J < Sz; J + +) {int m = 0;
			s[m++] = MP (0, 0);
			s[m++] = MP (1, 0);
			Point A = P[i][j], B = p[i][(j + 1)% SZ];
					for (int k = 0; k < n; k++) {if (i! = k) {int siz = P[k].size (); for (int ii = 0; II < siz;
						ii++) {point c = p[k][ii], d = p[k][(ii + 1)% siz];
						int C1 = DCMP (Cross (b-a, c-a));
						int C2 = dcmp (Cross (b-a, d-a)); if (C1 = = 0 && C2 = = 0) {if (dcmp (Dot (b-a, d-c)) > 0 && i > k) {s[
								m++] = MP (SEG (c, a, b), 1);
							s[m++] = MP (SEG (d, a, b),-1);
							}} else {double S1 = Cross (d-c, a-c);
							Double s2 = Cross (d-c, b-c);
							if (C1 >= 0 && C2 < 0) s[m++] = MP (S1/(S1-S2), 1);
						else if (C1 < 0 && C2 >= 0) s[m++] = MP (S1/(S1-S2),-1);
			}}}} sort (s, S + M);
			Double pre = MIN (max (S[0].first, 0.0), 1.0), now;
			Double sum = 0;
			int cov = S[0].second;
				for (int j = 1; j < M; J + +) {now = min (max (S[j].first, 0.0), 1.0);
				if (!cov) sum + = Now-pre;
				CoV + = S[j].second;
			Pre = now;
		} ret + = Cross (A, b) *sum;
}} return RET/2; } int Main() {int n, m, kcase = 1;
		while (scanf ("%d", &n)! = EOF) {pp[0].clear ();
		Pp[1].clear ();
		Point XP;
			for (int i = 0; i < n; i++) {scanf ("%lf%lf", &xp.x, &AMP;XP.Y);
		PP[0].PB (XP);
		} scanf ("%d", &m);
			for (int i = 0; i < m; i++) {scanf ("%lf%lf", &xp.x, &AMP;XP.Y);
		PP[1].PB (XP);
		} Double t1 = area (pp[0]) + area (pp[1]);
		Double t2 = polyunion (PP, 2);
		printf ("Case%d:", kcase++);
		if (dcmp (t1-t2) = = 0) puts ("No");
	Else puts ("Yes");
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.