Maximum Number of Visible Points Problem
Description
LeetCode Problem 1610.
You are given an array points, an integer angle, and your location, where location = [pos_x, pos_y] and points[i] = [x_i, y_i] 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_x and pos_y 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 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.
Example 1:

Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
Output: 3
Explanation: 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.
Example 2:
Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
Output: 4
Explanation: All points can be made visible in your field of view, including the one at your location.
Example 3:

Input: points = [[1,0],[2,1]], angle = 13, location = [1,1]
Output: 1
Explanation: You can only see one of the two points, as shown above.
Constraints:
1 <= points.length <= 10^5points[i].length == 2location.length == 20 <= angle < 3600 <= pos_x, pos_y, x_i, y_i <= 100
Solution
Approach: Sort the Angles, Slide a Window
First set aside the points at your own location: they are visible from any direction, so count them separately as here.
For every other point, compute its angle in degrees as seen from location: atan2(y - posy, x - posx), converted from radians. Now the question is: what is the largest number of angles that fit in an arc of width angle?
Sort the angles. The best arc can be assumed to start at some point's angle, and then the visible points are a contiguous run of the sorted angles, except that the arc may wrap past 180° to -180°. To handle that, append each angle plus 360 to the end of the list. Now every arc is a contiguous window of this doubled list.
Slide a window with two pointers: for each right, move left forward while angles[right] - angles[left] > angle. The window then holds the points visible with the arc starting at angles[left], and the answer is the largest window size plus here. A window can never contain both a point and its copy, because they are 360° apart and angle < 360.
The field of view is inclusive, and atan2 is computed in floating point, so two points that are exactly angle degrees apart can come out a tiny bit more than angle apart. Compare with a small tolerance (1e-9) so they still count.
Complexity: O(n log n) time for sorting and O(n) extra space.
import math
class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
px, py = location
here = 0 # points at the location are always visible
angles = []
for x, y in points:
if x == px and y == py:
here += 1
else:
angles.append(math.degrees(math.atan2(y - py, x - px)))
angles.sort()
# Append every angle + 360 so a view that wraps past 180 degrees is a plain window.
angles += [a + 360 for a in angles]
best = left = 0
eps = 1e-9 # the field of view is inclusive; absorb rounding in atan2
for right in range(len(angles)):
while angles[right] - angles[left] > angle + eps:
left += 1
best = max(best, right - left + 1)
# A window never holds a point twice: angle < 360, so a and a + 360 never fit together.
return best + here