6

This may be a more math focused question, but wanted to ask here because it is in a CS context. I'm looking to inscribe a rectangle inside another (arbitrary) quad with the inscribed quad having the largest height and width possible. Since I think the algorithm will be similar, I'm looking to see if I can do this with a circle as well.

To be more clear hear is what I mean by the bounding quadrilateral as an example. enter image description here

Here are 2 examples of the inscribed maximization I'm trying to achieve: enter image description here enter image description here

I have done some preliminary searching but have not found anything definitive. It seems that some form of dynamic programming could be the solution. It seems that this should be a linear optimization problem that should be more common than I have found, and perhaps I'm searching for the wrong terms.

Notes: For the inscribed square assume that we know a target w/h ratio we are looking for (e.g. 4:3). For the quad, assume that the sides will not cross and that it will be concave (if that simplifies the calculation).

Scott
  • 16,711
  • 14
  • 75
  • 120
  • Re. the circle: You can treat the quadrangle as a cut-off triangle. I.e. for each edge of the quadrangle, make the adjacent edges longer until they meet. Inscribe a circle into your new triangle. Check if it fits into your original quadrangle. The biggest circle thus obtained should be the optimal one. Obviously you will need to take care of quadrangles with parallel edges separately. – toochin Feb 06 '11 at 14:26
  • You might have a difficult time with any arbitrary quadrilateral if you allow convex quads and those whose segments overlap. Do you mean any arbitrary *convex* quadrilateral? – Jim Mischel Feb 06 '11 at 14:37
  • Can the rectangle also be rotated, or does it have to be parallel to the "horizont" ? – kohlehydrat Feb 06 '11 at 15:07
  • The rectangle cannot be rotated that is inscribed, this should make it a bit easier. The sides should not overlap. I think I can say that the external quad will be convex as well. I can see how a concave quad could make things more difficult. – Scott Feb 07 '11 at 17:45
  • For the circle, there are four angle bisectors (one for each vertex of the quadrilateral). Adjacent angle bisectors can be paired in four ways, leading to four possible centers for the circle. Pick the center that leads to the largest circle. For the inscribed rectangle with given aspect ratio, I believe the problem reduces to a simple linear programming problem. – Edward Doolittle Jun 04 '15 at 03:13
  • There is a deleted answer that had a useful link in it: http://cgm.cs.mcgill.ca/~athens/cs507/Projects/2003/DanielSud/ – Scott Nov 30 '21 at 06:27

2 Answers2

4

1) Circle.
For a triangle, this is a standard math question from school program.
For quadrilateral, you can notice that maximum inner circle will touch at least three of its sides. So, take every combination of three sides and solve the problem for each triangle.
A case of parallel sides have to be considered separately (since they don't form a triangle), but it's not terribly difficult.

2) Rectangle.
You can't have "largest height and width", you need to choose another criteria. E.g., on your picture I can increase width by reducing height and vice versa.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • For the circle case, exhaustive search will work, but keep in mind that's O(n!) and may only be practical for small polygons. A 20-sided polygon will have over 1100 combinations. – payne Feb 06 '11 at 15:37
  • @payne 'quadrilateral' usually implies `n = 4` :) – Nikita Rybak Feb 06 '11 at 16:15
  • The first suggestion will not work when the sides are parallel, which will be quite often. It relies on extending the sides until they meet and form a triangle. If parallel, they will not meet. – Scott Feb 07 '11 at 17:50
  • assume for response 2) that you know the ratio you want to maintain (e.g. 4:3) – Scott Feb 07 '11 at 17:55
  • @Scott First suggestion will work fine, because to find incircle you need bisections of two angles only. – Nikita Rybak Feb 07 '11 at 17:59
  • @Scott I'm not sure about 2), but choosing segment and searching for the point (rectangle's angle) on that segment should work: the function probably has only one local maximum. – Nikita Rybak Feb 07 '11 at 18:02
  • Another wikipedia article to check out is http://en.wikipedia.org/wiki/Tangential_quadrilateral – Scott Feb 11 '11 at 18:46
1

4 year old thread, but I happened to stumble accross it when googling my problem.

I have a problem like this in a current CV application. I came up with a simple and somewhat clumsy solution for the finding the largest. Not exactly the same though, cause I maximize the area of the rectangle without a fixed ratio of sides.

I don't know yet wether my solutions finds the optimum or whether it works in all cases. I also think there should be a more efficient way, so I am looking forward to your input.

First, assume a set of 4 points forming our (convex) quadrilateral:

    x   y
P1  -2  -5
P2  1   7
P3  4   5  
P4  3   -2

For this procedure the leftmost point is P1, the following points are numbered clockwise. It looks like this:

quadrilateral

We then create the linear functions between the Points. For each function we have to know the slope k and the distance from 0: d. k is simply the difference in Y of the two points divided by the difference in X. d can be calculated by solving the linear function to d. So we have

k=dy/dx
d=y1-k*x1

We will also want the inverse functions.

k_inv = 1/k
d_inv = -d/k

We then create the function and inverse function for each side of the quadrilateral

        k        d                        k         d
p1p2    4       3           p1p2_inv    0.25    -0.75
p2p3    -0.67   7.67        p2p3_inv    -1.5    11.5
p3p4    7       -23         p3p4_inv    0.14    3.29
p4p1    0.6     -3.8        p4p1_inv    1.67    6.33

If we had completely horizontal or vertical lines we would end up with a DIV/0 in one of the functions or inverse functions, thus we would need to handle this case separately.

Now we go through all corners that are enclosed by two functions that have a k with a slope with a different sign. In our case that would be P2 and P3.

We start at P2 and iterate through the y values between P2 and the higher one of P1 and P3 with an appropriate step size and use the inverse functions to calculate the distance between the functions in horizontal direction. This would give us one side of the rectangle

a=p2p3_inv(y)-p1p2_inv(y)

At the two x values x = p2p3_inv(y) and x = p1p2_inv(y) we then calculate the difference in y to the two opposite functions and take the distance to our current y position as a candidate for the second side of our rectangle.

b_candidate_1 = y-p4p1(p2p3_inv(y))
b_candidate_2 = y-p4p1(p1p2_inv(y))
b_candidate_3 = y-P3p4(p2p3_inv(y))
b_candidate_4 = y-P3p4(p1p2_inv(y))

The lesser of the four parameters would be the solution for side b. The area obviously becomes a*b.

I did a quick example in excel to demonstrate:

enter image description here

the minimum b here is 6.9, so the upper right corner of the solution is on p2p3 and the rectangle extends a in horizontal and b in vertical direction to the left and bottom respectively.

The four points of the rectangle are thus

Rect    x       y
R1      0.65    -1.3
R2      0.65    5.6
R3      3.1     5.6
R4      3.1     -1.3

enter image description here

I will have to put this into C++ code and will run a few tests to see if the solution generalizes or if this was just "luck".

I think it should also be possible to substitute a and b in A=a*b by the functions and put it into one linear formula that has to be maximized under the condition that p1p2 is only defined between P1 and P2 etc...

  • I think your problem can be framed as a simple [quadratic programming](http://en.wikipedia.org/wiki/Quadratic_programming) problem. – Edward Doolittle Jun 04 '15 at 03:10