This page explains Java solution to problem `Maximum Number of Visible Points`

using `Geometry`

algorithm.

You are given an array `points`

, an integer `angle`

, and your `location`

, where `location = [posx, posy]`

and `points[i] = [xi, yi]`

both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, `pos`

and _{x}`pos`

cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let _{y}`d`

be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles `[d - angle/2, d + angle/2]`

.

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]Output: 3Explanation: The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.

Input: [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]Output: 4Explanation: All points can be made visible in your field of view, including the one at your location.

Input: points = [[1,0],[2,1]], angle = 13, location = [1,1]Output: 1Explanation: You can only see one of the two points, as shown above.

```
package com.vc.hard;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class MaximumNumberOfVisiblePoints {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
List<Double> angles = new ArrayList<>();
int locationX = location.get(0);
int locationY = location.get(1);
int overlap = 0;
for(List<Integer> point: points) {
int pointX = point.get(0);
int pointY = point.get(1);
if(pointX == locationX && pointY == locationY) overlap++;
else {
int dx = pointX - locationX;
int dy = pointY - locationY;
double angleForPoint = Math.atan2(dy, dx) * (180 / Math.PI);
angles.add(angleForPoint);
}
}
Collections.sort(angles);
/**
Handle a case where rotation completes 360 degree
Because of which few points which are on the edge of 360 and before the 360, Will come in the sliding window of points of which are at the start of the rotation and which as angle of 0 degree
*/
List<Double> anglesCompleteCircle = new ArrayList<>(angles);
for(Double a: angles) anglesCompleteCircle.add(a + 360);
//Sliding window over angles to see if they are within field of view of rotation
int end = 1, start = 0, res = 0;
while(end < anglesCompleteCircle.size()) {
while(anglesCompleteCircle.get(end) - anglesCompleteCircle.get(start) > angle) {
start++;
}
res = Math.max(res, end - start + 1);
end++;
}
return overlap + res;
}
}
```

O(N + N logN) Where

N is number of elements in an input array points

O(N) Where

N is number of elements in an input array points