Using recursion, compute the area of a polygon. Cut off a triangle and use the fact that a triangle with corners (x1, y1), (x2, y2), (x3, y3) has area

.

Complete the following files:

Polygon.java

import java.awt.geom.Point2D; import java.util.ArrayList; /** A polygon with a number of Point2D.Double corners */ public class Polygon { private ArrayList<Point2D.Double> corners; /** Constructs a Polygon object with no corners */ public Polygon() { corners = new ArrayList<Point2D.Double>(); } /** Adds a point to the list. @param p the point to add */ public void add(Point2D.Double p) { corners.add(p); } /** Computes the area of a polygon. @return area of a polygon */ public double getArea() { . . . } }

PolygonTester2.java

Use the following file:

PolygonTester.java

import java.awt.geom.Point2D;

/**
   A tester class for Polygon.
*/
public class PolygonTester
{
   public static void main(String[] args)
   {
      // square
      Polygon p = new Polygon();
      p.add(new Point2D.Double(10, 20));
      p.add(new Point2D.Double(20, 20));
      p.add(new Point2D.Double(20, 10));
      p.add(new Point2D.Double(10, 10));
      
      System.out.println("Area: " + p.getArea());
      System.out.println("Expected: 100.0");

      // regular hexagon with radius 1
      p = new Polygon();
      for (int i = 0; i < 6; i++)
         p.add(new Point2D.Double(Math.sin(i * Math.PI / 3),
               Math.cos(i * Math.PI / 3)));
      System.out.println("Area: " + p.getArea());
      System.out.println("Expected: " + 3 * Math.sqrt(3) / 2);      
   }
}