I was Reading the Graham Scan Algorithm to Find the convex Hull From CLRS. The algorithm given in CLRS for Convex hull is ::
I am not able to understand this line (step 2 of the algorithm):
If two or more points have the same polar angle relative to p0, all but the farthest such point are convex combinations of p0 and the farthest point, and so we remove them entirely from consideration.
Also Lets say, I had made a structure
struct points
{
int x, y;
} P[10000];
sort(P+1, P+N, comparator)
?What does this mean,what should i do if more than one points have the same polar angle with respect to Po?
Let's say P0 is (0,0)
, P1 is (1,1)
and P2 is (2,2)
. Now P1 and P2 have the same angle relative to P0, in which case you discard P1. If there are more point between P0 and P2 with the same angle, discard all except P2 (and P0 of course).
How should i implement the sorting step using C++ STL (sort function in Algorithm ) Library?Especially i mean sort(P+1,P+N,comparator). How should I define the comparator function?
Not really familiar with C++ (STL), but check if it has a atan2
function you can use. Also see: Find angle between two points, respective to horizontal axis? and How to calculate the angle between a line and the horizontal axis?