Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-057

Triangle Area via Cross Product

Problem

Compute the area of the triangle with 3D vertices p, q, r:

area = 0.5 * ||(q - p) x (r - p)||_2

A degenerate (collinear) triangle has area 0.0.

Hint

Half the area of the parallelogram spanned by q - p and r - p.

Test cases

case 1
in: [[0,0,0],[1,0,0],[0,1,0]]
expected: 0.5
case 2
in: [[1,1,1],[2,1,1],[1,3,1]]
expected: 1
case 3
in: [[0,0,0],[1,1,1],[2,2,2]]
expected: 0
case 4
in: [[0,0,0],[2,0,0],[0,2,0]]
expected: 2
Starter codePython
def triangle_area(p, q, r):
    # Your code here
    pass

4 test cases · run your code against them in the editor