给定直径,以及线段或弦的长度。我的问题的直径是 12,弦是 10。你必须找到阴影部分的高度,然后打印该区域。原始公式为 A=2/3ch + h^3/2c。我的同学在该地区得到了 18,但是当我使用我的代码时,我得到了 41。
这是我能找到的最接近的图片表示。但是从 ϴ 到 s 有一条虚线。
from math import sqrt
diamStr=input("Enter the length of the diameter: ")
diameter=int(diamStr)
chordStr = input( " Enter the chord length: ")
chord = int(chordStr)
radius = (diameter/2)
s = sqrt (diameter**2+chord**2)
h = (s/2-radius)
i= (2/3*chord*h)
j=(h**3/2*chord)
area = (i+j)
print (area)
请您参考如下方法:
不幸的是,您的公式有问题,但如果用一些初等数学看问题,您可能会注意到可以使用 cosine rule 找到角度 ϴ因为我们知道 3 个长度 (两个半径和弦长)
在 Python 中它将是:
theta = math.acos((radius**2 + radius**2 - chord**2)/(2*radius**2))
由于变量 theta 已经以弧度为单位,我们可以使用这个公式来计算线段的面积:
在 Python 中是 area = 1/2 * (theta - math.sin(theta)) * radius**2
因此在合并所有这些之后我们想出了一个优雅的解决方案:
import math
diamStr=input("Enter the length of the diameter: ")
diameter=int(diamStr)
chordStr = input( " Enter the chord length: ")
chord = int(chordStr)
radius = (diameter/2)
theta = math.acos((radius**2 + radius**2 - chord**2)/(2*radius**2))
area = 1/2 * (theta - math.sin(theta)) * radius**2
#print(round((area),2))
print(area)
如果您输入的直径为 12 厘米,弦长为 10,您将得到 18.880864248381847 但您可以通过 round() 将其四舍五入到您想要的任意小数位数> 功能。
例如:print(round((area),2)) 打印 18.88
