python turtle模塊(三)畫等邊三角形,用圓形組成等邊三角形陣列


#!/user/bin/env python
# -*- coding:UTF-8 -*-
import math
import turtle
import day2

# 畫一個等邊三角形 邊長作為參數
def draw_a_isoscelestriangle(length):

    print(turtle.screensize())

    # 設置繪圖速度
    turtle.speed(1)
    turtle.penup()
    turtle.setx(300-length/2)
    turtle.sety(300-length)
    turtle.pendown()
    turtle.fd(length)
    # 設置畫筆角度為120
    turtle.seth(120)
    turtle.fd(length)
    # 設置畫筆角度為240度
    turtle.seth(-120)
    turtle.fd(length)


def draw_a_isoscelestriangle_by_circle(floors, radius):
    # 需要畫N層 因為range函數是左閉右開的,所以+1處理
    floors = floors+1
    for floor in range(floors):
        turtle.penup()
        # 每層X坐標左移一個半徑,第0層不左移
        turtle.setx(0-radius*floor)
        # 上下兩層圓正好相切的話,Y坐標需要下移根號下3乘以半徑的距離
        turtle.sety(300-radius*floor*math.sqrt(3))
        # 畫每一層的圓
        for i in range(floor):
            turtle.pendown()
            turtle.seth(0)
            day2.draw_a_circle(radius)
            turtle.penup()
            # 畫好一個圓,右移一個直徑的距離
            turtle.fd(2 * radius)

if __name__ == "__main__":

    # 等邊三角形邊長
    length = 50

    # 圓半徑
    radius = 50

    # 圓的層數
    floors = 6

    # 畫一個等邊三角形
    draw_a_isoscelestriangle(length)

    draw_a_isoscelestriangle_by_circle(floors, radius)

執行效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM