RAG_Triangle / app.py
Shakeel1979's picture
Update app.py
3ef1ecb verified
Raw History Blame Contribute Delete
7.34 kB
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import pandas as pd
# Constants
PDF_ATTACHMENT = "PDF"
CLOUD_LINK_ATTACHMENT = "Cloud Link"
# Function to calculate the distance between two points
def calculate_distance(x1, y1, x2, y2):
return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# Function to calculate angles using the Law of Cosines
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
# Function to calculate area using Heron's formula
def calculate_area(a, b, c):
s = (a + b + c) / 2
area = np.sqrt(s * (s - a) * (s - b) * (s - c))
return area
# Function to calculate the perimeter
def calculate_perimeter(a, b, c):
return a + b + c
# Function to calculate the radius of the inscribed circle
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
# Function to calculate the radius of the circumscribed circle
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
# Function to calculate the centroid coordinates
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
# Function to calculate the incenter coordinates
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
# Function to calculate the circumcenter coordinates
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 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2)) / D
U_y = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1)) / D
except ZeroDivisionError:
U_x, U_y = 0, 0
return U_x, U_y
# Function to calculate midpoints of sides
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)
# Function to format values close to zero as 0
def format_zero(val):
if abs(val) < 1e-9:
return 0
return val
# Use the format_zero function to display results
st.title("Triangle Calculator")
# Input sidebar
st.sidebar.title("Input")
x1 = float(st.sidebar.text_input("x1", value=0))
y1 = float(st.sidebar.text_input("y1", value=0))
x2 = float(st.sidebar.text_input("x2", value=4))
y2 = float(st.sidebar.text_input("y2", value=0))
x3 = float(st.sidebar.text_input("x3", value=2))
y3 = float(st.sidebar.text_input("y3", value=3))
# Calculate distances
a = calculate_distance(x2, y2, x3, y3)
b = calculate_distance(x1, y1, x3, y3)
c = calculate_distance(x1, y1, x2, y2)
# Calculate angles
angle_A = calculate_angle(a, b, c)
angle_B = calculate_angle(b, a, c)
angle_C = calculate_angle(c, a, b)
# Calculate area and perimeter
area = calculate_area(a, b, c)
perimeter = calculate_perimeter(a, b, c)
# Calculate radii
radius_inscribed_circle = calculate_radius_inscribed_circle(a, b, c)
radius_circumscribed_circle = calculate_radius_circumscribed_circle(a, b, c)
# Calculate centroid and incenter
centroid_x, centroid_y = calculate_centroid(x1, y1, x2, y2, x3, y3)
incenter_x, incenter_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
# Calculate circumcenter
circumcenter_x, circumcenter_y = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
# Calculate midpoints
midpoint1_x, midpoint1_y = calculate_midpoints(x1, y1, x2, y2, x3, y3)[0]
midpoint2_x, midpoint2_y = calculate_midpoints(x1, y1, x2, y2, x3, y3)[1]
midpoint3_x, midpoint3_y = calculate_midpoints(x1, y1, x2, y2, x3, y3)[2]
# Display results
st.title("Results")
st.write(f"Distance a: {format_zero(a)}")
st.write(f"Distance b: {format_zero(b)}")
st.write(f"Distance c: {format_zero(c)}")
st.write(f"Angle A: {format_zero(angle_A)}°")
st.write(f"Angle B: {format_zero(angle_B)}°")
st.write(f"Angle C: {format_zero(angle_C)}°")
st.write(f"Area: {format_zero(area)}")
st.write(f"Perimeter: {format_zero(perimeter)}")
st.write(f"Radius of inscribed circle: {format_zero(radius_inscribed_circle)}")
st.write(f"Radius of circumscribed circle: {format_zero(radius_circumscribed_circle)}")
st.write(f"Centroid: ({format_zero(centroid_x)}, {format_zero(centroid_y)})")
st.write(f"Incenter: ({format_zero(incenter_x)}, {format_zero(incenter_y)})")
st.write(f"Circumcenter: ({format_zero(circumcenter_x)}, {format_zero(circumcenter_y)})")
st.write(f"Midpoint 1: ({format_zero(midpoint1_x)}, {format_zero(midpoint1_y)})")
st.write(f"Midpoint 2: ({format_zero(midpoint2_x)}, {format_zero(midpoint2_y)})")
st.write(f"Midpoint 3: ({format_zero(midpoint3_x)}, {format_zero(midpoint3_y)})")
# Plot triangle
fig, ax = plt.subplots()
triangle = Polygon([[x1, y1], [x2, y2], [x3, y3]], fill=False)
ax.add_patch(triangle)
ax.set_xlim(min(x1, x2, x3) - 1, max(x1, x2, x3) + 1)
ax.set_ylim(min(y1, y2, y3) - 1, max(y1, y2, y3) + 1)
ax.set_aspect('equal')
st.pyplot(fig)
# Attachment options
attachment = st.selectbox("Attachment", [PDF_ATTACHMENT, CLOUD_LINK_ATTACHMENT])
if attachment == PDF_ATTACHMENT:
# Code to generate PDF attachment
pass
elif attachment == CLOUD_LINK_ATTACHMENT:
# Code to generate cloud link attachment
pass
# Add a button to download the results as a CSV file
@st.cache_data
def convert_df(results_df):
return results_df.to_csv(index=False)
# Fixing the issue by making sure all rows have the same length
results_data = {
'Parameter': [
'Distance a', 'Distance b', 'Distance c',
'Angle A', 'Angle B', 'Angle C',
'Area', 'Perimeter',
'Radius of Inscribed Circle', 'Radius of Circumscribed Circle',
'Centroid', 'Incenter', 'Circumcenter',
'Midpoint 1', 'Midpoint 2', 'Midpoint 3'
],
'Value': [
format_zero(a), format_zero(b), format_zero(c),
format_zero(angle_A), format_zero(angle_B), format_zero(angle_C),
format_zero(area), format_zero(perimeter),
format_zero(radius_inscribed_circle), format_zero(radius_circumscribed_circle),
f"({format_zero(centroid_x)}, {format_zero(centroid_y)})",
f"({format_zero(incenter_x)}, {format_zero(incenter_y)})",
f"({format_zero(circumcenter_x)}, {format_zero(circumcenter_y)})",
f"({format_zero(midpoint1_x)}, {format_zero(midpoint1_y)})",
f"({format_zero(midpoint2_x)}, {format_zero(midpoint2_y)})",
f"({format_zero(midpoint3_x)}, {format_zero(midpoint3_y)})"
]
}
results_df = pd.DataFrame(results_data)
csv = convert_df(results_df)
st.download_button(
label="Download results as CSV",
data=csv,
file_name='triangle_results.csv',
mime='text/csv',
)