Skip to content

Instantly share code, notes, and snippets.

@lennyferguson lennyferguson/
Last active Jan 14, 2019

What would you like to do?
Example of a function that generates a range of font sizes from min -> max -> min modeling the traversal of a sin wave
from typing import List, TypeVar, Callable
from math import sin, pi
# TypeVar for a generic number type
Num = TypeVar('Num', int, float)
Function that generates a List of integers that represent a interpolation of font sizes from min to max along
a sin wave function that has been transformed to have the desired properties.
Returns a List of Integers (List[int]) of size num_pages containing font sizes interpolated between min and max.
def get_font_sizes(num_pages: int, min: int = 6, max: int = 72, round_strategy: Callable[[Num], int] = round) -> List[int]:
# Define a function y that transforms an input t from [0 .. 2pi] to a font size between min and max
amplitude = (max - min) / 2.0
y = lambda t: round_strategy(amplitude * sin(t - (pi / 2.0)) + amplitude + min)
# Transform the range of inputs from [0 .. N] to [0 .. 2*pi] where N = num_pages
delta = 2.0 * pi / num_pages
# Invoke y with inputs translated to [0 .. 2pi] using delta constant
return [ y(t) for t in [ delta * n for n in range(num_pages) ] ]
# Example of interpolating over 40 pages from min to max back to min between 6 and 72
pages = 40
sizes = get_font_sizes(pages)
print("Example 1:\n{}\n".format(sizes))
# This example interpolates between size 10 and 60 instead of the default 6 and 72
pages = 100
sizes = get_font_sizes(pages, 10, 60)
print("Example 2:\n{}\n".format(sizes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.