Shakeel1979 commited on
Commit
9d36ff7
·
verified ·
1 Parent(s): 388dedf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -21
app.py CHANGED
@@ -10,7 +10,10 @@ def calculate_distance(x1, y1, x2, y2):
10
 
11
  # Function to calculate angles using the Law of Cosines
12
  def calculate_angle(a, b, c):
13
- angle = math.degrees(math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
 
 
 
14
  return angle
15
 
16
  # Function to calculate area using Heron's formula
@@ -25,21 +28,21 @@ def calculate_perimeter(a, b, c):
25
 
26
  # Function to calculate the radius of the inscribed circle
27
  def calculate_radius_inscribed_circle(a, b, c):
28
- s = (a + b + c) / 2
29
- area = calculate_area(a, b, c)
30
  try:
 
 
31
  radius = area / s
32
  except ZeroDivisionError:
33
- radius = 0
34
  return radius
35
 
36
  # Function to calculate the radius of the circumscribed circle
37
  def calculate_radius_circumscribed_circle(a, b, c):
38
- area = calculate_area(a, b, c)
39
  try:
 
40
  radius = (a * b * c) / (4 * area)
41
  except ZeroDivisionError:
42
- radius = 0
43
  return radius
44
 
45
  # Function to calculate the centroid coordinates
@@ -54,7 +57,7 @@ def calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c):
54
  I_x = (a * x1 + b * x2 + c * x3) / (a + b + c)
55
  I_y = (a * y1 + b * y2 + c * y3) / (a + b + c)
56
  except ZeroDivisionError:
57
- I_x, I_y = 0, 0
58
  return I_x, I_y
59
 
60
  # Function to calculate the circumcenter coordinates
@@ -64,9 +67,22 @@ def calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c):
64
  Ux = ((x1**2 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2)) / D
65
  Uy = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1)) / D
66
  except ZeroDivisionError:
67
- Ux, Uy = 0, 0
68
  return Ux, Uy
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  # Function to format values close to zero as 0
71
  def format_zero(val):
72
  if abs(val) < 1e-6:
@@ -74,40 +90,47 @@ def format_zero(val):
74
  return val
75
 
76
  # Function to plot the triangle with all points in different colors and a legend
77
- def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y):
78
  fig, ax = plt.subplots(figsize=(8, 6))
79
  triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue')
80
  ax.add_patch(triangle)
81
 
82
  # Define colors for different points
83
  vertex_color = 'blue'
84
- centroid_color = 'green'
85
  incenter_color = 'red'
86
  circumcenter_color = 'purple'
87
-
 
88
  # Plot the triangle vertices
89
  vertices = [(x1, y1), (x2, y2), (x3, y3)]
 
90
  for i, (vx, vy) in enumerate(vertices):
91
  ax.scatter(vx, vy, color=vertex_color, zorder=5)
92
- ax.text(vx, vy, f"({vx:.1f}, {vy:.1f})", fontsize=10, ha="left", va="bottom", color=vertex_color)
93
-
94
  # Plot key points with their corresponding colors
95
  key_points = [
96
- (I_x, I_y, incenter_color, "Incenter"),
97
- (Ux, Uy, circumcenter_color, "Circumcenter"),
98
- (G_x, G_y, centroid_color, "Centroid")
99
  ]
100
 
101
- for x, y, color, label in key_points:
102
  ax.scatter(x, y, color=color, zorder=5)
103
- ax.text(x, y, f"{label}\n({x:.1f}, {y:.1f})", fontsize=10, ha="left", va="bottom", color=color)
 
 
 
 
 
104
 
105
  # Add legend
106
  handles = [
107
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=10, label="Vertices"),
108
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=centroid_color, markersize=10, label="Centroid"),
109
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=incenter_color, markersize=10, label="Incenter"),
110
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=circumcenter_color, markersize=10, label="Circumcenter")
 
111
  ]
112
  ax.legend(handles=handles, loc='upper left', fontsize=12)
113
 
@@ -124,6 +147,11 @@ def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y):
124
  plt.grid(True)
125
  st.pyplot(fig)
126
 
 
 
 
 
 
127
  # Main function to interact with the user
128
  def main():
129
  st.title("Advanced Triangle Solver")
@@ -144,11 +172,21 @@ def main():
144
  b = calculate_distance(x1, y1, x3, y3)
145
  c = calculate_distance(x1, y1, x2, y2)
146
 
 
 
 
 
 
147
  # Calculate angles using the Law of Cosines
148
  A = calculate_angle(a, b, c)
149
  B = calculate_angle(b, a, c)
150
  C = calculate_angle(c, a, b)
151
 
 
 
 
 
 
152
  # Calculate area, perimeter, and radius of inscribed and circumscribed circles
153
  area = calculate_area(a, b, c)
154
  perimeter = calculate_perimeter(a, b, c)
@@ -160,6 +198,9 @@ def main():
160
  I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
161
  Ux, Uy = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
162
 
 
 
 
163
  # Display results in columns
164
  col1, col2, col3 = st.columns(3)
165
 
@@ -199,8 +240,8 @@ def main():
199
  st.markdown(f"Perimeter: **{perimeter:.3f}** units")
200
  st.markdown(f"Centroid: **({G_x:.3f}, {G_y:.3f})**")
201
 
202
- # Display triangle graph with colored points and legend
203
- plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y)
204
 
205
  if __name__ == "__main__":
206
  main()
 
10
 
11
  # Function to calculate angles using the Law of Cosines
12
  def calculate_angle(a, b, c):
13
+ try:
14
+ angle = math.degrees(math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
15
+ except ValueError:
16
+ angle = 0 # Handle possible domain error in acos
17
  return angle
18
 
19
  # Function to calculate area using Heron's formula
 
28
 
29
  # Function to calculate the radius of the inscribed circle
30
  def calculate_radius_inscribed_circle(a, b, c):
 
 
31
  try:
32
+ s = (a + b + c) / 2
33
+ area = calculate_area(a, b, c)
34
  radius = area / s
35
  except ZeroDivisionError:
36
+ radius = 0 # Handle case where area or perimeter is zero
37
  return radius
38
 
39
  # Function to calculate the radius of the circumscribed circle
40
  def calculate_radius_circumscribed_circle(a, b, c):
 
41
  try:
42
+ area = calculate_area(a, b, c)
43
  radius = (a * b * c) / (4 * area)
44
  except ZeroDivisionError:
45
+ radius = 0 # Handle case where area is zero
46
  return radius
47
 
48
  # Function to calculate the centroid coordinates
 
57
  I_x = (a * x1 + b * x2 + c * x3) / (a + b + c)
58
  I_y = (a * y1 + b * y2 + c * y3) / (a + b + c)
59
  except ZeroDivisionError:
60
+ I_x, I_y = 0, 0 # Handle division by zero if sides sum to zero
61
  return I_x, I_y
62
 
63
  # Function to calculate the circumcenter coordinates
 
67
  Ux = ((x1**2 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2)) / D
68
  Uy = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1)) / D
69
  except ZeroDivisionError:
70
+ Ux, Uy = 0, 0 # Handle division by zero in circumcenter calculation
71
  return Ux, Uy
72
 
73
+ # Function to calculate midpoints of sides
74
+ def calculate_midpoints(x1, y1, x2, y2, x3, y3):
75
+ # Midpoint of AB
76
+ M1_x = (x1 + x2) / 2
77
+ M1_y = (y1 + y2) / 2
78
+ # Midpoint of BC
79
+ M2_x = (x2 + x3) / 2
80
+ M2_y = (y2 + y3) / 2
81
+ # Midpoint of CA
82
+ M3_x = (x3 + x1) / 2
83
+ M3_y = (y3 + y1) / 2
84
+ return (M1_x, M1_y), (M2_x, M2_y), (M3_x, M3_y)
85
+
86
  # Function to format values close to zero as 0
87
  def format_zero(val):
88
  if abs(val) < 1e-6:
 
90
  return val
91
 
92
  # Function to plot the triangle with all points in different colors and a legend
93
+ def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y, midpoints):
94
  fig, ax = plt.subplots(figsize=(8, 6))
95
  triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue')
96
  ax.add_patch(triangle)
97
 
98
  # Define colors for different points
99
  vertex_color = 'blue'
100
+ centroid_color = 'orange'
101
  incenter_color = 'red'
102
  circumcenter_color = 'purple'
103
+ midpoint_color = 'green'
104
+
105
  # Plot the triangle vertices
106
  vertices = [(x1, y1), (x2, y2), (x3, y3)]
107
+ vertex_labels = [f"Vertex A ({x1:.1f}, {y1:.1f})", f"Vertex B ({x2:.1f}, {y2:.1f})", f"Vertex C ({x3:.1f}, {y3:.1f})"]
108
  for i, (vx, vy) in enumerate(vertices):
109
  ax.scatter(vx, vy, color=vertex_color, zorder=5)
110
+
 
111
  # Plot key points with their corresponding colors
112
  key_points = [
113
+ (I_x, I_y, incenter_color),
114
+ (Ux, Uy, circumcenter_color),
115
+ (G_x, G_y, centroid_color)
116
  ]
117
 
118
+ for x, y, color in key_points:
119
  ax.scatter(x, y, color=color, zorder=5)
120
+ ax.text(x, y, fontsize=10, ha="left", va="bottom", color=color)
121
+
122
+ # Plot midpoints of sides
123
+ for i, (mx, my) in enumerate(midpoints):
124
+ ax.scatter(mx, my, color=midpoint_color, zorder=5)
125
+ ax.text(mx, my, fontsize=10, ha="left", va="bottom", color=midpoint_color)
126
 
127
  # Add legend
128
  handles = [
129
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=10, label="Vertices"),
130
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=centroid_color, markersize=10, label="Centroid"),
131
  plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=incenter_color, markersize=10, label="Incenter"),
132
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=circumcenter_color, markersize=10, label="Circumcenter"),
133
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=10, label="Midpoints")
134
  ]
135
  ax.legend(handles=handles, loc='upper left', fontsize=12)
136
 
 
147
  plt.grid(True)
148
  st.pyplot(fig)
149
 
150
+ # Function to check if the sides form a valid triangle
151
+ def is_valid_triangle(a, b, c):
152
+ # Check if the sum of two sides is greater than the third side (Triangle Inequality Theorem)
153
+ return a + b > c and b + c > a and c + a > b
154
+
155
  # Main function to interact with the user
156
  def main():
157
  st.title("Advanced Triangle Solver")
 
172
  b = calculate_distance(x1, y1, x3, y3)
173
  c = calculate_distance(x1, y1, x2, y2)
174
 
175
+ # Validate if it's a valid triangle
176
+ if not is_valid_triangle(a, b, c):
177
+ st.error("The entered points do not form a valid triangle.")
178
+ return
179
+
180
  # Calculate angles using the Law of Cosines
181
  A = calculate_angle(a, b, c)
182
  B = calculate_angle(b, a, c)
183
  C = calculate_angle(c, a, b)
184
 
185
+ # Check if angles sum up to 180 degrees
186
+ if abs(A + B + C - 180) > 1e-2:
187
+ st.error("The sum of the angles is not 180 degrees.")
188
+ return
189
+
190
  # Calculate area, perimeter, and radius of inscribed and circumscribed circles
191
  area = calculate_area(a, b, c)
192
  perimeter = calculate_perimeter(a, b, c)
 
198
  I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
199
  Ux, Uy = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
200
 
201
+ # Calculate midpoints of the sides
202
+ midpoints = calculate_midpoints(x1, y1, x2, y2, x3, y3)
203
+
204
  # Display results in columns
205
  col1, col2, col3 = st.columns(3)
206
 
 
240
  st.markdown(f"Perimeter: **{perimeter:.3f}** units")
241
  st.markdown(f"Centroid: **({G_x:.3f}, {G_y:.3f})**")
242
 
243
+ # Display triangle graph with midpoints and colored points
244
+ plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y, midpoints)
245
 
246
  if __name__ == "__main__":
247
  main()