算法:点的凸包【分治】

这篇博客介绍了如何使用分治策略找到一组二维平面上点的凸包。内容包括输入输出格式、示例输入输出,以及求解思路。在求解过程中,涉及判断点相对于直线的位置,并考虑特殊情况如所有点在一条直线上或凸包点在线上的处理。
摘要由CSDN通过智能技术生成

点的凸包

Description

Convex Hull of a set of points, in 2D plane, is a convex polygon with minimum area such that each point lies either on the boundary of polygon or inside it. Now given a set of points the task is to find the convex hull of points.

Input

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the no of points. Then in the next line are N*2 space separated values denoting the points ie x and y.Constraints:1<=T<=100,1<=N<=100,1<=x,y<=1000

Output

For each test case in a new line print the points x and y of the convex hull separated by a space in sorted order (increasing by x) where every pair is separated from the other by a ‘,’. If no convex hull is possible print -1.

Sample Input 1

2
3
1 2 3 1 5 6
3
1 2 4 4 5 1

Sample Output 1

1 2, 3 1, 5 6
1 2, 4 4, 5 1

思路分析

参考
在这里插入图片描述
上述为点到直线的距离公式,当distance>0时,点在直线的左边(或上方),当distance<0时,点在直线的右边(或下方)。
注意在本题中:
(1)如果所有点在一条直线上,那么中间的点要循环两次,相当于绕直线一圈
(2)如果有凸包点在线上,即distance==0,需要纳入该点。

python3

# 凸包问题的分治解法
def convex_problem(first, final, lis, result):
    maxDistance = 0
    index = -1
    # 处理上凸包
    if first < final:
        for i in range(first, final):
            firstPoint = lis[first]
            finalPoint = lis[final]
            first_x = firstPoint[0]
            first_y = firstPoint[1]
            final_x = finalPoint[0]
            final_y = finalPoint[1]
            indexPoint = lis[i]
            index_x = indexPoint[0]
            index_y = indexPoint[1]
            distance = first_x * final_y + index_x * first_y + final_x * index_y 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值