# Area of Polygon [duplicate]

This question already has an answer here:

Given the number of vertices N of a convex polygon and each of their (x, y) calculate the polygon's area. vertexes are in clockwise order

You must handle all N <= 45 ,-600 <= x,y <= 600, and your answer must be accurate to at least 2 decimal places.

Example:

3
1 6
1 2
-4 2


The area is 10.00.

This is , fewest bytes wins. Input to be taken in code.

## marked as duplicate by Peter Taylor code-golf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Nov 6 '15 at 20:11

• What order are the vertices given in? Can we assume the polygon is always convex? – lirtosiast Nov 1 '15 at 6:49
• Does the input have to be taken in the format of the example? There's no reason for the scripting languages only restriction, and I don't even know what that means. – xnor Nov 1 '15 at 17:50
• "Code in scripting languages only" is subjective (what qualifies as a scripting language?) and likely excludes many of the languages commonly used on the site. It's an arbitrary and unnecessary condition. – Alex A. Nov 2 '15 at 1:25
• Sounds like a good challenge, but bad written. – Akangka Nov 3 '15 at 10:37
• @PeterTaylor Why is that? Isn't determining the area of a complex polygon much simpler than determining that of a self-intersecting polygon with even-odd area counting rule? – Martin Ender Apr 16 '16 at 17:46

# Mathematica, 29 bytes

RegionMeasure@*ConvexHullMesh


Example usage:

(RegionMeasure@*ConvexHullMesh)[{{1, 6}, {1, 2}, {-4, 2}}]
(* 10. *)

• perfect tool for the job – phase Nov 1 '15 at 7:12
• @phase Even so, I know I'm still going to be beat by the golfing languages. =) – 2012rcampion Nov 1 '15 at 7:15
• Someone needs to make Pyth for Mathematica – phase Nov 1 '15 at 7:16
• @phase Hmmm what a good idea ;) – Beta Decay Nov 1 '15 at 7:39
• Area@*Polygon – alephalpha Nov 1 '15 at 8:39

# JavaScript (ES6) 131

f=l=>(p=l.pop(t=Math.atan2),l.sort((a,b)=>t(a.y-p.y,a.x-p.x)-t(b.y-p.y,b.x-p.x)),[...l,p].map(t=>(v+=p.x*t.y-p.y*t.x,p=t),v=0),v/2)

u=l=>(
t=Math.atan2,
p=l.pop(),
l.sort((a,b)=>t(a.y-p.y,a.x-p.x)-t(b.y-p.y,b.x-p.x)),
v=0,
[...l,p].map(t=>(v+=p.x*t.y-p.y*t.x,p=t)),
v/2
)

//TEST
console.log=x=>O.innerHTML+=x+'\n'

T=[{x:1,y:6},{x:1,y:2},{x:-4,y:2}] // area 10
console.log(JSON.stringify(T)+' '+f(T))
T=[{x:12,y:12},{x:2,y:2},{x:4,y:10},{x:11,y:2}] // area 75
console.log(JSON.stringify(T)+' '+f(T))
<pre id=O></pre>

## C# - 268

with all the output handling included, yup this ain't gonna win

using System;class P{static void Main(string[]a){int i,v=int.Parse(a),j=v-1;var x=new float[v];var y=new float[v];for(i=0;i++<v;){x[i-1]=float.Parse(a[i]);y[i-1]=float.Parse(a[i+v]);}fl‌​oat s=0;for(i=0;i<v;j=i++)s+=(x[j]+x[i])*(y[j]-y[i]);Console.Write(s/2);}}


Program reads input from the string array args. Example input: 3 1 1 -4 6 2 2. Output: 10

First number is the number of vertices, all the rest are X & Y coordinates. After vertice number input all x coordinates, then all y coordinates, each one separated by whitespace.

I'm not fully certain if I need to include using, namespace, main void etc... but I included them anyway.

EDIT: Saved some bytes, thanks to @edc65 !

• You need to take the input in code. – hotrod Nov 1 '15 at 7:27
• @hotrod Alright, one second. – Yytsi Nov 1 '15 at 7:28
• @hotrod Done. See the explanation. – Yytsi Nov 1 '15 at 8:09
• namespace is not needed and Program could be P – edc65 Nov 1 '15 at 14:23
• and surely yod don't need console.read() at the end – edc65 Nov 1 '15 at 14:34