Skip to content

Commit

Permalink
Added a version of set_aspect_ratio() that works for drectangle.
Browse files Browse the repository at this point in the history
  • Loading branch information
davisking committed Jul 3, 2015
1 parent ad9a9e6 commit 16efd46
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dlib/geometry/drectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ namespace dlib
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2);
}

inline drectangle centered_drect (
const drectangle& rect,
double width,
double height
)
{
return centered_drect(dcenter(rect), width, height);
}

inline const drectangle shrink_rect (
const drectangle& rect,
double num
Expand Down Expand Up @@ -419,6 +428,21 @@ namespace dlib
return shrink_rect(rect, -width, -height);
}

inline drectangle set_aspect_ratio (
const drectangle& rect,
double ratio
)
{
DLIB_ASSERT(ratio > 0,
"\t drectangle set_aspect_ratio()"
<< "\n\t ratio: " << ratio
);

const double h = std::sqrt(rect.area()/ratio);
const double w = h*ratio;
return centered_drect(rect, w, h);
}

// ----------------------------------------------------------------------------------------

}
Expand Down
30 changes: 30 additions & 0 deletions dlib/geometry/drectangle_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,18 @@ namespace dlib
- R.height() == height
!*/

// ----------------------------------------------------------------------------------------

drectangle centered_drect (
const drectangle& rect,
double width,
double height
);
/*!
ensures
- returns centered_drect(center(rect), width, height)
!*/

// ----------------------------------------------------------------------------------------

const drectangle shrink_rect (
Expand Down Expand Up @@ -554,6 +566,24 @@ namespace dlib
(i.e. grows the given drectangle by expanding its border)
!*/

// ----------------------------------------------------------------------------------------

drectangle set_aspect_ratio (
const drectangle& rect,
double ratio
);
/*!
requires
- ratio > 0
ensures
- This function reshapes the given rectangle so that it has the given aspect
ratio. In particular, this means we return a rectangle R such that the
following equations are true:
- R.width()/R.height() == ratio
- R.area() == rect.area()
- dcenter(rect) == dcenter(R)
!*/

// ----------------------------------------------------------------------------------------

}
Expand Down

0 comments on commit 16efd46

Please sign in to comment.