#include "graph.hpp"
#include "windows.h"
#include "point.hpp"
#include "unaryoperators.hpp"

TGraph::TGraph(HDC CompatibleDC, int xMaxModX, int xMaxModY, RECT xClientRect)
{
 //Set up class globals
 MaxModX = xMaxModX; //Maximum of |x|
 MaxModY = xMaxModY; //Maximum of |y|
 ClientRect = xClientRect;

 //Set up the dimension variables
 Width = ClientRect.right;
 Height = ClientRect.bottom;
 hw = (int) Width / 2;
 hh = (int) Height / 2;
 sw = (int) hw / MaxModX;
 sh = (int) (-1)*(hh / MaxModY);

 //Bitmap Creation
 DC = CreateCompatibleDC(CompatibleDC);
 Bitmap = CreateCompatibleBitmap(DC, Width, Height); //We use DC because it is a monochrome bitmap.
 SelectObject(DC, Bitmap);
 Clear();
}

TGraph::~TGraph()
{
 DeleteObject(Bitmap);
 DeleteDC(DC);
}

COLORREF TGraph::PlotPoint(double x, double y)
{
 return SetPixel(DC, (int) round(sw*x + hw), (int) round(sh*y + hh), RGB(0,0,0));
}

COLORREF TGraph::PlotPoint(TPoint p)
{
 return PlotPoint(p.x, p.y);
}

int TGraph::Clear()
{
 return FillRect(DC, &ClientRect, (HBRUSH) GetStockObject(WHITE_BRUSH));
}

BOOL TGraph::BitBltTo(HDC Dest)
{
 return BitBlt(Dest, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);
}
