'Fitting a contour inside another contour using OpenCV

The idea is to have a rectangular contour, searching for the points of intersection with the triangle one (as shown in the image below)

contours and points of intersection

and then move the rectangle in order to fit inside the triangle without intersection. In my code I have the contours and the points of intersection. I was wondering if there is any OpenCV function I could exploit to directly fit the contour or to know how to move the rectangle in order to not have any intersection.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source