title: Perimeter for any polygon - java
body: <p>How can I efficiently find the perimeter of any given regular or irregular polygon?</p>
  
  <p>I have a list that contains the points of a polygon (at least it contains 3 points) so the polygons begin can be triangle, rectangle, pentagon, hexagon and so forth</p>
  
  <pre><code>protected List&lt;Point&gt; coordinates = new ArrayList&lt;Point&gt;();
  </code></pre>
  
  <p>My class Point is the following:</p>
  
  <pre><code>public class Point {
  
      private double _x_, _y_;
      public double y;
      public double x;
      public Point() {
          _x_ = 0;
          _y_ = 0;
      }
      public Point(double x, double y) { 
          setX(x); setY(y); 
      }
      public double getX() { return _x_; }
      public double getY() { return _y_; }
      public void setX(double x) { assert x&gt;0; _x_ = x; }
      public void setY(double y) { assert y&gt;0; _y_ = y; }
  
      public double dist (Point p) { 
          double dx = getX() - p.getX();
          double dy = getY() - p.getY();
          return Math.sqrt(dx*dx + dy*dy); 
      }
  
  
      public String toString() {
          return ((int)getX()+" "+(int)getY());
      }
  }
  </code></pre>
  
  <p>So I am looking for a general formula for the perimeter of any given polygon?
  Is this even possible ?</p>
  
  <p>I want to implement this on a abstract Polygon class that has subclasses triangle and rectangle, but i want a general formula for all polygons.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>public double perimeter() {
      double distance = 0;
      for(int i = 0; i &lt; coordinates.size(); i++) {
          for(int j = i+1; j &lt; coordinates.size(); j++) {
              if(j &gt; i) {
                  distance += coordinates.get(i).dist(coordinates.get(j));
              }
          }
      }
      return distance;
  }
  </code></pre>