Skip to content

Instantly share code, notes, and snippets.

@rileypeterson
Last active February 8, 2020 07:50
Show Gist options
  • Save rileypeterson/834a6a75346662161bc070b3e7020c20 to your computer and use it in GitHub Desktop.
Save rileypeterson/834a6a75346662161bc070b3e7020c20 to your computer and use it in GitHub Desktop.
Recursion baby
pts = [(-1, 1), (0, 1), (1, 0), (2, 2), (3, 3), (4, 2)]
def fd(*points, values=None):
if values is None:
values = []
if len(points) == 2:
x0, y0 = points[0]
x1, y1 = points[1]
val = (y1 - y0) / (x1 - x0)
values.append(val)
return val, values
num = (fd(*points[1:], values=values)[0] - fd(*points[:-1], values=values)[0])
denom = (points[1:][-1][0] - points[:-1][0][0])
val = num / denom
values.append(val)
return val, values
l = [(0, 1), (1, 0), (2, 2)]
print(fd(*l))
l = [(1, 0), (2, 2), (3, 3)]
print(fd(*l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment