作品展示
背景需求
分享Lab|更新啦~图层顺序挑战游戏 - 小红书 (xiaohongshu.com)https://www.xiaohongshu.com/discovery/item/62f21760000000000900ec6d?app_platform=android&ignoreEngage=true&app_version=8.35.0&share_from_user_hidden=true&type=normal&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1715477372
数一数后发现一共有6块三角形。
第一步:设计等边三角形底板卡
小红书上的参考图是左侧边线2等分,右侧和底边4等分,
我设计等边三角形的是底边2等分,左右两侧边线4等分
'''
800图卡上制作一个等边三角形(60度内角),底边2等分(1点),左右两边4等分(3点)
作者:AI对话大师,阿夏
时间:2024年5月11日
'''
from PIL import Image, ImageDraw
import math
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
# 创建800x1200的画布
canvas_width = 800
canvas_height = 800
canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
draw = ImageDraw.Draw(canvas)
# 初始边长和缩放因子
initial_side_length = 250
scale_factor = 2.5
# 计算等边三角形的边长
side_length = initial_side_length * scale_factor
# 计算等边三角形的顶点坐标
center_x = canvas_width // 2
center_y = canvas_height // 2+130
angle = math.radians(30) # 将角度转换为弧度
x1 = center_x - side_length // 2
y1 = center_y + int((side_length * math.sqrt(3)) / 6)
x2 = center_x + side_length // 2
y2 = y1
x3 = center_x
print(x3)
y3 = center_y - int((side_length * math.sqrt(3)) / 3)
# 获取左侧边的左侧点坐标
left_side_x = x1
left_side_y = (y1 + y3) // 2
# 将顶点1和顶点2的连线进行四等分
divide_1_t = 1/2
divide_2_t = 1/2
divide_1_x = x1 + (x2 - x1) * divide_1_t
divide_1_y = y1 + (y2 - y1) * divide_1_t
divide_2_x = x1 + (x2 - x1) * divide_2_t
divide_2_y = y1 + (y2 - y1) * divide_2_t
# divide_3_x = x1 + (x2 - x1) * divide_3_t
# divide_3_y = y1 + (y2 - y1) * divide_3_t
# 将顶点2和顶点3的连线进行四等分
divide_4_t = 1/4
divide_5_t = 1/4 * 2
divide_6_t = 1/4 * 3
divide_4_x = x2 + (x3 - x2) * divide_4_t
divide_4_y = y2 + (y3 - y2) * divide_4_t
divide_5_x = x2 + (x3 - x2) * divide_5_t
divide_5_y = y2 + (y3 - y2) * divide_5_t
divide_6_x = x2 + (x3 - x2) * divide_6_t
divide_6_y = y2 + (y3 - y2) * divide_6_t
# 将顶点3和顶点1的连线进行四等分
divide_7_t = 1/4
divide_8_t = 1/4 * 2
divide_9_t = 1/4 * 3
divide_7_x = x3 + (x1 - x3) * divide_7_t
divide_7_y = y3 + (y1 - y3) * divide_7_t
divide_8_x = x3 + (x1 - x3) * divide_8_t
divide_8_y = y3 + (y1 - y3) * divide_8_t
divide_9_x = x3 + (x1 - x3) * divide_9_t
divide_9_y = y3 + (y1 - y3) * divide_9_t
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 保存绘制好的图像
canvas.save(path + r'\triangle.png')
第二步:查找各个小圆点的坐标,以坐标为顶点,制作7个固定位置的等边三角形。
代码展示:
'''
800图卡上制作一个等边三角形(60度内角),底边2等分(1点),左右两边4等分(3点)+固定7个三角出现的位置和颜色
作者:AI对话大师,阿夏
时间:2024年5月11日
'''
from PIL import Image, ImageDraw
import math
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
# 创建800x1200的画布
canvas_width = 800
canvas_height = 800
canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
draw = ImageDraw.Draw(canvas)
# 初始边长和缩放因子
initial_side_length = 250
scale_factor = 2.5
# 计算等边三角形的边长
side_length = initial_side_length * scale_factor
# 计算等边三角形的顶点坐标
center_x = canvas_width // 2
center_y = canvas_height // 2+130
angle = math.radians(30) # 将角度转换为弧度
x1 = center_x - side_length // 2
y1 = center_y + int((side_length * math.sqrt(3)) / 6)
x2 = center_x + side_length // 2
y2 = y1
x3 = center_x
print(x3)
y3 = center_y - int((side_length * math.sqrt(3)) / 3)
# 获取左侧边的左侧点坐标
left_side_x = x1
left_side_y = (y1 + y3) // 2
# 将顶点1和顶点2的连线进行四等分
divide_1_t = 1/2
divide_2_t = 1/2
divide_1_x = x1 + (x2 - x1) * divide_1_t
divide_1_y = y1 + (y2 - y1) * divide_1_t
divide_2_x = x1 + (x2 - x1) * divide_2_t
divide_2_y = y1 + (y2 - y1) * divide_2_t
# divide_3_x = x1 + (x2 - x1) * divide_3_t
# divide_3_y = y1 + (y2 - y1) * divide_3_t
# 将顶点2和顶点3的连线进行四等分
divide_4_t = 1/4
divide_5_t = 1/4 * 2
divide_6_t = 1/4 * 3
divide_4_x = x2 + (x3 - x2) * divide_4_t
divide_4_y = y2 + (y3 - y2) * divide_4_t
divide_5_x = x2 + (x3 - x2) * divide_5_t
divide_5_y = y2 + (y3 - y2) * divide_5_t
divide_6_x = x2 + (x3 - x2) * divide_6_t
divide_6_y = y2 + (y3 - y2) * divide_6_t
# 将顶点3和顶点1的连线进行四等分
divide_7_t = 1/4
divide_8_t = 1/4 * 2
divide_9_t = 1/4 * 3
divide_7_x = x3 + (x1 - x3) * divide_7_t
divide_7_y = y3 + (y1 - y3) * divide_7_t
divide_8_x = x3 + (x1 - x3) * divide_8_t
divide_8_y = y3 + (y1 - y3) * divide_8_t
divide_9_x = x3 + (x1 - x3) * divide_9_t
divide_9_y = y3 + (y1 - y3) * divide_9_t
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 以顶点3坐标为顶部画一个填充红色、黑色边框、大小为10的等边三角形
triangle_side_length = 470
# 第1种
# 计算新等边三角形的顶点坐标
triangle_x1 = x3 - triangle_side_length // 3
triangle_y1 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x2 = x3 + triangle_side_length // 3
triangle_y2 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x3 = x3
triangle_y3 = y3
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='red')
# 第2种
# 获取顶点2和顶点3连线上3/4的圆点坐标
triangle_center_x = divide_6_x
triangle_center_y = divide_6_y
# 计算新等边三角形的边长
triangle_side_length = 626
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='yellow')
# 第3种
# 获取顶点2和顶点3连线上2/4的圆点坐标
triangle_center_x = divide_5_x
triangle_center_y = divide_5_y
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='green')
# 第4中种 # 获取顶点3和顶点1连线上2/4的圆点坐标
triangle_center_x = divide_8_x
triangle_center_y = divide_8_y
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='blue')
# 第5种 # 获取顶点3和顶点1连线上1/4的圆点坐标
triangle_center_x = divide_7_x
triangle_center_y = divide_7_y
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='pink')
# 第6种
# 获取顶点1和顶点2连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', fill='purple')
# 保存绘制好的图像
canvas.save(path + r'\triangle.png')
通过代码,将6个三角形的图形位置进行固定
第三步:生成不重复的6块三角形排列方式720种
将6个位置的三角形做成函数体,然后测算有多少种不同的排列顺序(显示6个图片有6*5*4*3*2*1=720个不重复排列方法)
'''
项目:图层顺序挑战(三角形版)6块等边三角形,用函数测算有不同的排列方式
作者:AI对话大师,阿夏
时间:20240511
'''
from PIL import Image, ImageDraw
import math,random,os
colors1 = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
ss=626
w=3
#
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
os.makedirs(path,exist_ok=True)
# 创建800x1200的画布
canvas_width = 800
canvas_height = 800
canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
draw = ImageDraw.Draw(canvas)
# 初始边长和缩放因子
initial_side_length = 250
scale_factor = 2.5
# 计算等边三角形的边长
side_length = initial_side_length * scale_factor
# 计算等边三角形的顶点坐标
center_x = canvas_width // 2
center_y = canvas_height // 2+130
angle = math.radians(30) # 将角度转换为弧度
x1 = center_x - side_length // 2
y1 = center_y + int((side_length * math.sqrt(3)) / 6)
x2 = center_x + side_length // 2
y2 = y1
x3 = center_x
y3 = center_y - int((side_length * math.sqrt(3)) / 3)
# 获取左侧边的左侧点坐标
left_side_x = x1
left_side_y = (y1 + y3) // 2
# 将顶点1和顶点2的连线进行四等分
divide_1_t = 1/2
divide_2_t = 1/2
divide_1_x = x1 + (x2 - x1) * divide_1_t
divide_1_y = y1 + (y2 - y1) * divide_1_t
divide_2_x = x1 + (x2 - x1) * divide_2_t
divide_2_y = y1 + (y2 - y1) * divide_2_t
# divide_3_x = x1 + (x2 - x1) * divide_3_t
# divide_3_y = y1 + (y2 - y1) * divide_3_t
# 将顶点2和顶点3的连线进行四等分
divide_4_t = 1/4
divide_5_t = 1/4 * 2
divide_6_t = 1/4 * 3
divide_4_x = x2 + (x3 - x2) * divide_4_t
divide_4_y = y2 + (y3 - y2) * divide_4_t
divide_5_x = x2 + (x3 - x2) * divide_5_t
divide_5_y = y2 + (y3 - y2) * divide_5_t
divide_6_x = x2 + (x3 - x2) * divide_6_t
divide_6_y = y2 + (y3 - y2) * divide_6_t
# 将顶点3和顶点1的连线进行四等分
divide_7_t = 1/4
divide_8_t = 1/4 * 2
divide_9_t = 1/4 * 3
divide_7_x = x3 + (x1 - x3) * divide_7_t
divide_7_y = y3 + (y1 - y3) * divide_7_t
divide_8_x = x3 + (x1 - x3) * divide_8_t
divide_8_y = y3 + (y1 - y3) * divide_8_t
divide_9_x = x3 + (x1 - x3) * divide_9_t
divide_9_y = y3 + (y1 - y3) * divide_9_t
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 以顶点3坐标为顶部画一个填充红色、黑色边框、大小为ss的等边三角形
triangle_side_length = 470
def draw_triangle_1():
# 获取顶点3和顶点1连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = 470
# 第1种
# 计算新等边三角形的顶点坐标
triangle_x1 = x3 - triangle_side_length // 3
triangle_y1 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x2 = x3 + triangle_side_length // 3
triangle_y2 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x3 = x3
triangle_y3 = y3
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[0])
def draw_triangle_2():
# 获取顶点2和顶点3连线上3/4的圆点坐标
triangle_center_x = divide_6_x
triangle_center_y = divide_6_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[1])
def draw_triangle_3():
# 获取顶点2和顶点3连线上2/4的圆点坐标
triangle_center_x = divide_5_x
triangle_center_y = divide_5_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[2])
def draw_triangle_4():
# 获取顶点3和顶点1连线上2/4的圆点坐标
triangle_center_x = divide_8_x
triangle_center_y = divide_8_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[3])
def draw_triangle_5():
# 获取顶点3和顶点1连线上1/4的圆点坐标
triangle_center_x = divide_7_x
triangle_center_y = divide_7_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[4])
def draw_triangle_6():
# 获取顶点1和顶点2连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[5])
# for xx in range(1000):
# 随机打乱函数的顺序(可能会有重复)
# functions = [draw_triangle_1, draw_triangle_2, draw_triangle_3, draw_triangle_4, draw_triangle_5, draw_triangle_6]
# random.shuffle(functions)
import itertools
# 定义函数列表
function_names = ['draw_triangle_1', 'draw_triangle_2', 'draw_triangle_3', 'draw_triangle_4', 'draw_triangle_5', 'draw_triangle_6']
# 生成所有可能的排列,是元祖()
permutations = list(itertools.permutations(function_names))
# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 720
n=1
# 打印所有排列
for permutation in permutations:
# print(permutation)
# 将元组转换为函数对象列表,
functions = [eval(function_name) for function_name in permutation[::-1]]
# # 打印函数对象列表,一长串文字
# print(functions)
# [<function draw_triangle_2 at 0x000001A4B402F3A8>, <function draw_triangle_1 at 0x000001A4B402FB88>, <function draw_triangle_6 at 0x000001A4B4061288>, <function draw_triangle_3 at 0x000001A4B23C5AF8>, <function draw_triangle_4 at 0x000001A4B4061168>, <function draw_triangle_5 at 0x000001A4B40611F8>]
# 运行一个7元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
random.shuffle(colors)
# 调用函数绘制等边三角形
for func in functions:
func()
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 保存绘制好的图像
canvas.save(path + fr'\{n:03d}.png')
n+=1
# # 调用函数绘制等边三角形
# draw_triangle_1()
# draw_triangle_2()
# draw_triangle_3()
# draw_triangle_4()
# draw_triangle_5()
# draw_triangle_6()
# # 保存绘制好的图像
# canvas.save(path + fr'\{xx:02d}triangle.png')
720种6图的图层排列方法,全部变成图片。
存在问题:
本次图层排列需要6个颜色,而720种里有部分三角形被遮挡覆盖了,只能看到3-5个图形
第四步:检测图片上的颜色数量
检测图片上的颜色种类,如果小于8种(6种彩色+黑+白),就自动删除。
为了不存在JPG2里,
'''
项目:图层顺序挑战(三角形版)6块等边三角形,720种不同排列方式,删除不是6种颜色的图片 420种显示6个颜色的排列方式
作者:AI对话大师,阿夏
时间:20240511
'''
from PIL import Image, ImageDraw
import math,random,os
colors1 = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
ss=626
w=3
#
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
os.makedirs(path,exist_ok=True)
# 创建800x1200的画布
canvas_width = 800
canvas_height = 800
canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
draw = ImageDraw.Draw(canvas)
# 初始边长和缩放因子
initial_side_length = 250
scale_factor = 2.5
# 计算等边三角形的边长
side_length = initial_side_length * scale_factor
# 计算等边三角形的顶点坐标
center_x = canvas_width // 2
center_y = canvas_height // 2+130
angle = math.radians(30) # 将角度转换为弧度
x1 = center_x - side_length // 2
y1 = center_y + int((side_length * math.sqrt(3)) / 6)
x2 = center_x + side_length // 2
y2 = y1
x3 = center_x
y3 = center_y - int((side_length * math.sqrt(3)) / 3)
# 获取左侧边的左侧点坐标
left_side_x = x1
left_side_y = (y1 + y3) // 2
# 将顶点1和顶点2的连线进行四等分
divide_1_t = 1/2
divide_2_t = 1/2
divide_1_x = x1 + (x2 - x1) * divide_1_t
divide_1_y = y1 + (y2 - y1) * divide_1_t
divide_2_x = x1 + (x2 - x1) * divide_2_t
divide_2_y = y1 + (y2 - y1) * divide_2_t
# divide_3_x = x1 + (x2 - x1) * divide_3_t
# divide_3_y = y1 + (y2 - y1) * divide_3_t
# 将顶点2和顶点3的连线进行四等分
divide_4_t = 1/4
divide_5_t = 1/4 * 2
divide_6_t = 1/4 * 3
divide_4_x = x2 + (x3 - x2) * divide_4_t
divide_4_y = y2 + (y3 - y2) * divide_4_t
divide_5_x = x2 + (x3 - x2) * divide_5_t
divide_5_y = y2 + (y3 - y2) * divide_5_t
divide_6_x = x2 + (x3 - x2) * divide_6_t
divide_6_y = y2 + (y3 - y2) * divide_6_t
# 将顶点3和顶点1的连线进行四等分
divide_7_t = 1/4
divide_8_t = 1/4 * 2
divide_9_t = 1/4 * 3
divide_7_x = x3 + (x1 - x3) * divide_7_t
divide_7_y = y3 + (y1 - y3) * divide_7_t
divide_8_x = x3 + (x1 - x3) * divide_8_t
divide_8_y = y3 + (y1 - y3) * divide_8_t
divide_9_x = x3 + (x1 - x3) * divide_9_t
divide_9_y = y3 + (y1 - y3) * divide_9_t
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 以顶点3坐标为顶部画一个填充红色、黑色边框、大小为ss的等边三角形
triangle_side_length = 470
def draw_triangle_1():
# 获取顶点3和顶点1连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = 470
# 第1种
# 计算新等边三角形的顶点坐标
triangle_x1 = x3 - triangle_side_length // 3
triangle_y1 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x2 = x3 + triangle_side_length // 3
triangle_y2 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x3 = x3
triangle_y3 = y3
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[0])
def draw_triangle_2():
# 获取顶点2和顶点3连线上3/4的圆点坐标
triangle_center_x = divide_6_x
triangle_center_y = divide_6_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[1])
def draw_triangle_3():
# 获取顶点2和顶点3连线上2/4的圆点坐标
triangle_center_x = divide_5_x
triangle_center_y = divide_5_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[2])
def draw_triangle_4():
# 获取顶点3和顶点1连线上2/4的圆点坐标
triangle_center_x = divide_8_x
triangle_center_y = divide_8_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[3])
def draw_triangle_5():
# 获取顶点3和顶点1连线上1/4的圆点坐标
triangle_center_x = divide_7_x
triangle_center_y = divide_7_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[4])
def draw_triangle_6():
# 获取顶点1和顶点2连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[5])
# for xx in range(1000):
# 随机打乱函数的顺序(可能会有重复)
# functions = [draw_triangle_1, draw_triangle_2, draw_triangle_3, draw_triangle_4, draw_triangle_5, draw_triangle_6]
# random.shuffle(functions)
import itertools
# 定义函数列表
function_names = ['draw_triangle_1', 'draw_triangle_2', 'draw_triangle_3', 'draw_triangle_4', 'draw_triangle_5', 'draw_triangle_6']
# 生成所有可能的排列,是元祖()
permutations = list(itertools.permutations(function_names))
# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 720
n=1
# 打印所有排列
for permutation in permutations:
# print(permutation)
# 将元组转换为函数对象列表,
functions = [eval(function_name) for function_name in permutation[::-1]]
# # 打印函数对象列表,一长串文字
# print(functions)
# [<function draw_triangle_2 at 0x000001A4B402F3A8>, <function draw_triangle_1 at 0x000001A4B402FB88>, <function draw_triangle_6 at 0x000001A4B4061288>, <function draw_triangle_3 at 0x000001A4B23C5AF8>, <function draw_triangle_4 at 0x000001A4B4061168>, <function draw_triangle_5 at 0x000001A4B40611F8>]
# 运行一个7元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
random.shuffle(colors)
# 调用函数绘制等边三角形
for func in functions:
func()
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 保存绘制好的图像
canvas.save(path + fr'\{n:03d}.png')
n+=1
# # 调用函数绘制等边三角形
# draw_triangle_1()
# draw_triangle_2()
# draw_triangle_3()
# draw_triangle_4()
# draw_triangle_5()
# draw_triangle_6()
# # 保存绘制好的图像
# canvas.save(path + fr'\{xx:02d}triangle.png')
print('------------检测生成的图片是否正好6个图片,有时候会覆盖只有3-5个图片,没有--------------')
from PIL import Image
import os
# 设定路径和文件名前缀
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
# 创建存储需要删除的图片文件名的列表
delete_images = []
# 遍历文件夹内的所有图片
for filename in os.listdir(path):
image_path = os.path.join(path, filename)
image = Image.open(image_path)
# 获取图片中的颜色数量
color_set = set()
for pixel_color in image.getdata():
color_set.add(pixel_color)
# 如果颜色数量小于8种,将该图片的文件名添加到删除列表中(六种彩色+黑白)
if len(color_set) < 8:
delete_images.append(filename)
# 删除颜色少于8种的图片
for filename in delete_images:
image_path = os.path.join(path, filename)
os.remove(image_path)
# 打印已删除的图片文件名
if len(delete_images) > 0:
print("已删除以下图片:")
for filename in delete_images:
print(filename)
else:
print("没有需要删除的图片。")
# 720个不重复的里面,留下420个显示6色
生成720张,然后对所有图片进行检测,提取没有6个颜色的图片的路径,全部提取完成后,批量删除这些图片。
001-040的图片
检测需要时间:大约3分钟
删除后只有420张符号要求
前四行40个最后一个序号是078
现在获得的420个图片都是有6种三角形颜色,且排序方法都不同
第五步:为筛选出来的420张图片添加文字说明,另存为001-420.png
为了防止新生成的图片的数字名称与JPG文件夹里的筛选图片有同样的名称,图片存在JPG2文件夹里,
'''
项目:图层顺序挑战(三角形版)6块等边三角形,720种不同排列方式,420种显示6个颜色的排列方式,添加文字说明,另存为420张
作者:AI对话大师,阿夏
时间:20240511
'''
from PIL import Image, ImageDraw
import math,random,os
colors1 = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
ss=626
w=3
#
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
os.makedirs(path,exist_ok=True)
# 创建800x1200的画布
canvas_width = 800
canvas_height = 800
canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
draw = ImageDraw.Draw(canvas)
# 初始边长和缩放因子
initial_side_length = 250
scale_factor = 2.5
# 计算等边三角形的边长
side_length = initial_side_length * scale_factor
# 计算等边三角形的顶点坐标
center_x = canvas_width // 2
center_y = canvas_height // 2+130
angle = math.radians(30) # 将角度转换为弧度
x1 = center_x - side_length // 2
y1 = center_y + int((side_length * math.sqrt(3)) / 6)
x2 = center_x + side_length // 2
y2 = y1
x3 = center_x
y3 = center_y - int((side_length * math.sqrt(3)) / 3)
# 获取左侧边的左侧点坐标
left_side_x = x1
left_side_y = (y1 + y3) // 2
# 将顶点1和顶点2的连线进行四等分
divide_1_t = 1/2
divide_2_t = 1/2
divide_1_x = x1 + (x2 - x1) * divide_1_t
divide_1_y = y1 + (y2 - y1) * divide_1_t
divide_2_x = x1 + (x2 - x1) * divide_2_t
divide_2_y = y1 + (y2 - y1) * divide_2_t
# divide_3_x = x1 + (x2 - x1) * divide_3_t
# divide_3_y = y1 + (y2 - y1) * divide_3_t
# 将顶点2和顶点3的连线进行四等分
divide_4_t = 1/4
divide_5_t = 1/4 * 2
divide_6_t = 1/4 * 3
divide_4_x = x2 + (x3 - x2) * divide_4_t
divide_4_y = y2 + (y3 - y2) * divide_4_t
divide_5_x = x2 + (x3 - x2) * divide_5_t
divide_5_y = y2 + (y3 - y2) * divide_5_t
divide_6_x = x2 + (x3 - x2) * divide_6_t
divide_6_y = y2 + (y3 - y2) * divide_6_t
# 将顶点3和顶点1的连线进行四等分
divide_7_t = 1/4
divide_8_t = 1/4 * 2
divide_9_t = 1/4 * 3
divide_7_x = x3 + (x1 - x3) * divide_7_t
divide_7_y = y3 + (y1 - y3) * divide_7_t
divide_8_x = x3 + (x1 - x3) * divide_8_t
divide_8_y = y3 + (y1 - y3) * divide_8_t
divide_9_x = x3 + (x1 - x3) * divide_9_t
divide_9_y = y3 + (y1 - y3) * divide_9_t
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 以顶点3坐标为顶部画一个填充红色、黑色边框、大小为ss的等边三角形
triangle_side_length = 470
def draw_triangle_1():
# 获取顶点3和顶点1连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = 470
# 第1种
# 计算新等边三角形的顶点坐标
triangle_x1 = x3 - triangle_side_length // 3
triangle_y1 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x2 = x3 + triangle_side_length // 3
triangle_y2 = y3 + int((triangle_side_length * math.sqrt(3)) / 3)
triangle_x3 = x3
triangle_y3 = y3
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[0])
def draw_triangle_2():
# 获取顶点2和顶点3连线上3/4的圆点坐标
triangle_center_x = divide_6_x
triangle_center_y = divide_6_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[1])
def draw_triangle_3():
# 获取顶点2和顶点3连线上2/4的圆点坐标
triangle_center_x = divide_5_x
triangle_center_y = divide_5_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[2])
def draw_triangle_4():
# 获取顶点3和顶点1连线上2/4的圆点坐标
triangle_center_x = divide_8_x
triangle_center_y = divide_8_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[3])
def draw_triangle_5():
# 获取顶点3和顶点1连线上1/4的圆点坐标
triangle_center_x = divide_7_x
triangle_center_y = divide_7_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y + int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[4])
def draw_triangle_6():
# 获取顶点1和顶点2连线上1/2的圆点坐标
triangle_center_x = divide_1_x
triangle_center_y = divide_1_y
# 计算新等边三角形的边长
triangle_side_length = ss
# 计算新等边三角形的顶点坐标
triangle_x1 = triangle_center_x - triangle_side_length // 4
triangle_y1 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x2 = triangle_center_x + triangle_side_length // 4
triangle_y2 = triangle_center_y - int((triangle_side_length * math.sqrt(3)) / 4)
triangle_x3 = triangle_center_x
triangle_y3 = triangle_center_y
# 绘制新等边三角形
draw.polygon([(triangle_x1, triangle_y1), (triangle_x2, triangle_y2), (triangle_x3, triangle_y3)],
outline='black', width=w,fill=colors[5])
# for xx in range(1000):
# 随机打乱函数的顺序(可能会有重复)
# functions = [draw_triangle_1, draw_triangle_2, draw_triangle_3, draw_triangle_4, draw_triangle_5, draw_triangle_6]
# random.shuffle(functions)
import itertools
# 定义函数列表
function_names = ['draw_triangle_1', 'draw_triangle_2', 'draw_triangle_3', 'draw_triangle_4', 'draw_triangle_5', 'draw_triangle_6']
# 生成所有可能的排列,是元祖()
permutations = list(itertools.permutations(function_names))
# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 720
n=1
# 打印所有排列
for permutation in permutations:
# print(permutation)
# 将元组转换为函数对象列表,
functions = [eval(function_name) for function_name in permutation[::-1]]
# # 打印函数对象列表,一长串文字
# print(functions)
# [<function draw_triangle_2 at 0x000001A4B402F3A8>, <function draw_triangle_1 at 0x000001A4B402FB88>, <function draw_triangle_6 at 0x000001A4B4061288>, <function draw_triangle_3 at 0x000001A4B23C5AF8>, <function draw_triangle_4 at 0x000001A4B4061168>, <function draw_triangle_5 at 0x000001A4B40611F8>]
# 运行一个7元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'pink']
random.shuffle(colors)
# 调用函数绘制等边三角形
for func in functions:
func()
# 绘制连线上的圆点
dot_radius = 5
draw.ellipse([(divide_1_x - dot_radius, divide_1_y - dot_radius),
(divide_1_x + dot_radius, divide_1_y + dot_radius)], fill='black')
draw.ellipse([(divide_2_x - dot_radius, divide_2_y - dot_radius),
(divide_2_x + dot_radius, divide_2_y + dot_radius)], fill='black')
# draw.ellipse([(divide_3_x - dot_radius, divide_3_y - dot_radius),
# (divide_3_x + dot_radius, divide_3_y + dot_radius)], fill='black')
draw.ellipse([(divide_4_x - dot_radius, divide_4_y - dot_radius),
(divide_4_x + dot_radius, divide_4_y + dot_radius)], fill='black')
draw.ellipse([(divide_5_x - dot_radius, divide_5_y - dot_radius),
(divide_5_x + dot_radius, divide_5_y + dot_radius)], fill='black')
draw.ellipse([(divide_6_x - dot_radius, divide_6_y - dot_radius),
(divide_6_x + dot_radius, divide_6_y + dot_radius)], fill='black')
draw.ellipse([(divide_7_x - dot_radius, divide_7_y - dot_radius),
(divide_7_x + dot_radius, divide_7_y + dot_radius)], fill='black')
draw.ellipse([(divide_8_x - dot_radius, divide_8_y - dot_radius),
(divide_8_x + dot_radius, divide_8_y + dot_radius)], fill='black')
draw.ellipse([(divide_9_x - dot_radius, divide_9_y - dot_radius),
(divide_9_x + dot_radius, divide_9_y + dot_radius)], fill='black')
# 绘制等边三角形的边框
draw.polygon([(x1, y1), (x2, y2), (x3, y3), (x1, y1)], outline='black')
# 三个顶点画圆点
# 在顶点1绘制黑色圆点
draw.ellipse([(x1 - dot_radius, y1 - dot_radius),
(x1 + dot_radius, y1 + dot_radius)], fill='black')
# 在顶点2绘制黑色圆点
draw.ellipse([(x2 - dot_radius, y2 - dot_radius),
(x2 + dot_radius, y2 + dot_radius)], fill='black')
# 在顶点3绘制黑色圆点
draw.ellipse([(x3 - dot_radius, y3 - dot_radius),
(x3 + dot_radius, y3 + dot_radius)], fill='black')
# 保存绘制好的图像
canvas.save(path + fr'\{n:03d}.png')
n+=1
# # 调用函数绘制等边三角形
# draw_triangle_1()
# draw_triangle_2()
# draw_triangle_3()
# draw_triangle_4()
# draw_triangle_5()
# draw_triangle_6()
# # 保存绘制好的图像
# canvas.save(path + fr'\{xx:02d}triangle.png')
print('------------检测生成的图片是否正好6个图片,有时候会覆盖只有3-5个图片,没有--------------')
from PIL import Image
import os
# 设定路径和文件名前缀
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
# 创建存储需要删除的图片文件名的列表
delete_images = []
# 遍历文件夹内的所有图片
for filename in os.listdir(path):
image_path = os.path.join(path, filename)
image = Image.open(image_path)
# 获取图片中的颜色数量
color_set = set()
for pixel_color in image.getdata():
color_set.add(pixel_color)
# 如果颜色数量小于8种,将该图片的文件名添加到删除列表中(六种彩色+黑白)
if len(color_set) < 8:
delete_images.append(filename)
# 删除颜色少于8种的图片
for filename in delete_images:
image_path = os.path.join(path, filename)
os.remove(image_path)
# 打印已删除的图片文件名
if len(delete_images) > 0:
print("已删除以下图片:")
for filename in delete_images:
print(filename)
else:
print("没有需要删除的图片。")
# 720个不重复的里面,留下420个显示6色
print('----3、给图片重新编序号,左上角添加数字')
from PIL import Image, ImageDraw, ImageFont
import os
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg'
path2 = r'C:\Users\jg2yXRZ\OneDrive\桌面\三角重叠\jpg2'
os.makedirs(path2,exist_ok=True)
# 指定文件夹路径和要写入的文字
text = "三角形图层顺序挑战" # 要写入的文字
n=1
# 遍历文件夹中的PNG图片
for filename in os.listdir(path):
if filename.endswith('.png'):
# 构建图片路径
image_path = os.path.join(path, filename)
# 打开图片
image = Image.open(image_path)
# 在图片上绘制文字
# 打开图片
image = Image.open(image_path)
# 在图片上绘制文字
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(r"C:\Windows\Fonts\simhei.ttf", 40) # 指定字体和字号
text_position = (200, 80) # 文字位置
text_to_write = f"{text}-{n}" # 构建要写入的文字内容
draw.text(text_position, text_to_write, font=font, fill=(0, 0, 0)) # 绘制文字并添加描边效果
# 保存修改后的图片
output_path = os.path.join(path2 , f"{n:03d}.png")
image.save(output_path)
n+=1
# 关闭图片
image.close()