背景需求:
【教学类-50-03】20240408“数一数”图片样式03:透明图形与边框不相交,透明图形和其他透明图形重叠-CSDN博客文章浏览阅读867次,点赞28次,收藏25次。【教学类-50-03】20240408“数一数”图片样式03:透明图形与边框不相交,透明图形和其他透明图形重叠https://blog.csdn.net/reasonsummer/article/details/137547784
问题一:浅色相交不容易点数
前期幼儿对透明相交的几何图形进行选择,发现星3星4的部分学具完全没有填写过,幼儿表示:“太难了!”“我不会!”“好多图案!”也有几位孩子对3-1、星3-5、星4-2进行的点数,正确率较高。
| | |
图11-8-1 星3-1正确率高
星4-2与星3-2的对比发现:星4-2随机生成的透明几何图案相对数量少(2-5个),交叉重叠少,便于幼儿点数。星3-2的生成的透明几何图案较多,相交重叠的面积大、多个图案密集交叉,幼儿无法辨认而放弃。
问题二:浅黄色看不清
有幼儿说自己看不清楚星3星4中重叠交叉的透明柠檬黄(明度太高,太亮刺眼)。直方图上也显示黄色点数的错误率都是大于等于红色和蓝色的错误率。(图13-1),对于看不见的黄色,我鼓励幼儿用记号笔画圈。
问题三:圈画图案容易弄脏
针对透明图案看不清图形的情况,我鼓励孩子们用记号笔画上黑色边框在点数。少量幼儿圈画图形,但描边不整齐;手掌擦抹,容易弄脏图案。
图15-2 手动圈画黑色边框
综上所述,我希望在图案边框加上黑色边框线,让透明色相交重叠后也能看清楚图形的外形结构
第1次添加边框效果
透明相交图案代码
'''
02数一数图形(都是一个方向三角形)边框内+图案重叠(透明相交有边框) 0.3
作者: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=0.3 # 透明相交,有边框,能分辨差异
# float(input('透明度(1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\数一数2\02几何框内连接'
end=output_dir+r'\02透明相交有框线'
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, 0])
# height_point = base_point + np.array([0, side_length])
# # 检查三角形是否在画布内部
# if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c):
# break
# # 随机选择颜色
# color = np.random.choice(colors)
# # 创建并填充等腰直角三角形
# triangle_vertices = np.array([base_point, top_point, height_point])
# triangle = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black')
# ax.add_patch(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)
# 创建并填充等边三角形
triangle_vertices = np.array([left_point, right_point, top_point])
triangle = Polygon(triangle_vertices)
triangle_patch = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black')
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
# 检查正方形是否在画布内部
if np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充正方形
square = patches.Rectangle((center[0] - side_length/2, center[1] - side_length/2), side_length, side_length, alpha=alp, facecolor=color,edgecolor='black')
ax.add_patch(square)
# 随机生成圆形
for _ in range(num_cicle):
while True:
# 随机生成圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成圆形的半径
radius = np.random.rand() * 2 + 1
# 检查圆形是否在画布内部
if np.all(center - radius >= 0) and np.all(center + radius <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充圆形
circle = patches.Circle(center, radius, alpha=alp, facecolor=color,edgecolor='black')
ax.add_patch(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, facecolor=color,edgecolor='black')
# 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,
facecolor=color,edgecolor='black')
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, facecolor=color,edgecolor='black')
# 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)
但是幼儿操作中,我感觉打印出来的纸片上的黑色边框线并不明显。
所以有些孩子就在透明色的重叠图案上画边框做区分。
这种情况下,我有两个思路:
一、把透明度0.3改成0.9,增加背景色的纯度。
二、保留0.3的填充色透明度,但增加边框线条的粗细
第2次添加边框效果
实验一:把透明度0.3改成0.9,增加背景色的纯度。
'''
02数一数图形(都是一个方向三角形)边框内+图案重叠(透明相交有边框)0.9
作者: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=0.9 # 透明相交,有边框,能分辨差异
# float(input('透明度(1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\数一数2\02几何框内连接'
end=output_dir+r'\02透明相交有框线'
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, 0])
# height_point = base_point + np.array([0, side_length])
# # 检查三角形是否在画布内部
# if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c):
# break
# # 随机选择颜色
# color = np.random.choice(colors)
# # 创建并填充等腰直角三角形
# triangle_vertices = np.array([base_point, top_point, height_point])
# triangle = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black')
# ax.add_patch(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)
# 创建并填充等边三角形
triangle_vertices = np.array([left_point, right_point, top_point])
triangle = Polygon(triangle_vertices)
triangle_patch = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black')
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
# 检查正方形是否在画布内部
if np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充正方形
square = patches.Rectangle((center[0] - side_length/2, center[1] - side_length/2), side_length, side_length, alpha=alp, facecolor=color,edgecolor='black')
ax.add_patch(square)
# 随机生成圆形
for _ in range(num_cicle):
while True:
# 随机生成圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成圆形的半径
radius = np.random.rand() * 2 + 1
# 检查圆形是否在画布内部
if np.all(center - radius >= 0) and np.all(center + radius <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充圆形
circle = patches.Circle(center, radius, alpha=alp, facecolor=color,edgecolor='black')
ax.add_patch(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, facecolor=color,edgecolor='black')
# 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,
facecolor=color,edgecolor='black')
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, facecolor=color,edgecolor='black')
# 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)
再测试其他透明度。
透明度0.8中出现多图案同位置重叠,因为填充色纯度高,所以后面图案就看不见了。
这种情况需要用代码来剔除,至少让图形一部分漏出来,否则影响最后的统计结果(代码写出来5个圆,但明面上只能看到4个圆)
通过0.9-0.1的透明度测试,我感觉0.3的透明度出现透明,能够看到底层的几何图形,不会像0.9时颜色深,把最底层的图片遮挡了,完全看不见。
因此为了让图案边框线更星期,需要对黑色边框加粗(1磅变成10磅?)
实验二、图形黑色边框线加粗
代码展示
'''
02数一数图形(都是一个方向三角形)边框内+图案重叠(透明相交有边框)5磅黑色边框
作者: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 # 透明相交,有边框,能分辨差异
cx=5
# float(input('透明度(1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\数一数2\02几何框内连接'
end=output_dir+r'\02透明相交有框线'
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, 0])
# height_point = base_point + np.array([0, side_length])
# # 检查三角形是否在画布内部
# if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c):
# break
# # 随机选择颜色
# color = np.random.choice(colors)
# # 创建并填充等腰直角三角形
# triangle_vertices = np.array([base_point, top_point, height_point])
# triangle = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black',linewidth=10)
# ax.add_patch(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)
# 创建并填充等边三角形
triangle_vertices = np.array([left_point, right_point, top_point])
triangle = Polygon(triangle_vertices)
triangle_patch = patches.Polygon(triangle_vertices, closed=True, alpha=alp, facecolor=color,edgecolor='black',linewidth=cx)
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
# 检查正方形是否在画布内部
if np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充正方形
square = patches.Rectangle((center[0] - side_length/2, center[1] - side_length/2), side_length, side_length, alpha=alp, facecolor=color,edgecolor='black',linewidth=cx)
ax.add_patch(square)
# 随机生成圆形
for _ in range(num_cicle):
while True:
# 随机生成圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成圆形的半径
radius = np.random.rand() * 2 + 1
# 检查圆形是否在画布内部
if np.all(center - radius >= 0) and np.all(center + radius <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
# 创建并填充圆形
circle = patches.Circle(center, radius, alpha=alp, facecolor=color,edgecolor='black',linewidth=cx)
ax.add_patch(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, facecolor=color,edgecolor='black',linewidth=cx)
# 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,
facecolor=color,edgecolor='black',linewidth=cx)
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, facecolor=color,edgecolor='black',linewidth=cx)
# 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)
结果:此时可以发现,透明度影响边框线颜色,如果透明度越低,边框线黑色会转为灰色,所以看不清楚。也就是说,这个透明度实际上的填充和边框共有的透明度
第3次添加边框效果
前期问题:设置填充出现透明后,边框也变成透明(黑变灰)
AI修改:让边框的透明度与填充透明度分开
代码展示
'''
02数一数图形(都是一个方向三角形)边框内+图案重叠(透明相交有边框)填充透明度0.3,边框深黑 透明度1.0 边框线粗细3磅
作者: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
import matplotlib.colors as mcolors
c = int(input('画布大小(15)\n'))
num=int(input('多少张\n'))
bjs=0.3 # 填充色的透明度
bkx=1.0 # 边框线的透明度(深黑
cx=3 # 黑色边框线的粗细
# alp=1.0 # 透明相交,有边框,能分辨差异
# float(input('透明度(1.0=分开纯色)\n'))
# 创建目录
output_dir = r'C:\Users\jg2yXRZ\OneDrive\桌面\数一数2\02几何框内连接'
end=output_dir+r'\02透明相交有框线'
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, 0])
# height_point = base_point + np.array([0, side_length])
# # 检查三角形是否在画布内部
# if np.all(base_point >= 0) and np.all(top_point <= c) and np.all(height_point <= c):
# break
# # 随机选择颜色
# color = np.random.choice(colors)
# # 创建并填充等腰直角三角形
# triangle_vertices = np.array([base_point, top_point, height_point])
# triangle = patches.Polygon(triangle_vertices, closed=True, facecolor=fill_color, edgecolor=edge_color,linewidth=10)
# ax.add_patch(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)
fill_color = mcolors.to_rgba(color, alpha=bjs)
edge_color = mcolors.to_rgba('black', alpha=bkx)
# 创建并填充等边三角形
triangle_vertices = np.array([left_point, right_point, top_point])
triangle = Polygon(triangle_vertices)
triangle_patch = patches.Polygon(triangle_vertices, closed=True, facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
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
# 检查正方形是否在画布内部
if np.all(center - side_length/2 >= 0) and np.all(center + side_length/2 <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
fill_color = mcolors.to_rgba(color, alpha=bjs)
edge_color = mcolors.to_rgba('black', alpha=bkx)
# 创建并填充正方形
square = patches.Rectangle((center[0] - side_length/2, center[1] - side_length/2), side_length, side_length, facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
ax.add_patch(square)
# 随机生成圆形
for _ in range(num_cicle):
while True:
# 随机生成圆形的中心点坐标
center = np.random.rand(2) * c
# 随机生成圆形的半径
radius = np.random.rand() * 2 + 1
# 检查圆形是否在画布内部
if np.all(center - radius >= 0) and np.all(center + radius <= c):
break
# 随机选择颜色
color = np.random.choice(colors)
fill_color = mcolors.to_rgba(color, alpha=bjs)
edge_color = mcolors.to_rgba('black', alpha=bkx)
# 创建并填充圆形
circle = patches.Circle(center, radius, facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
ax.add_patch(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)
# fill_color = mcolors.to_rgba(color, alpha=bjs)
# edge_color = mcolors.to_rgba('black', alpha=bkx)
# # 创建并填充椭圆形
# ellipse_patch = patches.Ellipse((center[0], center[1]), major_axis, minor_axis, facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
# 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)
fill_color = mcolors.to_rgba(color, alpha=bjs)
edge_color = mcolors.to_rgba('black', alpha=bkx)
# 创建并填充椭圆形
ellipse_patch = patches.Ellipse((center[0], center[1]), major_axis, minor_axis, angle=np.rad2deg(angle), facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
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)
# fill_color = mcolors.to_rgba(color, alpha=bjs)
# edge_color = mcolors.to_rgba('black', alpha=bkx)
# # 创建并填充长方形
# rectangle_patch = patches.Rectangle((center[0] - width / 2, center[1] - height / 2), width, height,
# facecolor=fill_color, edgecolor=edge_color,linewidth=cx)
# 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)