Advertisement
calcpage

APCS_CH8_Polygon.java

Feb 14th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.58 KB | None | 0 0
  1. //Polygon.java      MrG     2013.0208
  2. import java.util.ArrayList;
  3. public class Polygon
  4. {
  5.     private ArrayList<Point> vertices;
  6.  
  7.     public Polygon()
  8.     {
  9.         vertices = new ArrayList<Point>();
  10.     }
  11.  
  12.     public void add(Point p)
  13.     {
  14.         vertices.add(p);
  15.     }
  16.  
  17.     public String toString()
  18.     {
  19.         return "" + vertices;
  20.     }
  21.  
  22.     public double getArea()
  23.     {
  24.         int stop=vertices.size();
  25.         double sum=0;
  26.         for(int i=0; i<stop;i++)
  27.         {
  28.             sum+=vertices.get(i).getX()*vertices.get((i+1)%stop).getY();
  29.             sum-=vertices.get((i+1)%stop).getX()*vertices.get(i).getY();
  30.         }
  31.         return sum/2;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement