// Elements.cpp : implementation file
//

#include "stdafx.h"
#include <math.h>
#include "Sketcher.h"
#include "Elements.h"


// CElement

CElement::CElement()

{
}
CRect CElement::GetBoundRect()
{
	CRect BoundingRect;
	BoundingRect = m_EnclosingRect;

	// Increase the rectangle by the pen width
	int Offset = m_Pen == 0 ? 1 : m_Pen;	// width must be at least 1
	BoundingRect.InflateRect(Offset, Offset);
	return BoundingRect;
}

CElement::~CElement()
{
}


// CElement member functions

CLine::CLine(CPoint Start, CPoint End, COLORREF aColor, int PenWidth)
{
	m_StartPoint = Start;
	m_EndPoint = End;
	m_Color = aColor;
	m_Pen = PenWidth;	

	// Define the enclosing rectangle
	m_EnclosingRect = CRect(Start, End);
	m_EnclosingRect.NormalizeRect();
}

void CLine::Draw(CDC* pDC)
{
	// Create a pen for this object
	CPen aPen;
	if(!aPen.CreatePen(PS_SOLID, m_Pen, m_Color))
	{
		AfxMessageBox(_T("Pen creation failed drawing a line"), MB_OK);
		AfxAbort();
	}

	CPen* pOldPen = pDC->SelectObject(&aPen);

	// Now draw the line
	pDC->MoveTo(m_StartPoint);
	pDC->LineTo(m_EndPoint);

	pDC->SelectObject(pOldPen);
}


CLine::~CLine(void)
{
}

CRectangle::CRectangle(CPoint Start, CPoint End, COLORREF aColor, int PenWidth)
{
	m_Color = aColor;
	m_Pen = PenWidth;

	// Define the enclosing rectangle
	m_EnclosingRect = CRect(Start, End);
	m_EnclosingRect.NormalizeRect();
}

void CRectangle::Draw(CDC* pDC)
{
	// Create a pen for this object
	CPen aPen;
	if(!aPen.CreatePen(PS_SOLID, m_Pen, m_Color))
	{
		AfxMessageBox(_T("Pen creation failed drawing a line"), MB_OK);
		AfxAbort();
	}

	CPen* pOldPen = pDC->SelectObject(&aPen);
	CBrush* pOldBrush = (CBrush*) pDC->SelectStockObject(NULL_BRUSH);


	// Now draw the line
	pDC->Rectangle(m_EnclosingRect);
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
}
					  

CRectangle::~CRectangle(void)
{
}

CCircle::CCircle(CPoint Start, CPoint End, COLORREF aColor, int PenWidth)
{
	long Radius = static_cast<long> (sqrt(
		static_cast<double>((End.x-Start.x)*(End.x-Start.x) + (End.y-Start.y)*(End.y-Start.y))));
	// Now calculate the rectangle enclosing
	m_EnclosingRect = CRect(Start.x-Radius, Start.y-Radius,
							Start.x+Radius, Start.y+Radius);
	m_Color = aColor;
	m_Pen = PenWidth;
}
// Draw a circle
void CCircle::Draw(CDC* pDC)
{
	CPen aPen;
	if(!aPen.CreatePen(PS_SOLID, m_Pen, m_Color))
	{
		AfxMessageBox(_T("Pen creation failed drawing a circle"), MB_OK);
		AfxAbort();
	}

	CPen* pOldPen = pDC->SelectObject(&aPen);
	CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);

	// Now draw the circle
	pDC->Ellipse(m_EnclosingRect);
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush);
}

CCircle::~CCircle(void)
{
}

// Constructor for a curve object
CCurve::CCurve(COLORREF aColor, int PenWidth)
{
	m_Color = aColor;
	m_EnclosingRect = CRect(0,0,0,0);
	m_Pen = PenWidth;
}

// Draw a curve
void CCurve::Draw(CDC* pDC)
{
}

CCurve::~CCurve(void)
{
}
