I'm looking for a way to find quadrangle with largest area. I have already computed points of convex hull and got them sorted clockwise. I tried brute force but of course it's too slow. So I've found here this algorithm for largest triangle:
How to find largest triangle in convex hull aside from brute force search
It looks really nice, so I tried to remake it. I've got function that computes area of any quadrangle by dividing it to two triangles (in this function I'm sorting input points to be sure I'm calculating right triangles). Here it is:
int n = convexHull.size();
int A = 0; int B = 1; int C = 2; int D = 3;
int bestA = A; int bestB = B; int bestC = C; int bestD = D;
while(true) { // loop A
while(true) { // loop B
while(true) { // loop C
while(quadrangleArea(A, B, C, D) <= quadrangleArea(A, B, C, (D+1)%n) ) { // loop D
D = (D+1)%n;
}
if(quadrangleArea(A, B, C, D) <= quadrangleArea(A, B, (C+1)%n, D) ) {
C = (C+1)%n;
continue;
}
else break;
}
if(quadrangleArea(A, B, C, D) <= quadrangleArea(A, (B+1)%n, C, D) ) {
B = (B+1)%n;
continue;
}
else break;
}
if(quadrangleArea(A, B, C, D) > quadrangleArea(bestA, bestB, bestC, bestD) ) {
bestA = A; bestB = B; bestC = C; bestD = D;
}
A = (A+1)%n;
if (A==B) B = (B+1)%n;
if (B==C) C = (C+1)%n;
if (C==D) D = (D+1)%n;
if (A==0) break;
}
It looks fine and gives good results for my simple tests, but I'm afraid something is not right. Going further with this reasoning, I could make algorithm for every polygon with n vertices - but by intuition I think it's impossible. Am I right?
I'm trying to solve "SHAMAN" problem on spoj, and I've got wrong answer. I'm 99% sure rest of my program is OK, so something is wrong with code above. Could you help me to improve it? Maybe you've got some tricky tests that could prove this algorithm doesn't work properly? I'll be grateful for any tips!