In Python 2.7 An arbitrary triangle can be described by the coordinates of its t
ID: 672300 • Letter: I
Question
In Python 2.7
An arbitrary triangle can be described by the coordinates of its three vertices: (x1, y1), (x2,y2), and (x3, y3), numbered in a counter-clockwise direction. The area of the triangle is given by the formula
A= (1/2)*abs(x2*y3 - x3*y2 - x1*y3 + x3*y1 + x1*y2 - x2*y1)
Write a function area(vertices) that returns the area of the triangle whose vertices are specifed by the argument "vertices," which is a nested list of the vertex coordinates. For example, "vertices" can be [[0,0],[1,0],[0,2]] if the three coordinates of the triangle have coordinates (0,0), (1,0), and (0,2). Test the area function with known area.
Test your function with the following statement:
area([[0,0],[1,0],[0,2]])
Explanation / Answer
Below are function area to find area of triangle .
# A function to calculate area of triangle .
def areaOfTriangle(X1,X2,Y1,Y2,Z1,Z2 ):
area = (1/2)*abs(x2*y3 - x3*y2 - x1*y3 + x3*y1 + x1*y2 - x2*y1)
return area
# Main execution area of your program.
# Ask user for base and height, store in variable.
# The numbers entered are still just keyboard characters
# We need to cast them to floats (numbers with decimal points)
X1 = float( X1)
X2 = float( X2)
Y1 = float( Y1)
Y2 = float( Y2)
Z1 = float( Z1)
Z2 = float( Z2)
# Pass our variables to the function
print 'Area of triangle is', areaOfTriangle(X1,X2,Y1,Y2,Z1,Z2)
# We can also store the return value in a variable
# and use it however we want.
area = areaOfTriangle(X1,X2,Y1,Y2,Z1,Z2)
print 'The area of the triangle is', area
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.