📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division1213import turtle1415from polygon import circle, arc1617# LEVEL 0 PRIMITIVES18# fd, bk, lt, rt, pu, pd1920def fd(t, length):21t.fd(length)2223def bk(t, length):24t.bk(length)2526def lt(t, angle=90):27t.lt(angle)2829def rt(t, angle=90):30t.rt(angle)3132def pd(t):33t.pd()3435def pu(t):36t.pu()373839# LEVEL 1 PRIMITIVES are simple combinations of Level 0 primitives.40# They have no pre- or post-conditions.4142def fdlt(t, n, angle=90):43"""forward and left"""44fd(t, n)45lt(t, angle)4647def fdbk(t, n):48"""forward and back, ending at the original position"""49fd(t, n)50bk(t, n)5152def skip(t, n):53"""lift the pen and move"""54pu(t)55fd(t, n)56pd(t)5758def stump(t, n, angle=90):59"""Makes a vertical line and leave the turtle at the top, facing right"""60lt(t)61fd(t, n)62rt(t, angle)6364def hollow(t, n):65"""move the turtle vertically and leave it at the top, facing right"""66lt(t)67skip(t, n)68rt(t)697071# LEVEL 2 PRIMITIVES use primitives from Levels 0 and 172# to draw posts (vertical elements) and beams (horizontal elements)73# Level 2 primitives ALWAYS return the turtle to the original74# location and direction.7576def post(t, n):77"""Makes a vertical line and return to the original position"""78lt(t)79fdbk(t, n)80rt(t)8182def beam(t, n, height):83"""Makes a horizontal line at the given height and return."""84hollow(t, n*height)85fdbk(t, n)86hollow(t, -n*height)8788def hangman(t, n, height):89"""Makes a vertical line to the given height and a horizontal line90at the given height and then return.91This is efficient to implement, and turns out to be useful, but92it's not so semantically clean."""93stump(t, n * height)94fdbk(t, n)95lt(t)96bk(t, n*height)97rt(t)9899def diagonal(t, x, y):100"""Makes a diagonal line to the given x, y offsets and return"""101from math import atan2, sqrt, pi102angle = atan2(y, x) * 180 / pi103dist = sqrt(x**2 + y**2)104lt(t, angle)105fdbk(t, dist)106rt(t, angle)107108def vshape(t, n, height):109diagonal(t, -n/2, height*n)110diagonal(t, n/2, height*n)111112def bump(t, n, height):113"""Makes a bump with radius n at height*n114"""115stump(t, n*height)116arc(t, n/2.0, 180)117lt(t)118fdlt(t, n*height+n)119120121"""122The letter-drawing functions all have the precondition123that the turtle is in the lower-left corner of the letter,124and postcondition that the turtle is in the lower-right125corner, facing in the direction it started in.126127They all take a turtle as the first argument and a size (n)128as the second. Most letters are (n) units wide and (2n) units129high.130131"""132133def draw_a(t, n):134diagonal(t, n/2, 2*n)135beam(t, n, 1)136skip(t, n)137diagonal(t, -n/2, 2*n)138139def draw_b(t, n):140bump(t, n, 1)141bump(t, n, 0)142skip(t, n/2)143144def draw_c(t, n):145hangman(t, n, 2)146fd(t, n)147148def draw_d(t, n):149bump(t, 2*n, 0)150skip(t, n)151152def draw_ef(t, n):153hangman(t, n, 2)154hangman(t, n, 1)155156def draw_e(t, n):157draw_ef(t, n)158fd(t, n)159160def draw_f(t, n):161draw_ef(t, n)162skip(t, n)163164def draw_g(t, n):165hangman(t, n, 2)166fd(t, n/2)167beam(t, n/2, 2)168fd(t, n/2)169post(t, n)170171def draw_h(t, n):172post(t, 2*n)173hangman(t, n, 1)174skip(t, n)175post(t, 2*n)176177def draw_i(t, n):178beam(t, n, 2)179fd(t, n/2)180post(t, 2*n)181fd(t, n/2)182183def draw_j(t, n):184beam(t, n, 2)185arc(t, n/2, 90)186fd(t, 3*n/2)187skip(t, -2*n)188rt(t)189skip(t, n/2)190191def draw_k(t, n):192post(t, 2*n)193stump(t, n, 180)194vshape(t, 2*n, 0.5)195fdlt(t, n)196skip(t, n)197198def draw_l(t, n):199post(t, 2*n)200fd(t, n)201202def draw_n(t, n):203post(t, 2*n)204skip(t, n)205diagonal(t, -n, 2*n)206post(t, 2*n)207208def draw_m(t, n):209post(t, 2*n)210draw_v(t, n)211post(t, 2*n)212213def draw_o(t, n):214skip(t, n)215circle(t, n)216skip(t, n)217218def draw_p(t, n):219bump(t, n, 1)220skip(t, n/2)221222def draw_q(t, n):223draw_o(t, n)224diagonal(t, -n/2, n)225226def draw_r(t, n):227draw_p(t, n)228diagonal(t, -n/2, n)229230def draw_s(t, n):231fd(t, n/2)232arc(t, n/2, 180)233arc(t, n/2, -180)234fdlt(t, n/2, -90)235skip(t, 2*n)236lt(t)237238def draw_t(t, n):239beam(t, n, 2)240skip(t, n/2)241post(t, 2*n)242skip(t, n/2)243244def draw_u(t, n):245post(t, 2*n)246fd(t, n)247post(t, 2*n)248249def draw_v(t, n):250skip(t, n/2)251vshape(t, n, 2)252skip(t, n/2)253254def draw_w(t, n):255draw_v(t, n)256draw_v(t, n)257258def draw_x(t, n):259diagonal(t, n, 2*n)260skip(t, n)261diagonal(t, -n, 2*n)262263def draw_v(t, n):264skip(t, n/2)265diagonal(t, -n/2, 2*n)266diagonal(t, n/2, 2*n)267skip(t, n/2)268269def draw_y(t, n):270skip(t, n/2)271stump(t, n)272vshape(t, n, 1)273rt(t)274fdlt(t, n)275skip(t, n/2)276277def draw_z(t, n):278beam(t, n, 2)279diagonal(t, n, 2*n)280fd(t, n)281282def draw_(t, n):283# draw a space284skip(t, n)285286if __name__ == '__main__':287288# create and position the turtle289size = 20290bob = turtle.Turtle()291292for f in [draw_h, draw_e, draw_l, draw_l, draw_o]:293f(bob, size)294skip(bob, size)295296turtle.mainloop()297298299