Sort by

recency

|

11 Discussions

|

  • + 0 comments

    i overcomplicate this problem way too much.

    enum class PARTS
    {
    	OX = 0,//Ox axis
    	I = 1,//quadrant I
    	OY = 2,//Oy axis
    	II = 3,//quadrant II
    	OX_n = 4,
    	III = 5,//quadrant III
    	OY_n = 6,
    	IV = 7,//quadrant IV
    
    	O = -1 //origin
    };
    
    inline PARTS get_part(float x, float y) {
    	if (x == 0 && y == 0)return PARTS::O;
    
    	if (y == 0) {
    		//OX or OX_n
    		if (x > 0)return PARTS::OX;
    
    		return PARTS::OX_n;
    	}
    
    	if (x == 0) {
    		//OY or OY_n
    		if (y > 0)return PARTS::OY;
    
    		return PARTS::OY_n;
    	}
    
    	if (y > 0) {
    		//I or II
    		if (x > 0)return PARTS::I;
    
    		return PARTS::II;
    	}
    
    	//y < 0
    
    	if (x > 0)return PARTS::IV;
    
    	return PARTS::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)
    inline int is_above(float p1x, float p1y, float p2x, float p2y) {
    	//OY: a*x + y = 0
    	if (p2x == 0)throw 2;
    
    	float num = -p2y * p1x + p1y * p2x;
    
    	if (num == 0)return -1;
    
    	if (num > 0) {
    		if (p2x > 0)return 1;
    		return 0;
    	}
    
    	if (num < 0) {
    		if (p2x > 0)return 0;
    		return 1;
    	}
    
    	return 0;
    }
    
    //if the polar angle of p1 is larger than p2
    //1: p1 > p2; 0: p1 < p2; -1: p1 == p2
    int compare_points_polar_angle(float p1x, float p1y, float p2x, float p2y) {
    	PARTS
    		PN1 = get_part(p1x, p1y),
    		PN2 = get_part(p2x, p2y);
    
    	if (PN1 == PARTS::O || PN2 == PARTS::O)throw 1;
    
    	//if p1 belongs in part with higher value than p2
    	if (PN1 > PN2)return 1;
    
    	if (PN1 < PN2)return 0;
    
    	//p1 and p2 is in the same part
    
    	//if p1 and p2 is on the axis
    	if (PN1 == PARTS::OX || PN1 == PARTS::OY || PN1 == PARTS::OX_n || PN1 == PARTS::OY_n)return -1;
    
    	int i_a = is_above(p1x, p1y, p2x, p2y);
    
    	if (i_a == -1)return -1;
    
    	if (PN1 == PARTS::I)return i_a;
    
    	if (PN1 == PARTS::II) {
    		if (i_a == 1)return 0;
    		return 1;
    	}
    
    	if (PN1 == PARTS::III) {
    		if (i_a == 1)return 0;
    		return 1;
    	}
    
    	//if (PN1 == PARTS::IV)return i_a;
    	return i_a;
    }
    
    bool comp(std::vector<int>p1, std::vector<int> p2) {
    	//if p2 > p1
    	int re = compare_points_polar_angle(p2[0], p2[1], p1[0], p1[1]);
    
    	if (re != -1) {
    		if (re == 1)return true;
    		return false;
    	}
    
    	//p1 and p2 have the same polar angle
    
    	if (p1[0] * p1[0] + p1[1] * p1[1] < p2[0] * p2[0] + p2[1] * p2[1])return true;
    
    	return false;
    }
    
    std::vector<std::vector<int>> solve(std::vector<std::vector<int>> coordinates) {
    	std::sort(coordinates.begin(), coordinates.end(), comp);
    	return coordinates;
    }
    
  • + 0 comments

    Look ma - mappend! Haskell snippet:

    sol = sortBy (comparing Θ `mappend` comparing r)
    
    Θ [x, y] = if a < 0 then a + 2*pi else a 
        where a = atan2 (fromIntegral y) (fromIntegral x)
    
    r [x, y] = x^2 + y^2
    
  • + 0 comments

    Using Pair and priorityQueue in Java is working. https://www.hackerrank.com/challenges/polar-angles/submissions/code/27166160

  • + 1 comment

    Crazy 2-liner in Python 3. :D

    from math import pi, atan2
    print("\n".join(map("{} {}".format, *zip(*sorted((list(map(int,input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))
    
  • + 2 comments

    i am unable to download test cases, no button for that