Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
7.6. Functions Calling Functions - 2
Now that we have written and tested distance
, we can use it to help write triangle_area
.
This function will take all six coordinates and return the area.
def triangleArea(x1, y1, x2, y2, x3, y3):
Do work to calculate area - use distance function to help
return area
Recall that the formula we need to use is: \(\sqrt{s (s - a) (s - b) (s - c)}\)
The work for this function will involve calling the distance function three times to calculate
the lengths of the three sides (a, b, c). We then need to use those to calculate s,
the semi-perimeter.
Below is the code for the final program but jumbled up - figure out the right order.
Put the blocks in the right order and indentation. You will use all of the blocks.
def distance(x1, y1, x2, y2):
xDifference = x2 - x1
yDifference = y2 - y1
distance = math.sqrt( xDifference ** 2 + yDifference ** 2 )
return distance
---
def triangle_area(x1, y1, x2, y2, x3, y3):
---
a = distance(x1, y1, x2, y2)
b = distance(x2, y2, x3, y3)
c = distance(x1, y1, x3, y3)
---
perimeter = a + b + c
---
s = perimeter / 2 #semi-perimeter
---
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
---
return area
---
# Main program
import math
p1x = 0
p1y = 0
p2x = 3
p2y = 1
p3x = 2
p3y = 6
testArea = triangle_area(p1x, p1y, p2x, p2y, p3x, p3y)
You have attempted
of
activities on this page