

背景需求:
中班益智区素材:三角形变魔术 - 小红书自制益智区教玩具:三角形变魔术,共24题 玩法一:根据图示在空白格子里摆放。 玩法二:根据图示在空白格子里用黑笔涂。##自制玩具益智区 #幼儿园益智区 #中班益智区 #区域素材 #幼儿园益智区可打印素材
https://www.xiaohongshu.com/discovery/item/642edea90000000012032c3d?app_platform=android&ignoreEngage=true&app_version=8.36.0&share_from_user_hidden=true&type=normal&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1716531629
小红书上看到一个三角黑块拼图,我去年带的班级里也有这样的积木块。
我想用编程来做一下9块积木块共有多少种不同的排列方式。
样式是3*3的,感觉数量会很多,先做一个2*2的拼图。
代码展示:
1、800*800画布上制作2*2单元格
2、读取四个单元格的每个单元格的的四个坐标,都随机抽取3个
3、梳理出不重复的样式坐标
4、根据三点坐标制作黑色直角三角形
'''
黑白三角(2*2), 4个单元格每个有四个坐标,四个坐标随机抽取3个,进行组合,共有256种不重复排序
AI对话大师,阿夏
2024年5月24日
'''
print('---1、制作2*2格子-------')
from PIL import Image, ImageDraw
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白三角'
# 创建800x800的画布
canvas = Image.new('RGB', (800, 800), (255, 255, 255))
draw = ImageDraw.Draw(canvas)
# 定义表格的行数和列数
rows = 2
cols = 2
# 计算单元格的宽度和高度
cell_width = 800 // cols
cell_height = 800 // rows
# 绘制表格的竖直线
for i in range(1, cols):
x = i * cell_width
draw.line([(x, 0), (x, 800)], fill=(0, 0, 0), width=2)
# 绘制表格的水平线
for i in range(1, rows):
y = i * cell_height
draw.line([(0, y), (800, y)], fill=(0, 0, 0), width=2)
mb='2格模板.png'
# 保存画布
canvas.save(path+fr'\{mb}')
print('---2、计算三个坐标点的黑色三角形不重复图案有几个-------')
# 创建一个空列表用于存储单元格的坐标
cell_coordinates = []
# 计算每个单元格的四个顶点坐标
for row in range(rows):
for col in range(cols):
top_left = (col * cell_width, row * cell_height)
top_right = ((col + 1) * cell_width, row * cell_height)
bottom_left = (col * cell_width, (row + 1) * cell_height)
bottom_right = ((col + 1) * cell_width, (row + 1) * cell_height)
# 将四个顶点坐标添加到列表中
cell_coordinates.append([top_left, top_right, bottom_left, bottom_right])
# print(cell_coordinates)
# [[(0, 0), (400, 0), (0, 400), (400, 400)], [(400, 0), (800, 0), (400, 400), (800, 400)], [(0, 400), (400, 400), (0, 800), (400, 800)], [(400, 400), (800, 400), (400, 800), (800, 800)]]
import itertools,os
# 生成所有组合方式
combinations = list(itertools.product(*[itertools.combinations(sublist, 3) for sublist in cell_coordinates]))
# print(combinations)
print(len(combinations))
# 256
print('---3、制作三个坐标点的黑色三角形(4个)-------')
from PIL import Image, ImageDraw
new=pat