#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

string f(double x, double y)
{
   ostringstream s;
   double r= hypot(x,y);
   r= (r==0) ? 1 : sin(r)/r;
   r= 5*r;
   s << "<" << x << "," << r << "," << y << ">";
   return s.str();
}

string normal(double x, double y)
{
   ostringstream s;
   double r= hypot(x,y);
   double nx, ny, nz;

   if (r==0)
      {
       nx= 0;
       ny= 0;
       nz= -1;
      }
   else
      {
       nx= (x*cos(r))/(r*r) - (x*sin(r))/(r*r*r);
       ny= (y*cos(r))/(r*r) - (y*sin(r))/(r*r*r);
       nz= -1;
      }
   nx*= 5;
   ny*= 5;
   nz*= 5;
   s << "<" << nx << "," << nz << "," << ny << ">";
   return s.str();
}

int main(void)
{
    double a, b, x, hx;
    int    n;
    double c, d, y, hy;
    int    m;

    a= -3*M_PI;
    b=  3*M_PI;
    n= 30;
    hx = (b-a) / n;

    c= - 3*M_PI;
    d=   3*M_PI;
    m= 30;
    hy = (d-c) / m;

    cout << "#include \"colors.inc\"\n";
    cout << "light_source { <0, -30, -5> color White }\n";
    cout << "light_source { <0,  30, -5> color White }\n";
    cout << "camera { location <0,5,-20> direction z look_at <0,0,0> }\n";
    cout << "#declare green_plastic =\n"
         << "   texture {\n"
         << "   pigment { color rgb <0, 1, 0> }\n"
         << "   finish {ambient 0.1 diffuse 0.8 phong 0.5 phong_size 100 }\n"
         << "}\n";
    cout << "#declare white_plastic =\n"
         << "   texture {\n"
         << "   pigment { color rgb <1, 1, 1> }\n"
         << "   finish {ambient 0.1 diffuse 0.8 phong 0.5 phong_size 100 }\n"
         << "}\n";



    
    for (int i=0; i<n; ++i)
       {
        double x = a + i*hx;
        double x1= x + hx;
        for (int j=0; j<m; ++j)
           {
            double y= c+j*hy;
            double y1= y + hy;
            
            cout << "smooth_triangle {\n" 
                 << "   " << f(x,y) << ",\n"
                 << "   " << normal(x,y) << ",\n"
                 << "   " << f(x1,y) << ",\n"
                 << "   " << normal(x1,y) << ",\n"
                 << "   " << f(x1,y1) << ",\n"
                 << "   " << normal(x1,y1) << "\n"
                 << "   texture {\n"
                    "      green_plastic\n"
                    "   }\n"
                    "}\n";
             
            cout << "smooth_triangle {\n" 
                 << "   " << f(x,y)   << ",\n"
                 << "   " << normal(x,y)   << ",\n"
                 << "   " << f(x,y1)  << ",\n"
                 << "   " << normal(x,y1)  << ",\n"
                 << "   " << f(x1,y1) << "\n"
                 << "   " << normal(x1,y1) << "\n"
                 << "   texture {\n"
                    "      white_plastic\n"
                    "   }\n"
                    "}\n";
           }
       }

}

