AllenDowney / ThinkPython2

LaTeX source and supporting code for Think Python, 2nd edition, by Allen Downey.
Other
2.5k stars 1.65k forks source link

Section 4.6 circle function should ensure n >= 3 #22

Closed hayeah closed 8 years ago

hayeah commented 8 years ago

hi,

Thank you for your excellent book. A sharp-eyed student in my class raised a question with the circle function:

def circle(t, r):
    circumference = 2 * math.pi * r
    n = int(circumference / 3) + 1
    length = circumference / n
    polygon(t, n, length)

We are wondering if n should always be larger than 3, otherwise the polygon function wouldn't work.

So we should have this line instead:

n = int(circumference / 3) + 3

The relevant line in the tex source is here:

https://github.com/AllenDowney/ThinkPython2/blob/9b78593fe9300854de6d7f63fdb722a6a58405b3/book/book.tex#L3210

The polygon function draws a regular n-sided polygon, defined this way:

def polygon(t, n, length):
    angle = 360 / n
    for i in range(n):
        t.fd(length)
        t.lt(angle)
AllenDowney commented 8 years ago

Good idea. I've made that change in the book and in polygon.py

Thanks!