PointPairArea { public static double computeArea(Point a, Point b) { if (a.getTimestamp() == b.getTimestamp()) { return 0; } if (a.getTimestamp() > b.getTimestamp()) { return computeArea(b, a); } final double x1 = a.getTimestamp(); final double x2 = b.getTimestamp(); final double y1 = a.getValue(); final double y2 = b.getValue(); if (sameSign(y1, y2)) { final double area = areaPositivePoints(x1, Math.abs(y1), x2, Math.abs(y2)); return Math.copySign(area, y1); } else { final double interceptsX = computeInterceptsX(x1, x2, y1, y2); final double area1 = areaPositivePoints(x1, Math.abs(y1), interceptsX, 0); final double area2 = areaPositivePoints(interceptsX, 0, x2, Math.abs(y2)); return Math.copySign(area1, y1) + Math.copySign(area2, y2); } }  static double computeArea(Point a, Point b);  }
@Test public void testComputePositivePoints() throws Exception { final Point a = new Point(2, 4); final Point b = new Point(4, 4); assertEquals(8.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputePositivePointsTriangle() throws Exception { final Point a = new Point(3, 2); final Point b = new Point(5, 0); assertEquals(2.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputePositivePointsRectangleAndTriangle() throws Exception { final Point a = new Point(2, 2); final Point b = new Point(7, 4); assertEquals(15.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputeNegativePoints() throws Exception { final Point a = new Point(2, -4); final Point b = new Point(4, -4); assertEquals(-8.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputeNegativePointsTriangle() throws Exception { final Point a = new Point(3, -2); final Point b = new Point(5, 0); assertEquals(-2.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputeNegativePointsRectangleAndTriangle() throws Exception { final Point a = new Point(2, -2); final Point b = new Point(7, -4); assertEquals(-15.0, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputePositiveAndNegativePoints() throws Exception { final Point a = new Point(3, 2); final Point b = new Point(6, -1); assertEquals(1.5, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputeNegativeAndPositivePoints() throws Exception { final Point a = new Point(3, -2); final Point b = new Point(6, 1); assertEquals(-1.5, PointPairArea.computeArea(a, b), DELTA); }
@Test public void testComputeZeroPoints() throws Exception { final Point a = new Point(3, 0); final Point b = new Point(6, 0); assertEquals(0, PointPairArea.computeArea(a, b), DELTA); }