/* File : plotsmileyface.cpp
   Plot a smiley face.
*/
#include <stdio.h>
#include <math.h>  
#include <chplot.h>

#define FACE_RADIUS 2.0
#define EYE_RADIUS 0.4
#define EYE_X_OFFSET 0.7
#define EYE_Y_OFFSET 1
#define MOUTH_RADIUS 1.0
#define MOUTH_Y_OFFSET 0.5
#define FACE_COLOR "yellow"
#define EYE_COLOR "blue"
#define MOUTH_COLOR "blue"

#define N  100
#define N2 200

int main() {
    array double x[N2], y[N2], x1[N], y1[N];
    CPlot plot;
    double x0, xf, r, step;
    int i, lineType=1, lineWidth=1;

    // for face
    x0 = -FACE_RADIUS;
    xf =  FACE_RADIUS;
    r  =  FACE_RADIUS;
    step = (xf-x0)/(N-1);
    for (i=0;i<N ;i++ ) {
        x[i] = x0 + i*step;
        y[i] = sqrt(r*r - x[i]*x[i]);
        x[i+N] = xf - i*step;   // or x[i+N] = -x[i];
        y[i+N] = -y[i];
    }
    plot.data2DCurve(x, y, N2);
    plot.lineType(0, lineType, lineWidth, FACE_COLOR);
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 0, "closed");

    // for right eye 
    x0 = -EYE_RADIUS+EYE_X_OFFSET;
    xf =  EYE_RADIUS+EYE_X_OFFSET;
    r  =  EYE_RADIUS;
    step = (xf-x0)/(N-1);
    for (i=0;i<N;i++ ) {
        x[i] = x0 + i*step;
        y[i] = sqrt(r*r- (x[i]-EYE_X_OFFSET)*(x[i]-EYE_X_OFFSET))+EYE_Y_OFFSET;
        x[i+N] = xf - i*step;   // or x[i+N] = -x[i];
        y[i+N] = -sqrt(r*r- (x[i]-EYE_X_OFFSET)*(x[i]-EYE_X_OFFSET))+EYE_Y_OFFSET;
    }
    plot.data2DCurve(x, y, N2);
    plot.lineType(1, lineType, lineWidth, EYE_COLOR);
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 1, "closed");
   
    // for left eye 
    x0 = -EYE_RADIUS-EYE_X_OFFSET;
    xf =  EYE_RADIUS-EYE_X_OFFSET;
    r  =  EYE_RADIUS;
    step = (xf-x0)/(N-1);
    for (i=0;i<N;i++ ) {
        x[i] = x0 + i*step;
        y[i] = sqrt(r*r- (x[i]+EYE_X_OFFSET)*(x[i]+EYE_X_OFFSET))+EYE_Y_OFFSET;
        x[i+N] = xf - i*step;   // or x[i+N] = -x[i];
        y[i+N] = -sqrt(r*r- (x[i]+EYE_X_OFFSET)*(x[i]+EYE_X_OFFSET))+EYE_Y_OFFSET;
    }
    plot.data2DCurve(x, y, N2);
    plot.lineType(2, lineType, lineWidth, EYE_COLOR);
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 2, "closed");
    
    // for mouth 
    x0 = -MOUTH_RADIUS;
    xf =  MOUTH_RADIUS;
    r  =  MOUTH_RADIUS;
    step = (xf-x0)/(N-1);
    for (i=0;i<N;i++ ) {
        x1[i] = x0 + i*step;
        y1[i] = -sqrt(r*r- x1[i]*x1[i]) - MOUTH_Y_OFFSET;
    }
    plot.data2DCurve(x1, y1, N);
    plot.lineType(3, lineType, lineWidth, MOUTH_COLOR);
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 3, "closed");
    
    plot.sizeRatio(1.0);    
    plot.plotting();

    /* plot another one without labels, border, tics, and axes */
    plot.label(PLOT_AXIS_XY, NULL);
    plot.border(PLOT_BORDER_ALL, PLOT_OFF);
    plot.tics(PLOT_AXIS_XY, PLOT_OFF);
    plot.axis(PLOT_AXIS_XY, PLOT_OFF);
    plot.plotting();
    return 0 ;
}
