'Python Area of triangle - At least one of the sides will be parallel to the x or y axis

Came up with the following but can't seem to figure out why in some cases i'm getting a negative number:

def getTriangleArea(x, y):
    area = ( (x[0]*(y[1]-y[2])) + (x[1]*(y[2]-y[0])) + (x[2]*(y[0]-y[1]))) / 2
return int(area)

Any thoughts?



Solution 1:[1]

You just need to return the absolute value of the area.

def getTriangleArea(x, y):
    area = ( (x[0]*(y[1]-y[2])) + (x[1]*(y[2]-y[0])) + (x[2]*(y[0]-y[1]))) / 2
    return abs(int(area))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Srivatsava