You are given a camera in 3D space, defined by its position camera_pos, its direction vector camera_dir, and a field of view angle fov_degrees (in degrees). You are also given a list of objects, where each object is a point in 3D space represented as [x, y, z].
An object is visible if the angle between the camera's direction vector and the vector from the camera's position to the object is less than or equal to half of fov_degrees. An object located exactly at the camera's position is not visible, because the direction to it is undefined.
Return a list of booleans where the i-th value is True if the i-th object is within the camera's field of view, and False otherwise.
Function Signature
def visible_objects(camera_pos: list[float], camera_dir: list[float], fov_degrees: float, objects: list[list[float]]) -> list[bool]:
Rules
-
camera_dir
does not have to be a unit vector. Only its direction matters.
-
Angles are compared with an absolute tolerance of
1e-9
radians, so a point that lies exactly on the edge of the field of view (such as
[1, 0, 1]
in Example 1) counts as visible.
-
An object whose distance from
camera_pos
is below
1e-9
is treated as being at the camera position and is not visible.
-
The output has one entry per object, in input order.
Constraints
-
1 <= len(objects) <= 10^4
-
camera_pos
,
camera_dir
and each object are lists of exactly 3 floats.
-
camera_dir
is not the zero vector.
-
0 < fov_degrees <= 360
Examples
Input: camera_pos = [0, 0, 0], camera_dir = [0, 0, 1], fov_degrees = 90,
objects = [[0, 0, 5], [5, 0, 0], [1, 0, 1], [0, 0, -1], [-1, 0, 1]]
Output: [True, False, True, False, True]
The camera looks along the positive z-axis with 45 degrees allowed on each side. [0, 0, 5] is straight ahead (0 degrees), [5, 0, 0] is 90 degrees off-axis, [1, 0, 1] and [-1, 0, 1] sit exactly on the 45 degree edge and count as visible, and [0, 0, -1] is directly behind (180 degrees).
Input: camera_pos = [1, 2, 3], camera_dir = [1, 0, 0], fov_degrees = 180,
objects = [[10, 2, 3], [-5, 2, 3], [2, 100, 3], [1, 2, 3]]
Output: [True, False, True, False]
Half the field of view is 90 degrees. The offsets from the camera are (9, 0, 0) (0 degrees), (-6, 0, 0) (180 degrees), (1, 98, 0) (about 89.4 degrees, slightly forward) and (0, 0, 0), which is the camera position itself.
Input: camera_pos = [0, 0, 0], camera_dir = [0, 2, 0], fov_degrees = 270,
objects = [[0, -1, 0], [2, -1, 0], [3, 0, 0], [1, -2, 0]]
Output: [False, True, True, False]
Half the field of view is 135 degrees, so the camera also sees part of the space behind it. Measured from the positive y direction, the four points sit at 180, about 116.6, 90 and about 153.4 degrees, and only the second and third are within 135.