[Question]
  [
# Calculate the area of a polygon.
Inspired by this [shoelace algorithm video.](https://www.youtube.com/watch?v=0KjG8Pg6LGk&t=0s)
## Task
Your job is to create a program or function that calculates the area of a polygon. Program or function is defined according the the default definition in meta.
## Input
You will receive the X and Y coordinates of each vertex of the polygon. You can take the input as a list of tuples (`[[x1, y1], [x2, y2], etc]`), a matrix, or a flat list (`[x1, y1, x2, y2, etc]`). Two lists containing `x` and `y` coordinates respectively are allowed as well. The vertices are numbered counterclockwise and the first vertex is the same as the last vertex provided, thus closing the polygon.
If you want you can take the input without the last vertex (so receive each coordinate just once).
You can assume that the edges of the polygons don't intersect. You can also assume that all vertices have integer coordinates.
## Output
The area of the polygon. All standard output methods are allowed. If your language does not allow for float division and the solution would not be an integer, you are allowed to return a fraction. The fraction does not necessarily have to be simplified, so returning `2/4` would be allowed.
## Winning criterium
Shortest code wins!
## Test cases
```
[[4,4],[0,1],[-2,5],[-6,0],[-1,-4],[5,-2],[4,4]]
55
```
[![enter image description here](https://i.stack.imgur.com/GWDit.png)](https://i.stack.imgur.com/GWDit.png)
```
[[1,1],[0,1],[1,0],[1,1]]
0.5
1/2
```
[![enter image description here](https://i.stack.imgur.com/hwdr5.png)](https://i.stack.imgur.com/hwdr5.png)
  
[Answer]
## Mathematica, 13 bytes
```
Area@*Polygon
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 9 bytes
```
@polyarea
```
Inputs are a vector with the *x* values and a vector with the *y* values. This works in MATLAB too.
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en99@hID@nMrEoNfF/mka0iYKBgq6Rgq6Zgq6hgqmCSayOAlAMxASKm4CkTGI1uYAKDYEChgqGIHkgBeLEav4HAA)
[Answer]
# JavaScript (ES6), ~~69 67 47~~ 44 bytes
*Thanks to @Rick for noticing that we don't need the absolute value if the vertices are guaranteed to be sorted in counterclockwise order and for suggesting to take a flat list as input, saving 20 bytes!  
Saved 3 more bytes thanks to @Shaggy*
Takes input as a flat list of vertices, including the last vertex.
```
f=([x,y,...a])=>a+a?x*a[1]/2-y*a[0]/2+f(a):0
```
[Try it online!](https://tio.run/##XYtBDoIwFET3nqLLVqa1n4ALE/QgTRc/SI2GUCPGwOnrx6WbyczLvAd/eO5f9@fbTvk6lJI6HRascM5xNN2ZK74sew4UD7VdpXgpVdJsTr70eZrzOLgx33TSoUED5UFQtkYreYSXJFjhLQQquURjdn8ibc5PpM2QKafyBQ "JavaScript (Node.js) – Try It Online")
### How?
This is based on the formula used in many other answers. For \$n\$ 0-indexed vertices:
$$area=\left|\frac{(x\_0y\_1-y\_0x\_1)+(x\_1y\_2-y\_1x\_2)+\ldots+(x\_{n-1}y\_0-y\_{n-1}x\_0)}{2}\right|$$
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~8~~ 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-1 byte thanks to Emigna (redundant `€`, `ÆḊ` has a left-depth of 2)  
-1 byte thanks to Emigna, again (halve, `H`, is floating point no need to `÷2`)
```
ṡ2ÆḊSH
```
A monadic link taking a list of pairs of coordinates in counter-clockwise fashion as per the examples (with the one repeat) and returning the area.
**[Try it online!](https://tio.run/##y0rNyan8///hzoVGh9se7ugK9vj//390tImOSaxOtIGOIZDUNdIxBVFmOgYgylBHFyRnqqNrBKRACmMB "Jelly – Try It Online")**
### How?
Applies the shoelace algorithm, just as described in the video (which I happened to also watch just the other day!)
```
ṡ2ÆḊSH - Link: list of [x,y] coordinate pairs anticlockwise & wrapped, p
ṡ2     - all overlapping slices of length 2
  ÆḊ   - determinant (vectorises)
    S  - sum
     H - halve
```
[Answer]
# [Python 3](https://www.python.org), ~~72~~ 71 bytes
```
from numpy import*
g=lambda x,y:(dot(x[:-1],y[1:])-dot(x[1:],y[:-1]))/2
```
Takes two lists, as it was allowed in the comments
```
x = [x0,x1,x2, ...]
y = [y0,y1,y2, ...] 
```
[Try it online!](https://tio.run/##K6gsycjPM/7/P60oP1chrzS3oFIhM7cgv6hEiyvdNicxNyklUaFCp9JKIyW/RKMi2krXMFanMtrQKlZTFyICZAIFQOKamvpG//8DAA)
This is basically just the implementation of the [shoelace-formula](https://en.wikipedia.org/wiki/Shoelace_formula#Definition).
May I get plus points for a golf that you actually would implement like that? :D
-1, there is no need for a space behind `x,y:`.
---
[Answer]
# R, ~~54~~ 52 bytes
```
pryr::f({for(i in 2:nrow(x))F=F+det(x[i-1:0,]);F/2})
```
Which evaluates to the function:
```
function (x) 
{
    for (i in 2:nrow(x)) F = F + det(x[i - 1:0, ])
    F/2
}
```
Makes use of the predefined `F = FALSE = 0`. Implements the shoelace algorithm in the linked video :)
-2 bytes thanks to Giuseppe
[Answer]
# [Mathics](http://mathics.github.io/), 31 bytes
```
Total[Det/@Partition[#,2,1]/2]&
```
[Try it online!](https://tio.run/##y00sychMLv6fpmCr8D8kvyQxJ9oltUTfISCxqCSzJDM/L1pZx0jHMFbfKFbtf0BRZl5JdFp0dbWJjkmtTrWBjiGQ1DXSMQVRZjoGIMpQRxckZ6qjawSkQAprY2O5EFoNwZogWg3BWkAiQDX/AQ "Mathics – Try It Online")
---
# Mathematica, 25 bytes
```
Tr@BlockMap[Det,#,2,1]/2&
```
[Answer]
# [Factor](https://factorcode.org/), 35 bytes
```
[ dup pick [ rest v. ] 2bi@ - 2 / ]
```
[Try it online!](https://tio.run/##VYyxCsIwGIR3n@JeINGE6qCLm7i4iFPpEOMvhsYak7Qg0mePSTvJwc/dfcd/Vzq@fLqcj6fDFi35jiyeKj6mwwcqOMB5ivHjvOkiAr176jQF7FKNW@/gjG5Rw1OIGDgayKvZg0FiiSZ9UWEFJsE2YALrHMdFKYvPoCosd/MbrawFz1xkJrLGyc/pf6MtKZ9@ "Factor – Try It Online")
Takes two arrays of x- and y-coordinates on the top of the stack, and returns the area as an integer or a rational.
Exploits the vector dot product `v.` which ignores excess elements on either side, so calculating one side of the shoelace is as simple as "remove the first element from the top array, and compute the dot product". And we can reuse it to evaluate the other side of the shoelace by simply swapping the two input arrays.
Honorable mention goes to the Apply combinator `2bi@`, which lets us simplify multi-application of the inner function.
```
Input:
{ 4 0 -2 -6 -1  5 4 }
{ 4 1  5  0 -4 -2 4 }
Remove the first element from top:
{ 4 0 -2 -6 -1 5 4 }
{ 1 5  0 -4 -2 4 }
Evaluate the dot product (sum of elementwise product, ignoring any excess element)
[                  ! Input: ( x y )
  dup pick         ! Copy stack items to form ( x y y x )
  [ rest v. ]      ! The function that evaluates one side of shoelace
  2bi@             ! Apply the above to ( x y ), then to ( y x )
  - 2 /            ! Subtract the two and halve
]
```
[Answer]
# JS (ES6), ~~98~~ ~~95~~ ~~94~~ ~~93~~ ~~88~~ ~~86~~ ~~82~~ ~~81~~ ~~77~~ 73 bytes
```
(X,Y)=>{for(i in X){a+=(X[i]+X[i-1])*(Y[i]-Y[i-1]);if(!+i)a=0}return a/2}
```
Takes input like `[x1, x2, x3], [y1, y2, y3]`, and skips the repeated coord pair.
-3 bytes thanks to @JarkoDubbeldam
-4 bytes thanks to @JarkoDubbeldam
-1 byte thanks to @ZacharyT
-4 bytes thanks to @ZacharyT
-4 bytes thanks to @Rick
[Answer]
# J, 12 bytes
Assuming the input is a list of 2 element lists (ie, a table)
```
-:+/-/ .*2[\
```
* `2[\` -- breaks it down into the shoelace Xs, ie, overlapping squares of 4 elms
* `-/ .*` -- the determinant of each
* `+/` -- sum it
* `-:` -- divide by 2
If we get the input as a single list, we need to first transform into a table, giving us 20 bytes:
```
-:+/-/ .*2[\ _2&(,\)
```
[Answer]
# MS-SQL, 66 bytes
```
SELECT geometry::STPolyFromText('POLYGON('+p+')',0).STArea()FROM g
```
MS SQL 2008 and higher support Open Geospatial Consortium (OGC)-standard spatial data/functions, which I'm taking advantage of here.
Input data is stored in field *p* of pre-existing table *g*, [per our input standards](https://codegolf.meta.stackexchange.com/a/5341/70172).
Input is a text field with ordered pairs in the following format: `(4 4,0 1,-2 5,-6 0,-1 -4,5 -2,4 4)`
Now just for fun, if you allowed my *input* table to hold Open Geospatial Consortium-standard geometry objects (instead of just text data), then it becomes almost trivial:
```
--Create and populate input table, not counted in byte total
CREATE TABLE g (p geometry)
INSERT g VALUES (geometry::STPolyFromText('POLYGON((5 5, 10 5, 10 10, 5 5))', 0))
--23 bytes!
SELECT p.STArea()FROM g
```
[Answer]
# [Haskell](https://www.haskell.org/), 45 bytes
```
(a:b:c)?(d:e:f)=(a*e-d*b)/2+(b:c)?(e:f)
_?_=0
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/XyPRKskqWdNeI8Uq1SpN01YjUStVN0UrSVPfSFsDIgMS54q3j7c1@J@bmJmnYKtQUJSZV6KgohBtomOgo2uko2umo2uoY6pjEqtgDxIEsYESJiA5k9j/AA "Haskell – Try It Online")
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 14 bytes
```
2÷⍨1⊥2-/⍤×∘⌽/⊢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/3@jw9ke9KwwfdS010tV/1Lvk8PRHHTMe9ezVf9S16H/ao7YJj3r7HnU1P@pd86h3y6H1xo/aJj7qmxoc5AwkQzw8g/@naZgomGgqKGgYKBiCqEPrjRRMIQwzBQMIw1Dh0HqwGlMgwwjEAOsBAA "APL (Dyalog Extended) – Try It Online")
Same formula as in Arnauld's answer.
Takes input as a list of points, sorted in counterclockwise order.
## Explanation
```
2÷⍨1⊥2-/⍤×∘⌽/⊢
             ⊢ take the input (list of pts)
     2      /  reduce overlapping pairs by the following function:
         ×       multiply the first
          ∘⌽     with the second reversed
      -/⍤        reduce the multipled pair by subraction
   1⊥          sum the resultant array (using decoding)
2÷⍨            halve it
```
[Answer]
# PARI/GP 61 bytes
```
c=concat;s(z,v=1)=c(z,z)[^#x*2]+c(z[^1],z)*v
s(x,-1)*s(y)~/4
```
assumes coordinates given by two vectors x=[x1,...xn], y=[y1,...,yn]
## Test cases
```
A(x,y)={my(c=concat,s(z,v=1)=c(z,z)[^#x*2]+c(z[^1],z)*v);s(x,-1)*s(y)~/4};
A([1, 0, 1, 1],[1, 1, 0, 1])
1/2
A([4,0,-2,-6,-1,5,4],[4,1,5,0,-4,-2,4])
55
```
[Answer]
# [PHP](https://php.net/), 76 75 73 bytes
Well, i probably shoot myself in the knee with PHP and his $ prefix for variable but i just wanna try the challenge with it anyway.
Header just remove error reporting for conviniance and format input as 2 array like it's allowed in post... i was just lazzy to retype it myself XD. (you can test both case by switching var in the for loop)
Edit: Figure out i can pass 2 arg to for() and save carac from while keyword with the same logic
Edit : Saved 2 bytes by reverse the iteration order and use juste `$i` for condition, saved `<0` (need to substract for 2nd option since result will be negative otherwise, but same bytes length)
```
for($i=count($x);$i;){$s-=$x[$j=$i--]*$y[$i]-$y[$j]*$x[$i];}$s=abs($s)/2;
```
[Try it online!](https://tio.run/##TY7BbsMgEETvfMUe9gAVKMZKeiGoH4JQ5bq0JoeAwJUcRfl2ByiVupedHc0bbVzifn6LSyQupZDek4shrf76TQemCK4urxI0GHPkcLQczMBB1i1GDqcmXjkMTUgOomVORYxVNMj2nrH1yM7/9chON79ECW41V6FbF18huWlegPZ3pgw4h5A@GdwJlMHN2JL9Nc1QkObe/ruyuA@yly6KXs/h57pS3JhCr9gds9ClBC8avRD2paDorajrUq6tXuqBWU8fmWJmh1Htbl4CYFb7Ew "PHP – Try It Online")
# [PHP](https://php.net/), 67 66 64 bytes
Solution without the `abs()` who work on both given case but feel like cheat since can return negative values in some cases ^^'
```
for($i=count($x);$i;){$s-=($x[$j=$i--]*$y[$i]-$y[$j]*$x[$i])/2;}
```
[Try it online!](https://tio.run/##TYzBbsMgEETvfMUe9mAqUIyV9EKsfAhCVeTQmhwCwo7kyPK3uwulUvayM6N5E8e4ny9xjMylFNJXcjGk2T9@mpZrhrObZgU9GHMUcLQCTCtA5S87AaciPgW0RSgBsnROJLosCmTrTld2VOX/d1SlS05VhkvuZehVxXdI7jqM0NSZ6wQ4hJBuHFYGdLgYS92/0LSElPT1nipKN7bTVoO@H8LzMTe4cI1e8xUn2ZMzeO/RS2k/iEVvZX53ckt2/NDpbXfDGAAnvf8C "PHP – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-pa`, 62 bytes
```
map$\+=$F[$i]*($a[($i+1)%@a]-$a[$i++-1]),@a=eval<>}{$\=abs$\/2
```
[Try it online!](https://tio.run/##K0gtyjH9/z83sUAlRttWxS1aJTNWS0MlMVpDJVPbUFPVITFWF8gDcrR1DWM1dRwSbVPLEnNs7GqrVWJsE5OKVWL0jf7/N9FRMNBR0DUCYjMgNtRRMNVRMOECCkOYIEkTiAKTf/kFJZn5ecX/dX1N9QwMDf7rFiQCAA "Perl 5 – Try It Online")
Takes input as a list of X coordinates on the first line followed by a list of Y coordinates on the second.
[Answer]
# [R](https://www.r-project.org/), ~~51~~ 46 bytes
*Edit: save 5 more bytes since no need to take `abs()` if points always encircle the polygon in counter-clockwise fashion*
```
pryr::f(sum(x*(1:0-.5)*x[2:1,c(2:ncol(x),1)]))
```
[Try it online!](https://tio.run/##dU/LCsMgELz7FUIvGtYSxfQg5EtKKTZN04DRYBKafL1VSB@klIVl9jGzOz70ziyNs2fta12G3i9eqRsZpo7MGeEqZ/uCZvNRKA4VEcpWzpCZAqcnSsPoW20bU/Oy0xHPpCI5pBDAOPAEKQiKdzjJY45eBPFL2Oybwa0kfJlGfNNVaxs83mvsYvL4oZc4d5O9otXCR1KCjFIcmIAC2CHi@A2TqRBxJNMN9G2cvI386W/314KiEJ4 "R – Try It Online")
After 3 years, ~~1~~ 6 bytes shaved from the previously-best [R](https://www.r-project.org/) solution!
[Answer]
# [Python 3](https://docs.python.org/3/), 58 bytes
```
lambda x,y:sum(x[i-1]*(j-y[i-2])/2for i,j in enumerate(y))
```
[Try it online!](https://tio.run/##FYxBCsMgFESv4vIrXxpt0kUhJ7EuLFVqqEasgdjLW93MG3jMpFree7w2tz7ax4Tny5AT6/17BDiV50Iz2HjtTWp6kW7PxONGfCQ2HsFmUyxUSlvKPhZwwH4@AVNqxlmjmlD05BKXgRtOAwL5cAv2S0379g8 "Python 3 – Try It Online")
Implementation of the shoelace algorithm.
[Answer]
# Excel, 97 bytes
```
=LET(a,A1#,x,ROW(a),y,{2,1},b,IF(x=ROWS(a),INDEX(a,1,y),INDEX(a,x+1,y)),SUM(MMULT(a*b,{1;-1}))/2)
```
LET and dynamic arrays were not Excel features in 2017.
]