#include <math.h>
#include "noidZ.h"


//extern SDL_Surface *screen;

void brescircle(SDL_Surface *surface,float radius,float xcenter,float ycenter,Uint32 color) 
{
  float xstart = 0;
  float ystart = radius;
  float sum = 0.03-2*radius;
  while(xstart <= ystart)
  {
                    SDL_LockSurface(surface);
                    noidZ_Giv_Pixel(surface,xstart+xcenter,ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-xstart+xcenter,ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,xstart+xcenter,-ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-xstart+xcenter,-ystart+ycenter,color);
                    noidZ_Giv_Pixel(surface,ystart+xcenter,xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-ystart+xcenter,xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,ystart+xcenter,-xstart+ycenter,color);
                    noidZ_Giv_Pixel(surface,-ystart+xcenter,-xstart+ycenter,color);
                    SDL_UnlockSurface(surface);
      //set_eight(xstart,ystart,xcenter,ycenter);
      if(sum <= 0)
      {
        sum = sum + 4*xstart + 0.06;
        }
      else
      {
          sum = sum + 4*(xstart-ystart) + 0.10;
          ystart = ystart - 0.003;
      }
      xstart = xstart + 0.003;
  }
}

void bresline(SDL_Surface *surface,float lx1,float lx2, float ly1, float ly2,Uint32 color)
{
     float deltax,deltay,lx,ly,xinc1,xinc2,yinc1,yinc2,den,num,numadd,numpixels,curpixel;
     deltax = fabs(lx2 - lx1);        // The difference between the x's
     deltay = fabs(ly2 - ly1);        // The difference between the y's
     lx = lx1;                       // Start x off at the first pixel
     ly = ly1;                       // Start y off at the first pixel

     if (lx2 >= lx1)                 // The x-values are increasing
     {
           xinc1 = 0.01;
           xinc2 = 0.01;
     } else {
           xinc1 = -0.01;
           xinc2 = -0.01;
     }

     if (ly2 >= ly1)                 // The y-values are increasing
     {
           yinc1 = 0.01;
           yinc2 = 0.01;
     } else {
           yinc1 = -0.01;
           yinc2 = -0.01;
     }

     if (deltax >= deltay)         // There is at least one x-value for every y-value
     {
           xinc1 = 0;                  // Don't change the x when numerator >= denominator
           yinc2 = 0;                  // Don't change the y for every iteration
           den = deltax;
           num = deltax / 2;
           numadd = deltay;
           numpixels = deltax;         // There are more x-values than y-values
     } else {
           xinc2 = 0;                  // Don't change the x for every iteration
           yinc1 = 0;                  // Don't change the y when numerator >= denominator
           den = deltay;
           num = deltay / 2;
           numadd = deltax;
           numpixels = deltay;         // There are more y-values than x-values
     }
           SDL_LockSurface(surface);
     for (curpixel = 0; curpixel <= numpixels; curpixel = curpixel + 0.01)
     {
           noidZ_Giv_Pixel(surface,lx,ly,color);
           num += numadd;              // Increase the numerator by the top of the fraction
           if (num >= den)             // Check if numerator >= denominator
           {
                  num -= den;               // Calculate the new numerator value
                  lx += xinc1;               // Change the x as appropriate
                  ly += yinc1;               // Change the y as appropriate
           }
           lx += xinc2;                 // Change the x as appropriate
           ly += yinc2;                 // Change the y as appropriate
     }
           SDL_UnlockSurface(surface);
}
