File size: 7,338 Bytes
23dfc09
 
 
bf433b7
 
23dfc09
1b9f23d
23dfc09
 
 
bf433b7
23dfc09
 
 
bf433b7
23dfc09
 
1b9f23d
23dfc09
 
 
 
bf433b7
23dfc09
 
 
 
 
bf433b7
23dfc09
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
bf433b7
23dfc09
 
 
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
bf433b7
 
 
 
 
 
 
1b9f23d
23dfc09
 
bf433b7
23dfc09
bf433b7
 
23dfc09
bf433b7
 
23dfc09
bf433b7
 
23dfc09
bf433b7
23dfc09
 
 
 
bf433b7
23dfc09
 
 
 
bf433b7
23dfc09
 
 
bf433b7
23dfc09
 
 
bf433b7
23dfc09
 
 
bf433b7
23dfc09
 
bf433b7
23dfc09
 
 
 
bf433b7
23dfc09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf433b7
 
 
 
 
 
 
 
 
1b9f23d
 
 
 
 
 
 
 
 
 
 
23dfc09
 
 
3ef1ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23dfc09
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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',
)