Skip to content

Instantly share code, notes, and snippets.

@roskakori
Created December 9, 2020 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roskakori/868b27375a31a14ad19c48930000ac3d to your computer and use it in GitHub Desktop.
Save roskakori/868b27375a31a14ad19c48930000ac3d to your computer and use it in GitHub Desktop.
Test for Zoom calculations
MIN_DPI = 200
MM_PER_INCH = 25.4
class ZoomCalculator:
def __init__(
self,
view_width_in_mm: float,
view_width_in_pixel: int,
view_height_in_pixel: int,
image_width_in_pixel: int,
image_height_in_pixel: int,
):
view_width_in_inch = view_width_in_mm / MM_PER_INCH
view_ratio = view_width_in_pixel / view_height_in_pixel
image_ratio = image_width_in_pixel / image_height_in_pixel
is_scale_on_width = view_ratio > image_ratio
if is_scale_on_width:
self.min_zoom = view_width_in_pixel / image_width_in_pixel
min_image_width_in_inch = image_width_in_pixel / MIN_DPI
self.max_zoom = min_image_width_in_inch / view_width_in_inch
else:
self.min_zoom = view_height_in_pixel / image_height_in_pixel
view_height_in_inch = view_width_in_inch * view_ratio
min_image_height_in_inch = image_height_in_pixel / MIN_DPI
self.max_zoom = min_image_height_in_inch / view_height_in_inch
self.ugliness = max(0.0, (self.min_zoom / self.max_zoom) - 1.0)
def __str__(self):
return f"ZoomCalculator(min_zoom={self.min_zoom}, max_zoom={self.max_zoom}, ugliness={self.ugliness})"
def test_can_compute_zoom():
print()
print("*****************************")
print(ZoomCalculator(150.0, 600, 200, 800, 600))
print(ZoomCalculator(150.0, 600, 200, 8000, 6000))
print(ZoomCalculator(30.0, 300, 500, 800, 600))
print(ZoomCalculator(30.0, 300, 500, 200, 200))
print("*****************************")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment