【教学类-50-07】20240411“数一数”图片中四种图形出现的数量随机或固定

 

背景需求

今天孩子们点数时,我核对答案,突然发现有两张图片上的三角、正方、椭圆、圆形只有1个,我感觉这个随机的概率有问题。

仔细点数后发现以下代码生成的几何图形数量是相同的

(如果三角有3个,那么正方形、圆形和椭圆形都是3个)(如果三角有1个,那么正方形、圆形和椭圆形都是1个)c939e426573c4a7eb22d3596d1b4397f.png

【教学类-50-01】20240407“数一数”图片样式01:纯色图形与边框不相交,纯色图形和其他纯色图形不相交-CSDN博客文章浏览阅读606次,点赞31次,收藏13次。【教学类-50-01】20240407“数一数”图片样式01:纯色图形与边框不相交,纯色图形和其他纯色图形不相交https://blog.csdn.net/reasonsummer/article/details/137511583

产生原因:随机抽取了一个数量,并且每个图形都用了这个数量

1fd4273cc9e64fe7b09ba47588e6dd6e.png

 

一、一张图上,四种图案出现数量不相等(随机1-4)

代码需要修改为

f0f1f27af3fd4875922c494cc5a4ebb1.png

代码展示:

'''
中班个别化学习材料-数形颜色的点数-难度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)

 

88d0e3f7ab3147e7971ca1286ca36367.png

这下四种图案出现的数量不会相等了。

436f322c0b7b495eb85a5c24d830597b.png

82ff732661034994a08c8f03b5610af8.png

27f088bee5e044dd89f039808f5893d0.png

现在每张图上,4种几何图案出现的数量不相等了。

 

二、一张图上,四种图案出现数量固定(确定、三角形4个、正方形3个,圆形2个,椭圆形1个)

如果图形的数量不用随机数,也可以指定数量

c8ac0e469c81468baf882eb094eea1d2.png

代码展示

'''
中班个别化学习材料-数形颜色的点数-难度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)

88d0e3f7ab3147e7971ca1286ca36367.png

点数验证——每张偏上都是三角形4个、正方形3个,圆形2个,椭圆形1个

04ea5b20a34646a09d9aca9c4d5f78cc.pngcb79e4458f0b48598e33673f77a2e5f8.png

133fc38ada0d4769b9c1bcc19bfe63b6.png

1f9ae41671e84b0b8c26b9dd1e7b4d20.png

87dab3a23ab34d09b38e57e473d50688.png

这种方式生成N张几何图案出现位置不同的图片,但是最后的答案是一样的(10个图案,三角形4个、正方形3个,圆形2个,椭圆形1个)

这样就不用老师自己去点数,来浪费验证时间了。比较适合集体教学活动。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值