#include "draw2difs.hpp"
#include "ifs.hpp"
#include "point.hpp"

void Draw2DIFS(TGraph *g, TPointNode *FirstOld, TReadOnlyIFS s)
{
 TPointNode *CurrentOld;
 TPointNode *CurrentNew;
 TPointNode *FirstNew;

 FirstNew = new TPointNode;
 CurrentOld = FirstOld;
 CurrentNew = FirstNew;

 while (CurrentOld->Next != NULL)
 {
  CurrentNew->Point = s(CurrentOld->Point);
  g->PlotPoint(CurrentNew->Point);
  CurrentNew->Next = new TPointNode;
  CurrentNew = CurrentNew->Next;
  CurrentOld = CurrentOld->Next;
 }

 CurrentNew->Point = s(CurrentOld->Point);
 g->PlotPoint(CurrentNew->Point);
 CurrentOld->Next = FirstNew;
 CurrentNew->Next = NULL;

 return;
}

void LoopDraw2DIFS(TGraph *g, void *IFS, int *PaintProgressPtr)
{
 TPointNode *First;
 register int i;
 TPoint Origin = {0, 0};
 TPointNode *NextToDelete;

 First = new TPointNode;
 (First->Point).x = 0;
 (First->Point).y = 0;
 First->Next = 0;

 g->PlotPoint(Origin);

 for (i = 1; i <= ITERATIONS; i++)
 {
  Draw2DIFS(g, First, *((TReadOnlyIFS *) IFS));
  *(PaintProgressPtr) = (int) ((((float) i) / ((float) ITERATIONS)) * 99);
 }

 while ((First->Next) != 0)
 {
  NextToDelete = First;
  First = First->Next;
  delete NextToDelete;
 }
 delete First;

 *(PaintProgressPtr) = 100;

 return;
}
