UVA10173Smallest Bounding Rectangle+最小矩形覆盖

本文介绍了一种计算平面上多个点的最小包围矩形的方法。首先通过计算这些点的凸包,然后找到覆盖这些点的最小面积矩形。文中提供了一个完整的C++实现示例,包括凸包计算和最小矩形面积覆盖的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given the Cartesian coordinates of n(>0) 2-dimensional points, write a program that computes the
area of their smallest bounding rectangle (smallest rectangle containing all the given points).
Input
The input le may contain multiple test cases. Each test case begins with a line containing a positive
integer n(<1001) indicating the number of points in this test case. Then follows n lines each containing two real numbers giving respectively the x and y-coordinates of a point. The input terminates with a test case containing a value 0 for n which must not be processed.
Output
For each test case in the input print a line containing the area of the smallest bounding rectangle
rounded to the 4th digit after the decimal point.
SampleInput
3
-3.000 5.000
7.000 9.000
17.000 5.000
4
10.000 10.000
10.000 20.000
20.000 20.000
20.000 10.000
0
SampleOutput
80.0000
100.0000

最小矩形覆盖,给出很多点,求最小覆盖的矩形,
先求凸包,再求最小覆盖矩形。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define e exp(1.0); //2.718281828
#define mod 1000000007
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

#define zero(x) (((x)>0?(x):(-x))<eps)
const double eps=1e-8;
const double pi=acos(-1.0);

int sgn(double x) {
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}
inline double sqr(double x) {
    return x*x;
}
struct point {
    double x,y;
    point() {};
    point(double a,double b):x(a),y(b) {};
    void input() {
        scanf("%lf %lf",&x,&y);
    }
    friend point operator + (const point &a,const point &b) {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (const point &a,const point &b) {
        return point(a.x-b.x,a.y-b.y);
    }
    friend bool operator == (const point &a,const point &b) {
        return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;
    }
    friend point operator * (const point &a,const double &b) {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (const double &a,const point &b) {
        return point(a*b.x,a*b.y);
    }
    friend point operator / (const point &a,const double &b) {
        return point(a.x/b,a.y/b);
    }
    friend bool operator < (const point &a, const point &b) {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    double norm() {
        return sqrt(sqr(x)+sqr(y));
    }
};
//计算两个向量的叉积
double cross(const point &a,const point &b) {
    return a.x*b.y-a.y*b.x;
}
double cross3(point A,point B,point C) { //叉乘
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}
//计算两个点的点积
double dot(const point &a,const point &b) {
    return a.x*b.x+a.y*b.y;
}
double dot3(point A,point B,point C) { //点乘
    return (C.x-A.x)*(B.x-A.x)+(C.y-A.y)*(B.y-A.y);
}
//求平面点集的凸包 点集p,个数cnt,凸包点集 res,函数返回值凸包点集个数
int ConvexHull(point* P, int cnt, point* res) {  
    sort(P, P + cnt);
    cnt = unique(P, P + cnt) - P;
    int m = 0;
    for (int i = 0; i < cnt; i++) {
        while (m > 1 && cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    int k = m;
    for (int i = cnt - 2; i >= 0; i--) {
        while (m > k && cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    if (cnt > 1) m--;
    return m;
}

double len(point A,point B) { //返回向量AB的模平方
    return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
}
double minRetangleCover(point *res,int n) { //最小矩形面积覆盖(旋转卡壳)
    if(n < 3) return 0.0;
    res[n] = res [0];
    double ans = -1;
    int r = 1, p = 1,q;
    for(int i = 0; i < n; ++ i) {
        //卡出离边 res[i]-res[i+1]最远的点
        while(sgn(cross3(res[i],res[i+1],res[r+1])-cross3(res[i],res[i+1],res[r]))>=0)
            r = (r+1)% n;
        //卡出res[i]-res[i+1]方向上正向n最远的点
        while(sgn(dot3(res[i],res[i+1],res[p+1])-dot3(res[i],res[i+1],res[p]))>=0)
            p = (p+1)% n;
        if(i == 0) q = p;
        //卡出res[i]-res[i+1]方向上负向最远的点
        while(sgn(dot3(res[i],res[i+1],res[q+1])-dot3(res[i],res[i+1],res[q]))<=0)
            q = (q+1)% n;
        double d = len(res[i],res[i+1]);
        double temp = cross3(res[i],res[i+1],res[r])*(dot3(res[i],res[i+1],res[p])-dot3(res[i],res[i+1],res[q]))/d;
        if(ans < 0 || ans > temp) ans = temp;
    }
    return ans;
}
const int N=1005;
point a[N];
point cha[N];
int main() {
    int n;
    while(scanf("%d",&n)==1 && n) {
        for(int i = 0; i < n; ++ i) {
            scanf("%lf %lf",&a[i].x,&a[i].y);
        }
        n = ConvexHull(a,n,cha);//构造凸包
        printf("%.4f\n",minRetangleCover(cha,n));
    }
    return 0;
}
在Helicon AI (一种计算机视觉库) 中,`smallest_rectangle2` 算子通常用于找到图像区域内的最小边界框,即最小外接矩形。如果返回的矩形是斜的,说明原始目标不是正方形或者长宽比偏离了正常水平方向。为了将斜的外接矩形变回接近水平的方向,可以采取以下几个步骤: 1. **旋转坐标系**:首先,你需要确定矩形的中心点和角度。使用类似 `get_rotation_matrix` 的函数,根据角度旋转坐标轴,使得新的X轴尽量平行于原始矩形较长的那一边。 ```python import cv2 import numpy as np angle = calculate_angle(rect) rotation_matrix = get_rotation_matrix(angle) # Apply the rotation to your image or points if necessary rotated_points = apply_rotation(points, rotation_matrix) ``` 这里的 `calculate_angle`, `get_rotation_matrix`, 和 `apply_rotation` 需要你自己实现或者参考相关库提供的功能。 2. **计算新的尺寸**:应用旋转后,你可以计算新矩形的新宽度和高度,取两者中的较大值作为新的宽度,较小值作为高度,确保变为更接近水平的样子。 3. **调整矩形大小**:基于新的宽度和高度创建一个新的矩形,其位置保持不变,即保持原来矩形的左上角坐标。 4. **重新调整坐标**:如果需要,还需要对点集或像素数据进行相应的旋转,以便适应新的矩形。 5. **使用smallest_rectangle2或其他算子**:现在你应该有一个更接近水平的矩形,可以用`smallest_rectangle2` 或者其他边界框算子,如`minAreaRect` 或 `fitEllipse` 来确认新的边界框。 ```python new_rect = smallest_rectangle2(rotated_points) ``` 记得在每个步骤后检查结果的合理性,并根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值