求阴影部分的面积 小学升初中的数学题

求阴影部分的面积 小学升初中的数学题

基于Python语言使用蒙特卡罗模拟求解阴影部分的面积。如下图求阴影部分的面积。

Python代码如下:

 

# -*- coding: utf-8 -*-
"""
Created on Fri Feb  7 06:01:33 2020

@author: 尹立庆
@wechat: 13521526165

Find the area of shadow part

求阴影部分面积

"""

import random
import math

# random.seed(138)

l = 10  # 正方形边长为l

r1 = l/2 # 内切圆直径为l,半径为r1 = l/2
r2 = l   # 扇形半径为r2 = l

count = 0
POINT = 100000000
for i in range(POINT):
    # random.random()用于生成一个0到1的随机浮点数: 0 <= n < 1.0 即 [0,1)
    x = random.random() * l # 在边长为l的正方形内均匀的随机生成点
    y = random.random() * l
    # print(x,y)
    # 内切圆的中心为 (l/2, l/2)
    # 计算随机生成的点(x,y)到内切圆的中心点(l/2, l/2)的距离
    d_inscribed_circle = math.sqrt((x - l/2) ** 2 + (y - l/2) ** 2)
    # 计算点到(10,0)和(0,10)距离
    # 点到(10,0)和(0,10)距离大于扇形半径r2=l
    # x大于y时即点在右下方,计算点到(0,10)距离;x小于y时即点在左上方,计算点到(10,0)距离;
    # x等于y时不用计算,因为这个点不可能在阴影区域
    sector_center = (10,0)
    if x > y :
        sector_center = (0,10)
    
    # 计算点到扇形中心点的距离
    d_sector_center = math.sqrt((x - sector_center[0]) ** 2 + (y - sector_center[1]) ** 2)
    
    # (x,y)点到内切圆中心点的距离小于半径r1的长度,并切(x,y)点到扇形中心点的距离大于半径r2的长度
    if r1 >= d_inscribed_circle and r2 <= d_sector_center:
        count = count + 1
        
area_shadow_part = l * l * (count / POINT)

print('the area of shadow part: ', area_shadow_part)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值