像计算机科学家一样学习python(3)

案例研究:接口设计

Python学习笔记-像计算机科学家一样学习python(3)

案例研究:接口设计

4.3 练习

Python
# 1 封装
import turtle
def square(t): 
    for i in range(4):
        t.fd(100)
        t.lt(90)
    print(t)
    turtle.mainloop()
bob = turtle.Turtle()
square(bob)

# 2 泛化
import turtle
def square(t,length): 
    for i in range(4):
        t.fd(length)
        t.lt(90)
    print(t)
    turtle.mainloop()
bob = turtle.Turtle()
square(bob,200)

# 3 泛化
import turtle
def square(t,length): 
    for i in range(4):
        t.fd(length)
        t.lt(90)
    print(t)
    turtle.mainloop()
def polygon(t,length,n): 
    for i in range(n):
        t.fd(length)
        t.lt(360/n)
    print(t)
    turtle.mainloop()
bob = turtle.Turtle()
polygon(bob,200,6)

# 4 接口设计
import turtle
import math

def polygon(t,length,n): 
    for i in range(n):
        t.fd(length)
        t.lt(360/n)
    print(t)
    turtle.mainloop()

def circle(r): 
    polygon(bob,2*math.pi/100,100*r)

bob = turtle.Turtle()
bob.delay = 2
# 此处bob.delay没有起作用,原因可能是Circle没有调用t?
circle(10)

# 此处的示例:
def circle(t,r):
    circumference = 2 * math.pi * r
    n = int(circumference / 3) + 1
    length = circumference / n
    polygon(t, n, length)
# 那么则会变成

import turtle
import math

def polygon(t,n,length): 
    n = int(n) + 1
    for i in range(n):
        t.fd(length)
        t.lt(360/n)
        t.delay = 1
# t.delay依然没有起作用
    print(t)
    turtle.mainloop()

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

bob = turtle.Turtle()
circle(bob,100)

# 5 重构
import turtle
import math

def arc(t,r,angle):
    for i in range(100*r*angle//360):
        t.fd(2*math.pi/100)
        t.lt(360/100/r)
    print(t)

bob = turtle.Turtle()
bob.delay = 0.01
arc(bob,10,90)
turtle.mainloop()

练习

4-1

在画花之前,我们先完善arc函数

Python
import turtle
import math


def polyline(t, n, length, angle):
    """Draws n line segments.

    t: Turtle object
    n: number of line segments
    length: length of each segment
    angle: degrees between segments
    """
    for i in range(n):
        t.fd(length)
        t.lt(angle)

def arc(t, r, angle):
    """Draws an arc with the given radius and angle.

    t: Turtle
    r: radius
    angle: angle subtended by the arc, in degrees
    """
    arc_length = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_length / 4) + 3
    step_length = arc_length / n
    step_angle = float(angle) / n
    # making a slight left turn before starting reduces the error caused by the linear approximation of the arc (虽然我并不知道为什么)
    t.lt(step_angle/2)
    polyline(t, n, step_length, step_angle)
    t.rt(step_angle/2)

bob = turtle.Turtle()
arc(bob,100,360)
turtle.mainloop()

​ 然后从一个花瓣开始,泛化成为花。

Python
import turtle
import math


def polyline(t, n, length, angle):
    """Draws n line segments.

    t: Turtle object
    n: number of line segments
    length: length of each segment
    angle: degrees between segments
    """
    for i in range(n):
        t.fd(length)
        t.lt(angle)

def arc(t, r, angle):
    """Draws an arc with the given radius and angle.

    t: Turtle
    r: radius
    angle: angle subtended by the arc, in degrees
    """
    arc_length = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_length / 4) + 3
    step_length = arc_length / n
    step_angle = float(angle) / n
    # making a slight left turn before starting reduces the error caused by the linear approximation of the arc
    t.lt(step_angle/2)
    polyline(t, n, step_length, step_angle)
    t.rt(step_angle/2)

def petal(t, r, angle):
    """Draws a petal using two arcs.

    t: Turtle
    r: radius of the arcs
    angle: angle (degrees) that subtends the arcs
    """
    for i in range(2):
        arc(t, r, angle)
        t.lt(180-angle)

def flower(t, n, r, angle):
    """Draws a flower with n petals.

    t: Turtle
    n: number of petals
    r: radius of the arcs
    angle: angle (degrees) that subtends the arcs
    """
    for i in range(n):
        petal(t, r, angle)
        t.lt(360.0/n)

bob = turtle.Turtle()
flower(bob,10,100,90)
turtle.mainloop()

4-2

显然,这是由数个三角形拼接成的。重构一下之前的函数,先画一个三角形,然后泛化。

Python
import turtle
import math

def polyline(t, length, angle, bot_length, top_angle):
        t.fd(length)
        t.lt(angle)
        t.fd(bot_length)
        t.lt(angle)
        t.fd(length)
        t.lt(top_angle)


def pie(t,n): 
    for i in range(n):
        length = 100
        angle = 90 * (n + 2) / n
        bot_length = 2 * length * math.sin(math.pi /  n)
        top_angle = 180
        polyline(t, length, angle, bot_length, top_angle)

bob = turtle.Turtle()
# 测试用:
# polyline(bob, 100, 120, 100, 60)
pie(bob, 7)
turtle.mainloop()
最后编辑于
文章链接: http://pheustal.com/2019/12-04/thinkpython3
本作品采用CC-BY-SA许可。