1
\$\begingroup\$

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.

\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Nov 6 '15 at 20:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 8
    \$\begingroup\$ What order are the vertices given in? Can we assume the polygon is always convex? \$\endgroup\$ – lirtosiast Nov 1 '15 at 6:49
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Nov 1 '15 at 17:50
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ – Alex A. Nov 2 '15 at 1:25
  • 1
    \$\begingroup\$ Sounds like a good challenge, but bad written. \$\endgroup\$ – Akangka Nov 3 '15 at 10:37
  • 1
    \$\begingroup\$ @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? \$\endgroup\$ – Martin Ender Apr 16 '16 at 17:46
6
\$\begingroup\$

Mathematica, 29 bytes

RegionMeasure@*ConvexHullMesh

Example usage:

(RegionMeasure@*ConvexHullMesh)[{{1, 6}, {1, 2}, {-4, 2}}]
(* 10. *)
\$\endgroup\$
  • \$\begingroup\$ perfect tool for the job \$\endgroup\$ – phase Nov 1 '15 at 7:12
  • \$\begingroup\$ @phase Even so, I know I'm still going to be beat by the golfing languages. =) \$\endgroup\$ – 2012rcampion Nov 1 '15 at 7:15
  • 2
    \$\begingroup\$ Someone needs to make Pyth for Mathematica \$\endgroup\$ – phase Nov 1 '15 at 7:16
  • 1
    \$\begingroup\$ @phase Hmmm what a good idea ;) \$\endgroup\$ – Beta Decay Nov 1 '15 at 7:39
  • 6
    \$\begingroup\$ Area@*Polygon \$\endgroup\$ – alephalpha Nov 1 '15 at 8:39
2
\$\begingroup\$

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)

// More readable
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>

\$\endgroup\$
1
\$\begingroup\$

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 !

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

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