Trig
Trig
import math
import cmath # For complex number operations
app = Flask(__name__)
# Helper functions
def validate_triangle_angles(angle_a, angle_b, angle_c):
if angle_a and angle_b and angle_c:
return abs(angle_a + angle_b + angle_c - 180) < 0.001
return True
return jsonify({
'a': round(side_a, 4) if side_a else None,
'b': round(side_b, 4) if side_b else None,
'c': round(side_c, 4) if side_c else None,
'A': round(angle_a, 4) if angle_a else None,
'B': round(angle_b, 4) if angle_b else None
})
return render_template('right_triangle.html')
sin_val = math.sin(radians)
cos_val = math.cos(radians)
tan_val = math.tan(radians)
return jsonify({
'sin': round(sin_val, 4),
'cos': round(cos_val, 4),
'tan': round(tan_val, 4) if abs(tan_val) < 1e10 else "undefined"
})
return render_template('basic_trig_ratios.html')
sin_val = math.sin(radians)
cos_val = math.cos(radians)
tan_val = math.tan(radians)
return jsonify({
'cosec': round(cosec_val, 4) if isinstance(cosec_val, float) else
cosec_val,
'sec': round(sec_val, 4) if isinstance(sec_val, float) else sec_val,
'cot': round(cot_val, 4) if isinstance(cot_val, float) else cot_val
})
return render_template('reciprocal_trig_ratios.html')
radians = math.radians(angle)
sin_val = math.sin(radians)
cos_val = math.cos(radians)
tan_val = math.tan(radians)
return jsonify({
'angle': angle,
'sin': round(sin_val, 4),
'cos': round(cos_val, 4),
'tan': round(tan_val, 4) if abs(tan_val) < 1e10 else "undefined",
'exact': get_exact_values(angle)
})
return render_template('special_angles.html')
def get_exact_values(angle):
exact = {
'sin': {0: '0', 30: '1/2', 45: '√2/2', 60: '√3/2', 90: '1'},
'cos': {0: '1', 30: '√3/2', 45: '√2/2', 60: '1/2', 90: '0'},
'tan': {0: '0', 30: '√3/3', 45: '1', 60: '√3', 90: '∞'}
}
return {
'sin': exact['sin'][angle],
'cos': exact['cos'][angle],
'tan': exact['tan'][angle]
}
# 5. Trigonometric Identities
@app.route('/trig_identities', methods=['GET', 'POST'])
def trig_identities():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])
radians = math.radians(angle)
# Pythagorean identities
sin_val = math.sin(radians)
cos_val = math.cos(radians)
pythag1 = round(sin_val**2 + cos_val**2, 4)
pythag2 = round(1 + (sin_val/cos_val)**2, 4) if cos_val != 0 else
"undefined"
pythag3 = round(1 + (cos_val/sin_val)**2, 4) if sin_val != 0 else
"undefined"
return jsonify({
'sin²θ + cos²θ': pythag1,
'1 + tan²θ': pythag2,
'1 + cot²θ': pythag3
})
return render_template('trig_identities.html')
comp_angle = 90 - angle
sin_val = math.sin(math.radians(angle))
cos_comp = math.cos(math.radians(comp_angle))
return jsonify({
'angle': angle,
'complement': comp_angle,
'sin(θ)': round(sin_val, 4),
'cos(90°-θ)': round(cos_comp, 4),
'equal': round(sin_val, 4) == round(cos_comp, 4)
})
return render_template('complementary_angles.html')
# 7. Unit Circle
@app.route('/unit_circle', methods=['GET', 'POST'])
def unit_circle():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])
radians = math.radians(angle)
x = math.cos(radians)
y = math.sin(radians)
return jsonify({
'angle': angle,
'x': round(x, 4),
'y': round(y, 4),
'sin': round(y, 4),
'cos': round(x, 4),
'tan': round(y/x, 4) if x != 0 else "undefined"
})
return render_template('unit_circle.html')
if function == 'sin':
y = amplitude * math.sin(math.radians(scaled_x - phase_shift)) +
vertical_shift
elif function == 'cos':
y = amplitude * math.cos(math.radians(scaled_x - phase_shift)) +
vertical_shift
elif function == 'tan':
try:
y = amplitude * math.tan(math.radians(scaled_x - phase_shift))
+ vertical_shift
if abs(y) > 10: # Limit extreme values for better
visualization
y = None
except:
y = None
return jsonify({
'amplitude': a,
'period': round(period, 2),
'phase_shift': round(phase_shift, 2),
'vertical_shift': d,
'equation': f"y = {a} * trig({b}(x {'+' if c >=0 else ''}{c})) {'+' if
d >=0 else ''}{d}"
})
return render_template('amplitude_period_phase.html')
solutions = []
if equation_type == 'sin':
principal = math.degrees(math.asin(value))
solutions = [principal, 180 - principal]
elif equation_type == 'cos':
principal = math.degrees(math.acos(value))
solutions = [principal, 360 - principal]
elif equation_type == 'tan':
principal = math.degrees(math.atan(value))
solutions = [principal]
return jsonify({
'principal_solutions': [round(s, 2) for s in solutions],
'general_solutions': general
})
return render_template('solve_trig_equations.html')
if function == 'asin':
if -1 <= value <= 1:
angle = math.degrees(math.asin(value))
else:
angle = "undefined"
elif function == 'acos':
if -1 <= value <= 1:
angle = math.degrees(math.acos(value))
else:
angle = "undefined"
elif function == 'atan':
angle = math.degrees(math.atan(value))
return jsonify({
'function': function,
'value': value,
'angle': round(angle, 4) if isinstance(angle, float) else angle,
'range': get_inverse_range(function)
})
return render_template('inverse_trig.html')
def get_inverse_range(function):
ranges = {
'asin': "-90° to 90°",
'acos': "0° to 180°",
'atan': "-90° to 90°"
}
return ranges.get(function, "")
return jsonify({
'a': round(side_a, 4) if side_a else None,
'b': round(side_b, 4) if side_b else None,
'c': round(side_c, 4) if side_c else None,
'A': round(angle_a, 4) if angle_a else None,
'B': round(angle_b, 4) if angle_b else None,
'C': round(angle_c, 4) if angle_c else None
})
return render_template('law_of_sines.html')
return jsonify({
'a': round(side_a, 4) if side_a else None,
'b': round(side_b, 4) if side_b else None,
'C': round(angle_c, 4) if angle_c else None,
'area': round(area, 4) if area else None
})
return render_template('area_triangle.html')
z = complex(real, imag)
r = abs(z)
theta = math.degrees(cmath.phase(z))
return jsonify({
'rectangular': f"{real} + {imag}i",
'polar': f"{round(r, 4)} (cos {round(theta, 4)}° + i sin {round(theta,
4)}°)",
'magnitude': round(r, 4),
'angle': round(theta, 4)
})
return render_template('complex_numbers.html')
# if operation == 'sin_sum':
# result = math.sin(a1) * math.cos(a2) + math.cos(a1) * math.sin(a2)
# actual = math.sin(a1 + a2)
# elif operation == 'sin_diff':
# result = math.sin(a1) * math.cos(a2) - math.cos(a1) * math.sin(a2)
# actual = math.sin(a1 - a2)
# elif operation == 'cos_sum':
# result = math.cos(a1) * math.cos(a2) - math.sin(a1) * math.sin(a2)
# actual = math.cos(a1 + a2)
# elif operation == 'cos_diff':
# result = math.cos(a1) * math.cos(a2) + math.sin(a1) * math.sin(a2)
# actual = math.cos(a1 - a2)
# return jsonify({
# 'formula_result': round(result, 4),
# 'actual_value': round(actual, 4),
# 'match': abs(result - actual) < 0.0001
# })
# return render_template('sum_difference.html')
# a = math.radians(angle)
# if function == 'sin':
# formula = "2 sinθ cosθ"
# result = 2 * math.sin(a) * math.cos(a)
# actual = math.sin(2 * a)
# elif function == 'cos':
# formula = "cos²θ - sin²θ"
# result = math.cos(a)**2 - math.sin(a)**2
# actual = math.cos(2 * a)
# elif function == 'tan':
# formula = "2 tanθ / (1 - tan²θ)"
# tan_val = math.tan(a)
# result = (2 * tan_val) / (1 - tan_val**2) if tan_val**2 != 1 else
"undefined"
# actual = math.tan(2 * a) if tan_val**2 != 1 else "undefined"
# return jsonify({
# 'formula': formula,
# 'formula_result': round(result, 4) if isinstance(result, float) else
result,
# 'actual_value': round(actual, 4) if isinstance(actual, float) else
actual,
# 'match': abs(result - actual) < 0.0001 if isinstance(result, float)
and isinstance(actual, float) else result == actual
# })
# return render_template('double_angle.html')
# # 18. Half Angle Formulas
# @app.route('/half_angle', methods=['GET', 'POST'])
# def half_angle():
# if request.method == 'POST':
# data = request.json
# angle = float(data['angle'])
# function = data['function']
# half_a = math.radians(angle) / 2
# if function == 'sin':
# formula = "±√[(1 - cosθ)/2]"
# result = math.sqrt((1 - math.cos(2 * half_a)) / 2)
# actual = math.sin(half_a)
# elif function == 'cos':
# formula = "±√[(1 + cosθ)/2]"
# result = math.sqrt((1 + math.cos(2 * half_a)) / 2)
# actual = math.cos(half_a)
# elif function == 'tan':
# formula = "±√[(1 - cosθ)/(1 + cosθ)]"
# numerator = 1 - math.cos(2 * half_a)
# denominator = 1 + math.cos(2 * half_a)
# if denominator != 0:
# result = math.sqrt(numerator / denominator)
# actual = math.tan(half_a)
# else:
# result = "undefined"
# actual = "undefined"
# return jsonify({
# 'formula': formula,
# 'formula_result': round(result, 4) if isinstance(result, float) else
result,
# 'actual_value': round(actual, 4) if isinstance(actual, float) else
actual,
# 'match': abs(result - actual) < 0.0001 if isinstance(result, float)
and isinstance(actual, float) else result == actual
# })
# return render_template('half_angle.html')
# a = math.radians(angle1)
# b = math.radians(angle2)
# if formula_type == 'product_to_sum':
# sin_sin = 0.5 * (math.cos(a - b) - math.cos(a + b))
# cos_cos = 0.5 * (math.cos(a + b) + math.cos(a - b))
# sin_cos = 0.5 * (math.sin(a + b) + math.sin(a - b))
# cos_sin = 0.5 * (math.sin(a + b) - math.sin(a - b))
# return jsonify({
# 'sinA sinB': round(sin_sin, 4),
# 'cosA cosB': round(cos_cos, 4),
# 'sinA cosB': round(sin_cos, 4),
# 'cosA sinB': round(cos_sin, 4)
# })
# else: # sum_to_product
# sin_add = 2 * math.sin((a + b)/2) * math.cos((a - b)/2)
# sin_sub = 2 * math.cos((a + b)/2) * math.sin((a - b)/2)
# cos_add = 2 * math.cos((a + b)/2) * math.cos((a - b)/2)
# cos_sub = -2 * math.sin((a + b)/2) * math.sin((a - b)/2)
# return jsonify({
# 'sinA + sinB': round(sin_add, 4),
# 'sinA - sinB': round(sin_sub, 4),
# 'cosA + cosB': round(cos_add, 4),
# 'cosA - cosB': round(cos_sub, 4)
# })
# return render_template('product_sum_formulas.html')
# radians = math.radians(normalized)
# sin_val = math.sin(radians)
# cos_val = math.cos(radians)
# tan_val = math.tan(radians)
# return jsonify({
# 'original_angle': angle,
# 'normalized_angle': normalized,
# 'sin': round(sin_val, 4),
# 'cos': round(cos_val, 4),
# 'tan': round(tan_val, 4) if abs(tan_val) < 1e10 else "undefined",
# 'quadrant': get_quadrant(normalized)
# })
# return render_template('circular_functions.html')
# def get_quadrant(angle):
# if 0 <= angle < 90:
# return "I"
# elif 90 <= angle < 180:
# return "II"
# elif 180 <= angle < 270:
# return "III"
# else:
# return "IV"
# if problem_type == 'height_distance':
# angle = float(data['angle'])
# distance = float(data['distance'])
# if function == 'sin':
# y = amplitude * math.sin(radians) + vertical_shift
# elif function == 'cos':
# y = amplitude * math.cos(radians) + vertical_shift
# elif function == 'tan':
# try:
# y = amplitude * math.tan(radians) + vertical_shift
# if abs(y) > 10: # Limit extreme values
# y = None
# except:
# y = None
# return jsonify({
# 'distance': round(distance, 2),
# 'bearing': round(bearing, 2),
# 'x_components': [round(x1, 2), round(x2, 2)],
# 'y_components': [round(y1, 2), round(y2, 2)]
# })
# return render_template('navigation.html')
# return jsonify({
# 'a': round(side_a, 4) if side_a else None,
# 'b': round(side_b, 4) if side_b else None,
# 'c': round(side_c, 4) if side_c else None,
# 'A': round(angle_a, 4) if angle_a else None,
# 'B': round(angle_b, 4) if angle_b else None,
# 'C': round(angle_c, 4) if angle_c else None,
# 'area': round(area, 4) if area else None
# })
# return render_template('herons_formula.html')
# return jsonify({
# 'angle': angle,
# 'function': function,
# 'result': round(result, 4) if result is not None else None
# })
# return render_template('multiple_angle_identities.html')
# # 26. Trig in Coordinate Geometry
# @app.route('/coordinate_geometry', methods=['GET', 'POST'])
# def coordinate_geometry():
# if request.method == 'POST':
# data = request.json
# x = float(data.get('x', 0)) if data.get('x') not in [None, ''] else 0
# y = float(data.get('y', 0)) if data.get('y') not in [None, ''] else 0
# if x != 0 or y != 0:
# r = math.sqrt(x**2 + y**2)
# sin_theta = y / r
# cos_theta = x / r
# tan_theta = y / x if x != 0 else float('inf')
# else:
# r = sin_theta = cos_theta = tan_theta = None
# return jsonify({
# 'x': x,
# 'y': y,
# 'r': round(r, 4) if r is not None else None,
# 'sinθ': round(sin_theta, 4) if sin_theta is not None else None,
# 'cosθ': round(cos_theta, 4) if cos_theta is not None else None,
# 'tanθ': round(tan_theta, 4) if tan_theta is not None else None
# })
# return render_template('coordinate_geometry.html')
# return jsonify({
# 'identity': identity,
# 'verified': verified
# })
# return render_template('verify_identities.html')
# x = r * math.cos(omega * t)
# y = r * math.sin(omega * t)
# return jsonify({
# 'x': round(x, 4),
# 'y': round(y, 4),
# 't': t,
# 'r': r,
# 'omega': omega
# })
# return render_template('parametric_equations.html')
# return jsonify({
# 'amplitude': amplitude,
# 'period': period,
# 'phase_shift': phase_shift,
# 'vertical_shift': vertical_shift,
# 'x': x,
# 'y': round(y, 4)
# })
# return render_template('periodic_phenomena.html')
# return jsonify({
# 'v1': {'x': x1, 'y': y1},
# 'v2': {'x': x2, 'y': y2},
# 'dot_product': round(dot_product, 4),
# 'angle': round(angle, 4) if angle is not None else None
# })
# return render_template('vector_angle.html')
if __name__ == '__main__':
app.run(debug=True)