Skip to content

Instantly share code, notes, and snippets.

@AndreaPasqualini
Created June 26, 2020 22:38
Show Gist options
  • Save AndreaPasqualini/1b39ef7d17f5b0a4f576a6cc73d7561a to your computer and use it in GitHub Desktop.
Save AndreaPasqualini/1b39ef7d17f5b0a4f576a6cc73d7561a to your computer and use it in GitHub Desktop.
Small Python+Numpy function to compute screen sizes given aspect ratio and diagonal length.
from numpy import array, sqrt
def xy_sizes(diagonal, aspect_ratio, units='centimeters'):
r"""
This function computes the horizontal and vertical lengths of a screen
given its diagonal length and the aspect ratio. The system of equations to
be solved admits a unique feasible solution in closed form. The system is
given by these two equations:
x / y = aspect_ratio
x^2 + y^2 = diagonal^2
Solve for x and y, keeping in mind that both have to be strictly positive,
and you obtain the formulae below.
"""
y = sqrt( diagonal**2 / (1 + aspect_ratio**2) )
x = aspect_ratio * y
sizes = array((x, y))
if units == 'centimeters':
sizes *= 2.54
return sizes
sl315 = xy_sizes(15, 3/2)
t470 = xy_sizes(14, 16/9)
n550 = xy_sizes(15.6, 16/9)
xps15 = xy_sizes(15.6, 16/10)
xps17 = xy_sizes(17, 16/10)
mbp16 = xy_sizes(16, 16/10)
print( '| Device | Width (cm) | Height (cm) |')
print( '|------------------------|------------|-------------|')
print(f'| Surface Laptop 3, 15" | {sl315[0]:10.2f} | {sl315[1]:11.2f} |')
print(f'| ThinkPad T470, 14" | {t470[0]:10.2f} | {t470[1]:11.2f} |')
print(f'| ASUS VivoBook Pro, 15" | {n550[0]:10.2f} | {n550[1]:11.2f} |')
print(f'| Dell XPS 15 | {xps15[0]:10.2f} | {xps15[1]:11.2f} |')
print(f'| Dell XPS 17 | {xps17[0]:10.2f} | {xps17[1]:11.2f} |')
print(f'| MacBook Pro 16" | {mbp16[0]:10.2f} | {mbp16[1]:11.2f} |')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment