Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook assignments/Week1/Herons_formula.ipynb

12 views
Kernel: Python 2 (SageMath)
# Heron's formula states the area of a triangle is given # by the square root of the product s(s−a)(s−b)(s−c) # where a, b and c are the lengths of the sides of the # triangle and s=(a+b+c)/2 is the semi-perimeter of the triangle. # Compute the area of a triangle (using Heron's formula), # given its vertices. ################################################### # Header print('--------------------------------') print(" Heron's Formula") print('--------------------------------') print('') ################################################### # Input print('Supply the coordinates (x1, y1) of the first vertex:') x1 = input('x1 = ') y1 = input('y1 = ') print('Supply the coordinates (x2, y2) of the second vertex:') x2 = input('x2 = ') y2 = input('y2 = ') print('Supply the coordinates (x3, y3) of the third vertex:') x3 = input('x3 = ') y3 = input('y3 = ') ################################################### # Triangle area (Heron's) formula # Student should enter formulas on the next lines. # Define and calculate a, b, c, s and area import math a = math.sqrt((x2-x1)**2 + (y2-y1)**2) b = math.sqrt((x3-x2)**2 + (y3-y2)**2) c = math.sqrt((x1-x3)**2 + (y1-y3)**2) s = ((a+b+c)/2) area = math.sqrt(s*(s-a)*(s-b)*(s-c)) ################################################### # Output print('') print('A triangle with vertices (' + str(x1) + ',' + str(y1) + '),'), print('(' + str(x2) + ',' + str(y2) + '), and'), print('(' + str(x3) + ','+ str(y3) + ') has an area of '+ str(area) + '.') ################################################### # Expected output: #A triangle with vertices (0,0), (3,4), and (1,1) has an area of 0.5. #A triangle with vertices (-2,4), (1,6), and (2,1) has an area of 8.5. #A triangle with vertices (10,0), (0,0), and (0,10) has an area of 50.
-------------------------------- Heron's Formula -------------------------------- Supply the coordinates (x1, y1) of the first vertex: x1 = 10 y1 = 0 Supply the coordinates (x2, y2) of the second vertex: x2 = 0 y2 = 0 Supply the coordinates (x3, y3) of the third vertex: x3 = 0 y3 = 10 A triangle with vertices (10,0), (0,0), and (0,10) has an area of 50.0.