作品展示:
![](https://img-blog.csdnimg.cn/direct/0ed127f808044dd9aedf5d8ff49d2c06.png)
背景需求:
【教学类-50-01】20240407“数一数”图片样式01:纯色图形与边框不相交,纯色图形和其他纯色图形不相交-CSDN博客文章浏览阅读807次,点赞33次,收藏14次。【教学类-50-01】20240407“数一数”图片样式01:纯色图形与边框不相交,纯色图形和其他纯色图形不相交
https://blog.csdn.net/reasonsummer/article/details/137511583
前期制作数一数几何图形时
1、椭圆形和圆形混淆:不仅是幼儿,连老师也会把椭圆形看成圆形
![](https://img-blog.csdnimg.cn/direct/a7a34013b39c4f7f98b4853686c0212c.png)
——这款椭圆形的代码是随机生成椭圆形的长宽直径,长轴与宽轴是10:9,基本看不出差异,默认是正圆形。
![](https://img-blog.csdnimg.cn/direct/21ff83c9afcf4db8965a03802724c892.png)
![](https://img-blog.csdnimg.cn/direct/42dd96d6c53e44d9b42ec3504c5b13b0.png)
我希望固定椭圆形的外形:长轴与宽轴2:1,方向是横排、竖排
![](https://img-blog.csdnimg.cn/direct/81673898f5eb448fb1dc5e1752e58985.png)
![](https://img-blog.csdnimg.cn/direct/0474d4a54a234aa8b42735e168559d43.png)
2、等腰直角三角形,都是“
”的样式。
![](https://img-blog.csdnimg.cn/direct/4fcf0e11eb514c4398fb608f643c508e.png)
我觉得平时用的最多的还是等边三角形(45度角)
![](https://img-blog.csdnimg.cn/direct/bf50326ca71842ca9921c87e537cf40f.png)
![](https://img-blog.csdnimg.cn/direct/2fda00953f6f43b8910a5f4c38115b35.png)
代码展示:
![](https://img-blog.csdnimg.cn/direct/b7d1c3e6010845f1b8ab33959d822f26.png)
![](https://img-blog.csdnimg.cn/direct/df0a40cf67e34f8fa0342f6ad4be98bf.png)
![](https://img-blog.csdnimg.cn/direct/23211345ef88431f81c4bc80b96e43c0.png)
'''
01数一数图形(等腰三角形、椭圆2:1)
作者:AI对话大师、阿夏
时间:2024年4月28日 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('画布大小(15)\n'))
num=int(input('多少张\n'))
alp=1.0
# float(input('透明度(1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\数一数2\01几何框内不连接'
end=output_dir+r'\01无框线'
os.makedirs(output_dir, exist_ok=True)
os.makedirs(end, 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, 5)
num_square =random.randint(1, 5)
num_cicle = random.randint(1, 5)
num_ellipse = random.randint(1, 5)
num_rectangle = random.randint(1, 5)
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 / 2, np.sqrt(3) * side_length / 2])
# height_point = base_point + np.array([side_length, 0])
# # 检查三角形是否在画布内部
# 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_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)
# 倒置的直角三角形
# import math
# for _ in range(num_triangles):
# while True:
# # 随机生成等边三角形的顶点坐标
# base_point = np.random.rand(2) * c
# side_length = np.random.rand() * 2 + 1
# # 计算等边三角形的顶点坐标
# height = (math.sqrt(3) / 2) * side_length
# top_point = base_point + np.array([side_length / 2, height])
# left_point = base_point + np.array([0, height])
# right_point = base_point + np.array([side_length, height])
# # 检查三角形是否在画布内部
# triangle = Polygon([base_point, top_point, left_point])
# if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(left_point <= c) and np.all(right_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, left_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)
# 等边三角形
import math
for _ in range(num_triangles):
while True:
# 随机生成等边三角形的顶点坐标
base_point = np.random.rand(2) * c
side_length = np.random.rand() * 2 + 1
# 计算等边三角形的顶点坐标
height = side_length * math.sqrt(3) / 2
top_point = base_point + np.array([side_length / 2, height])
left_point = base_point + np.array([0, 0])
right_point = base_point + np.array([side_length, 0])
# 检查三角形是否在画布内部
triangle = Polygon([left_point, right_point, top_point])
if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(left_point >= 0) and np.all(right_point <= c) and not any(shape.intersects(triangle) for shape in shapes):
break
# 随机选择颜色
color = np.random.choice(colors)
print(color)
# 创建并填充等边三角形
triangle_vertices = np.array([left_point, right_point, top_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_ellipse):
# while True:
# # 随机生成椭圆形的中心点坐标
# center = np.random.rand(2) * c
# # 随机生成椭圆形的长轴和短轴
# major_axis = np.random.rand() * 2 + 1
# minor_axis = major_axis / 2 # 将短轴设为长轴的一半,以满足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)
# # # 随机生成椭圆形水平的垂直的(任意角度倾斜)
# import random
# for _ in range(num_ellipse):
# while True:
# # 随机生成椭圆形的中心点坐标
# center = np.random.rand(2) * c
# # 随机生成椭圆形的长轴和短轴
# major_axis = np.random.rand() * 2 + 1
# minor_axis = major_axis / 2 # 将短轴设为长轴的一半,以满足2:1的长宽比
# # 随机生成椭圆形的旋转角度
# angle = np.random.rand() * 2 * np.pi
# # 检查椭圆形是否在画布内部
# ellipse_vertices = []
# num_points = 100
# angle_points = np.linspace(0, 2 * np.pi, num_points)
# for a in angle_points:
# x = center[0] + major_axis / 2 * np.cos(a) * np.cos(angle) - minor_axis / 2 * np.sin(a) * np.sin(angle)
# y = center[1] + major_axis / 2 * np.cos(a) * np.sin(angle) + minor_axis / 2 * np.sin(a) * np.cos(angle)
# 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, angle=np.rad2deg(angle), alpha=alp,
# color=color)
# ax.add_patch(ellipse_patch)
# shapes.append(ellipse)
# # # 随机生成椭圆形水平的垂直的(90和180)
# import random
for _ in range(num_ellipse):
while True:
# 随机生成椭圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成椭圆形的长轴和短轴
major_axis = np.random.rand() * 2 + 1
minor_axis = major_axis / 2 # 将短轴设为长轴的一半,以满足2:1的长宽比
# 随机选择椭圆形的旋转角度(90度或180度)
angle = np.random.choice([np.pi / 2, np.pi])
# 检查椭圆形是否在画布内部
ellipse_vertices = []
num_points = 100
angle_points = np.linspace(0, 2 * np.pi, num_points)
for a in angle_points:
x = center[0] + major_axis / 2 * np.cos(a) * np.cos(angle) - minor_axis / 2 * np.sin(a) * np.sin(angle)
y = center[1] + major_axis / 2 * np.cos(a) * np.sin(angle) + minor_axis / 2 * np.sin(a) * np.cos(angle)
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, angle=np.rad2deg(angle), 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(end, f'{i:02d}.png')
plt.savefig(output_path, dpi=200, bbox_inches='tight')
# 关闭画布
plt.close(fig)
# 暂停3秒
time.sleep(3)
![](https://img-blog.csdnimg.cn/direct/81d968abca714a338a7b004d391da7dc.png)
修改学具中的几何图案,把它们改成特征明显的样式(等边三角形、2:1椭圆形)这样,孩子们判断三角、椭圆形就不会错误了!