(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);
}
}
size_t i{}
forsize_t i{0}
, at least just for clarity. 2) I know you cannot change the test, but have you tried doing the translation in last place? 3) Another suggestion, just to limit where to look at: try each transformation separately; then check if one of them is doing weird things. – rturrado