#include "repere.h"
#include <QDebug>


repere::repere(QObject *parent) :
    QObject(parent)
{
}
//--------------------------------------------------------------------------------------------------
repere::repere(QObject *parent,QGraphicsView* G,double xmin1, double xmax1,double ymin1, double ymax1) :
    QObject(parent)
{    Scene=new QGraphicsScene(this);
     G->setScene(Scene);
     xmin=xmin1; xmax=xmax1; ymin=ymin1; ymax=ymax1;
     Scene->setSceneRect(0,0,G->geometry().width()-3,G->geometry().height()-3);
     DrawBox(xmin,xmax,ymin,ymax,QPen(Qt::red),QBrush(Qt::NoBrush));
}
//--------------------------------------------------------------------------------------------------
double repere::xToPix(double x)
{   double echx = Scene->width()/(xmax-xmin);
    return (x-xmin)*echx;
}
//--------------------------------------------------------------------------------------------------
double repere::yToPix(double y)
{
    double echy = Scene->height()/(ymax-ymin);
    return Scene->height()-(y-ymin)*echy;
}
//--------------------------------------------------------------------------------------------------
void repere::Center(double x1,double y1)
{   double Rx=(xmax-xmin)/2.0;
    double Ry=(ymax-ymin)/2.0;
    xmin=x1-Rx;    xmax=x1+Rx;
    ymin=y1-Ry;    ymax=y1+Ry;
}
//--------------------------------------------------------------------------------------------------
void repere::Zoom(double z)
{   double rx=(xmax-xmin)/2.0;
    double ry=(ymax-ymin)/2.0;
    double cx=(xmax+xmin)/2.0;
    double cy=(ymax+ymin)/2.0;
    xmin=cx-rx*z;    xmax=cx+rx*z;
    ymin=cy-ry*z;    ymax=cy+ry*z;
}
//--------------------------------------------------------------------------------------------------
void repere::Clean()
{
    Scene->clear();
}

//--------------------------------------------------------------------------------------------------
void repere::DrawBox(double xmin,double xmax,double ymin,double ymax, QPen pen1, QBrush brush1)
{       QPolygonF box1;
        box1 << QPointF(xToPix(xmin),yToPix(ymin))<< QPointF(xToPix(xmax),yToPix(ymin))
             << QPointF(xToPix(xmax),yToPix(ymax))<< QPointF(xToPix(xmin),yToPix(ymax));
        QGraphicsPolygonItem *P;
        P=Scene->addPolygon(box1,pen1,brush1);
 }
//--------------------------------------------------------------------------------------------------
void repere::DrawArrow(double x1,double y1,double dx,double dy, double r, QPen pen1)
{       QPolygonF Arrow1;
        double x2=x1+dx;
        double y2=y1+dy;
        double a=3.14-1.0;
        double px=x2+r*(cos(a)*dx-sin(a)*dy);    // coté de pointe
        double py=y2+r*(sin(a)*dx+cos(a)*dy);
        double qx=x2+r*(cos(-a)*dx-sin(-a)*dy);  // autre coté de pointe
        double qy=y2+r*(sin(-a)*dx+cos(-a)*dy);
        Scene->addLine(xToPix(x1),yToPix(y1),xToPix(x2),yToPix(y2),pen1);
        Scene->addLine(xToPix(x2),yToPix(y2),xToPix(px),yToPix(py),pen1);
        Scene->addLine(xToPix(x2),yToPix(y2),xToPix(qx),yToPix(qy),pen1);

 }
//--------------------------------------------------------------------------------------------------
void repere::DrawEllipse(double cx, double cy, double r, QPen pen1, QBrush brush1)
{
     Scene->addEllipse(xToPix(cx-r),yToPix(cy+r),xToPix(cx+r)-xToPix(cx-r),yToPix(cy-r)-yToPix(cy+r),pen1,brush1);
}
//--------------------------------------------------------------------------------------------------
void repere::DrawLine(double x1,double y1,double x2,double y2, QPen pen1)
{       Scene->addLine(xToPix(x1),yToPix(y1),xToPix(x2),yToPix(y2),pen1);
}
//--------------------------------------------------------------------------------------------------
void repere::DrawPolygone(double x,double y,double theta,vector<double> X, vector<double> Y, QPen pen1, QBrush brush1)
{       QPolygonF poly1;
        for (int k=0;k<X.size();k++)
        {
            double x1=x+cos(theta)*X[k]-sin(theta)*Y[k];
            double y1=y+sin(theta)*X[k]+cos(theta)*Y[k];
            poly1 << QPointF(xToPix(x1),yToPix(y1));
        }
        QGraphicsPolygonItem *P;
        P=Scene->addPolygon(poly1,pen1,brush1);
 }
//--------------------------------------------------------------------------------------------------
void repere::DrawRobot(double x,double y,double theta)
{   vector<double> X,Y;
    X.push_back(0); Y.push_back(-1);
    X.push_back(3); Y.push_back(0);
    X.push_back(0); Y.push_back(1);
    DrawPolygone(x,y,theta,X,Y, QPen(Qt::blue), QBrush(Qt::black));
}
//--------------------------------------------------------------------------------------------------
void repere::DrawSailboat(double x,double y,double theta,QColor colr,double deltag,QColor colg, double deltav,QColor colv)

{   double a=0.8;
    vector<double> Xc,Yc;                //coque
    Xc.push_back(-a);  Yc.push_back(-a);
    Xc.push_back(4*a); Yc.push_back(0);
    Xc.push_back(-a);  Yc.push_back(a);
    Xc.push_back(-a);  Yc.push_back(-a);
    DrawPolygone(x,y,theta,Xc,Yc,colr, colr);

    vector<double> Xr,Yr;  //gouvernail
    Xr.push_back(-2*a);  Yr.push_back(0);
    Xr.push_back(0);   Yr.push_back(0);
    DrawPolygone(x-a*cos(theta),y-a*sin(theta),theta+deltag,Xr,Yr,QPen(colg),QBrush(colg));

    vector<double> Xv,Yv;  //voile
    Xv.push_back(-5*a);  Yv.push_back(0);
    Xv.push_back(0);     Yv.push_back(0);
    DrawPolygone(x+3*a*cos(theta),y+3*a*sin(theta),theta+deltav,Xv,Yv,colv,colv);
}


//--------------------------------------------------------------------------------------------------
void repere::DrawText(QString s, int n)
{       for (int k=1;k<=n;k++)
        s="\r\n"+s;
        //Pour la syntaxe:  int a; double p; s=QString("a=%1,\r\n p=%2").arg(k).arg(p);
        Scene->addText(s);
}
//--------------------------------------------------------------------------------------------------
void repere::Save(QString nom)
{
    QImage *image = new QImage(QSize(1000,1000),QImage::Format_ARGB32);
    image->fill(QColor(Qt::white).rgb());
    QPainter *pngPainter = new QPainter(image);
    pngPainter->setRenderHint(QPainter::Antialiasing);
    Scene->render(pngPainter);
    pngPainter->end();
    image->save(nom+".png","PNG",100);
}
//--------------------------------------------------------------------------------------------------

