int main()
{
	int test, i, j;
	double area;
	TPolygon polygon, new_polygon;
	scanf("%d", &test);
	while(test--){
		scanf("%d", &polygon.n);
		for(i = 0;i <= polygon.n - 1;i++){
			scanf("%lf%lf", &polygon.p[i].x, &polygon.p[i].y);
		}
		/*若是逆时针转化为顺时针*/
		if(polygonArea(polygon) > 0) ChangeClockwise(polygon);
		polygon.p[polygon.n] = polygon.p[0];
		new_polygon = polygon;
		for(i = 0;i <= polygon.n - 1;i++){
			new_polygon = Cut_polygon(polygon.p[i], polygon.p[i + 1], new_polygon);
		}	
		area = polygonArea(new_polygon);
		if(area < 0) printf("%.2lf\n", -area);
		else printf("%.2lf\n", area);
	}
	return 0;	
}
int main()
{
	int i, j;
	TPolygon polygon, new_polygon;
	while(scanf("%d", &polygon.n) && polygon.n){
		for(i = 0;i <= polygon.n - 1;i++){
			scanf("%lf%lf", &polygon.p[i].x, &polygon.p[i].y);
		}
		/*若是逆时针转化为顺时针*/
		if(polygonArea(polygon) > 0) ChangeClockwise(polygon);		
		polygon.p[polygon.n] = polygon.p[0];
		new_polygon = polygon;
		for(i = 0;i <= polygon.n - 1;i++){
			new_polygon = Cut_polygon(polygon.p[i], polygon.p[i + 1], new_polygon);
		}
		if(new_polygon.n > 0) printf("1\n");
		else printf("0\n");	
	}
	return 0;	
}
示例#3
0
int main() {
    const auto numTestCases = get<uint>();
    for (uint test_case_num = 0; test_case_num < numTestCases; ++test_case_num ) {
        // std::cout << "=== Test Case #" << test_case_num << " ===\n";

        std::vector<Point> border; // will be in CCW order
        const auto numBorderPoints = get<uint>();
        int max_x = 0;
        int max_y = 0;
        int min_x = 0;
        int min_y = 0;
        for (uint i = 0; i < numBorderPoints; ++i) {
            const auto x = get<int>();
            const auto y = get<int>();
            border.emplace_back(x,y);
            max_x = std::max(x,max_x);
            max_y = std::max(y,max_y);
            min_x = std::min(x,min_x);
            min_y = std::min(y,min_y);
        }

        TreeManager trees;
        const auto numTrees = get<uint>();
        for (uint i = 0; i < numTrees; ++i) {
            const auto x = get<int>();
            const auto y = get<int>();
            trees.addTree(Point(x,y));
        }

        // for (auto y : xrange<int>(max_y,min_y)) {
        // 	std::cout << y << ":";
        // 	for (auto x : xrange<int>(min_x,max_x)) {
        // 		if (std::find(border.begin(),border.end(), Point(x,y)) == border.begin()) {
        // 			std::cout << '0';
        // 		} else if (std::find(border.begin(),border.end(), Point(x,y)) != border.end()) {
        // 			std::cout << 'B';
        // 		} else if (std::find(trees.trees().begin(),trees.trees().end(),Point(x,y)) != trees.trees().end()) {
        // 			std::cout << 'T';
        // 		} else {
        // 			std::cout << ' ';
        // 		}
        // 	}
        // 	std::cout << '\n';
        // }
        // std::cout << "  ";
        // for (auto x : xrange<int>(min_x,max_x)) {
        // 	std::cout << x;
        // }
        // std::cout << '\n';

        if (numTrees == 0) {
            std::cout << polygonArea(border,0,border.size(),border.back()) << '\n';
            return 0;
        }

        trees.buildCaches();

        double best_area = 0;
        std::pair<int,int> best_indexes = {0,0};

        size_t other_index = 0;
        for (size_t i_border_point = 0; i_border_point < border.size(); ++i_border_point) {
            other_index = std::max(other_index,i_border_point+2);
            while (true) {
                // if ((other_index + 1) % border.size() == i_border_point) {
                // 	break; // loop around condition. confuses new tree checking
                // }

                Line line(
                    cyclic_access(border)[other_index],
                    cyclic_access(border)[i_border_point]
                );

                // std::cout << "trying " << line.first << "->" << line.second << " (" << other_index << "->" << i_border_point << ")\n";
                if (!trees.allTreesAreToTheRight(line)) {
                    // std::cout << "\tbut there were trees\n\n";
                    break;
                }
                double area = polygonArea(border, i_border_point, other_index+1, line.first);
                // std::cout << "\tarea=" << area << '\n';
                if (area > best_area) {
                    // std::cout << "\tit was better\n";
                    best_area = area;
                    best_indexes = {other_index,i_border_point};
                }
                other_index += 1;
            }
        }

        // std::cout << "best cut: " << best_indexes.first << ',' << best_indexes.second << "\nFINAL ANSWER: ";
        std::cout << best_area << '\n';
    }
}
int main()
{
	int n, m, i;
	TPolygon polygon1, polygon2;
	double ymin1, ymax2, ans, d;
	int k1, k2;
	while(scanf("%d%d", &n, &m) && n)
	{
		polygon1.n = n;
		polygon2.n = m;
		for(i = 0;i < n;i++)
			scanf("%lf%lf", &polygon1.p[i].x, &polygon1.p[i].y);
		for(i = 0;i < m;i++)
			scanf("%lf%lf", &polygon2.p[i].x, &polygon2.p[i].y);	
		if(polygonArea(polygon1) < 0) ChangeClockwise(polygon1);
		if(polygonArea(polygon2) < 0) ChangeClockwise(polygon2);
		ymin1 = inf, ymax2 = -inf;
		for(i = 0;i < n;i++)
			if(polygon1.p[i].y < ymin1) ymin1 = polygon1.p[i].y , k1 = i;
		for(i = 0;i < m;i++)
			if(polygon2.p[i].y > ymax2) ymax2 = polygon2.p[i].y , k2 = i;	
		double SlewRate = 0;
		double angle1, angle2;
		ans = inf;
		double Slope = 0;
		while(Slope <= 360)	
		{	
			while(SlewRate >= pi) SlewRate -= pi;
			if(fabs(pi - SlewRate) < eps) SlewRate = 0;
			angle1 = angle(polygon1.p[k1], polygon1.p[(k1 + 1) % n], SlewRate);
			angle2 = angle(polygon2.p[k2], polygon2.p[(k2 + 1) % m], SlewRate);	
			if(fabs(angle1 - angle2) < eps)
			{
				d = disPallSeg(polygon1.p[k1], polygon1.p[(k1 + 1) % n], polygon2.p[k2], polygon2.p[(k2 + 1) % m]); 
				if(d < ans) ans = d;
                k1++;
				k1 %= n;
				k2++;
				k2 %= m; 
				SlewRate += angle1;
				Slope += angle1;
			}
			else if(angle1 < angle2)
			{
				d = disPointToSeg(polygon2.p[k2], polygon1.p[k1], polygon1.p[(k1 + 1) % n]);
				if(d < ans) ans = d;
				k1++;
				k1 %= n;
				SlewRate += angle1;
				Slope += angle1;
			}
			else 
			{
				d = disPointToSeg(polygon1.p[k1], polygon2.p[k2], polygon2.p[(k2 + 1) % m]);
				if(d < ans) ans = d;
				k2++;
				k2 %= m;
				SlewRate += angle2;
				Slope += angle2;
			}
		}
		printf("%.5lf\n", ans);
	}
	return 0;
} 
int main(int argc,char* argv[]) {
    if (argc < 3) {
        printf("%s target_point.txt label_point.txt\n",argv[0]);
        return 1;
    }

    FILE* target = fopen(argv[1],"r");
    if (!target) {
        printf("Cannot open %s\n",argv[1]);
        return 1;
    }
    FILE* label = fopen(argv[2],"r");
    if (!label) {
        printf("Cannot open %s\n",argv[2]);
        return 1;
    }
    char buffer[512];
    std::vector< std::vector<Coordinate> > target_box;
    std::vector< std::vector<Coordinate> > label_box;
    int numObjects;
    while (fgets(buffer,512,target)) {
        float x,y;
        int numMatch;
        char* c = buffer;
        int n = strtol(c,&c,10);
        numObjects = n/4;
        std::vector<Coordinate> box;
        for (int i=0; i<n; i++) {
            x = strtod(c,&c);
            y = strtod(c,&c);
            numMatch = strtol(c,&c,10);
            Coordinate p = {x,y};
            box.push_back(p);
            if (i%4==3) {
                target_box.push_back(box);
                box.clear();
            }
        }
    }
    while (fgets(buffer,512,label)) {
        float x,y;
        int numMatch;
        char* c = buffer;
        int n = strtol(c,&c,10);
        std::vector<Coordinate> box;
        if (n==0) {
            for (int i=0; i<numObjects; i++)
                label_box.push_back(box);
            continue;
        }
        for (int i=0; i<n; i++) {
            x = strtod(c,&c);
            y = strtod(c,&c);
            numMatch = strtol(c,&c,10);
            Coordinate p = {x,y};
            box.push_back(p);
            if (i%4==3) {
                label_box.push_back(box);
                box.clear();
            }
        }
    }

    float acc_avg=0,acc_stddev=0;
    float acc_min,acc_max;
    float prec_avg=0,prec_stddev=0;
    float prec_min,prec_max;
    float norm_acc=0;
    int count=0;
    for (size_t i=0; i<target_box.size(); i++) {
        if (label_box[i].size()==0)
            continue;
        float A1 = polygonArea(convexHull(target_box[i]));
        float A2 = polygonArea(convexHull(label_box[i]));
        float A3 = polygonArea(convexHull(polygonCombine(target_box[i],label_box[i])));
        float acc = isnan(A3) ? 0 : A3 > A1 + A2 ? 0 : (A1 + A2 - A3) / A2;
        float prec = isnan(A3) ? 0 : A3 > A1 + A2 ? 0 : (A1 + A2 - A3) / A1;
        if (acc > 1) acc = 1;
        if (prec > 1) prec = 1;
        norm_acc += acc > prec ? acc : prec;
        acc_avg += acc;
        acc_stddev += acc*acc;
        if (i==0 || acc < acc_min) acc_min = acc;
        if (i==0 || acc > acc_max) acc_max = acc;
        prec_avg += prec;
        prec_stddev += prec*prec;
        if (i==0 || prec < prec_min) prec_min = prec;
        if (i==0 || prec > prec_max) prec_max = prec;
        printf("Box %lu: %.2f %.2f %8.2f %.4f %.4f\n",i,A1,A2,A3,acc,prec);
        count++;
    }
    acc_avg /= count;
    acc_stddev = sqrt(acc_stddev/count - acc_avg*acc_avg);
    prec_avg /= count;
    prec_stddev = sqrt(prec_stddev/count - prec_avg*prec_avg);
    norm_acc /= count;
    printf("Accuracy Min %.4f Max %.4f Average %.4f +- %.4f Overlap\n",acc_min,acc_max,acc_avg,acc_stddev);
    printf("Precision Min %.4f Max %.4f Average %.4f +- %.4f Overlap\n",prec_min,prec_max,prec_avg,prec_stddev);
    printf("Normalized Accuracy %.4f\n",norm_acc);
    fclose(target);
    fclose(label);

}