背景需求
今天孩子们点数时,我核对答案,突然发现有两张图片上的三角、正方、椭圆、圆形只有1个,我感觉这个随机的概率有问题。
仔细点数后发现以下代码生成的几何图形数量是相同的
(如果三角有3个,那么正方形、圆形和椭圆形都是3个)(如果三角有1个,那么正方形、圆形和椭圆形都是1个)
产生原因:随机抽取了一个数量,并且每个图形都用了这个数量
一、一张图上,四种图案出现数量不相等(随机1-4)
代码需要修改为
代码展示:
'''
中班个别化学习材料-数形颜色的点数-难度1.0
1、图形都在边框内(没有遮挡)
2、图形之间不相交(没有重叠部分)
3、颜色是最深的(因为没有重叠,不需要降低透明度)
4、每个图形随机抽取的数量不相等
作者:AI对话大师、阿夏
时间:2024年4月10日 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\桌面\个别化\01框内不连接'
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, 4)
num_square = random.randint(1, 4)
num_cicle = random.randint(1, 4)
num_ellipse = random.randint(1, 4)
num_rectangle = random.randint(1, 4)
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 np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c) and 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 np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c) and 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 np.all(center - radius >= 0) and np.all(center + radius <= c) and 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)
这下四种图案出现的数量不会相等了。
现在每张图上,4种几何图案出现的数量不相等了。
二、一张图上,四种图案出现数量固定(确定、三角形4个、正方形3个,圆形2个,椭圆形1个)
如果图形的数量不用随机数,也可以指定数量
代码展示
'''
中班个别化学习材料-数形颜色的点数-难度1.0
1、图形都在边框内(没有遮挡)
2、图形之间不相交(没有重叠部分)
3、颜色是最深的(因为没有重叠,不需要降低透明度)
4、每个图形固定一个数量,图案数量限定
作者:AI对话大师、阿夏
时间:2024年4月10日 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\桌面\个别化\01框内不连接'
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 = 4
num_square = 3
num_cicle = 2
num_ellipse =1
num_rectangle = 2
# num_triangles = random.randint(1, 4)
# num_square = random.randint(1, 2)
# num_cicle = random.randint(1, 3)
# num_ellipse = random.randint(1, 1)
# num_rectangle = random.randint(1, 2)
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 np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c) and 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 np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c) and 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 np.all(center - radius >= 0) and np.all(center + radius <= c) and 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)
点数验证——每张偏上都是三角形4个、正方形3个,圆形2个,椭圆形1个
这种方式生成N张几何图案出现位置不同的图片,但是最后的答案是一样的(10个图案,三角形4个、正方形3个,圆形2个,椭圆形1个)
这样就不用老师自己去点数,来浪费验证时间了。比较适合集体教学活动。