⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 point.cpp

📁 financal instrument pricing using c
💻 CPP
字号:
// Point.cpp
//
// Point class.
//
// 5 june 1998	MB	Started
//
// (C) Datasim Education BV 1998

#include "Point.hpp"

void Point::init(double xs, double ys)
{ // Initialize the point

	x = xs;
	y = ys;
}

// Constructors and destructor
Point::Point(): Shape()
{ // Default constructor

	init(0.0, 0.0);
}

Point::Point(double xs, double ys): Shape()
{ // Normal constructor with coordinates
 
	init(xs, ys);
}

Point::Point(const Angle& angle, double radius)
{ // Construct point using polar coordinates

	init(radius*angle.cos(), radius*angle.sin());
}
	
Point::Point(const Point &source): Shape(source)
{ // Copy constructor

	init(source.x, source.y);
}

Point:: ~Point()
{ // Destructor

} 

// Selectors
double Point::X() const
{// Return x
	return x;
}

double Point::Y() const
{// Return y
	return y;
}

double Point::Distance() const
{ // Calculate distance between point and origin(0,0)

	return Distance(Point(0.0, 0.0));
}

double Point::Distance(const Point& p2) const
{ // Calculate distance between this point and second point 
  // To compute this, the law of Pythagoras is used: sqrt(dx^2 + dy^2)

	// If coordinates are equal, return distance of 0.0 
	if ((*this)==p2) return 0.0;   

	// Difference in x direction
	double dif_x = (x - p2.x);	

	// Difference in y direction 
	double dif_y = (y - p2.y);  

	// Calculate squares of coordinates
	dif_x *= dif_x;		
	dif_y *= dif_y;

	// Return square root
	return sqrt(dif_x + dif_y); 
}

Point Point::MidPoint(const Point& p2) const
{ // Calculate the point between the two points

	return Point( (x+p2.x)*0.5 , (y+p2.y)*0.5 );
}

// Operator overloading
bool Point::operator == (const Point& p2) const
{ // Return true if all coordinates are equal

	if ( (x==p2.x) && (y==p2.y) ) return true;

	return false;
}
// Modifiers
void Point::X(double NewX)
{// Set x
	x = NewX;
}

void Point::Y(double NewY)
{// Set y
	y = NewY;
}

// Operators
bool Point::operator != (const Point& p2) const
{ // Return false if all coordinates are equal
   
	return !(*this==p2);
}

Point& Point::operator = (const Point &source)
{ // Assignment operator

	// Exit if same object
	if (this==&source) return *this;

	// Call base class assignment
	Shape::operator = (source);

	// Copy state
	init(source.x, source.y);    

	return *this;
}

std::ostream& operator << (std::ostream& os, const Point& p)
{ // Output point to ostream

	os<<"Point("<<p.x<<", "<<p.y<<")";
	return os;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -