|
import streamlit as st |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from matplotlib.patches import Polygon, Circle |
|
|
|
|
|
def calculate_distance(x1, y1, x2, y2): |
|
return np.sqrt((x2 - x1) * 2 + (y2 - y1) * 2) |
|
|
|
|
|
def calculate_angle(a, b, c): |
|
try: |
|
angle = np.degrees(np.acos((b * 2 + c * 2 - a ** 2) / (2 * b * c))) |
|
except ValueError: |
|
angle = 0 |
|
return angle |
|
|
|
|
|
def calculate_area(a, b, c): |
|
s = (a + b + c) / 2 |
|
area = np.sqrt(s * (s - a) * (s - b) * (s - c)) |
|
return area |
|
|
|
|
|
def calculate_perimeter(a, b, c): |
|
return a + b + c |
|
|
|
|
|
def calculate_radius_inscribed_circle(a, b, c): |
|
try: |
|
s = (a + b + c) / 2 |
|
area = calculate_area(a, b, c) |
|
radius = area / s |
|
except ZeroDivisionError: |
|
radius = 0 |
|
return radius |
|
|
|
|
|
def calculate_radius_circumscribed_circle(a, b, c): |
|
try: |
|
area = calculate_area(a, b, c) |
|
radius = (a * b * c) / (4 * area) |
|
except ZeroDivisionError: |
|
radius = 0 |
|
return radius |
|
|
|
|
|
def calculate_centroid(x1, y1, x2, y2, x3, y3): |
|
G_x = (x1 + x2 + x3) / 3 |
|
G_y = (y1 + y2 + y3) / 3 |
|
return G_x, G_y |
|
|
|
|
|
def calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c): |
|
try: |
|
I_x = (a * x1 + b * x2 + c * x3) / (a + b + c) |
|
I_y = (a * y1 + b * y2 + c * y3) / (a + b + c) |
|
except ZeroDivisionError: |
|
I_x, I_y = 0, 0 |
|
return I_x, I_y |
|
|
|
|
|
def calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c): |
|
try: |
|
D = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) |
|
U_x = ((x1*2 + y12) * (y2 - y3) + (x22 + y22) * (y3 - y1) + (x32 + y3*2) * (y1 - y2)) / D |
|
U_y = ((x1*2 + y12) * (x3 - x2) + (x22 + y22) * (x1 - x3) + (x32 + y3*2) * (x2 - x1)) / D |
|
except ZeroDivisionError: |
|
U_x, U_y = 0, 0 |
|
return U_x, U_y |
|
|
|
|
|
def calculate_midpoints(x1, y1, x2, y2, x3, y3): |
|
|
|
M1_x = (x1 + x2) / 2 |
|
M1_y = (y1 + y2) / 2 |
|
|
|
M2_x = (x2 + x3) / 2 |
|
M2_y = (y2 + y3) / 2 |
|
|
|
M3_x = (x3 + x1) / 2 |
|
M3_y = (y3 + y1) / 2 |
|
return (M1_x, M1_y), (M2_x, M2_y), (M3_x, M3_y) |
|
|
|
|
|
def format_zero(val): |
|
if abs(val) < 1e-6: |
|
return 0.0 |
|
return val |
|
|
|
|
|
def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c): |
|
fig, ax = plt.subplots(figsize=(8, 6)) |
|
triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue') |
|
ax.add_patch(triangle) |
|
|
|
|
|
vertex_color = 'blue' |
|
midpoint_color = 'green' |
|
centroid_color = 'orange' |
|
incenter_color = 'red' |
|
circumcenter_color = 'purple' |
|
|
|
|
|
vertices = [(x1, y1), (x2, y2), (x3, y3)] |
|
vertex_labels = [f"Vertex A ({x1:.3f}, {y1:.3f})", f"Vertex B ({x2:.3f}, {y2:.3f})", f"Vertex C ({x3:.3f}, {y3:.3f})"] |
|
for i, (vx, vy) in enumerate(vertices): |
|
ax.scatter(vx, vy, color=vertex_color, zorder=3) |
|
|
|
|
|
key_points = [ |
|
(I_x, I_y, incenter_color), |
|
(U_x, U_y, circumcenter_color), |
|
(G_x, G_y, centroid_color) |
|
] |
|
key_points_labels = [f"Incenter ({I_x:.3f}, {I_y:.3f})", f"Circumcenter ({U_x:.3f}, {U_y:.3f})", f"Centroid ({G_x:.3f}, {G_y:.3f})"] |
|
|
|
for x, y, color in key_points: |
|
ax.scatter(x, y, color=color, zorder=4) |
|
|
|
|
|
for i, (mx, my) in enumerate(midpoints): |
|
ax.scatter(mx, my, color=midpoint_color, zorder=5) |
|
|
|
|
|
radius_in = calculate_radius_inscribed_circle(a, b, c) |
|
incircle = Circle((I_x, I_y), radius_in, color=incenter_color, fill=False, linestyle='--', linewidth=2, label="Inscribed Circle") |
|
ax.add_patch(incircle) |
|
|
|
|
|
radius_circum = calculate_radius_circumscribed_circle(a, b, c) |
|
circumcircle = Circle((U_x, U_y), radius_circum, color=circumcenter_color, fill=False, linestyle='--', linewidth=2, label="Circumscribed Circle") |
|
ax.add_patch(circumcircle) |
|
|
|
|
|
handles = [ |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[0]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[1]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[2]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[0]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[1]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints[2]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=incenter_color, markersize=8, label=key_points_labels[0]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=circumcenter_color, markersize=8, label=key_points_labels[1]), |
|
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=centroid_color, markersize=8, label=key_points_labels[2]) |
|
] |
|
ax.legend(handles=handles, loc='upper left', fontsize=12) |
|
|
|
|
|
padding = 3 |
|
ax.set_xlim([min(x1, x2, x3) - padding, max(x1, x2, x3) + padding]) |
|
ax.set_ylim([min(y1, y2, y3) - padding, max(y1, y2, y3) + padding]) |
|
ax.set_aspect('equal', adjustable='datalim') |
|
|
|
ax.set_title('Solved Triangle', fontsize=18) |
|
ax.set_xlabel('X-axis', fontsize=12) |
|
ax.set_ylabel('Y-axis', fontsize=12) |
|
|
|
plt.grid(True) |
|
st.pyplot(fig) |
|
|
|
|
|
def is_valid_triangle(a, b, c): |
|
|
|
return a + b > c and b + c > a and c + a > b |
|
|
|
|
|
def main(): |
|
st.title("Advanced Triangle Solver") |
|
|
|
st.sidebar.header("Enter the coordinates of the three points:") |
|
|
|
|
|
x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
y2 = st.sidebar.number_input("Y2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
x3 = st.sidebar.number_input("X3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f") |
|
|
|
if st.sidebar.button("Calculate"): |
|
|
|
a = calculate_distance(x2, y2, x3, y3) |
|
b = calculate_distance(x1, y1, x3, y3) |
|
c = calculate_distance(x1, y1, x2, y2) |
|
|
|
|
|
if not is_valid_triangle(a, b, c): |
|
st.error("The entered points do not form a valid triangle.") |
|
return |
|
|
|
|
|
A = calculate_angle(a, b, c) |
|
B = calculate_angle(b, a, c) |
|
C = calculate_angle(c, a, b) |
|
|
|
|
|
if abs(A + B + C - 180) > 1e-2: |
|
st.error("The sum of the angles is not 180 degrees.") |
|
return |
|
|
|
|
|
area = calculate_area(a, b, c) |
|
perimeter = calculate_perimeter(a, b, c) |
|
radius_in = calculate_radius_inscribed_circle(a, b, c) |
|
radius_circum = calculate_radius_circumscribed_circle(a, b, c) |
|
|
|
|
|
G_x, G_y = calculate_centroid(x1, y1, x2, y2, x3, y3) |
|
I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c) |
|
U_x, U_y = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c) |
|
|
|
|
|
midpoints = calculate_midpoints(x1, y1, x2, y2, x3, y3) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.subheader("Coordinates of Triangle:") |
|
st.markdown(f"Vertex A: *({x1:.3f}, {y1:.3f})*") |
|
st.markdown(f"Vertex B: *({x2:.3f}, {y2:.3f})*") |
|
st.markdown(f"Vertex C: *({x3:.3f}, {y3:.3f})*") |
|
|
|
with col2: |
|
st.subheader("Mid-Points of Triangle:") |
|
st.markdown(f"Midpoint of AB: ({midpoints[0][0]:.3f}, {midpoints[0][1]:.3f})") |
|
st.markdown(f"Midpoint of BC: ({midpoints[1][0]:.3f}, {midpoints[1][1]:.3f})") |
|
st.markdown(f"Midpoint of CA: ({midpoints[2][0]:.3f}, {midpoints[2][1]:.3f})") |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.subheader("Angles of Triangle:") |
|
st.markdown(f"Angle A: *{format_zero(A):.3f}°*") |
|
st.markdown(f"Angle B: *{format_zero(B):.3f}°*") |
|
st.markdown(f"Angle C: *{format_zero(C):.3f}°*") |
|
|
|
with col2: |
|
st.subheader("Sides of Triangle:") |
|
st.markdown(f"Side a: *{format_zero(a):.3f}* units") |
|
st.markdown(f"Side b: *{format_zero(b):.3f}* units") |
|
st.markdown(f"Side c: *{format_zero(c):.3f}* units") |
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
st.subheader("Incenter of Triangle:") |
|
st.markdown(f"Coordinates: *({format_zero(I_x):.3f}, {format_zero(I_y):.3f})*") |
|
st.markdown(f"Radius: *{radius_in:.3f}* units") |
|
|
|
with col2: |
|
st.subheader("Circumcenter of Triangle:") |
|
st.markdown(f"Coordinates: *({format_zero(U_x):.3f}, {format_zero(U_y):.3f})*") |
|
st.markdown(f"Radius: *{radius_circum:.3f}* units") |
|
|
|
with col3: |
|
st.subheader("Other Properties:") |
|
st.markdown(f"Area: *{format_zero(area):.3f}* square units") |
|
st.markdown(f"Perimeter: *{format_zero(perimeter):.3f}* units") |
|
st.markdown(f"Centroid: *({format_zero(G_x):.3f}, {format_zero(G_y):.3f})*") |
|
|
|
|
|
plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c) |
|
|
|
if _name_ == "_main_": |
|
main() |