Maximum Points View Angle
Last updated
Last updated
Given a set of points along a Cartesian coordinate system, the position of a camera angle, and an angle that represents the cameras field of view, find the rotational angle of the camera that maximizes the number of points it sees in the graph.
The following example should return 180 degrees.
This example should return 270 degrees.
The Idea: Transform this question from a 2D into 1D. If we find the angle of the camera to every other point in the graph, then we could obtain a list of angles. The angle between any two points can be found by . If we sort this array, we obtain an ascending list of degrees that are between degrees 0 and 360. Iterating through this list would then allow us to rotate about the graph in the anti-clockwise direction. We know that camera can only accept points that are within the field of view of the camera, so that it would follow that if difference between any two angles in the graph are less than or equal to the field of view of the camera, then they are also viewable by the camera. To find the maximum amount of points within this field of view, we need to find a fixed window that carries the most points. We can achieve this in linear time by maintaining a left and right pointer. The algorithm for this goes a follows: first increment the right pointer until the difference between the left and right angles exceed the field of view of the camera. We can then reobtain the field of view by incrementing the left pointer until the difference between the left and right point become less or equal to the field of view. Continue rotating through this process until the right pointer reaches the final angle.
Complexity: O(nlogn) time and O(n) space, where n is the number of points