gpt4 book ai didi

利用Python求阴影部分的面积实例代码

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章利用Python求阴影部分的面积实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、前言说明 。

今天看到微信群里一道六年级数学题,如下图,求阴影部分面积 。

利用Python求阴影部分的面积实例代码

看起来似乎并不是很难,可是博主添加各种辅助线,写各种方法都没出来,不得已而改用写python代码来求面积了 。

2、思路介绍 。

1.用python将上图画在坐标轴上,主要是斜线函数和半圆函数 。

利用Python求阴影部分的面积实例代码

2.均匀的在长方形上面洒满豆子(假设是豆子),求阴影部分豆子占比*总面积 。

利用Python求阴影部分的面积实例代码

3、源码设计 。

1.做图源码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import matplotlib.pyplot as plt
import numpy as np
 
 
def init():
  plt.xlabel( 'x' )
  plt.ylabel( 'y' )
 
  fig = plt.gcf()
  fig.set_facecolor( 'lightyellow' )
  fig.set_edgecolor( "black" )
 
  ax = plt.gca()
  ax.patch.set_facecolor( "lightgray" ) # 设置ax区域背景颜色   
  ax.patch.set_alpha( 0.1 ) # 设置ax区域背景颜色透明度
  ax.spines[ 'right' ].set_color( 'none' )
  ax.spines[ 'top' ].set_color( 'none' )
  ax.xaxis.set_ticks_position( 'bottom' )
  ax.yaxis.set_ticks_position( 'left' )
  ax.spines[ 'bottom' ].set_position(( 'data' , 0 ))
  ax.spines[ 'left' ].set_position(( 'data' , 0 ))
 
 
# 原下半函数
def f1(px, r, a, b):
  return b - np.sqrt(r * * 2 - (px - a) * * 2 )
 
 
# 斜线函数
def f2(px, m, n):
  return px * n / m
 
 
# 斜线函数2
def f3(px, m, n):
  return n - 1 * px * n / m
 
 
if __name__ = = '__main__' :
  r = 4 # 圆半径
  m = 8 # 宽
  n = 4 # 高
  a, b = ( 4 , 4 ) # 圆心坐标
  init()
 
  x = np.linspace( 0 , m, 100 * m)
  y = np.linspace( 0 , n, 100 * n)
 
  # 半圆形
  y1 = f1(x, r, a, b)
  plt.plot(x, y1)
  # 矩形横线
  plt.plot((x. min (), x. max ()), (y. min (), y. min ()), 'g' )
  plt.plot((x. min (), x. max ()), (y. max (), y. max ()), 'g' )
  plt.plot((x. max (), x. max ()), (y. max () + 2 , y. max () + 2 ), 'g' ) # 画点(8,6)避免图形变形
  # 矩形纵向
  plt.plot((x. min (), x. min ()), (y. min (), y. max ()), 'g' )
  plt.plot((x. max (), x. max ()), (y. min (), y. max ()), 'g' )
  # 斜线方法
  y2 = f2(x, m, n)
  plt.plot(x, y2, 'purple' )
 
  # 阴影部分填充
  xf = x[np.where(x < = 0.5 * x. max ())]
  plt.fill_between(xf, y. min (), f1(xf, r, a, b), where = f1(xf, r, a, b) < = f2(xf, m, n),
       facecolor = 'y' , interpolate = true)
  plt.fill_between(xf, y. min (), f2(xf, m, n), where = f1(xf, r, a, b) > f2(xf, m, n),
       facecolor = 'y' , interpolate = true)
  # 半圆填充
  plt.fill_between(x, y1, y. max (), facecolor = 'r' , alpha = 0.25 )
  plt.show()
 
draw.py

2.计算源码,其中side是要不要计算图形边框上的点,理论上side只能为true;t设置越大运行时间越长也越精准 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
 
 
def f1(px, r, a, b):
  return b - np.sqrt(r * * 2 - (px - a) * * 2 )
 
 
def f2(px, m, n):
  return px * n / m
 
 
if __name__ = = '__main__' :
  r = 4 # 圆半径
  m = 8 # 宽
  n = 4 # 高
  a, b = ( 4 , 4 ) # 圆心坐标
  t = 100 # 精度
 
  xs = np.linspace( 0 , m, 2 * t * m)
  ys = np.linspace( 0 , n, t * n)
 
  # 半圆形
  y1 = f1(xs, r, a, b)
  # 斜线
  y2 = f2(xs, m, n)
 
  numin = 0
  numtotel = 0
  side = true # 是否计算边框
  for x in xs:
   for y in ys:
    if not side:
     if (x < = 0 ) | (x > = 8 ) | (y < = 0 ) | (y > = 4 ):
      continue
    numtotel + = 1
    if x > = 4 :
     continue
    y1 = f1(x, r, a, b)
    y2 = f2(x, m, n)
    if y1 - y2 > = 0 :
     if y2 - y > 0 :
      numin + = 1
     if (y2 - y = = 0 ) and side:
      numin + = 1
    elif y2 - y1 > 0 :
     if y1 - y > 0 :
      numin + = 1
     if (y2 - y = = 0 ) and side:
      numin + = 1
 
  print ( 32 * numin / numtotel)
 
calc.py

4、最后小结 。

  1.此种算法t为100时,阴影面积为1.268;t为1000时,阴影面积为1.253,已经非常接近正确答案(正确答案1.252) 。

  2.举一反三,类似于这种不规则的面积,只要可以写出来函数,就可以求解面积. 。

  2.下面有三种求解方法,第三种表示比大学高数还难看懂,你们呢?

利用Python求阴影部分的面积实例代码

利用Python求阴影部分的面积实例代码

利用Python求阴影部分的面积实例代码

总结 。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.

原文链接:http://www.cnblogs.com/Vrapile/p/10067297.html 。

最后此篇关于利用Python求阴影部分的面积实例代码的文章就讲到这里了,如果你想了解更多关于利用Python求阴影部分的面积实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com