## Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.

## Solution

Create matrices of point pairs as follows

1 2 |
|x1 x2| |y1 y2| |

1 2 |
|x2 x3| |y2 y3| |

…

Calculate the *determinat* of each matrix:

`x1 * y2 - x2 * y1`

And sum the determinants. In the end divide the sum with 2.

*Solution based on this*.

I created solution in:

All solutions are also available on my GitHub.

**Scala**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import scala.io.Source object ComputePolygonArea extends App { val lines = Source.stdin.getLines().toList val n = lines.head.toInt val pairs = lines.tail.map(_.split(" ").map(_.toInt)).map(array => Tuple2(array(0), array(1))) println(area(pairs)) def area(points: List[(Int, Int)]) = { val n = points.length (0 until n).map(i => determinant(pairs(i % n), pairs((i + 1) % n))).sum / 2.0 } def determinant(p1: (Int, Int), p2: (Int, Int)) = { p1._1 * p2._2 - p1._2 * p2._1 } } |