Скачиваний:
8
Добавлен:
01.05.2014
Размер:
4.78 Кб
Скачать
#include "stdafx.h"

#include "Point.h"
#include <math.h>
#include <iostream>
using namespace std;

#define M_PI 3.1415926
#define M_PI_2 3.1415926/2
#define M_1_PI 1/3.1415926

/**
* constructor
* param x - 'x' coordinate
* param y - 'y' coordinate
*/
Point::Point(const double x, const double y): id(++total){
	this->x = x;
	this->y = y;
	convertXYToRFi();

	// increase number of class objects
	count++;

	cout.precision(3);
	cout << "Point #" << id <<" with coordinates ("
		<< x << "; " << y << ") " << "was created." << endl
		<< "Current number of points is " << count << endl;
}

Point::Point(const Point* const point): id(++total){
	this->x = point->getX();
	this->y = point->getY();
	convertXYToRFi();

	// increase number of class objects
	count++;

	cout.precision(3);
	cout << "Point #" << id <<" with coordinates ("
		<< x << "; " << y << ") " << "was created via cc." << endl
		<< "Current number of points is " << count << endl;
}

Point::Point(const Point& point): id(++total){
	this->x = point.getX();
	this->y = point.getY();
	convertXYToRFi();

	// increase number of class objects
	count++;

	cout.precision(3);
	cout << "Point #" << id <<" with coordinates ("
		<< x << "; " << y << ") " << "was created via cc." << endl
		<< "Current number of points is " << count << endl;
}

/**
* destructor   
*/
Point::~Point(){
	// decrease number of class objects
	count--;

	cout.precision(3);
	cout << "Point #" << id <<" with coordinates (" 
		<< x << "; " << y << ") " << "was deleted." << endl
		<< "Current number of points is " << count << endl;
}

/**
* function that calculates point's angle
* by it's 'x' and 'y' coordinates
*/  
void Point::calculatePolarAngle(){
	// temporary variable
	double angle;

	// check whether the point is (0,0)
	if ((x==0)&&(y==0))	{
		angle = 0;
	}
	// otherwise 
	else{
		// if point is somewhere in 'y' axis
		// ('y' coordinate isn't '0')
		if (x==0) {
			if (y>0) 
				angle = M_PI_2;
			else 
				angle = M_PI_2*3;
		}
		// otherwise
		else{
			// calculate angle  
			angle = atan(y/x);
			
			// 1-st and 4-th quarters
			if (x>0){  
				// 4-th quarter  
				if (y<0)
					angle += 2*M_PI;
				// else 1-st quarter  
			}        
			// 2-nd and 3-rd quarters
			else 
				angle += M_PI;
		}
	}

	// copy 'angle' value to 'fi'
	fi = angle;
}

/**
* function that converts (X,Y) 
* coordinates to (R,Fi)
*/  
void Point::convertXYToRFi(){
	r = sqrt(x*x+y*y); 
	calculatePolarAngle(); 
}

/**
* function that converts (R,Fi)
* coordinates to (X,Y) 
*/  
void Point::convertRFiToXY(){
	x = r*cos(fi); 
	y = r*sin(fi);
}

/**
* 'x' field accessor
*/
double Point::getX() const{
	return x;
}

/**
* 'y' field accessor
*/  
double Point::getY() const{
	return y;
}

/**
* 'r' field accessor
*/ 
double Point::getR() const{
	return r;
}

/**
* 'fi' field accessor
*/ 
double Point::getFi() const{
	// convert radians to degrees 
	// and return it's value
	return fi*(180*M_1_PI); 
}

/**
* move point
*/
void Point::moveBy(const double dx, const double dy){
	x += dx;
	y += dy;
	// convert (x,y) to (r,fi)
	convertXYToRFi(); 

	cout.precision(3);
	cout << "Point was moved by (" << dx << ";" << dy << ")" << endl;
}

/**
* turn point
*/
void Point::turnBy(const double angle){
	// convert degrees to radians
	double psi = angle*(2*M_PI)/360;

	// add 'fi' to angle
	psi += fi;

	// calculate 'x' and 'y' coordinates
	x = r*cos(psi);
	y = r*sin(psi);

	// calculate polar angle
	calculatePolarAngle();

	cout.precision(3);
	cout << "Point was turned by " << angle << " degree(s)" << endl;
}

/**
* turn point around other point
*/
void Point::turnAroundBy(const double angle, const Point* const point){
	// change point coordinates as if base point is (0,0)
	x = x - point->x;
	y = y - point->y;

	// calculate polar angle and radius
	convertXYToRFi();

	// turn point around (0,0)
	turnBy(angle);

	// change coordinates back
	x = x + point->x;
	y = y + point->y;

	// calculate polar angle and radius
	convertXYToRFi();
}

/**
* print object
*/
ostream& Point::print(ostream& os) const {
	return os << "Point#" << id << ": "
			<< toString();
};

string Point::toString() const {
	return "(" + buildString(x) + ";" + buildString(y) + ")";
}

Point& Point::operator= (const Point& o){ 
	if(this == &o) 
		return *this;
	this->x = o.x;
	this->y = o.y;
	convertXYToRFi();
	return *this;
}

int Point::operator==(const Point& o) const {
	return ((x == o.x) && (y == o.y));
};

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