diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index 62b05a9b9f..701fa4c3ae 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -30,6 +30,20 @@ MultiPoint::translate(const Point &vector) this->translate(vector.x, vector.y); } +void +MultiPoint::rotate(double angle) +{ + double s = sin(angle); + double c = cos(angle); + for (Points::iterator it = points.begin(); it != points.end(); ++it) { + (*it).rotate(angle); + double cur_x = (double)it->x; + double cur_y = (double)it->y; + it->x = (coord_t)round(c * cur_x - s * cur_y); + it->y = (coord_t)round(c * cur_y + s * cur_x); + } +} + void MultiPoint::rotate(double angle, const Point ¢er) { diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp index 1f0eb489fd..9c71f42f42 100644 --- a/xs/src/libslic3r/MultiPoint.hpp +++ b/xs/src/libslic3r/MultiPoint.hpp @@ -22,6 +22,7 @@ class MultiPoint void scale(double factor); void translate(double x, double y); void translate(const Point &vector); + void rotate(double angle); void rotate(double angle, const Point ¢er); void reverse(); Point first_point() const; diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp index 07462a351a..c70164e19b 100644 --- a/xs/src/libslic3r/Point.cpp +++ b/xs/src/libslic3r/Point.cpp @@ -54,13 +54,26 @@ Point::translate(const Vector &vector) this->translate(vector.x, vector.y); } +void +Point::rotate(double angle) +{ + double cur_x = (double)this->x; + double cur_y = (double)this->y; + double s = sin(angle); + double c = cos(angle); + this->x = (coord_t)round(c * cur_x - s * cur_y); + this->y = (coord_t)round(c * cur_y + s * cur_x); +} + void Point::rotate(double angle, const Point ¢er) { double cur_x = (double)this->x; double cur_y = (double)this->y; - this->x = (coord_t)round( (double)center.x + cos(angle) * (cur_x - (double)center.x) - sin(angle) * (cur_y - (double)center.y) ); - this->y = (coord_t)round( (double)center.y + cos(angle) * (cur_y - (double)center.y) + sin(angle) * (cur_x - (double)center.x) ); + double s = sin(angle); + double c = cos(angle); + this->x = (coord_t)round( (double)center.x + c * (cur_x - (double)center.x) - s * (cur_y - (double)center.y) ); + this->y = (coord_t)round( (double)center.y + c * (cur_y - (double)center.y) + s * (cur_x - (double)center.x) ); } bool @@ -295,6 +308,12 @@ operator+(const Point& point1, const Point& point2) return Point(point1.x + point2.x, point1.y + point2.y); } +Point +operator-(const Point& point1, const Point& point2) +{ + return Point(point1.x - point2.x, point1.y - point2.y); +} + Point operator*(double scalar, const Point& point2) { @@ -343,13 +362,26 @@ Pointf::translate(const Vectorf &vector) this->translate(vector.x, vector.y); } +void +Pointf::rotate(double angle) +{ + double cur_x = this->x; + double cur_y = this->y; + double s = sin(angle); + double c = cos(angle); + this->x = c * cur_x - s * cur_y; + this->y = c * cur_y + s * cur_x; +} + void Pointf::rotate(double angle, const Pointf ¢er) { double cur_x = this->x; double cur_y = this->y; - this->x = center.x + cos(angle) * (cur_x - center.x) - sin(angle) * (cur_y - center.y); - this->y = center.y + cos(angle) * (cur_y - center.y) + sin(angle) * (cur_x - center.x); + double s = sin(angle); + double c = cos(angle); + this->x = center.x + c * (cur_x - center.x) - s * (cur_y - center.y); + this->y = center.y + c * (cur_y - center.y) + s * (cur_x - center.x); } Pointf diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp index 8fe2ded7e6..9c23252cb6 100644 --- a/xs/src/libslic3r/Point.hpp +++ b/xs/src/libslic3r/Point.hpp @@ -42,6 +42,7 @@ class Point void scale(double factor); void translate(double x, double y); void translate(const Vector &vector); + void rotate(double angle); void rotate(double angle, const Point ¢er); bool coincides_with(const Point &point) const { return this->x == point.x && this->y == point.y; } bool coincides_with_epsilon(const Point &point) const; @@ -64,6 +65,7 @@ class Point }; Point operator+(const Point& point1, const Point& point2); +Point operator-(const Point& point1, const Point& point2); Point operator*(double scalar, const Point& point2); class Point3 : public Point @@ -92,6 +94,7 @@ class Pointf void scale(double factor); void translate(double x, double y); void translate(const Vectorf &vector); + void rotate(double angle); void rotate(double angle, const Pointf ¢er); Pointf negative() const; Vectorf vector_to(const Pointf &point) const;