旋转卡壳模版(求凸包直径)

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAXN = 100022 << 2;

struct Point
{
    int x, y;
    Point( int x = 0, int y = 0 ):x(x), y(y) { }
};

typedef Point Vector;

Vector operator+( Vector A, Vector B )       //向量加
{
    return Vector( A.x + B.x, A.y + B.y );
}

Vector operator-( Vector A, Vector B )       //向量减
{
    return Vector( A.x - B.x, A.y - B.y );
}

Vector operator*( Vector A, double p )      //向量数乘
{
    return Vector( A.x * p, A.y * p );
}

Vector operator/( Vector A, double p )      //向量数除
{
    return Vector( A.x / p, A.y / p );
}

bool operator<( const Point& A, const Point& B )   //两点比较
{
    return A.x < B.x || ( A.x == B.x && A.y < B.y );
}

double Cross( Vector A, Vector B )   //向量叉积
{
    return A.x * B.y - A.y * B.x;
}
//求凸包
int ConvexHull( Point *p, int n, Point *ch )
{
    sort( p, p + n );
    int m = 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用旋转卡壳算法凸包直径的 Python 代码示例: ```python import numpy as np from scipy.spatial import ConvexHull def dist(p1, p2): return np.linalg.norm(p1 - p2) def diameter(hull): """Calculate the diameter of a convex hull using rotating calipers.""" if len(hull.vertices) < 2: return 0 elif len(hull.vertices) == 2: return dist(hull.points[hull.vertices[0]], hull.points[hull.vertices[1]]) else: edges = hull.points[hull.vertices] farthest_points = [None, None] max_distance = 0 i, j = 0, 1 for k in range(len(edges)): # Find the farthest point from the current edge while np.cross(edges[j] - edges[i], edges[(i+1)%len(edges)] - edges[i]) \ > np.cross(edges[j] - edges[i], edges[(j+1)%len(edges)] - edges[i]): j = (j+1)%len(edges) d = dist(edges[i], edges[j]) if d > max_distance: max_distance = d farthest_points[0] = edges[i] farthest_points[1] = edges[j] i = (i+1)%len(edges) return dist(*farthest_points) points = np.random.rand(10, 2) # 随机生成 10 个点 hull = ConvexHull(points) print("凸包直径:", diameter(hull)) ``` 这个代码示例中,我们先随机生成了 10 个点,然后使用 scipy 库的 ConvexHull 函数凸包。接着,我们使用旋转卡壳算法凸包直径,最后输出结果。 在旋转卡壳算法中,我们使用了叉积来判断两个向量的方向关系,从而找到凸包上距离当前边最远的点。这个算法的核心思想是,对于凸包上的每条边,我们都可以找到一个与其垂直的边,然后随着旋转角度的增加,这条垂直边的端点将会沿着凸包移动,直到另一条边成为新的垂直边。在这个过程中,我们可以计算出每个垂直边的长度,最终得到凸包直径

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值