Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
2.1 kB
3
Indexable
Never
class Solution {
    public int visiblePoints(String direction, int radiusInt, int[] xs, int[] ys) {
        double angle1, angle2, angle3, angle4;

        if (direction.equals("D")) {
            angle1 = -135;
            angle2 = -90;
            angle3 = -90;
            angle4 = -45;
        } else if (direction.equals("R")) {
            angle1 = -45;
            angle2 = 0;
            angle3 = 0;
            angle4 = 45;
        } else if (direction.equals("U")) {
            angle1 = 45;
            angle2 = 90;
            angle3 = 90;
            angle4 = 135;
        } else {
            angle1 = 135;
            angle2 = 180;
            angle3 = -180;
            angle4 = -135;
        }
        angle1 = toRadian(angle1);
        angle2 = toRadian(angle2);
        angle3 = toRadian(angle3);
        angle4 = toRadian(angle4);

        long radius = ((long) radiusInt) * ((long) radiusInt);

        int numPoints = xs.length;
        int originPoints = 0;

        List<Point> points = new ArrayList<>();
        for (int i = 0; i < numPoints; i++) {
            int x = xs[i];
            int y = ys[i];
            if (x == 0 && y == 0) {
                originPoints++;
            } else {
                points.add(new Point(x, y));
            }
        }

        int result = 0;
        for (Point point: points) {
            if ((point.angle >= angle1 && point.angle <= angle2) || (point.angle >= angle3 && point.angle <= angle4)) {
                if (point.distanceFromOrigin <= radius) {
                    result++;
                }
            }
        }

        return originPoints + result;
    }

    private double toRadian(double deg) {
        return (deg * Math.PI) / 180.0;
    }

    static class Point {
        int x, y;
        double angle;
        long distanceFromOrigin;

        Point(int x, int y) {
            this.x = x;
            this.y = y;

            this.angle = Math.atan2(y, x);
            this.distanceFromOrigin = ((long) x) * ((long) x) + ((long) y) * ((long) y);
        }
    }
}