# Area of Polygon [duplicate]

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.

• What order are the vertices given in? Can we assume the polygon is always convex? Nov 1, 2015 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, 2015 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. Nov 2, 2015 at 1:25
• Sounds like a good challenge, but bad written. Nov 3, 2015 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? Apr 16, 2016 at 17:46

# Mathematica, 29 bytes

RegionMeasure@*ConvexHullMesh


Example usage:

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

• perfect tool for the job
Nov 1, 2015 at 7:12
• @phase Even so, I know I'm still going to be beat by the golfing languages. =) Nov 1, 2015 at 7:15
• Someone needs to make Pyth for Mathematica
Nov 1, 2015 at 7:16
• @phase Hmmm what a good idea ;) Nov 1, 2015 at 7:39
• Area@*Polygon Nov 1, 2015 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[0]),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. Nov 1, 2015 at 7:27
• @hotrod Alright, one second. Nov 1, 2015 at 7:28
• @hotrod Done. See the explanation. Nov 1, 2015 at 8:09
• namespace is not needed and Program could be P Nov 1, 2015 at 14:23
• and surely yod don't need console.read() at the end Nov 1, 2015 at 14:34