用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()

 

得出結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章