Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from matplotlib.patches import Polygon
|
| 6 |
+
|
| 7 |
+
# Function to calculate the distance between two points
|
| 8 |
+
def calculate_distance(x1, y1, x2, y2):
|
| 9 |
+
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
|
| 10 |
+
|
| 11 |
+
# Function to calculate angles using Law of Cosines
|
| 12 |
+
def calculate_angle(a, b, c):
|
| 13 |
+
# a, b, c are sides of the triangle
|
| 14 |
+
angle = math.degrees(math.acos((b**2 + c**2 - a**2) / (2 * b * c)))
|
| 15 |
+
return angle
|
| 16 |
+
|
| 17 |
+
# Function to calculate area using Heron's formula
|
| 18 |
+
def calculate_area(a, b, c):
|
| 19 |
+
s = (a + b + c) / 2
|
| 20 |
+
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
|
| 21 |
+
return area
|
| 22 |
+
|
| 23 |
+
# Function to calculate the perimeter
|
| 24 |
+
def calculate_perimeter(a, b, c):
|
| 25 |
+
return a + b + c
|
| 26 |
+
|
| 27 |
+
# Function to plot the triangle
|
| 28 |
+
def plot_triangle(x1, y1, x2, y2, x3, y3):
|
| 29 |
+
# Create a plot
|
| 30 |
+
fig, ax = plt.subplots()
|
| 31 |
+
|
| 32 |
+
# Create the triangle with vertices (x1, y1), (x2, y2), and (x3, y3)
|
| 33 |
+
triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue')
|
| 34 |
+
ax.add_patch(triangle)
|
| 35 |
+
|
| 36 |
+
# Set the limits and labels
|
| 37 |
+
ax.set_xlim([min(x1, x2, x3) - 1, max(x1, x2, x3) + 1])
|
| 38 |
+
ax.set_ylim([min(y1, y2, y3) - 1, max(y1, y2, y3) + 1])
|
| 39 |
+
ax.set_aspect('equal', 'box')
|
| 40 |
+
ax.set_title('Triangle Visualization')
|
| 41 |
+
ax.set_xlabel('X')
|
| 42 |
+
ax.set_ylabel('Y')
|
| 43 |
+
|
| 44 |
+
# Show the plot
|
| 45 |
+
plt.grid(True)
|
| 46 |
+
plt.show()
|
| 47 |
+
|
| 48 |
+
# Main function to interact with the user
|
| 49 |
+
def main():
|
| 50 |
+
st.title("Triangle Solver from Coordinates")
|
| 51 |
+
|
| 52 |
+
st.sidebar.header("Enter the coordinates of the three points:")
|
| 53 |
+
|
| 54 |
+
# Coordinates input (X1, Y1), (X2, Y2), (X3, Y3)
|
| 55 |
+
x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 56 |
+
y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 57 |
+
x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 58 |
+
y2 = st.sidebar.number_input("Y2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 59 |
+
x3 = st.sidebar.number_input("X3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 60 |
+
y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
|
| 61 |
+
|
| 62 |
+
if st.sidebar.button("Calculate"):
|
| 63 |
+
# Calculate the lengths of the sides of the triangle using Euclidean distance
|
| 64 |
+
a = calculate_distance(x2, y2, x3, y3) # Distance between points (X2, Y2) and (X3, Y3)
|
| 65 |
+
b = calculate_distance(x1, y1, x3, y3) # Distance between points (X1, Y1) and (X3, Y3)
|
| 66 |
+
c = calculate_distance(x1, y1, x2, y2) # Distance between points (X1, Y1) and (X2, Y2)
|
| 67 |
+
|
| 68 |
+
# Calculate angles using the Law of Cosines
|
| 69 |
+
A = calculate_angle(b, c, a) # Angle at vertex (X1, Y1)
|
| 70 |
+
B = calculate_angle(a, c, b) # Angle at vertex (X2, Y2)
|
| 71 |
+
C = calculate_angle(a, b, c) # Angle at vertex (X3, Y3)
|
| 72 |
+
|
| 73 |
+
# Calculate area and perimeter
|
| 74 |
+
area = calculate_area(a, b, c)
|
| 75 |
+
perimeter = calculate_perimeter(a, b, c)
|
| 76 |
+
|
| 77 |
+
# Display results
|
| 78 |
+
st.subheader("Results:")
|
| 79 |
+
st.write(f"Angle A: {A:.3f}°")
|
| 80 |
+
st.write(f"Angle B: {B:.3f}°")
|
| 81 |
+
st.write(f"Angle C: {C:.3f}°")
|
| 82 |
+
st.write(f"Side a: {a:.3f} units")
|
| 83 |
+
st.write(f"Side b: {b:.3f} units")
|
| 84 |
+
st.write(f"Side c: {c:.3f} units")
|
| 85 |
+
st.write(f"Area: {area:.3f} square units")
|
| 86 |
+
st.write(f"Perimeter: {perimeter:.3f} units")
|
| 87 |
+
|
| 88 |
+
# Display triangle graph
|
| 89 |
+
plot_triangle(x1, y1, x2, y2, x3, y3)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|