| Bytes | Lang | Time | Link |
|---|---|---|---|
| 315 | Java 10 | 190415T110306Z | Kevin Cr |
| 055 | [R + sp package] | 190415T200130Z | Nick Ken |
| 189 | JavaScript ES6 | 190415T110204Z | Arnauld |
| 078 | R | 190415T135352Z | Kirill L |
| 027 | Wolfram Language Mathematica | 190415T001452Z | att |
| 084 | SQL Server 2012+ | 190414T224624Z | MickyT |
Java 10, 405 ...didn't fit anymore; see edit history.. 316 315 bytes
P->{int n=P.length,l=0,i=0,p,q,t[],h[][]=P.clone(),s=0;for(;++i<n;)l=P[i][0]<P[l][0]?i:l;p=l;do for(h[s++]=P[p],q=-~p%n,i=n;i-->0;q=(t[1]-P[p][1])*(P[q][0]-t[0])<(t[0]-P[p][0])*(P[q][1]-t[1])?i:q)t=P[i];while((p=q)!=l);for(p=i=0;i<s;p-=(t[0]+h[++i%s][0])*(t[1]-h[i%s][1]))t=h[i];return Math.round(.5*p/~(p%=2))*~p;}
-52 bytes thanks to @OlivierGrégoire
-3 bytes thanks to @PeterTaylor
-8 bytes thanks to @ceilingcat
Or 298 bytes without rounding...
Explanation:
There are three steps to do:
- Calculate the points for the Convex Hull based on the input-coordinates (using Jarvis' Algorithm/Wrapping)
- Calculate the area of this Convex Hull
- Banker's rounding..
To calculate the coordinates that are part of the Convex Hull, we use the following approach:
Set point \$l\$ and \$p\$ to the left-most coordinate. Then calculate the next point \$p\$ in a counterclockwise rotation; and continue doing so until we've reached back at the initial point \$l\$. Here a visual for this:
As for the code:
P->{ // Method with 2D integer array as parameter & long return-type
int n=P.length, // Integer `n`, the amount of points in the input
l=0, // Integer `l`, to calculate the left-most point
i=0, // Index-integer `i`
p, // Integer `p`, which will be every next counterclockwise point
q, // Temp integer `q`
t[], // Temp integer-array/point
h[][]=P.clone(), // Initialize an array of points `h` for the Convex Hull
s=0; // And a size-integer for this Convex Hull array, starting at 0
for(;++i<n;) // Loop `i` in the range [1, `n`):
l= // Change `l` to:
P[i][0]<P[l][0]? // If i.x is smaller than l.x:
i // Replace `l` with the current `i`
:l; // Else: leave `l` unchanged
p=l; // Now set `p` to this left-most coordinate `l`
do // Do:
for(h[s++]=P[p], // Add the `p`'th point to the 2D-array `h`
q=-~p%n, // Set `q` to `(p+1)` modulo-`n`
i=n;i-->0; // Loop `i` in the range (`n`, 0]:
;q= // After every iteration: change `q` to:
// We calculate: (i.y-p.y)*(q.x-i.x)-(i.x-p.x)*(q.y-i.y),
// which results in 0 if the three points are collinear;
// a positive value if they are clockwise;
// or a negative value if they are counterclockwise
(t[1]-P[p][1])*(P[q][0]-t[0])<(t[0]-P[p][0])*(P[q][1]-t[1])?
// So if the three points are counterclockwise:
i // Replace `q` with `i`
:q) // Else: leave `q` unchanged
t=P[i]; // Set `t` to the `i`'th Point (to save bytes)
while((p=q) // And after every while-iteration: replace `p` with `q`
!=l); // Continue the do-while as long as `p` is not back at the
// left-most point `l` yet
// Now step 1 is complete, and we have our Convex Hull points in the List `h`
for(p=i=0; // Set `p` (the area) to 0
i<s // Loop `i` in the range [0, `s`):
;p-= // After every iteration: Decrease the area `p` by:
(t[0]+h[++i%s][0])// i.x+(i+1).x
*(t[1]-h[i%s][1]))// Multiplied by i.y-(i+1).y
t=h[i]; // Set `t` to the `i`'th point (to save bytes)
return Math.round(.5*p/~(p%=2))*~p;}
// And return `p/2` rounded to integer with half-even
[R + sp package], 55 bytes
function(x)round(sp::Polygon(x[chull(x),,drop=F])@area)
A function which takes a n x 2 matrix and returns the rounded area. This uses the sp package. The drop=F is needed to handle the one co-ordinate case. RDRR used for demo since TIO lacks the sp package.
JavaScript (ES6), 191 189 bytes
Implements the Jarvis march (aka gift wrapping algorithm).
P=>(r=(g=p=>([X,Y]=P[p],Y*h-X*v)+(P.map(([x,y],i)=>q=(y-Y)*(P[q][0]-x)<(x-X)*(P[q][1]-y)?i:q,q=P[++p]?p:0,h=X,v=Y)|q?g(q):V*h-H*v))(v=h=0,([[H,V]]=P.sort(([x],[X])=>x-X)))/2)+(r%1&&r&1)/2|0
Or 170 bytes without the cumbersome rounding scheme.
R, 85 81 78 bytes
function(i,h=chull(i),j=c(h,h[1]))round((i[h,1]+i[j[-1],1])%*%diff(-i[j,2])/2)
Takes input as a 2-column matrix - first for x, second for y. R's round actually uses banker's rounding method, so we are quite lucky here.
The code uses a built-in function to determine, which points form the convex hull, and then applies the standard formula \$\sum_{i}{(x_{i-1}+x)\cdot(y_{i-1}-y_i)}/2\$ to get the polygon surface area.
Thanks to Giuseppe for -3 bytes.
SQL Server 2012+, 84 bytes
SELECT Round(Geometry::ConvexHullAggregate(Geometry::Point(x,y,0)).STArea(),0)FROM A
Makes use of the geometry functions and aggregates in SQL Server.
Coordindates are from table A with columns x and y.
