Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.5 kB
2
Indexable
Never
public class Solution {

    public int solution(String direction, int radius, int X[], int Y[]) {
        int count = 0;
        double pi = angle(-1, 0);
        double piBy4 = angle(1, 1);
        double threePiBy4 = angle(-1, 1);
        double minusPiBy4 = angle(1, -1);
        double minusThreePiBy4 = angle(-1, -1);

        for (int i = 0; i < X.length; i++) {
            double angle = angle(X[i], Y[i]);
            int d = distance(X[i], Y[i]);
            if (d > radius * radius) {
                continue;
            }
            switch (direction) {
                case "U":
                    if (angle >= piBy4 && angle <= threePiBy4) {
                        count++;
                    }
                    break;
                case "R":
                    if (angle <= piBy4 && angle >= minusPiBy4) {
                        count++;
                    }
                    break;
                case "D":
                    if (angle >= minusThreePiBy4 && angle <= minusPiBy4) {
                        count++;
                    }
                    break;
                case "L":
                    if ((angle >= threePiBy4 && angle <= pi) || angle <= minusThreePiBy4) {
                        count++;
                    }
                    break;
            }
        }
        return count;
    }

    private double angle(int x, int y) {
        return Math.atan2(y, x);
    }

    private int distance(int x, int y) {
        return x * x + y * y;
    }
}