旋转测试运算符重载

问题描述投票：-1回答：1

**********我的旋转测试**********

**********学校的旋转测试**********

``````   Point Point::operator%( double angle)
{
Point p;
double rad=angle * PI / 180.0;

// rotate point
double xnew = p.x * c - p.y * s;
double ynew = p.x * s + p.y * c;

p.x = xnew;
p.y = ynew;
return p;
// return Point(cos(degree) * (p.x) - sin(degree) * (p.y),
// sin(degree) * (p.x) - cos(degree) * (p.y));

// return p;
}
``````

`````` void RotateTest(void)
{
cout << "\n********** Rotate test ********** " << endl;

Point pt1(-50, -50);
double angle = 45;

Point pt2 = pt1 % angle;

cout.setf(std::ios_base::fixed, std::ios::floatfield);
std::cout.precision(3);
cout << "Point " << pt1 << " rotated " << angle << " degrees is " <<
pt2 << endl;

pt1 = Point(-6, -6);
angle = 45;

pt2 = pt1 % angle;
cout << "Point " << pt1 << " rotated " << angle << " degrees is " <<
pt2 << endl;

cout << endl;
cout.unsetf(std::ios_base::fixed);
std::cout.precision(6);
}
``````

list.h文件：

`````` class Point
{
public:
// Point(double X, double Y);    // Constructors (2)
explicit Point(double x, double y);

Point();

double getX() const;

double getY() const;

Point operator+(const Point& other)const ;

Point& operator+(double val)  ;

Point operator*(double value) ;

Point operator%(double angle);

double operator-(const Point& other)const ;

Point operator-(double val);

Point operator^(const Point& other);

Point& operator+=(double val);
Point& operator+=(const Point& other) ;

Point& operator++();
Point operator++(int);

Point& operator--();
Point operator--(int);

Point operator-() const;

// Overloaded operators (14 member functions)
friend std::ostream &operator<<( std::ostream &output, const Point
&point );
friend std::istream &operator>>( std::istream  &input, Point
&point );

// Overloaded operators (2 friend functions)

private:
double x; // The x-coordinate of a Point
double y; // The y-coordinate of a Point

// Helper functions
};
``````
1个回答
0

``````Point Point::operator%( double angle)
{
Point p;
double rad=angle * PI / 180.0;

// rotate point
double xnew = this->x * c - this->y * s;
double ynew = this->x * s + this->y * c;

p.x = xnew;
p.y = ynew;
return p;
}
``````

``````Point Point::operator%( double angle)
{
double rad=angle * PI / 180.0;