/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: n_gon.cc 10061 2011-03-16 12:46:53Z herr $
*/

#include "polymake/client.h"
#include "polymake/Matrix.h"
#include "polymake/AccurateFloat.h"

namespace polymake { namespace polytope {

perl::Object n_gon(int n, const Rational& r)
{
   if ((n < 3) || (r <= 0)) {
      throw std::runtime_error("n_gon: n >= 3 and r > 0 required\n");
   }

   perl::Object p("Polytope<Rational>");
   p.set_description() << n << "-gon with radius " << r << endl;
   
   Matrix<Rational> V(n,3);
   V.col(0).fill(1);

   V(0,1)=r;
   if (!(n%2)) {
      V(n/2,1)=-r;
      if (!(n%4)) {
         V(n/4,2)=r;
         V((3*n)/4,2)=-r;
      }
   }

   const int iend= n%2 ? (n+1)/2 : (n+2)/4;
   const AccurateFloat angle = (2 * AccurateFloat::pi()) / n;
   AccurateFloat c, s;
   Rational x, y;
   for (int i=1; i<iend; ++i) {
      sin_cos(s, c, i*angle);
      x=r*c;
      y=r*s;
      V(i,1)=x;
      V(i,2)=y;
      V(n-i,1)=x;
      V(n-i,2)=-y;
      if (!(n%2)) {
         V(n/2-i,1)=-x;
         V(n/2-i,2)=y;
         V(n/2+i,1)=-x;
         V(n/2+i,2)=-y;
      }
   }

   p.take("CONE_AMBIENT_DIM") << 3;
   p.take("CONE_DIM") << 3;
   p.take("VERTICES") <<  V;
   p.take("N_VERTICES") << n;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << true;
   return p;
}

UserFunction4perl("# @category Producing from scratch"
                  "# Produce a regular //n//-gon."
                  "# All vertices lie on a circle of radius //r//."
                  "# The radius defaults to 1."
                  "# @param Int n the number of vertices"
                  "# @param Rational r the radius"
                  "# @return Polytope",
                  &n_gon,"n_gon($;$=1)");

} }

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
