#include "QtLturtle.hpp"

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