triangle_area_calculators库发布
最近将在pip网站上发布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:使用坐标法计算三角形面积
x1, y1 = 1, 1
x2, y2 = 3, 4
x3, y3 = 5, 2
print("坐标分别为(", 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("向量坐标分别为(", 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:计算圆内接三角形面积(这里传入角度制角度)
R_radius = 3
alpha_degrees = 90
print("圆半径为", R_radius, "中心角为", alpha_degrees, "度的圆内接三角形面积:", TriangleAreaCalculators.area_of_circumscribed_triangle(R_radius, alpha_degrees))
'''
代码链接🔗:
http://localhost:8888/tree