知名網(wǎng)站制作公百度搜索詞排名
最近將在pip網(wǎng)站上發(fā)布triangle_area_calculators庫(我編寫的python第三方庫)
triangle_area_calculators庫用于計算不同類型及不同已知量的三角形面積
在triangle_area_calculators庫中,有一個名為TriangleAreaCalculators的類
可以通過from triangle_area_calculators import TriangleAreaCalculators方式引用
?
?
import math
class TriangleAreaCalculators:
? ? HALF = 0.5
? ? @staticmethod
? ? def area_by_base_height(base, height):
? ? ? ? return TriangleAreaCalculators.HALF * base * height
? ? @staticmethod
? ? def area_by_heron(a, b, c):
? ? ? ? p = (a + b + c) / 2
? ? ? ? return math.sqrt(p * (p - a) * (p - b) * (p - c))
? ? @staticmethod
? ? def area_by_two_sides_angle(a, b, gamma_degrees):
? ? ? ? gamma_radians = math.radians(gamma_degrees)
? ? ? ? return TriangleAreaCalculators.HALF * a * b * math.sin(gamma_radians)
? ? @staticmethod
? ? def area_by_coordinates(x1, y1, x2, y2, x3, y3):
? ? ? ? return TriangleAreaCalculators.HALF * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
? ? @staticmethod
? ? def area_by_vectors(A_x, A_y, B_x, B_y):
? ? ? ? return TriangleAreaCalculators.HALF * abs(A_x * B_y - A_y * B_x)
? ? @staticmethod
? ? def area_of_right_triangle_by_two_legs(a, b):
? ? ? ? return TriangleAreaCalculators.HALF * a * b
? ? @staticmethod
? ? def area_of_right_triangle_by_hypotenuse_and_leg(c, a):
? ? ? ? theta_radians = math.acos(a / c)
? ? ? ? return TriangleAreaCalculators.HALF * a * c * math.sin(theta_radians)
? ? @staticmethod
? ? def area_of_equilateral_triangle(a):
? ? ? ? return (math.sqrt(3) / 4) * a ** 2
? ? @staticmethod
? ? def area_of_isosceles_triangle_by_base_angle(b, a, theta_degrees):
? ? ? ? theta_radians = math.radians(theta_degrees)
? ? ? ? return TriangleAreaCalculators.HALF * b * a * math.sin(theta_radians)
? ? @staticmethod
? ? def area_of_isosceles_triangle_by_height(b, h):
? ? ? ? return TriangleAreaCalculators.HALF * b * h
? ? @staticmethod
? ? def area_of_isosceles_right_triangle(a):
? ? ? ? return TriangleAreaCalculators.HALF * a ** 2
? ? @staticmethod
? ? def area_of_circumscribed_triangle(R, alpha_degrees):
? ? ? ? alpha_radians = math.radians(alpha_degrees)
? ? ? ? return TriangleAreaCalculators.HALF * R ** 2 * math.sin(alpha_radians)
? ? ? ??
'''
#使用示例
from triangle_area_calculators import TriangleAreaCalculators
# 示例1:使用底和高計算三角形面積
base = 5
height = 8
print("底為", base, "高為", height, "的三角形面積:", TriangleAreaCalculators.area_by_base_height(base, height))
# 示例2:使用海倫公式計算三角形面積(已知三邊)
a = 3
b = 4
c = 5
print("三邊分別為", a, b, c, "的三角形面積:", TriangleAreaCalculators.area_by_heron(a, b, c))
# 示例3:使用兩邊和夾角計算三角形面積(這里傳入角度制角度)
a_side = 6
b_side = 8
angle_gamma_degrees = 60
print("兩邊分別為", a_side, b_side, "夾角為", angle_gamma_degrees, "度的三角形面積:", TriangleAreaCalculators.area_by_two_sides_angle(a_side, b_side, angle_gamma_degrees))
# 示例4:使用坐標(biāo)法計算三角形面積
x1, y1 = 1, 1
x2, y2 = 3, 4
x3, y3 = 5, 2
print("坐標(biāo)分別為(", x1, y1, "), (", x2, y2, "), (", x3, y3, ")的三角形面積:", TriangleAreaCalculators.area_by_coordinates(x1, y1, x2, y2, x3, y3))
# 示例5:使用向量法計算三角形面積
A_x, A_y = 2, 3
B_x, B_y = 5, 7
print("向量坐標(biāo)分別為(", A_x, A_y, "), (", B_x, B_y, ")的三角形面積:", TriangleAreaCalculators.area_by_vectors(A_x, A_y, B_x, B_y))
# 示例6:計算直角三角形面積(已知兩條直角邊)
right_a = 3
right_b = 4
print("兩條直角邊分別為", right_a, b, "的直角三角形面積:", TriangleAreaCalculators.area_of_right_triangle_by_two_legs(right_a, right_b))
# 示例7:計算直角三角形面積(已知斜邊和一條直角邊)
right_c = 5
right_a = 3
print("斜邊為", right_c, "一條直角邊為", right_a, "的直角三角形面積:", TriangleAreaCalculators.area_of_right_triangle_by_hypotenuse_and_leg(right_c, right_a))
# 示例8:計算等邊三角形面積
equilateral_a = 6
print("邊長為", equilateral_a, "的等邊三角形面積:", TriangleAreaCalculators.area_of_equilateral_triangle(equilateral_a))
# 示例9:計算等腰三角形面積(已知底邊、等腰邊和頂角,這里傳入角度制角度)
isosceles_b = 4
isosceles_a = 5
isosceles_theta_degrees = 45
print("底邊為", isosceles_b, "等腰邊為", isosceles_a, "頂角為", isosceles_theta_degrees, "度的等腰三角形面積:", TriangleAreaCalculators.area_of_isosceles_triangle_by_base_angle(isosceles_b, isosceles_a, isosceles_theta_degrees))
# 示例10:計算等腰三角形面積(已知等腰邊和底邊上的高)
isosceles_a_side = 5
isosceles_height = 3
print("等腰邊為", isosceles_a_side, "底邊上的高為", isosceles_height, "的等腰三角形面積:", TriangleAreaCalculators.area_of_isosceles_triangle_by_height(isosceles_a_side, isosceles_height))
# 示例11:計算等腰直角三角形面積
isosceles_right_a = 4
print("邊長為", isosceles_right_a, "的等腰直角三角形面積:", TriangleAreaCalculators.area_of_isosceles_right_triangle(isosceles_right_a))
# 示例12:計算圓內(nèi)接三角形面積(這里傳入角度制角度)
R_radius = 3
alpha_degrees = 90
print("圓半徑為", R_radius, "中心角為", alpha_degrees, "度的圓內(nèi)接三角形面積:", TriangleAreaCalculators.area_of_circumscribed_triangle(R_radius, alpha_degrees))
'''
?
?
代碼鏈接🔗:
http://localhost:8888/tree