Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. public class Polygon
  2. {
  3.    
  4.     int ncorners = 0;
  5.     Point[] points;
  6.    /**
  7.       Constructs a Polygon object with no corners
  8.    */
  9.    public Polygon()
  10.    {
  11.        points = new Point[0];
  12.    }
  13.    
  14.    /**
  15.       Adds a point to the list.
  16.       @param p the point to add
  17.    */
  18.    public void add(Point p)
  19.    {
  20.        points = addToArray(points, p);
  21.        ncorners++;
  22.    }
  23.    
  24.    /**
  25.       Computes the area of a polygon.
  26.       @return area of a polygon
  27.    */
  28.    public double getArea()
  29.    {
  30.       if (ncorners < 3) return 0;
  31.       if (ncorners == 3) {
  32.           return getTriangleSize(points);
  33.       }else {
  34.  
  35.          
  36.           Point startingPoint = points[0];
  37.  
  38.          
  39.          
  40.       }
  41.    }
  42.    
  43.    private double getTriangleSize(Point[] pointArray) {
  44.        
  45.        //Just making the triangle calculation algorithm easier to type.
  46.        double x1 = pointArray[0].getX();
  47.        double y1 = pointArray[0].getY();
  48.        double x2 = pointArray[1].getX();
  49.        double y2 = pointArray[1].getY();
  50.        double x3 = pointArray[2].getX();
  51.        double y3 = pointArray[2].getY();
  52.        
  53.        //Algorithm provided by assignment.
  54.        return ((x1*y2) + (x2*y3) + (x3*y1) - (y1*x2) - (y2*x3) - (y3*x1)) / 2;
  55.    }
  56.    
  57.    private Point[] addToArray(Point[] array, Point newPoint) {
  58.        Point[] point = new Point[array.length + 1];
  59.        for(int i = 0 ; i < array.length; i++) {
  60.            point[i] = array[i];
  61.        }
  62.        point[point.length-1] = newPoint;
  63.        return point;
  64.    }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement