背景需求
背景需求
已经制作了难度1.0的“数一数”——所有图形都在边框内,没有被遮挡,所有图形与其他几何图形都分开,不相交
下面制作2.0图形与边框重合,图形和其他图形不相交的样式,也就是部分图形会超过边框,只保留半个图案。
设计思路:
1、使用AI对话大师,设计需要的内容。
2、反复提出需求,输入错误的提示英文,终于获得自己想要的学具
素材准备:
先看看代码生成的图片样式
所以图形数量有多有少,有红、黄、蓝也是随机的
部分几何图形与边框相切,导致图案被遮掉一部分
'''
中班个别化学习材料-数形颜色的点数-难度2.0
1、图形部分在边框外(有遮挡)
2、图形之间不相交(没有重叠部分)
3、颜色是最深的(因为没有重叠,不需要降低透明度)
作者:AI对话大师、阿夏
时间:2024年4月7日 20:00
'''
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import os
import random
import time
from shapely.geometry import Polygon
from shapely.ops import cascaded_union
c = int(input('画布大小(c)\n'))
num=int(input('多少张\n'))
alp=float(input('透明度(0.3=透明重叠,1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\个别化\02框外不连接'
os.makedirs(output_dir, exist_ok=True)
# 随机生成多个等腰直角三角形
for i in range(num):
# 创建画布
fig, ax = plt.subplots(figsize=(c, c))
ax.set_xlim([0, c])
ax.set_ylim([0, c])
# 随机几个图形
num_triangles = random.randint(1, 6)
num_square = num_triangles
num_cicle = num_triangles
num_ellipse = num_triangles
num_rectangle = num_triangles
colors = ['red', 'yellow', 'blue']
shapes = []
for _ in range(num_triangles):
while True:
# 随机生成等腰直角三角形的顶点坐标
base_point = np.random.rand(2) * c
side_length = np.random.rand() * 2 + 1
# 计算等腰直角三角形的顶点坐标
top_point = base_point + np.array([side_length, 0])
height_point = base_point + np.array([0, side_length])
# 检查三角形是否在画布内部
triangle = Polygon([base_point, top_point, height_point])
# 检查三角形是否在画布内部
if not any(shape.intersects(triangle) for shape in shapes):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充等腰直角三角形
triangle_vertices = np.array([base_point, top_point, height_point])
triangle = Polygon(triangle_vertices)
triangle_patch = patches.Polygon(triangle_vertices, closed=True, alpha=alp, color=color)
ax.add_patch(triangle_patch)
shapes.append(triangle)
# 随机生成正方形
for _ in range(num_square):
while True:
# 随机生成正方形的中心点坐标
center = np.random.rand(2) * c
# 随机生成正方形的边长
side_length = np.random.rand() * 2 + 1
# 检查正方形是否在画布内部
square = Polygon([(center[0]-side_length/2, center[1]-side_length/2), (center[0]+side_length/2, center[1]-side_length/2),
(center[0]+side_length/2, center[1]+side_length/2), (center[0]-side_length/2, center[1]+side_length/2)])
if not any(shape.intersects(square) for shape in shapes):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充正方形
square_patch = patches.Rectangle((center[0]-side_length/2, center[1]-side_length/2), side_length, side_length, alpha=alp, color=color)
ax.add_patch(square_patch)
shapes.append(square)
# 随机生成圆形
for _ in range(num_cicle):
while True:
# 随机生成圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成圆形的半径
radius = np.random.rand() * 2 + 1
# 检查圆形是否在画布内部
circle = Polygon([(center[0]-radius, center[1]-radius), (center[0]+radius, center[1]-radius),
(center[0]+radius, center[1]+radius), (center[0]-radius, center[1]+radius)])
if not any(shape.intersects(circle) for shape in shapes):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充圆形
circle_patch = patches.Circle((center[0], center[1]), radius, alpha=alp, color=color)
ax.add_patch(circle_patch)
shapes.append(circle)
# 随机生成椭圆形
for _ in range(num_ellipse):
while True:
# 随机生成椭圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成椭圆形的长轴和短轴
major_axis = np.random.rand() * 2 + 1
minor_axis = np.random.rand() * 2 + 1
# 检查椭圆形是否在画布内部
ellipse_vertices = []
num_points = 100
angle = np.linspace(0, 2 * np.pi, num_points)
for a in angle:
x = center[0] + major_axis / 2 * np.cos(a)
y = center[1] + minor_axis / 2 * np.sin(a)
ellipse_vertices.append([x, y])
ellipse = Polygon(ellipse_vertices)
if np.all(center - np.array([major_axis, minor_axis]) / 2 >= 0) and np.all(
center + np.array([major_axis, minor_axis]) / 2 <= c) and not any(
shape.intersects(ellipse) for shape in shapes):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充椭圆形
ellipse_patch = patches.Ellipse((center[0], center[1]), major_axis, minor_axis, alpha=alp, color=color)
ax.add_patch(ellipse_patch)
shapes.append(ellipse)
# # 随机生成长方形
# for _ in range(num_rectangle):
# while True:
# # 随机生成长方形的中心点坐标
# center = np.random.rand(2) * c
# # 随机生成长方形的长和宽
# width = np.random.rand() * 2 + 1
# height = np.random.rand() * 2 + 1
# # 检查长方形是否在画布内部
# rectangle = Polygon([(center[0] - width / 2, center[1] - height / 2),
# (center[0] + width / 2, center[1] - height / 2),
# (center[0] + width / 2, center[1] + height / 2),
# (center[0] - width / 2, center[1] + height / 2)])
# if np.all(center - np.array([width, height]) / 2 >= 0) and np.all(
# center + np.array([width, height]) / 2 <= c) and not any(
# shape.intersects(rectangle) for shape in shapes):
# break
# # 随机选择颜色
# color = np.random.choice(colors)
# # 创建并填充长方形
# rectangle_patch = patches.Rectangle((center[0] - width / 2, center[1] - height / 2), width, height,
# alpha=alp, color=color)
# ax.add_patch(rectangle_patch)
# shapes.append(rectangle)
# 隐藏坐标轴
ax.axis('off')
# 保存图形
output_path = os.path.join(output_dir, f'{i:02d}.png')
plt.savefig(output_path, dpi=400, bbox_inches='tight')
# 关闭画布
plt.close(fig)
# 暂停3秒
time.sleep(3)