矩形缩放程序

矩形缩放程序

Principle of linear variable rectangle scaling矩形缩放原理运行图

Principle of linear variable rectangle scaling矩形缩放原理运行图

"""
   矩形缩放程序,本程序演示了矩形缩放原理。
"""

from turtle import *

def draw_rect(left,top,right,bottom):
    """根据左上角坐标和右下角坐标画矩形"""
    width = right - left
    height = top - bottom
    t.goto(left,top)
    t.pendown()
    t.goto(left+width,top)
    t.goto(right,bottom)
    t.goto(left,top-height)
    t.goto(left,top)
    t.penup()

left,top = -20,10        # 左上角坐标
right,bottom = 20,-10    # 右下角坐标
width = right - left     # 矩形宽度
height = top - bottom    # 矩形高度
x0 = left + width/2      # 中心点x坐标
y0 = bottom + height/2   # 中心点y坐标
      
t = Turtle()
t.penup()
t.color('blue')
t.screen.delay(0)

draw_rect(left,top,right,bottom)

for scale in range(2,150):        # 缩放比例
    scale = scale / 10
    x1 = x0 - 0.5 * width * scale
    y1 = y0 + 0.5 * height * scale
    x2 = x0 + 0.5 * width * scale
    y2 = y0 - 0.5 * height * scale
    draw_rect(x1,y1,x2,y2)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。