Скачиваний:
7
Добавлен:
01.05.2014
Размер:
4.62 Кб
Скачать
#include "stdafx.h"
#include "Triangle.h"
#include "Line.h"
#include <algorithm>

//IMPLEMENT_SERIAL(Triangle, Line, VERSION_NUMBER)

Triangle::Triangle(): id(++total){
	count++;
	cout << "create Triangle#" << id << endl;
	this->point = CPoint();

	buildEnclosingRectangle();
}

Triangle::Triangle(CPoint p1, CPoint p2, CPoint p3, COLORREF aColor): Line(p1, p2, aColor), id(++total){
	count++;
	cout << "create Triangle#" << id << endl;
	this->point = p3;

	buildEnclosingRectangle();
}

Triangle::Triangle(const Triangle* o): Line(o), id(++total){
	count++;
	cout << "create Triangle#" << id << " via cc" << endl;
	this->point =  CPoint(o->point);

	buildEnclosingRectangle();
}

Triangle::Triangle(const Triangle& o): Line(o), id(++total){
	count++;
	cout << "create Triangle#" << id << " via cc" << endl;
	this->point = CPoint(o.point);

	buildEnclosingRectangle();
}

Triangle::~Triangle(){
	count--;
	cout << "destroy Triangle#" << id << endl;
//	delete point;
}

void Triangle::buildEnclosingRectangle(){
	CPoint p1 = Line::p1;
	CPoint p2 = Line::p2;
	CPoint p3 = point;

	double xs[] = {p1.x, p2.x, p3.x};
	double ys[] = {p1.y, p2.y, p3.y};

	sort(&xs[0], &xs[3]);
	sort(&ys[0], &ys[3]);

	CPoint tl = CPoint(xs[0], ys[2]);
	CPoint br = CPoint(xs[2], ys[0]);

	double cX = (xs[2] + xs[0])/2;
	double cY = (ys[2] + ys[0])/2;

	this->center = CPoint(cX, cY);

	// Define the enclosing rectangle
	m_EnclosingRect = CRect(tl, br);
	m_EnclosingRect.NormalizeRect();
}

ostream& Triangle::print(ostream& os) const{
	os << "Triangle#" << id << ": " << endl
		<< toString();
	return os;
}

Triangle& Triangle::operator= (const Triangle& o){ 
	if(this == &o) 
		return *this;

	Line::operator=(o);
	// call copy-assignment
	this->point = o.point;

	buildEnclosingRectangle();
	return *this;
}

CPoint Triangle::getPoint(int n){
	if (n == 1)
		return Line::getPoint(1);
	else if (n == 2)
		return Line::getPoint(2);
	else
		return point;
}

string Triangle::toString() const {
	return "" ;//Line::toString() + "  Point3: " + point->toString();
}

int Triangle::operator==(const Triangle& o) const {
	return (Line::operator==(o) && (point == o.point));
};

// Draw a Triangle object
void Triangle::Draw(CDC* pDC, CElement* pElement, string key)
{
	/*
	if (this == pElement){
	COLORREF aColor1 = (this == pElement)? SELECT_COLOR: WHITE;

	CPen aPen1;
	aPen1.CreatePen(PS_SOLID, m_Pen, aColor1);
	CPen* pOldPen1 = pDC->SelectObject(&aPen1);  // Select the pen

	CBrush brush;
	brush.CreateHatchBrush(2, RGB (128, 128, 128));

	CBrush* pOldBrush1 = pDC->SelectObject(&brush);//(CBrush*)pDC->SelectStockObject(NULL_BRUSH);  // Select the brush

	pDC->Rectangle(m_EnclosingRect);

	pDC->SelectObject(pOldPen1);   
	pDC->SelectObject(pOldBrush1);              // Restore the old brush
	}
	*/
	// Create a pen for this object and
	// initialize it to the object color and line width of 1 pixel
	CPen aPen;
	COLORREF aColor = (this == pElement)? SELECT_COLOR: m_Color;

	if(!aPen.CreatePen(PS_SOLID, m_Pen, aColor))
	{  
		// Pen creation failed. Abort the program.
		AfxMessageBox("Pen creation failed drawing a line", MB_OK);
		AfxAbort();
	}

	// draw figure
	CPen* pOldPen = pDC->SelectObject(&aPen);  // Select the pen

	CBrush brush;
	CBrush* pOldBrush;

	pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);

	Line::Draw(pDC, pElement);

	// Now draw the line
	pDC->MoveTo(Line::getPoint(1));
	pDC->LineTo(point);
	// Now draw the line
	pDC->MoveTo(Line::getPoint(2));
	pDC->LineTo(point);

	//   CPoint p = CPoint(m_EnclosingRect.TopLeft().x, m_EnclosingRect.BottomRight().y);
	if (key != "")
		m_keyRectangle = drawKey(pDC, key);
	else
		m_keyRectangle = CRect(0,0,0,0);

	pDC->SelectObject(pOldPen);                // Restore the old pen
	pDC->SelectObject(pOldBrush);              // Restore the old brush
}

void Triangle::Move(CSize& aSize) {
	Line::p1 += aSize;           // Move points
	Line::p2 += aSize;           
	point += aSize;              
	m_EnclosingRect += aSize;    // Move the enclosing rectangle
}

// serialization
CArchive& Triangle::load(CArchive& ar) {
	CElement::load(ar);

	ar	>> Line::p1 
		>> Line::p2
		>> point;

	buildEnclosingRectangle();

	return ar;
}

CArchive& Triangle::save(CArchive& ar) { 
	CElement::save(ar);

	ar	<< Line::p1 
		<< Line::p2
		<< point;

	return ar;
}

// initialize static fields 
unsigned long int Triangle::count = 0;
unsigned long int Triangle::total = 0;
Соседние файлы в папке OLEApp