Another easy challenge for you.

Your task

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.


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

  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Dennis
    Commented Oct 14, 2015 at 21:07
  • 4
    \$\begingroup\$ I'm still confused. The post still says both "3 pairs" and "six ... integers". Note that removing either one would invalidate some answers. \$\endgroup\$
    – xnor
    Commented Oct 14, 2015 at 21:08
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – edc65
    Commented Oct 14, 2015 at 21:37
  • 1
    \$\begingroup\$ 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)? \$\endgroup\$
    – Glen O
    Commented Oct 15, 2015 at 5:16
  • 2
    \$\begingroup\$ @YiminRong The area of a triangle cannot be negative by definition. It does not matter what order the points are in. \$\endgroup\$
    – Rainbolt
    Commented Oct 15, 2015 at 19:19

18 Answers 18


CJam, 18 16 bytes


Try it online in the CJam interpreter.


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.


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

  • 17
    \$\begingroup\$ I love how this uses a Built-in and is still longer than the cjam answer. \$\endgroup\$ Commented Oct 14, 2015 at 16:14
  • 2
    \$\begingroup\$ @Carcigenicate the real problem is the Partition[t,2], which corresponds to the 2/ in CJam. ;) \$\endgroup\$ Commented Oct 14, 2015 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.


Test running the snippet below in an EcmaScript 6 compliant browser.


function test()
  var v=I.value.match(/\d+/g)
  I.value = v
<input id=I onchange="test()"><button onclick="test()">-></button><span id=R></span>

  • 1
    \$\begingroup\$ Couldn't you just take the values as standard parameters and save yourself 2 characters on creating the array? \$\endgroup\$
    – Mwr247
    Commented Oct 14, 2015 at 17:52
  • \$\begingroup\$ @Mwr247 the challenge says The input will be a vector with six base 10 positive integers. \$\endgroup\$
    – edc65
    Commented Oct 14, 2015 at 19:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mwr247
    Commented Oct 14, 2015 at 20:23
  • \$\begingroup\$ @Mwr247 now you're right \$\endgroup\$
    – edc65
    Commented Oct 14, 2015 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

I did not know about this built in so far=)


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 :(

  • \$\begingroup\$ you dont need to make it runnable? \$\endgroup\$ Commented Oct 14, 2015 at 15:24
  • 3
    \$\begingroup\$ The example just shows a function call, and that's a relatively normal default here. \$\endgroup\$
    – Geobits
    Commented Oct 14, 2015 at 15:25
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ Commented Oct 14, 2015 at 15:29
  • \$\begingroup\$ @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. \$\endgroup\$
    – Geobits
    Commented Oct 14, 2015 at 15:31

Minkolang 0.8, 34 bytes


Anyone want some egg-n0g?


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};


var nFunct = function(a,b,c,d,e,f){return (a*(d-f)+c*(f-b)+e*(b-d))/2};
  • \$\begingroup\$ what does g do? \$\endgroup\$ Commented Oct 14, 2015 at 22:40
  • \$\begingroup\$ @steveverrill Nothing, I'm just an idiot. Fixing... \$\endgroup\$
    – user42643
    Commented Oct 14, 2015 at 22:48

Ruby, 45


PHP – 68 88 89 bytes

Thanks to Martjin for some great pointers!


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; ?>

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
  • \$\begingroup\$ "You can assume the input is already stored in a variable such as t." $a -> $t, remove $a=$argv; saving 9 bytes \$\endgroup\$
    – Martijn
    Commented Oct 15, 2015 at 9:48
  • \$\begingroup\$ After that, you can replace <?php echo with <?=, saving another 7 bytes \$\endgroup\$
    – Martijn
    Commented Oct 15, 2015 at 9:52
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Oct 16, 2015 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


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)

Python 2, 48 47 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.

  • \$\begingroup\$ you can remove the 0 from 2.0 to leave 2. \$\endgroup\$
    – Blue
    Commented Oct 14, 2015 at 17:36
  • \$\begingroup\$ Quite true, @muddyfish, thanks! \$\endgroup\$
    – Celeo
    Commented Oct 14, 2015 at 17:38
  • \$\begingroup\$ Is this Python 2 or 3? Division works differently depending on the version... \$\endgroup\$
    – mbomb007
    Commented Oct 14, 2015 at 18:42
  • \$\begingroup\$ Clarified, @mbomb007. \$\endgroup\$
    – Celeo
    Commented Oct 14, 2015 at 18:46
  • 1
    \$\begingroup\$ You need an abs to make the answer positive. \$\endgroup\$
    – xnor
    Commented Oct 14, 2015 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"


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

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

PowerShell, 70 Bytes


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

  • \$\begingroup\$ Your comment about the penalty from using $ and [] inspired me to try an AWK solution which, by length, is not uncompetitive! \$\endgroup\$
    – user15259
    Commented Oct 14, 2015 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.


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


  • - * 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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.