Loading

用python求解数学题

 

  1 import math
  2 import matplotlib.pyplot as plt
  3 import numpy as np
  4 
  5 def generate_circle_points(center_x, center_y, radius, num_points=100):
  6     points = []
  7     for i in range(num_points):
  8         #把圆360°拆分成num_points个角度
  9         angle = 360 * i / num_points
 10         #循环迭代每个角度得到圆边上的所有坐标点D
 11         x = center_x + radius * math.cos(angle)
 12         y = center_y + radius * math.sin(angle)
 13         points.append((x, y))
 14     return points
 15 
 16 #找到BD的所有中心点坐标
 17 def find_midpoint(point1, point2):
 18     return ((point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2)
 19 
 20 #方法一,使用math库的sqrt函数:计算所有BD线上的中心点到原点(0,0)的距离
 21 # def calculate_distance(point1, point2):
 22 #     return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
 23 
 24 #方法二,使用numpy库的norm函数计算向量的长度:计算所有BD线上的中心点到原点(0,0)的距离
 25 def calculate_distance(point1, point2):
 26     vector = np.array([point1, point2])
 27     return np.linalg.norm(vector)
 28 
 29 if __name__ == "__main__":
 30     # A点的坐标
 31     A_x, A_y = 0, 6
 32 
 33     # B点的坐标
 34     B_x, B_y = 2 * math.sqrt(7), 0
 35 
 36     # C点的坐标
 37     C_x, C_y = 0, 0
 38 
 39     # 以A为圆心的圆的半径
 40     radius = 2
 41 
 42     # 找出以A为圆心的圆边上的所有点D
 43     D_points = generate_circle_points(A_x, A_y, radius)
 44 
 45     # 找到B点和每个点D之间的线的中心点F
 46     F_points = [find_midpoint((B_x, B_y), D) for D in D_points]
 47 
 48     # 计算所有F点和C点之间的距离,并找到最长和最短距离
 49     min_distance = float('inf')
 50     max_distance = 0.0
 51     #循环迭代所有F点到C点的距离,找到最短、最长的距离。
 52     for F in F_points:
 53         distance = calculate_distance(F, (C_x, C_y))
 54         min_distance = min(min_distance, distance)
 55         max_distance = max(max_distance, distance)
 56 
 57     # 输出结果
 58     print("F点和C点的最短距离是: {:.0f}".format(min_distance))
 59     print("F点和C点的最长距离是: {:.0f}".format(max_distance))
 60 
 61     # 绘制图形
 62     plt.figure(figsize=(8, 8))
 63 
 64     # 画圆
 65     circle = plt.Circle((A_x, A_y), radius, color='b', fill=False)
 66     plt.gca().add_patch(circle)
 67 
 68     # 画点A、B、C
 69     plt.scatter([A_x, B_x, C_x], [A_y, B_y, C_y], c='red', label='A, B, C')
 70 
 71     # 画线
 72     plt.plot([A_x, B_x],[A_y, B_y])
 73     plt.plot([A_x, C_x],[A_y, C_y])
 74     plt.plot([B_x, C_x],[B_y, C_y])
 75     
 76     # 画点D和F
 77     D_x, D_y = zip(*D_points)
 78     F_x, F_y = zip(*F_points)
 79     plt.scatter(D_x, D_y, c='green', label='D')
 80     plt.scatter(F_x, F_y, c='orange', label='F')
 81 
 82     # 添加标签
 83     plt.text(A_x, A_y, 'A', ha='right', va='bottom')
 84     plt.text(B_x, B_y, 'B', ha='right', va='bottom')
 85     plt.text(C_x, C_y, 'C', ha='right', va='bottom')
 86     plt.text(D_x[0], D_y[0], 'D', ha='right', va='bottom')
 87     plt.text(F_x[0], F_y[0], 'F', ha='right', va='bottom')
 88 
 89     plt.xlabel('X')
 90     plt.ylabel('Y')
 91     plt.title('Circle with points A, B, C, D, F')
 92     plt.legend()
 93 
 94     # 设置坐标轴范围
 95     plt.xlim(-5, 10)
 96     plt.ylim(-5, 10)
 97 
 98     # 显示图形
 99     plt.grid()
100     plt.axhline(y=0, color='k', linewidth=0.5)
101     plt.axvline(x=0, color='k', linewidth=0.5)
102     plt.gca().set_aspect('equal', adjustable='box')
103     plt.show()

 

得出结果:

 

posted @ 2023-08-03 00:10  江雪独钓翁  阅读(71)  评论(0编辑  收藏  举报