(Please help, still unable to solve) After doing some rotation and scaling of my polygon shaped objects, I managed to render an image but it's different from the correct image as shown below(Correct image). I am puzzled by why that is. I have found the center of the vertices and scaled and rotated my polygon shaped objects from the center of the vertices to hopefully, get a straight path. However, I still am not able to get the straight path as desired. As I am new to the rotations, scaling and translation methods, I would sincerely hope that you are able to help so that I will be able to get the image to match properly. I do not know what I need to change already. Do I also need to find the center of vertices for scaling? Then translate the point back to center OR back to the original pivot point? Same question I have for rotation. Please help. If you can, please help me identify the mistake in my code. Hope the question is clear.Thank you.
Note: In my test case provided, translation is called first, followed by rotate, and then scale.
So, t->translate({ 0.0f, 50.0f }); Then, r->rotate(0.25f);. Then, s->scale(0.85f);. Test case CANNOT be modified.
Translating method
template<typename T>
void translate(const T& displacement)
{
_pivotPt = T((_pivotPt.x() + displacement.x()),
(_pivotPt.y() + displacement.y()));
}
Scaling method
template<typename T>
void Polygon<T>::scale(const float factor) //temporarily treat other point as origin
{
for (size_t i{}; i < _nsize; i++)
{
center += _npts[i];
}
center = T(center.x() / _nsize, center.y() / _nsize);
for (auto& verts : _npts)
{
verts = T((static_cast<float>
(center.x()) + (factor) *
(static_cast<float>(verts.x() - center.x()))),
(static_cast<float
(center.y()) + (factor) *
(static_cast<float>(verts.y() - center.y()))));
}
}
Rotation method
template<typename T>
void Polygon<T>::rotate(const float angle)
{
typename Point<T>::type _xn, _yn;
for (size_t i{}; i < _nsize; i++)
{
center += _npts[i];
}
center = T(center.x() / _nsize, center.y() / _nsize); //Find center from all given coordinates
for (auto& verts : _npts)
{
float xn = verts.x() - center.x(); //Subtract pivot point
float yn = verts.y() - center.y();
_xn = (center.x() + std::cos(angle) * xn - std::sin(angle) * yn); //translate back to origin.
_yn = (center.y() + std::sin(angle) * xn + std::cos(angle) * yn);
verts = T(_xn, _yn);
}
}
Seems like you should rotate around the centroid, so I don't see why you are using _pivotPt.x() when computing new coordinates. It should be
_xn = (center.x() + std::cos(angle) * xn - std::sin(angle) * yn);
_yn = (center.y() + std::sin(angle) * xn + std::cos(angle) * yn);
edit : Seems like center and _pivotPt should always be the same.
Edit : Your center object is a global variable which keep being updated. Each time you try to compute the centroid, the old value mess up the computation
ps : It seems your translation method translate the centroid (pivot point), and assume the new value will be used correctly by the next functions.by itself it is not a bad idea, but it is error prone. Given your situation it make more sense to code conservatively and translate all points in _npts