Area of the triangle
CJam, 18 16 bytes
T(f.-~(+.*:-z.5*
Try it online in the CJam interpreter.
Idea
As mentioned on Wikipedia, the area of the triangle [[0 0] [x y] [z w]]
can be calculated as |det([[x y] [z w]])| / 2 = |xw-yz| / 2
.
For a generic triangle [[a b] [c d] [e f]]
, we can translate its first vertex to the origin, thus obtaining the triangle [[0 0] [c-a d-b] [e-a f-b]]
, whose area can be calculated by the above formula.
Code
T e# Push T.
e# [[a b] [c d] [e f]]
( e# Shift out the first pair.
e# [[c d] [e f]] [a b]
f.- e# For [c d] and [e f], perform vectorized
e# subtraction with [a b].
e# [[c-a d-b] [e-a f-b]]
~ e# Dump the array on the stack.
e# [c-a d-b] [e-a f-b]
(+ e# Shift and append. Rotates the second array.
e# [c-a d-b] [f-b e-a]
.* e# Vectorized product.
e# [(c-a)(f-b) (d-b)(e-a)]
:- e# Reduce by subtraction.
e# (c-a)(f-b) - (d-b)(e-a)
z e# Apply absolute value.
e# |(c-a)(f-b) - (d-b)(e-a)|
.5* e# Multiply by 0.5.
e# |(c-a)(f-b) - (d-b)(e-a)| / 2
JavaScript (ES6) 42 .44.
Edit Input format changed, I can save 2 bytes
An anonymous function that take the array as a parameter and returns the calculated value.
(a,b,c,d,e,f)=>(a*(d-f)+c*(f-b)+e*(b-d))/2
Test running the snippet below in an EcmaScript 6 compliant browser.
f=(a,b,c,d,e,f)=>(a*(d-f)+c*(f-b)+e*(b-d))/2
function test()
{
var v=I.value.match(/\d+/g)
I.value = v
R.innerHTML=f(...v)
}
<input id=I onchange="test()"><button onclick="test()">-></button><span id=R></span>
Mathematica, 27 bytes
Area@Polygon@Partition[t,2]