/*
____________________________________________________
|                                                  |
|          author => Martin Robinson               |
|                                                  |
|          you are authorised to use               |
|          this code in the way you                |
|          like, commercial, personal,             |
|          opensource or anything else             |
|          you could imagine. But you              |
|          are not authorised to own               |
|          copyright on this work                  |
|__________________________________________________|
____________________________________________________
|                                                  |
|           Turtle graphic like                    |
|           language C++                           |
|           Library Qt4.4                          |
|__________________________________________________|
*/
/*
todo
detect closed point
*/

#include "Lturtle.hpp"

void DrawLTurtle(QString * Lsystem, QQueue <QLineF> * queueline)
{
  QStack <QPointF> stpos;
  QStack <qreal> strad;
  
  stpos.push(QPointF(200.0, 400.0));
  strad.push(qreal(0.0));
  
  int start = (*Lsystem).lastIndexOf("}");
  int end = (*Lsystem).length();
  
  QPointF destination;
  for (int i = start+1; i< end; i++)
  {
    QChar instruction = (*Lsystem).at(i);
    if ((instruction == fwd)||(instruction == fwd2))
    {
      //draw forward
      destination = QPointF (stpos.top().x() + (qSin(strad.top())*magnitude), stpos.top().y() - (qCos(strad.top())*magnitude));
      (*queueline).enqueue(QLineF(stpos.top(),destination));
      stpos.top() = destination;
    }
    else if (instruction == nod)
    {
      //draw forward a leaf
      //temporary another line
      destination = QPointF (stpos.top().x() + (qSin(strad.top())*magnitude), stpos.top().y() - (qCos(strad.top())*magnitude));
      (*queueline).enqueue(QLineF(stpos.top(),destination));
      stpos.top() = destination;
    }
    else if (instruction == mov)
    {
      //move forward
      stpos.top().setX(stpos.top().x() + (qSin(strad.top())*magnitude));
      stpos.top().setY(stpos.top().y() - (qCos(strad.top())*magnitude));
    }
    else if (instruction==ccw)
    {
        strad.top() += rotate;
    }
    else if (instruction==cw)
    {
        strad.top() -= rotate;
    }
    else if (instruction==push)
    {
      stpos.push(stpos.top());
      strad.push(strad.top());
    }
    else if (instruction==pop)
    {
      stpos.pop();
      strad.pop();
    }
  }
  return;
}