点的凸包
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