# Area of the triangle

Another easy challenge for you.

Write a program or function that takes the input, which contains 3 pairs of x- and y-coordinates and calculates the area of the triangle formed inside them. For those who can't remember how to calculate it, you can find it here.

### Example:

1,2,4,2,3,7       # input as x1,y1,x2,y2,x3,y3
7.5               # output


See it at Wolfram Alpha

Some considerations:

• The input will be six base 10 positive integers.
• You may assume the input is in any reasonable format.
• The points will always form a valid triangle.
• You can assume the input is already stored in a variable such as t.
• The shortest code in bytes wins!

Edit: To avoid any confusion I've simplificated how the input should be dealt without jeopardizing any of the current codes.

Remember that the your program/function must output a valid area, so it can't give a negative number as output

• Re: your edit. Does that mean that I can have an actual array of pairs (e.g., [[1, 2], [4, 2], [3, 7]]) in T? – Dennis Oct 14 '15 at 21:07
• I'm still confused. The post still says both "3 pairs" and "six ... integers". Note that removing either one would invalidate some answers. – xnor Oct 14 '15 at 21:08
• I don't like seeing a question change after posting and answer. But this time I can save 2 more bytes, so it's all right – edc65 Oct 14 '15 at 21:37
• If we can take them in as three pairs, can we take them in as a multidimensional array? That is, [1 2;4 2;3 7] (using Julia syntax)? – Glen O Oct 15 '15 at 5:16
• @YiminRong The area of a triangle cannot be negative by definition. It does not matter what order the points are in. – Rainbolt Oct 15 '15 at 19:19

# 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


# Mathematica, 27 bytes

Area@Polygon@Partition[t,2]

• I love how this uses a Built-in and is still longer than the cjam answer. – Carcigenicate Oct 14 '15 at 16:14
• @Carcigenicate the real problem is the Partition[t,2], which corresponds to the 2/ in CJam. ;) – Martin Ender Oct 14 '15 at 21:33

# 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>

• Couldn't you just take the values as standard parameters and save yourself 2 characters on creating the array? – Mwr247 Oct 14 '15 at 17:52
• @Mwr247 the challenge says The input will be a vector with six base 10 positive integers. – edc65 Oct 14 '15 at 19:50
• Aha. I had initially interpreted that as meaning each pair makes up a coordinate vector (such as the Wolfram example), as opposed to the input itself being limited to an array, and as such could use other formats. Makes more sense now. – Mwr247 Oct 14 '15 at 20:23
• @Mwr247 now you're right – edc65 Oct 14 '15 at 21:34

# Julia, 32 bytes

abs(det(t[1:2].-t[[3 5;4 6]]))/2


Constructs a matrix of the appropriate terms of a cross product, uses det to get the resulting value, takes absolute value to deal with negatives, and then divides by 2 because it's a triangle and not a parallelogram.

# Matlab/Octave, 26 bytes

polyarea(t(1:2:5),t(2:2:6))


# Java, 79 88 bytes

float f(int[]a){return Math.abs(a[0]*(a[3]-a[5])+a[2]*(a[5]-a[1])+a[4]*(a[1]-a[3]))/2f;}


Just uses the basic formula, nothing special.

Edit: Forgot to take the absolute value :(

• you dont need to make it runnable? – downrep_nation Oct 14 '15 at 15:24
• The example just shows a function call, and that's a relatively normal default here. – Geobits Oct 14 '15 at 15:25
• Per the question, •You can assume the input is already stored in a variable such as 't'. So, return(t[0]*(t[3]... should suffice, no? – AdmBorkBork Oct 14 '15 at 15:29
• @TimmyD Feels shady doing it, but it would bring it down to 62 bytes. Hmmm.... I'm going to leave it as is, for now at least. – Geobits Oct 14 '15 at 15:31

## Minkolang 0.8, 34 bytes

ndndn0g-n1g-n0g-n0g-1R*1R*-$~2$:N.


Anyone want some egg-n0g?

### Explanation

Very straightforward. Uses the formula |(x2-x1)(y3-y1) - (x3-x1)(y2-y1)|/2.

nd      x1, x1
nd      x1, x1, y1, y1
n0g-    x1, y1, y1, x2-x1
n1g-    x1, y1, x2-x1, y2-y1
n0g-    y1, x2-x1, y2-y1, x3-x1
n0g-    x2-x1, y2-y1, x3-x1, y3-y1
1R*     y3-y1, x2-x1, (y2-y1)(x3-x1)
1R*     (y2-y1)(x3-x1), (y3-y1)(x2-x1)
-       (y2-y1)(x3-x1) - (y3-y1)(x2-x1)
$~ |(y2-y1)(x3-x1) - (y3-y1)(x2-x1)| 2$:     |(y2-y1)(x3-x1) - (y3-y1)(x2-x1)|/2 (float division)
N.      Output as integer and quit.


# JayScript, 58 bytes

Declares an anonymous function:

function(a,b,c,d,e,f){return (a*(d-f)+c*(f-b)+e*(b-d))/2};


Example:

var nFunct = function(a,b,c,d,e,f){return (a*(d-f)+c*(f-b)+e*(b-d))/2};
print(nFunct(1,2,4,2,3,7));

• what does g do? – Level River St Oct 14 '15 at 22:40
• @steveverrill Nothing, I'm just an idiot. Fixing... – user42643 Oct 14 '15 at 22:48

# Ruby, 45

->a,b,p,q,x,y{((a-x)*(q-y)-(p-x)*(b-y)).abs/2}


# PHP – 68 8889 bytes

Thanks to Martjin for some great pointers!

<?=.5*abs(($t[1]-$t[5])*($t[4]-$t[2])-($t[1]-$t[3])*($t[6]-$t[2]))?>


To use it, create a file area.php with this content, the extra line meets the assume the data is saved in a variable t part of the specs, and the ␍ at the end adds a carriage return so the output is nice and separated:

<?php $t =$argv; ?>
<?=.5*abs(($t[1]-$t[5])*($t[4]-$t[2])-($t[1]-$t[3])*($t[6]-$t[2]))?>
␍


Then provide the coordinates on the command line as x₁ y₁ x₂ y₂ x₃ y₃, e.g.

$php area.php 1 2 4 2 3 7 7.5  • "You can assume the input is already stored in a variable such as t." $a -> $t, remove $a=$argv; saving 9 bytes – Martijn Oct 15 '15 at 9:48 • After that, you can replace <?php echo with <?=, saving another 7 bytes – Martijn Oct 15 '15 at 9:52 • You can say that this is PHP4.1, with register_globals=On in your php.ini file (default). Read more at php.net/manual/en/security.globals.php – Ismael Miguel Oct 16 '15 at 1:07 # Pyth, 34 30 bytes KCcQ2c.asm*@hKd-@eKhd@eKtdU3 2  Try it online. Works by calculating abs(a*(d-f) + c*(f-b) + e*(b-d))/2 from input a,b,c,d,e,f. # R, 37 bytes cat(abs(det(rbind(matrix(t,2),1))/2))  Converts the vector of coordinates into a matrix and tacks on a row of 1's. Calculates the determinant and divides by 2. Returns the absolute result. If the order was always clockwise the abs would not be required. > t = c(1,2,4,2,3,7) > cat(det(rbind(matrix(t,2),1))/2) 7.5  # Python 2, 4847 50 bytes Very simple; follows the standard equation: lambda a,b,c,d,e,f:abs(a*(d-f)+c*(f-b)+e*(b-d))/2.  The other, similarly simple approaches are longer: def a(a,b,c,d,e,f):return abs(a*(d-f)+c*(f-b)+e*(b-d))/2. # 57 lambda t:abs(t[0]*(t[3]-t[5])+t[2]*(t[5]-t[1])+t[4]*(t[1]-t[3]))/2. # 67 def a(t):return abs(t[0]*(t[3]-t[5])+t[2]*(t[5]-t[1])+t[4]*(t[1]-t[3]))/2. # 74  Python's access to a determinate function is through numpy. Thanks to muddyfish for 1 byte and xnor for catching an error. • you can remove the 0 from 2.0 to leave 2. – Blue Oct 14 '15 at 17:36 • Quite true, @muddyfish, thanks! – Celeo Oct 14 '15 at 17:38 • Is this Python 2 or 3? Division works differently depending on the version... – mbomb007 Oct 14 '15 at 18:42 • Clarified, @mbomb007. – Celeo Oct 14 '15 at 18:46 • You need an abs to make the answer positive. – xnor Oct 14 '15 at 20:56 ## PHP, 77 Based on @Yimin Rong's answer, I felt I could improve upon it by a few bytes by using list() rather than straight $argv to abbreviate some variables. Also echo doesn't need a space if there is delimiter between echo and the thing being echoed.

echo$variable;, echo(4+2);, and echo'some string'; are equally valid whereas echofunction($variable) confuses PHP.

On the other hand, I also added abs() to be mathematically accurate, since some combinations of vertices yielded "negative area"

list($t,$a,$b,$c,$d,$e,$f)=$argv;echo.5*abs(($a-$e)*($d-$b)-($a-$c)*($f-$b));


You can run it via CLI

php -r "list($t,$a,$b,$c,$d,$e,$f)=$argv;echo.5*abs(($a-$e)*($d-$b)-($a-$c)*($f-$b));" 1 2 4 2 3 7
7.5


# AWK – 51 42 bytes

AWK has no built-in abs so using sqrt(x^2) to substitute.

{print sqrt((($1-$5)*($4-$2)-($1-$3)*($6-$2))^2)/2}


Save as area.awk and use as echo x₁ y₁ x₂ y₂ x₃ y₃ | awk -f area.awk, e.g.

$echo 1 2 4 2 3 7 | awk -f area.awk 7.5  # PowerShell, 70 Bytes [math]::Abs(($t[0]-$t[4])*($t[3]-$t[1])-($t[0]-$t[2])*($t[5]-$t[1]))/2  Uses the same standard formula as other solutions. Per the question, assumes the array is pre-populated, e.g. $t=(1,2,4,2,3,7). But ooof, does the $ and [] syntax kill this one... • Your comment about the penalty from using $ and [] inspired me to try an AWK solution which, by length, is not uncompetitive! – user15259 Oct 14 '15 at 16:22

## dc, 52 bytes

Assumes the input is in register t as: x1 y1 x2 y2 x3 y3 with x1 at the top of t's stack.

1kLtLtsaLtsbLtdscLtltrlalclbltla-*sd-*se-*leld++2/p


 1 2 4 2 3 7stStStStStSt #puts coordinates into register t (closest thing dc has to variables) 1kLtLtsaLtsbLtdscLtltrlalclbltla-*sd-*se-*leld++2/p 7.5 

This uses the following formula for area:

(x1(y2-y3) + x2(y3-y1) + x3(y1 - y2))/2

And for a quick breakdown of the process:

• 1k Lt Lt sa Lt sb Lt d sc Lt lt r: set decimal precision to 1 place, move parts of the stack in t to the main stack and move various parts of the main stack to other registers for storage (d duplicates the top of main stack, r reverses the top two elements of main stack, L/l move/copy from the given register to main, s moves top of main stack to the given register)

Main: y3 x3 y2 x1

a: y1, b: x2, c: y2, t: y3

• la lc lb lt la: copy the top of the stacks in registers a, c, b, t, and a to the main stack in that order

Main: y1 y3 x2 y2 y1 y3 x3 y2 x1

a: y1, b: x2, c: y2, t: y3

• - * sd: calculate ((y3-y1)*x2) and put result in d (registers a, b, c, and t are no longer used so I'll drop them from the list of stacks now)

Main: y2 y1 y3 x3 y2 x1

d:((y3-y1)*x2)

• - * se - *: compute ((y1-y2)*y3) and ((y2-x3)*x1); store the former in e and leave the latter on the main stack

Main: ((y2-x3)*x1)

d:((y3-y1)*x2), e:((y1-y2)*y3)

• le ld + +: copy top of register e and d to the main stack, calculate sum of top 2 stack values (pushing result back to main stack) twice

Main: (((y3-y1)*x2)+((y1-y2)*y3)+((y2-x3)*x1))

d:((y3-y1)*x2), e:((y1-y2)*y3)

• 2 /: push 2 onto main stack, divide 2nd values on stack by the 1st (d and e are no longer used, dropping them from list of stacks)

Main: (((y3-y1)*x2)+((y1-y2)*y3)+((y2-x3)*x1))/2

Rearranging the value on the stack we can see it's equivalent to the formula at the top of this explanation: (x1(y2-y3) + x2(y3-y1) + x3(y1 - y2))/2

• p: Print top of main stack to output.

# Jelly, 6 bytes

_ḢÆḊHA


Try it online!

Takes input as 3 pairs [a,b],[c,d],[e,f] as Jelly has no concept of variables

## How it works

This implements the method outlined on Wikipedia that Dennis' answer uses, in that, the area of a triangle $$\(0,0), (x,y), (v,w)\$$ is

$$\frac12\left|\det\left[\begin{matrix} x & y \\ v & w \end{matrix}\right]\right|$$

and that, for an arbitrary triple of points $$\(a,b),(c,d),(e,f)\$$, we can translate them to form a triangle with a corner on the origin: $$\(0,0),(c-a,d-b),(e-a,f-b)\$$, so the area is

$$\frac12\left|\det\left[\begin{matrix} c-a & d-b \\ e-a & f-b \end{matrix}\right]\right|$$

_ḢÆḊHA - Main link. Takes [[a, b], [c, d], [e, f]] on the left
Ḣ     - Remove [a,b] and yield it
_      - Vectorised subtraction from each of [[c, d], [e, f]] which yields
[[c-a, d-b], [e-a, f-b]]
ÆḊ   - Determinant
H  - Halve
A - Absolute value