Ok, here is my rectangle class. Ive written it by modifing circle class so there is bunch of unnecessary parts. I feel like Resize function is wrong.
Code:
#include "stdafx.h"
#include "shaperectangle.h"
#include <cmath>
using namespace std;
namespace CC {
Factory<Rectangle> rectangleFactory("Rectangle");
// class Line
Rectangle::Rectangle ()
{
}
Rectangle::Rectangle (const Appearance &someLook, CPoint p, CPoint q)
: Shape(someLook)
{
m_Pen=1;
m_enclosingrect = CRect(p,q);
m_enclosingrect.NormalizeRect();
}
Shape* Rectangle::Clone ()
{
return new Rectangle(*this);
}
void Rectangle::Paint (CDC *pDC)
{
// create the pen
CPen pen(PS_SOLID, look.lineWidth, look.lineColor);
CPen *oldpen = pDC->SelectObject(&pen);
// create the brush
CBrush brush;
if (look.transparent)
brush.CreateStockObject(NULL_BRUSH);
else
brush.CreateSolidBrush(look.fillColor);
CBrush *oldbrush = pDC->SelectObject(&brush);
// draw the line
pDC->Rectangle(m_enclosingrect);
pDC->SelectObject(oldpen);
pDC->SelectObject(oldbrush);
}
CRect Rectangle::GetRect ()
{
CRect BoundingRect;
BoundingRect = m_enclosingrect;
BoundingRect.InflateRect(m_Pen, m_Pen, m_Pen, m_Pen);
BoundingRect = m_enclosingrect + CRect(m_Pen, m_Pen, m_Pen, m_Pen);
BoundingRect=m_enclosingrect;
BoundingRect.top -= m_Pen;
BoundingRect.left -= m_Pen;
BoundingRect.bottom += m_Pen;
BoundingRect.right += m_Pen;
return BoundingRect;
}
void Rectangle::Resize (CPoint p, CPoint q)
{
m_Start = p;
m_End = q;
}
void Rectangle::Move (CPoint delta)
{
center += delta;
}
bool Rectangle::Contains (CPoint p)
{
int dx = p.x - center.x;
int dy = p.y - center.y;
return dx*dx + dy*dy < radius*radius;
}
bool Rectangle::Persist (Serializer &io)
{
return Shape::Persist(io) &&
io.Move(center.x) &&
io.Move(center.y) &&
io.Move(radius);
}
}