| 196 | // ---------------------------------------------------------------------------------------- |
| 197 | |
| 198 | class rectangle_transform |
| 199 | { |
| 200 | public: |
| 201 | |
| 202 | rectangle_transform ( |
| 203 | ) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | rectangle_transform ( |
| 208 | const point_transform_affine& tform_ |
| 209 | ) :tform(tform_) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | drectangle operator() ( |
| 214 | const drectangle& r |
| 215 | ) const |
| 216 | { |
| 217 | dpoint tl = r.tl_corner(); |
| 218 | dpoint tr = r.tr_corner(); |
| 219 | dpoint bl = r.bl_corner(); |
| 220 | dpoint br = r.br_corner(); |
| 221 | // The new rectangle would ideally have this area if we could actually rotate |
| 222 | // the box. Note the 1+ is because that's how the rectangles calculate their |
| 223 | // width and height. |
| 224 | double new_area = (1+length(tform(tl)-tform(tr)))*(1+length(tform(tl)-tform(bl))); |
| 225 | |
| 226 | // But if we rotate the corners of the rectangle and then find the rectangle |
| 227 | // that contains them we get this, which might have a much larger area than we |
| 228 | // want. |
| 229 | drectangle temp; |
| 230 | temp += tform(tl); |
| 231 | temp += tform(tr); |
| 232 | temp += tform(bl); |
| 233 | temp += tform(br); |
| 234 | // so we adjust the area to match the target area and have the same center as |
| 235 | // the above box. |
| 236 | double scale = std::sqrt(new_area/temp.area()); |
| 237 | |
| 238 | return centered_drect(dcenter(temp), temp.width()*scale, temp.height()*scale); |
| 239 | } |
| 240 | |
| 241 | rectangle operator() ( |
| 242 | const rectangle& r |
| 243 | ) const |
| 244 | { |
| 245 | const auto temp = (*this)(drectangle(r)); |
| 246 | return centered_rect(center(temp), std::round(temp.width()), std::round(temp.height())); |
| 247 | } |
| 248 | |
| 249 | const point_transform_affine& get_tform( |
| 250 | ) const { return tform; } |
| 251 | |
| 252 | inline friend void serialize (const rectangle_transform& item, std::ostream& out) |
| 253 | { |
| 254 | serialize(item.tform, out); |
| 255 | } |