Using python 27 An arbitrary triangle can be described by th
Using python 2.7,
An arbitrary triangle can be described by the coordinates of its three vertices: (x_1, y_1), (x_2, y_2), (x_3, y_3), numbered in a counterclockwise direction. The area of the triangle is given by the formula A = 1/2 |x_2 y_3 - x_3y_2 - x_1 y_3 + x_1 y_2 - x_2 y_1|. (3.8) Write a function area (vertices) that returns the area of a triangle whose vertices are specified 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 corners of the triangle have coordinates (0, 0), (1, 0), and (0, 2). Test the area function on a triangle with known area. Name of program file: area.triangle.py. Test your function with the following statement: area([[0, 0], [1, 0], [0, 2]])Solution
Following is the required python code :
def area( vertices ):
    #defining x1,y1 x2,y2 and x3,y3 just for the clarity
    x1 = vertices[0][0];
    y1 = vertices[0][1];
    x2 = vertices[1][0];
    y2 = vertices[1][1];
    x3 = vertices[2][0];
    y3 = vertices[2][1];
    area = abs( x2*y3 - x3*y2 - x1*y3 + x3*y1 + x1*y2 - x2*y1 );
    area = (area*1.0)/2;
    return area;
a = area( [[0,0],[1,0],[0,2]] );
 print a;

