Code:
include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
#include<sstream>
using namespace std;
#define PI 3.141592653
class VecPosition
{
public:
VecPosition(double vx = 0,double vy = 0);
VecPosition operator -- ();
VecPosition operator + (const double &d);
VecPosition operator + (const VecPosition &p);
VecPosition operator - (const double &d);
VecPosition operator - (const VecPosition &p);
VecPosition operator * (const double &d);
VecPosition operator * (const VecPosition &p);
VecPosition operator / (const double &d);
VecPosition operator / (const VecPosition &p);
void operator = (const double &d);
void operator += (const VecPosition &p);
void operator += (const double &d);
void operator -= (const VecPosition &p);
void operator -= (const double &d);
void operator *= (const VecPosition &p);
void operator *= (const double &d);
void operator /= (const VecPosition &p);
void operator /= (const double &d);
bool operator != (const VecPosition &p);
bool operator != (const double &d);
bool operator == (const VecPosition &p);
bool operator == (const double &d);
void draw(); // 绘制到屏幕
string str(double dX, double dY);// 将向量坐标转为字符串,格式为“(x=, y=)”
bool setX(double dX)
{
return false;
}
double getX() const
{
return m_x;
}
bool setY(double dY)
{
return false;
}
double getY() const
{
return m_y;
}
void setVecPosition(double dX=0 ,double dY=0 );
double getDistanceTo(const VecPosition p)
{
return sqrt((p.m_x-p.m_x)*(p.m_x-p.m_x) + (p.m_y-p.m_y)*(p.m_y - p.m_y));
}//获得两点间的距离
VecPosition setMagnitude(double d)
{
m_x = d*cos(this->getDirection());
m_y = d*sin(this->getDirection());
return *this;
}//设置向量长度为d
double getMagnitude(double d) const
{
return sqrt((m_x)*(m_x) + (m_y)*(m_y));
}//获取向量长度
double getDirection() const
{
double a[4] = { 0, 0, 1, 0 };
double b[4] = {0,0,m_x,m_y };
double t;
double angle;
t = ((a[0] - a[2])*(b[0] - b[2]) + (a[1] - a[3])*(b[1] - b[3])) / (sqrt(pow(a[0] - a[2], 2) + pow(a[1] - a[3], 2))*sqrt(pow(b[0] - b[2], 2) + pow(b[1] - b[3], 2)));
cout << "这两个向量的夹角为:" << acos(t)*(180 / PI) << "度" << endl;
return angle= acos(t)*(180 / PI);
}//获取向量方向(与X轴夹角)
bool isBetweenX(const VecPosition &p1, const VecPosition &p2)
{
return false;
}
bool isBetweenX(const double &d1, const double &d2)
{
return false;
}
bool isBetweenY(const VecPosition &p1, const VecPosition &p2)
{
return false;
}
bool isBetweenY(const double &d1, const double &d2)
{
return false;
} // 判断坐标是否在给定两点的X,Y之间
VecPosition normalize()
{
return VecPosition(cos(this->getDirection()), sin(this->getDirection()));
}// 正规化坐标
VecPosition rotate(double angle)
{
angle = angle / 180 * PI; //由于输进去的角度值不是幅度制,统一化成幅度值
angle += this->getDirection();
return VecPosition(this->getMagnitude(m_x)*cos(angle), this->getMagnitude(m_y)*sin(angle));
}// 向量旋转
static VecPosition getVecPositionFromPolar(double dMag, double ang)
{
ang = ang / 180 * PI;
return VecPosition(dMag*cos(ang), dMag*sin(ang));
}// 根据极坐标获取笛卡尔坐标
static double normalizeAngle(double angle)
{
angle = angle / PI * 180;//化成幅度值
int a = int(angle / 180);
if (a % 2 == 0)
return angle - a * 180;
else
return angle - (a + 1) * 180;
}// 将任意角度转为(-180,180]范围
private:
double m_x;
double m_y;
string a;
};
VecPosition::string str(double dX, double dY) {
ostringstream stream;
cout << "(x=" << dX << ", y=" << dY << ")";
return stream.str();
}
把VecPosition::string str(double dX, double dY) 写成 string VecPosition:: str(double dX, double dY) 试试
同意楼上。str才是VecPosition的成员函数。