Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Created December 19, 2022 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericoporto/b1283d882c3ca488ba37159320118b37 to your computer and use it in GitHub Desktop.
Save ericoporto/b1283d882c3ca488ba37159320118b37 to your computer and use it in GitHub Desktop.
inline Rect full_transform(const Rect &r, const glm::mat4 &m)
{
// Extract the translation, rotation, and scaling components from the transformation matrix
glm::vec3 translation = glm::vec3(m[3][0], m[3][1], m[3][2]);
glm::vec3 scaling = glm::vec3(m[0][0], m[1][1], m[2][2]);
glm::quat rotation = glm::quat_cast(m);
// Calculate the half-width and half-height of the original rectangle
float half_width = 0.5f * (float)(r.Right - r.Left);
float half_height = 0.5f * (float)(r.Bottom - r.Top);
// Calculate the transformed corner points of the rectangle
glm::vec3 p1 = translation + rotation * (scaling * glm::vec3(-half_width, -half_height, 0.0f));
glm::vec3 p2 = translation + rotation * (scaling * glm::vec3( half_width, -half_height, 0.0f));
glm::vec3 p3 = translation + rotation * (scaling * glm::vec3(-half_width, half_height, 0.0f));
glm::vec3 p4 = translation + rotation * (scaling * glm::vec3( half_width, half_height, 0.0f));
// Calculate the minimum and maximum x and y values of the transformed points
float xmin = std::min(p1.x, std::min(p2.x, std::min(p3.x, p4.x)));
float ymin = std::min(p1.y, std::min(p2.y, std::min(p3.y, p4.y)));
float xmax = std::max(p1.x, std::max(p2.x, std::max(p3.x, p4.x)));
float ymax = std::max(p1.y, std::max(p2.y, std::max(p3.y, p4.y)));
// Round the transformed x and y values to integer values
xmin = std::round(xmin);
ymin = std::round(ymin);
xmax = std::round(xmax);
ymax = std::round(ymax);
// Create and return the transformed rectangle
return Rect((int)xmin, (int)ymin, (int)xmax, (int)ymax);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment