猫史档案馆


【Python作品分享】爱心【作业帖】

用户:寡欢j7vl寡欢j7vl查看:0 回复:1 评论:0 创建时间:2022-11-22T15:06:03


【作品展示】

center_image

 

【作品介绍】

 

【作品源代码】

import numpy as np
import math
from tkinter import *
import random

pi = math.pi

width = 680
height = 680
center_x = width/2
center_y = height/2
color = "#990000"
c2 = '#990000'

#x = 16sint^3
#y = 13cost - 5cos2t - 2cos3t -cos4t


def heart_function(t):
    # t = np.linspace(0,2*pi)
    x = 16*(np.sin(t))**3
    y = 13*np.cos(t) - 5*np.cos(2*t) - 2*np.cos(3*t) - np.cos(4*t)

    return x, y
#在一定范围内随机抖动


def scatter(x, y, ratio):
    while(1):
        rand = random.random()
        if(rand != 0):
            break
    x = - ratio*math.log10(rand)
    y = - ratio*math.log2(rand)
    return x, y


#画图

def Graph(x_s, y_s, x0, y0, c=color, shrink_size=11):
    w1, w2 = shrink_size, shrink_size-1  # 缩放倍数
    # 内部填充
    add_scatters(x_s, y_s, x0, y0, shrink_size=w2)
    #填充外部
    for j in range(len(x_s)):
        ratio1 = math.sin(random.random())
        ratio2 = math.cos(random.random())
        dx, dy = scatter(x_s[j], y_s[j], ratio1)
        dx_2, dy_2 = scatter(x_s[j], y_s[j], ratio2)
        coord = x0 + (x_s[j] - dx) * w1, y0 - (y_s[j] - dy) * w1, x0 + \
            (x_s[j] - dx) * w1 + 1, y0 - (y_s[j] - dy) * w1 + 1
        coord2 = x0 + (x_s[j] - dx_2) * w2, y0 - (y_s[j] - dy_2) * \
            w2, x0 + (x_s[j] - dx_2) * w2 + 1, y0 - (y_s[j] - dy_2) * w2 + 1
        if abs(x * w1) < width / 2 and abs(y * w2) < height / 2:
            cv.create_line(coord, width=9, fill=c)
            cv.create_line(coord2, width=8, fill=c2)

    cv.update()


def add_scatters(x_s, y_s, x0, y0, c=color, shrink_size=11):
    w1, w2 = shrink_size, shrink_size - 0.8  # 缩放倍数
    for j in range(len(x_s)):
        count = x_s.count(x_s[j])
        # print(count)
        if count == 2:
            x_temp = x_s[j + 1:]
            if(x_temp.__contains__(x_s[j])):
                # print(1)
                index = x_s.index(x_s[j], j+1)
                k_min = min(y_s[j], y_s[index])
            # print(k_min)
                k_max = max(y_s[j], y_s[index])
            # print(k_max)
                step2 = (k_max - k_min) / 15
                for k in np.arange(k_min, k_max + step2, step2):
                    dx, dy = scatter(x, y, random.random())
                    coord4 = (x_s[j]) * w1 + x0 + dx*w1/2, y0 - k * w1, x_s[j] * w1 + \
                        x0, y0 - \
                        (k-dy + random.randint(int(k_min/3), int(k_max/3))) * w1
                    cv.create_line(coord4, fill=c2)

        # cv.update()


def move(frame, xs, ys, x0, y0, c=color):
    for i in range(frame):
        cv.delete('all')
        if(i % 3 == 0):
            Graph(xs, ys, x0, y0, c=color, shrink_size=15)

        else:
            Graph(xs, ys, x0, y0, c=color, shrink_size=13)


if __name__ == '__main__':
    x_s = []

    y_s = []
    step = pi/(2*400)
    tmin = np.float(0)
    tmax = np.float(2 * pi)
    for t in np.arange(tmin + step, tmax + step, step):
        x, y = heart_function(t)
        x_s.append((x < 0) and (-round(abs(x), 10)) or (round(x, 10)))
        y_s.append((y < 0) and (-round(abs(y), 10)) or (round(y, 10)))

    # print(x_s)
    win = Tk()
    cv = Canvas(win, width=width, height=height, bg='black')
    cv.pack()

    frame = 200
    move(frame, x_s, y_s, center_x, center_y)
    # add_scatters(x_s,y_s,center_x,center_y)
    win.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
我爱吃花卷我爱吃花卷

香收藏

点赞0


评论