Shakeel1979 commited on
Commit
e212818
·
verified ·
1 Parent(s): 6ba59ad

Create Old_working_app_py.txt

Browse files
Files changed (1) hide show
  1. Old_working_app_py.txt +259 -0
Old_working_app_py.txt ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from matplotlib.patches import Polygon
5
+
6
+ # Function to calculate the distance between two points
7
+ def calculate_distance(x1, y1, x2, y2):
8
+ return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
9
+
10
+ # Function to calculate angles using the Law of Cosines
11
+ def calculate_angle(a, b, c):
12
+ try:
13
+ angle = np.degrees(np.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
14
+ except ValueError:
15
+ angle = 0 # Handle possible domain error in acos
16
+ return angle
17
+
18
+ # Function to calculate area using Heron's formula
19
+ def calculate_area(a, b, c):
20
+ s = (a + b + c) / 2
21
+ area = np.sqrt(s * (s - a) * (s - b) * (s - c))
22
+ return area
23
+
24
+ # Function to calculate the perimeter
25
+ def calculate_perimeter(a, b, c):
26
+ return a + b + c
27
+
28
+ # Function to calculate the radius of the inscribed circle
29
+ def calculate_radius_inscribed_circle(a, b, c):
30
+ try:
31
+ s = (a + b + c) / 2
32
+ area = calculate_area(a, b, c)
33
+ radius = area / s
34
+ except ZeroDivisionError:
35
+ radius = 0 # Handle case where area or perimeter is zero
36
+ return radius
37
+
38
+ # Function to calculate the radius of the circumscribed circle
39
+ def calculate_radius_circumscribed_circle(a, b, c):
40
+ try:
41
+ area = calculate_area(a, b, c)
42
+ radius = (a * b * c) / (4 * area)
43
+ except ZeroDivisionError:
44
+ radius = 0 # Handle case where area is zero
45
+ return radius
46
+
47
+ # Function to calculate the centroid coordinates
48
+ def calculate_centroid(x1, y1, x2, y2, x3, y3):
49
+ G_x = (x1 + x2 + x3) / 3
50
+ G_y = (y1 + y2 + y3) / 3
51
+ return G_x, G_y
52
+
53
+ # Function to calculate the incenter coordinates
54
+ def calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c):
55
+ try:
56
+ I_x = (a * x1 + b * x2 + c * x3) / (a + b + c)
57
+ I_y = (a * y1 + b * y2 + c * y3) / (a + b + c)
58
+ except ZeroDivisionError:
59
+ I_x, I_y = 0, 0 # Handle division by zero if sides sum to zero
60
+ return I_x, I_y
61
+
62
+ # Function to calculate the circumcenter coordinates
63
+ def calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c):
64
+ try:
65
+ D = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
66
+ Ux = ((x1**2 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2)) / D
67
+ Uy = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1)) / D
68
+ except ZeroDivisionError:
69
+ Ux, Uy = 0, 0 # Handle division by zero in circumcenter calculation
70
+ return Ux, Uy
71
+
72
+ # Function to calculate midpoints of sides
73
+ def calculate_midpoints(x1, y1, x2, y2, x3, y3):
74
+ # Midpoint of AB
75
+ M1_x = (x1 + x2) / 2
76
+ M1_y = (y1 + y2) / 2
77
+ # Midpoint of BC
78
+ M2_x = (x2 + x3) / 2
79
+ M2_y = (y2 + y3) / 2
80
+ # Midpoint of CA
81
+ M3_x = (x3 + x1) / 2
82
+ M3_y = (y3 + y1) / 2
83
+ return (M1_x, M1_y), (M2_x, M2_y), (M3_x, M3_y)
84
+
85
+ # Function to format values close to zero as 0
86
+ def format_zero(val):
87
+ if abs(val) < 1e-6:
88
+ return 0.0
89
+ return val
90
+
91
+ # Function to plot the triangle with all points in different colors and a legend
92
+ def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y, midpoints):
93
+ fig, ax = plt.subplots(figsize=(8, 6))
94
+ triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue')
95
+ ax.add_patch(triangle)
96
+
97
+ # Define colors for different points
98
+ vertex_color = 'blue'
99
+ midpoint_color = 'green'
100
+ centroid_color = 'orange'
101
+ incenter_color = 'red'
102
+ circumcenter_color = 'purple'
103
+
104
+ # Plot the triangle vertices
105
+ vertices = [(x1, y1), (x2, y2), (x3, y3)]
106
+ vertex_labels = [f"Vertex A ({x1:.3f}, {y1:.3f})", f"Vertex B ({x2:.3f}, {y2:.3f})", f"Vertex C ({x3:.3f}, {y3:.3f})"]
107
+ for i, (vx, vy) in enumerate(vertices):
108
+ ax.scatter(vx, vy, color=vertex_color, zorder=3)
109
+
110
+ # Plot key points with their corresponding colors
111
+ key_points = [
112
+ (I_x, I_y, incenter_color),
113
+ (Ux, Uy, circumcenter_color),
114
+ (G_x, G_y, centroid_color)
115
+ ]
116
+ key_points_labels = [f"Incenter ({I_x:.3f}, {I_y:.3f})", f"Circumcenter ({Ux:.3f}, {Uy:.3f})", f"Centroid ({G_x:.3f}, {G_y:.3f})"]
117
+
118
+ for x, y, color in key_points:
119
+ ax.scatter(x, y, color=color, zorder=4)
120
+
121
+ # Plot midpoints of sides
122
+ for i, (mx, my) in enumerate(midpoints):
123
+ ax.scatter(mx, my, color=midpoint_color, zorder=5)
124
+
125
+ # Add legend
126
+ handles = [
127
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[0]),
128
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[1]),
129
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[2]),
130
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[0]),
131
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[1]),
132
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[2]),
133
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=incenter_color, markersize=8, label=key_points_labels[0]),
134
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=circumcenter_color, markersize=8, label=key_points_labels[1]),
135
+ plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=centroid_color, markersize=8, label=key_points_labels[2])
136
+ ]
137
+ ax.legend(handles=handles, loc='upper left', fontsize=12)
138
+
139
+ # Adjust the plot limits and aspect ratio
140
+ padding = 3
141
+ ax.set_xlim([min(x1, x2, x3) - padding, max(x1, x2, x3) + padding])
142
+ ax.set_ylim([min(y1, y2, y3) - padding, max(y1, y2, y3) + padding])
143
+ ax.set_aspect('equal', adjustable='datalim')
144
+
145
+ ax.set_title('Solved Triangle', fontsize=18)
146
+ ax.set_xlabel('X-axis', fontsize=12)
147
+ ax.set_ylabel('Y-axis', fontsize=12)
148
+
149
+ plt.grid(True)
150
+ st.pyplot(fig)
151
+
152
+ # Function to check if the sides form a valid triangle
153
+ def is_valid_triangle(a, b, c):
154
+ # Check if the sum of two sides is greater than the third side (Triangle Inequality Theorem)
155
+ return a + b > c and b + c > a and c + a > b
156
+
157
+ # Main function to interact with the user
158
+ def main():
159
+ st.title("Advanced Triangle Solver")
160
+
161
+ st.sidebar.header("Enter the coordinates of the three points:")
162
+
163
+ # Coordinates input (X1, Y1), (X2, Y2), (X3, Y3)
164
+ x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
165
+ y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
166
+ x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
167
+ y2 = st.sidebar.number_input("Y2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
168
+ x3 = st.sidebar.number_input("X3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
169
+ y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
170
+
171
+ if st.sidebar.button("Calculate"):
172
+ # Calculate the lengths of the sides of the triangle using Euclidean distance
173
+ a = calculate_distance(x2, y2, x3, y3)
174
+ b = calculate_distance(x1, y1, x3, y3)
175
+ c = calculate_distance(x1, y1, x2, y2)
176
+
177
+ # Validate if it's a valid triangle
178
+ if not is_valid_triangle(a, b, c):
179
+ st.error("The entered points do not form a valid triangle.")
180
+ return
181
+
182
+ # Calculate angles using the Law of Cosines
183
+ A = calculate_angle(a, b, c)
184
+ B = calculate_angle(b, a, c)
185
+ C = calculate_angle(c, a, b)
186
+
187
+ # Check if angles sum up to 180 degrees
188
+ if abs(A + B + C - 180) > 1e-2:
189
+ st.error("The sum of the angles is not 180 degrees.")
190
+ return
191
+
192
+ # Calculate area, perimeter, and radius of inscribed and circumscribed circles
193
+ area = calculate_area(a, b, c)
194
+ perimeter = calculate_perimeter(a, b, c)
195
+ radius_in = calculate_radius_inscribed_circle(a, b, c)
196
+ radius_circum = calculate_radius_circumscribed_circle(a, b, c)
197
+
198
+ # Calculate centroid, incenter, and circumcenter coordinates
199
+ G_x, G_y = calculate_centroid(x1, y1, x2, y2, x3, y3)
200
+ I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
201
+ Ux, Uy = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
202
+
203
+ # Calculate midpoints of the sides
204
+ midpoints = calculate_midpoints(x1, y1, x2, y2, x3, y3)
205
+
206
+ # Display results in columns
207
+ col1, col2 = st.columns(2)
208
+
209
+ with col1:
210
+ st.subheader("Coordinates of Triangle:")
211
+ st.markdown(f"Vertex A: **({x1:.3f}, {y1:.3f})**")
212
+ st.markdown(f"Vertex B: **({x2:.3f}, {y2:.3f})**")
213
+ st.markdown(f"Vertex C: **({x3:.3f}, {y3:.3f})**")
214
+
215
+ with col2:
216
+ st.subheader("Mid-Points of Triangle:")
217
+ st.markdown(f"Midpoint of AB: ({midpoints[0][0]:.3f}, {midpoints[0][1]:.3f})")
218
+ st.markdown(f"Midpoint of BC: ({midpoints[1][0]:.3f}, {midpoints[1][1]:.3f})")
219
+ st.markdown(f"Midpoint of CA: ({midpoints[2][0]:.3f}, {midpoints[2][1]:.3f})")
220
+
221
+
222
+ col1, col2 = st.columns(2)
223
+
224
+ with col1:
225
+ st.subheader("Angles of Triangle:")
226
+ st.markdown(f"Angle A: **{A:.3f}°**")
227
+ st.markdown(f"Angle B: **{B:.3f}°**")
228
+ st.markdown(f"Angle C: **{C:.3f}°**")
229
+
230
+ with col2:
231
+ st.subheader("Sides of Triangle:")
232
+ st.markdown(f"Side a: **{a:.3f}** units")
233
+ st.markdown(f"Side b: **{b:.3f}** units")
234
+ st.markdown(f"Side c: **{c:.3f}** units")
235
+
236
+
237
+ col1, col2, col3 = st.columns(3)
238
+
239
+ with col1:
240
+ st.subheader("Incenter of Triangle:")
241
+ st.markdown(f"Coordinates: **({format_zero(I_x):.3f}, {format_zero(I_y):.3f})**")
242
+ st.markdown(f"Radius: **{radius_in:.3f}** units")
243
+
244
+ with col2:
245
+ st.subheader("Circumcenter of Triangle:")
246
+ st.markdown(f"Coordinates: **({format_zero(Ux):.3f}, {format_zero(Uy):.3f})**")
247
+ st.markdown(f"Radius: **{radius_circum:.3f}** units")
248
+
249
+ with col3:
250
+ st.subheader("Other Properties:")
251
+ st.markdown(f"Area: **{area:.3f}** square units")
252
+ st.markdown(f"Perimeter: **{perimeter:.3f}** units")
253
+ st.markdown(f"Centroid: **({G_x:.3f}, {G_y:.3f})**")
254
+
255
+ # Display triangle graph with midpoints and colored points
256
+ plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, Ux, Uy, G_x, G_y, midpoints)
257
+
258
+ if __name__ == "__main__":
259
+ main()