We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
enumclassPARTS{OX=0,//Ox axisI=1,//quadrant IOY=2,//Oy axisII=3,//quadrant IIOX_n=4,III=5,//quadrant IIIOY_n=6,IV=7,//quadrant IVO=-1//origin};inlinePARTSget_part(floatx,floaty){if(x==0&&y==0)returnPARTS::O;if(y==0){//OX or OX_nif(x>0)returnPARTS::OX;returnPARTS::OX_n;}if(x==0){//OY or OY_nif(y>0)returnPARTS::OY;returnPARTS::OY_n;}if(y>0){//I or IIif(x>0)returnPARTS::I;returnPARTS::II;}//y < 0if(x>0)returnPARTS::IV;returnPARTS::III;}//is p1 above or below O-p2 (-1 if p1 is on O-p2)//p2 should not be on the OY axis (p2x != 0)inlineintis_above(floatp1x,floatp1y,floatp2x,floatp2y){//OY: a*x + y = 0if(p2x==0)throw2;floatnum=-p2y*p1x+p1y*p2x;if(num==0)return-1;if(num>0){if(p2x>0)return1;return0;}if(num<0){if(p2x>0)return0;return1;}return0;}//if the polar angle of p1 is larger than p2//1: p1 > p2; 0: p1 < p2; -1: p1 == p2intcompare_points_polar_angle(floatp1x,floatp1y,floatp2x,floatp2y){PARTSPN1=get_part(p1x,p1y),PN2=get_part(p2x,p2y);if(PN1==PARTS::O||PN2==PARTS::O)throw1;//if p1 belongs in part with higher value than p2if(PN1>PN2)return1;if(PN1<PN2)return0;//p1 and p2 is in the same part//if p1 and p2 is on the axisif(PN1==PARTS::OX||PN1==PARTS::OY||PN1==PARTS::OX_n||PN1==PARTS::OY_n)return-1;inti_a=is_above(p1x,p1y,p2x,p2y);if(i_a==-1)return-1;if(PN1==PARTS::I)returni_a;if(PN1==PARTS::II){if(i_a==1)return0;return1;}if(PN1==PARTS::III){if(i_a==1)return0;return1;}//if (PN1 == PARTS::IV)return i_a;returni_a;}boolcomp(std::vector<int>p1,std::vector<int>p2){//if p2 > p1intre=compare_points_polar_angle(p2[0],p2[1],p1[0],p1[1]);if(re!=-1){if(re==1)returntrue;returnfalse;}//p1 and p2 have the same polar angleif(p1[0]*p1[0]+p1[1]*p1[1]<p2[0]*p2[0]+p2[1]*p2[1])returntrue;returnfalse;}std::vector<std::vector<int>>solve(std::vector<std::vector<int>>coordinates){std::sort(coordinates.begin(),coordinates.end(),comp);returncoordinates;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Polar Angles
You are viewing a single comment's thread. Return to all comments →
i overcomplicate this problem way too much.