0% found this document useful (0 votes)
817 views17 pages

Trig

The document is a Flask web application that provides various functionalities related to trigonometry, including solving right triangles, calculating trigonometric ratios, and working with the unit circle. It features multiple routes for different trigonometric operations, such as basic ratios, identities, and solving equations, with support for both GET and POST requests. The application returns results in JSON format and includes validation for triangle angles and specific angle conditions.

Uploaded by

srishanth232007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
817 views17 pages

Trig

The document is a Flask web application that provides various functionalities related to trigonometry, including solving right triangles, calculating trigonometric ratios, and working with the unit circle. It features multiple routes for different trigonometric operations, such as basic ratios, identities, and solving equations, with support for both GET and POST requests. The application returns results in JSON format and includes validation for triangle angles and specific angle conditions.

Uploaded by

srishanth232007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

from flask import Flask, render_template, request, jsonify

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

# Home page with all methods overview


@app.route('/')
def home():
return render_template('home.html')

# 1. Solving Right Triangles


@app.route('/right_triangle', methods=['GET', 'POST'])
def right_triangle():
if request.method == 'POST':
data = request.json
side_a = float(data.get('a', 0)) if data.get('a') not in [None, ''] else 0
side_b = float(data.get('b', 0)) if data.get('b') not in [None, ''] else 0
side_c = float(data.get('c', 0)) if data.get('c') not in [None, ''] else 0
angle_a = float(data.get('A', 0)) if data.get('A') not in [None, ''] else 0
angle_b = float(data.get('B', 0)) if data.get('B') not in [None, ''] else 0

# Calculate missing sides/angles


if side_a and side_b:
side_c = math.sqrt(side_a**2 + side_b**2)
angle_a = math.degrees(math.atan(side_a/side_b))
angle_b = 90 - angle_a
elif side_a and side_c:
side_b = math.sqrt(side_c**2 - side_a**2)
angle_a = math.degrees(math.asin(side_a/side_c))
angle_b = 90 - angle_a
elif side_b and side_c:
side_a = math.sqrt(side_c**2 - side_b**2)
angle_b = math.degrees(math.asin(side_b/side_c))
angle_a = 90 - angle_b
elif side_a and angle_a:
side_b = side_a / math.tan(math.radians(angle_a))
side_c = side_a / math.sin(math.radians(angle_a))
angle_b = 90 - angle_a
elif side_b and angle_a:
side_a = side_b * math.tan(math.radians(angle_a))
side_c = side_b / math.cos(math.radians(angle_a))
angle_b = 90 - angle_a
elif side_c and angle_a:
side_a = side_c * math.sin(math.radians(angle_a))
side_b = side_c * math.cos(math.radians(angle_a))
angle_b = 90 - angle_a

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')

# 2. Basic Trigonometric Ratios


@app.route('/basic_trig_ratios', methods=['GET', 'POST'])
def basic_trig_ratios():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])
radians = math.radians(angle)

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')

# 3. Reciprocal Trigonometric Ratios


@app.route('/reciprocal_trig_ratios', methods=['GET', 'POST'])
def reciprocal_trig_ratios():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])
radians = math.radians(angle)

sin_val = math.sin(radians)
cos_val = math.cos(radians)
tan_val = math.tan(radians)

cosec_val = 1/sin_val if sin_val != 0 else "undefined"


sec_val = 1/cos_val if cos_val != 0 else "undefined"
cot_val = 1/tan_val if tan_val != 0 else "undefined"

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')

# 4. Trigonometric Ratios of Special Angles


@app.route('/special_angles', methods=['GET', 'POST'])
def special_angles():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])

if angle not in [0, 30, 45, 60, 90]:


return jsonify({'error': 'Angle must be 0°, 30°, 45°, 60°, or 90°'})

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')

# 6. Complementary Angle Identities


@app.route('/complementary_angles', methods=['GET', 'POST'])
def complementary_angles():
if request.method == 'POST':
data = request.json
angle = float(data['angle'])

if angle > 90:


return jsonify({'error': 'Angle must be ≤ 90°'})

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')

# 8. Graphs of Trigonometric Functions


@app.route('/trig_graphs', methods=['GET', 'POST'])
def trig_graphs():
if request.method == 'POST':
data = request.json
function = data['function']
amplitude = float(data.get('amplitude', 1))
period = float(data.get('period', 360))
phase_shift = float(data.get('phase_shift', 0))
vertical_shift = float(data.get('vertical_shift', 0))

# Generate points for the graph


points = []
for x in range(0, 721, 15): # 0° to 720° in 15° increments
radians = math.radians(x - phase_shift)
scaled_x = x * (360 / period)

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

points.append({'x': x, 'y': round(y, 4) if y is not None else None})

return jsonify({'points': points})


return render_template('trig_graphs.html')

# 9. Amplitude, Period, and Phase Shift


@app.route('/amplitude_period_phase', methods=['GET', 'POST'])
def amplitude_period_phase():
if request.method == 'POST':
data = request.json
a = float(data['amplitude'])
b = float(data['period_coeff'])
c = float(data['phase_shift'])
d = float(data['vertical_shift'])

period = 360 / abs(b)


phase_shift = -c / b

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')

# 10. Solving Trigonometric Equations


@app.route('/solve_trig_equations', methods=['GET', 'POST'])
def solve_trig_equations():
if request.method == 'POST':
data = request.json
equation_type = data['type']
value = float(data['value'])

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]

# Generate general solutions


general = []
for sol in solutions:
if equation_type == 'sin':
general.append(f"{round(sol, 2)}° + 360°n")
general.append(f"{round(180 - sol, 2)}° + 360°n")
elif equation_type == 'cos':
general.append(f"{round(sol, 2)}° + 360°n")
general.append(f"{round(-sol, 2)}° + 360°n")
elif equation_type == 'tan':
general.append(f"{round(sol, 2)}° + 180°n")

return jsonify({
'principal_solutions': [round(s, 2) for s in solutions],
'general_solutions': general
})
return render_template('solve_trig_equations.html')

# 11. Inverse Trigonometric Functions


@app.route('/inverse_trig', methods=['GET', 'POST'])
def inverse_trig():
if request.method == 'POST':
data = request.json
function = data['function']
value = float(data['value'])

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, "")

# 12. Law of Sines


@app.route('/law_of_sines', methods=['GET', 'POST'])
def law_of_sines():
if request.method == 'POST':
data = request.json
side_a = float(data.get('a', 0)) if data.get('a') not in [None, ''] else 0
side_b = float(data.get('b', 0)) if data.get('b') not in [None, ''] else 0
side_c = float(data.get('c', 0)) if data.get('c') not in [None, ''] else 0
angle_a = float(data.get('A', 0)) if data.get('A') not in [None, ''] else 0
angle_b = float(data.get('B', 0)) if data.get('B') not in [None, ''] else 0
angle_c = float(data.get('C', 0)) if data.get('C') not in [None, ''] else 0

# Calculate missing values


if side_a and angle_a:
if side_b and not angle_b:
angle_b = math.degrees(math.asin((side_b *
math.sin(math.radians(angle_a))) / side_a))
elif angle_b and not side_b:
side_b = (side_a * math.sin(math.radians(angle_b))) /
math.sin(math.radians(angle_a))

if side_c and not angle_c:


angle_c = math.degrees(math.asin((side_c *
math.sin(math.radians(angle_a))) / side_a))
elif angle_c and not side_c:
side_c = (side_a * math.sin(math.radians(angle_c))) /
math.sin(math.radians(angle_a))

# Calculate third angle if two are known


if angle_a and angle_b and not angle_c:
angle_c = 180 - angle_a - angle_b
elif angle_a and angle_c and not angle_b:
angle_b = 180 - angle_a - angle_c

# Validate triangle angles


if not validate_triangle_angles(angle_a, angle_b, angle_c):
return jsonify({'error': 'Angles do not sum to 180°'})

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')

# 13. Law of Cosines


@app.route('/law_of_cosines', methods=['GET', 'POST'])
def law_of_cosines():
if request.method == 'POST':
data = request.json
side_a = float(data.get('a', 0)) if data.get('a') not in [None, ''] else 0
side_b = float(data.get('b', 0)) if data.get('b') not in [None, ''] else 0
side_c = float(data.get('c', 0)) if data.get('c') not in [None, ''] else 0
angle_c = float(data.get('C', 0)) if data.get('C') not in [None, ''] else 0

# Calculate missing values


if side_a and side_b and side_c:
# Find angle C
angle_c = math.degrees(math.acos((side_a**2 + side_b**2 - side_c**2) /
(2 * side_a * side_b)))
elif side_a and side_b and angle_c:
# Find side c
side_c = math.sqrt(side_a**2 + side_b**2 - 2 * side_a * side_b *
math.cos(math.radians(angle_c)))
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,
'C': round(angle_c, 4) if angle_c else None
})
return render_template('law_of_cosines.html')

# 14. Area of Triangle using Trigonometry


@app.route('/area_triangle', methods=['GET', 'POST'])
def area_triangle():
if request.method == 'POST':
data = request.json
side_a = float(data.get('a', 0)) if data.get('a') not in [None, ''] else 0
side_b = float(data.get('b', 0)) if data.get('b') not in [None, ''] else 0
angle_c = float(data.get('C', 0)) if data.get('C') not in [None, ''] else 0

if side_a and side_b and angle_c:


area = 0.5 * side_a * side_b * math.sin(math.radians(angle_c))
else:
area = None

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')

# 15. Trigonometric Form of Complex Numbers


@app.route('/complex_numbers', methods=['GET', 'POST'])
def complex_numbers():
if request.method == 'POST':
data = request.json
real = float(data['real'])
imag = float(data['imag'])

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')

# # 16. Sum and Difference Formulas


# @app.route('/sum_difference', methods=['GET', 'POST'])
# def sum_difference():
# if request.method == 'POST':
# data = request.json
# angle1 = float(data['angle1'])
# angle2 = float(data['angle2'])
# operation = data['operation']
# a1 = math.radians(angle1)
# a2 = math.radians(angle2)

# 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')

# # 17. Double Angle Formulas


# @app.route('/double_angle', methods=['GET', 'POST'])
# def double_angle():
# if request.method == 'POST':
# data = request.json
# angle = float(data['angle'])
# function = data['function']

# 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')

# # 19. Product-to-Sum and Sum-to-Product Formulas


# @app.route('/product_sum_formulas', methods=['GET', 'POST'])
# def product_sum_formulas():
# if request.method == 'POST':
# data = request.json
# angle1 = float(data['angle1'])
# angle2 = float(data['angle2'])
# formula_type = data['type']

# 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')

# # 20. Circular Functions


# @app.route('/circular_functions', methods=['GET', 'POST'])
# def circular_functions():
# if request.method == 'POST':
# data = request.json
# angle = float(data['angle'])

# # Normalize angle to 0-360


# normalized = angle % 360
# if normalized < 0:
# normalized += 360

# 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"

# # 21. Trigonometric Word Problems


# @app.route('/word_problems', methods=['GET', 'POST'])
# def word_problems():
# if request.method == 'POST':
# data = request.json
# problem_type = data['type']

# if problem_type == 'height_distance':
# angle = float(data['angle'])
# distance = float(data['distance'])

# height = distance * math.tan(math.radians(angle))


# return jsonify({
# 'height': round(height, 2),
# 'angle': angle,
# 'distance': distance
# })
# elif problem_type == 'angle_elevation':
# height1 = float(data['height1'])
# height2 = float(data['height2'])
# distance = float(data['distance'])

# angle = math.degrees(math.atan((height2 - height1)/distance))


# return jsonify({
# 'angle': round(angle, 2),
# 'height_difference': round(height2 - height1, 2),
# 'distance': distance
# })
# return render_template('word_problems.html')

# # 22. Phase Shift and Vertical Shift


# @app.route('/phase_vertical_shift', methods=['GET', 'POST'])
# def phase_vertical_shift():
# if request.method == 'POST':
# data = request.json
# function = data['function']
# amplitude = float(data['amplitude'])
# period = float(data['period'])
# phase_shift = float(data['phase_shift'])
# vertical_shift = float(data['vertical_shift'])

# # Generate points for the graph


# points = []
# for x in range(0, 721, 15): # 0° to 720° in 15° increments
# scaled_x = x * (360 / period)
# radians = math.radians(scaled_x - phase_shift)

# 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

# points.append({'x': x, 'y': round(y, 4) if y is not None else None})


# return jsonify({
# 'points': points,
# 'equation': generate_shifted_equation(function, amplitude, period,
phase_shift, vertical_shift)
# })
# return render_template('phase_vertical_shift.html')

# def generate_shifted_equation(func, a, b, c, d):


# period_fraction = f"{360}/{'%.2f' % b}" if b != 360 else ""
# period_part = f"{period_fraction}(x {'-' if c > 0 else '+'} {abs(c)})" if c !
= 0 else period_fraction + "x"
# return f"y = {a} {func}({period_part}) {'+' if d >= 0 else ''} {d if d != 0
else ''}"

# # 23. Using Trigonometry in Navigation


# @app.route('/navigation', methods=['GET', 'POST'])
# def navigation():
# if request.method == 'POST':
# data = request.json
# bearing1 = float(data['bearing1'])
# distance1 = float(data['distance1'])
# bearing2 = float(data['bearing2'])
# distance2 = float(data['distance2'])

# # Convert bearings to standard angles (North = 0°, East = 90°)


# angle1 = (90 - bearing1) % 360
# angle2 = (90 - bearing2) % 360

# # Calculate x and y components


# x1 = distance1 * math.cos(math.radians(angle1))
# y1 = distance1 * math.sin(math.radians(angle1))
# x2 = distance2 * math.cos(math.radians(angle2))
# y2 = distance2 * math.sin(math.radians(angle2))

# # Calculate resultant vector


# x_total = x1 + x2
# y_total = y1 + y2

# # Calculate final distance and bearing


# distance = math.sqrt(x_total**2 + y_total**2)
# angle = math.degrees(math.atan2(y_total, x_total))
# bearing = (90 - angle) % 360

# 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')

# # 24. Solving Triangles using Heron's Formula and Trig


# @app.route('/herons_formula', methods=['GET', 'POST'])
# def herons_formula():
# if request.method == 'POST':
# data = request.json
# side_a = float(data.get('a', 0)) if data.get('a') not in [None, ''] else
0
# side_b = float(data.get('b', 0)) if data.get('b') not in [None, ''] else
0
# side_c = float(data.get('c', 0)) if data.get('c') not in [None, ''] else
0

# if side_a and side_b and side_c:


# # Calculate area using Heron's formula
# s = (side_a + side_b + side_c) / 2
# area = math.sqrt(s * (s - side_a) * (s - side_b) * (s - side_c))

# # Calculate angles using Law of Cosines


# angle_a = math.degrees(math.acos((side_b**2 + side_c**2 -
side_a**2) / (2 * side_b * side_c)))
# angle_b = math.degrees(math.acos((side_a**2 + side_c**2 -
side_b**2) / (2 * side_a * side_c)))
# angle_c = 180 - angle_a - angle_b
# else:
# area = None
# angle_a = angle_b = angle_c = None

# 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')

# # 25. Identities Involving Multiple Angles


# @app.route('/multiple_angle_identities', methods=['GET', 'POST'])
# def multiple_angle_identities():
# if request.method == 'POST':
# data = request.json
# angle = float(data.get('angle', 0)) if data.get('angle') not in [None,
''] else None
# function = data.get('function', 'sin')

# if angle is not None:


# rad = math.radians(angle)
# if function == 'sin':
# result = math.sin(3 * rad)
# elif function == 'cos':
# result = math.cos(3 * rad)
# elif function == 'tan':
# result = math.tan(3 * rad)
# else:
# result = None
# else:
# result = None

# 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')

# # 27. Verifying Trigonometric Identities


# @app.route('/verify_identities', methods=['GET', 'POST'])
# def verify_identities():
# if request.method == 'POST':
# data = request.json
# identity = data.get('identity', '')
# angle = float(data.get('angle', 0)) if data.get('angle') not in [None,
''] else None

# if angle is not None:


# rad = math.radians(angle)
# # Example: Verify sin²θ + cos²θ = 1
# if identity == 'pythagorean':
# left = math.sin(rad)**2 + math.cos(rad)**2
# right = 1
# verified = abs(left - right) < 1e-9
# else:
# verified = None
# else:
# verified = None

# return jsonify({
# 'identity': identity,
# 'verified': verified
# })
# return render_template('verify_identities.html')

# # 28. Parametric Equations with Trig Functions


# @app.route('/parametric_equations', methods=['GET', 'POST'])
# def parametric_equations():
# if request.method == 'POST':
# data = request.json
# t = float(data.get('t', 0)) if data.get('t') not in [None, ''] else 0
# r = float(data.get('r', 1)) if data.get('r') not in [None, ''] else 1
# omega = float(data.get('omega', 1)) if data.get('omega') not in [None,
''] else 1

# 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')

# # 29. Modeling Periodic Phenomena


# @app.route('/periodic_phenomena', methods=['GET', 'POST'])
# def periodic_phenomena():
# if request.method == 'POST':
# data = request.json
# amplitude = float(data.get('amplitude', 1)) if data.get('amplitude') not
in [None, ''] else 1
# period = float(data.get('period', 2*math.pi)) if data.get('period') not
in [None, ''] else 2*math.pi
# phase_shift = float(data.get('phase_shift', 0)) if
data.get('phase_shift') not in [None, ''] else 0
# vertical_shift = float(data.get('vertical_shift', 0)) if
data.get('vertical_shift') not in [None, ''] else 0
# x = float(data.get('x', 0)) if data.get('x') not in [None, ''] else 0

# # Calculate y = A*sin(2π/P*(x - C)) + D


# y = amplitude * math.sin((2*math.pi/period) * (x - phase_shift)) +
vertical_shift

# 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')

# # 30. Angle Between Two Vectors


# @app.route('/vector_angle', methods=['GET', 'POST'])
# def vector_angle():
# if request.method == 'POST':
# data = request.json
# x1 = float(data.get('x1', 0)) if data.get('x1') not in [None, ''] else 0
# y1 = float(data.get('y1', 0)) if data.get('y1') not in [None, ''] else 0
# x2 = float(data.get('x2', 0)) if data.get('x2') not in [None, ''] else 0
# y2 = float(data.get('y2', 0)) if data.get('y2') not in [None, ''] else 0

# dot_product = x1*x2 + y1*y2


# mag_v1 = math.sqrt(x1**2 + y1**2)
# mag_v2 = math.sqrt(x2**2 + y2**2)
# if mag_v1 != 0 and mag_v2 != 0:
# cos_theta = dot_product / (mag_v1 * mag_v2)
# # Handle possible floating point errors
# cos_theta = max(min(cos_theta, 1), -1)
# angle = math.degrees(math.acos(cos_theta))
# else:
# angle = None

# 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)

You might also like