#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2020/04/23 15:00:16 +0900> """ Calc camera fov area. * +x : right, +y : top, +z : front Windows10 x64 1909 + Python 3.7.7 64bit """ import math def get_rot_pos(x, y, ang): a = math.radians(ang) xr = x * math.cos(a) - y * math.sin(a) yr = x * math.sin(a) + y * math.cos(a) return xr, yr def dump_pos(p, s): print("%s = (%f, %f, %f)" % (s, p[0], p[1], p[2])) def get_fov_area(cx, cy, cz, cxrot, fov, w, h): print("Camera pos = (%f, %f, %f)" % (cx, cy, cz)) print("Camera x rot = %f" % cxrot) print("Camera fov = %f" % fov) print("Window size = %d x %d" % (w, h)) d = (h/2) / math.tan(math.radians(fov/2)) p1 = (-w/2, h/2, -d) p2 = (w/2, -h/2, -d) # Rotate by Camera angle z, y = get_rot_pos(p1[2], p1[1], -cxrot) p1r = (p1[0], y, z) z, y = get_rot_pos(p2[2], p2[1], -cxrot) p2r = (p2[0], y, z) y = -cy xr, yr, zr = p1r z = y * zr / yr + cz x = y * xr / yr topleft = (x, 0, z) xr, yr, zr = p2r z = y * zr / yr + cz x = y * xr / yr bottomright = (x, 0, z) dump_pos(p1, "p1") dump_pos(p2, "p2") dump_pos(p1r, "p1r") dump_pos(p2r, "p2r") dump_pos(topleft, "topleft") dump_pos(bottomright, "bottomright") get_fov_area(0, 40, 12, -65, 60, 1280, 720)