;
(function () {
  if (typeof HyperbolicCanvas === "undefined") {
    window.HyperbolicCanvas = {};
  }

  var Angle = window.HyperbolicCanvas.Angle;
  var Point = window.HyperbolicCanvas.Point;
  var Line = window.HyperbolicCanvas.Line;
  var Circle = window.HyperbolicCanvas.Circle;

  var Polygon = window.HyperbolicCanvas.Polygon = function (options) {
    this.vertices = options.vertices;
  };

  Polygon.prototype.containsPoint = function (p) {
    // TODO save lines ?
    var lines = [];

    for (var i = 0; i < this.vertices.length; i++) {
      lines.push(Line.givenTwoPoints(this.vertices[i], this.vertices[(i + 1) % vertices.length]));
    }

    // draw ray from Point p to somewhere off the canvas
    // count intersections
    
  };

  Polygon.prototype.rotate = function (angle) {
    var newVertices = [];
    this.vertices.forEach(function (v) {
      var distance = Line.givenTwoPoints(Point.CENTER, v).euclideanDistance();
      var angle = Circle.UNIT.angleAt(v) + angle;
      newVertices.push(Point.givenPolarCoordinates(distance, angle));
    });
    return Polygon.givenVertices(newVertices);
  };

  Polygon.givenVertices = function (vertices) {
    if (vertices.length < 3) {
      return false;
    }

    for (var i = 0; i < vertices.length; i++) {
      if (!vertices[i].isOnPlane()) {
        return false;
      }
    }

    return new Polygon({ vertices: vertices });
  };

  Polygon.givenNCenterRadius = function (n, center, radius, rotation, inverse) {
    if (n < 3){
      return false;
    }
    if (!center.isOnPlane()) {
      return false;
    }
    rotation = Angle.normalize(rotation);

    var direction = rotation ? rotation * (inverse ? -1 : 1) : 0;
    var increment = Math.TAU / n;
    var vertices = [] ;

    for (var i = 0; i < n; i++) {
      vertices.push(center.distantPoint(radius, direction))
      direction += increment;
      direction = Angle.normalize(direction);
    }

    return new Polygon({ vertices: vertices });
  };
})();

@@ -453,297 +453,153 @@
     
-// TODO save lines ?%0A    var lines = %5B%5D;%0A%0A    for (var i = 0; i %3C this.vertices.length; i++) %7B%0A      lines.push(Line.givenTwoPoints(this.vertices%5Bi%5D, this.vertices%5B(i + 1) %25 vertices.length%5D));%0A    %7D%0A%0A    // draw ray from Point p to somewhere off the canvas%0A    // count intersections%0A    
+var windingNumber = null; // TODO calculate winding number%0A%0A    return windingNumber %3E 0; // TODO what about polygons that cross into themselves?
 %0A  %7D
@@ -799,58 +799,8 @@
 ();%0A
-      var angle = Circle.UNIT.angleAt(v) + angle;%0A
     
@@ -855,16 +855,28 @@
 istance,
+ v.angle() +
  angle))

fix Polygon.rotate