20100704, 20:15  #1 
Apr 2010
231_{8} Posts 
Fitting rectangle
Here comes a nice gem for those of you who occasionally want to check "rectangularity" of the polygon given by four points , . (The method I have in mind is quite dimensionindependent.)
The ordering of the points shall matter; points with index distance 2 are interpreted as diagonally opposite. If you get this wrong, you have a shape containing an X or Z, and that shall be considered far from rectangular. The task is to find a measure for unrectangleness in the form of just one nonnegative real value, 0 being equivalent to "perfectly rectangular". A plurality of tests* e.g. for checking angles and lengths separately does not fulfill these requirements. Second, besides the measure, you need to choose a reference entity to compare with, such that if the measure exceeds some given fraction of the reference, the quadrilateral** given by the vertices shall be considered "not rectangular enough". The measure and the reference shall scale equally, so that the test is scaleinvariant. Moreover, the choice of reference shall be such that it bounds angle tolerance and relative length tolerance and is suitable for safe detection of point misordering (crossing sides) regardless of aspect ratio. The basic idea has already been given in the title: Think of as arbitrary, independent points, and construct a "fitting rectangle" with vertices that get as close as possible to the given points respectively, while maintaining perfect rectangleness. (1) What would you "naturally" choose as measure for unrectangleness then? (2) Find midpoint, diagonal directions and diagonal length of the "fitting rectangle". Compare with the given quadrilateral. (3) Choose a suitable reference for the measure. Demonstrate that it bounds errors in angles, relative side lengths, and is suitable for ruling out X shapes even when we are dealing with large aspect ratios. *: Is "plurality of [...]" common language, or Patentese only? **: Tetragon? Quadrangle? What do you prefer? Last fiddled with by ccorn on 20100704 at 20:21 
20100704, 22:48  #2 
Apr 2010
3^{2}·17 Posts 
Note: Using algebra and local optimization is allowed, as are Lagrange multipliers. This should ease dimensionindependent reasoning. The results shall be interpreted geometrically however.

20100708, 03:17  #3 
Apr 2010
10011001_{2} Posts 
Figure
Here is a figure.

20100708, 06:31  #4  
Jul 2006
Calgary
1A9_{16} Posts 
Quote:


20100708, 06:38  #5 
Jul 2006
Calgary
651_{8} Posts 

20100708, 10:23  #6 
"William"
May 2003
New Haven
4504_{8} Posts 
Don't you need to check planarity in addition to right angles when n>2?

20100708, 16:38  #7  
"Richard B. Woods"
Aug 2002
Wisconsin USA
2^{2}·3·641 Posts 
Quote:
In 3D: Suppose three points A,B,C are vertices of a right triangle (Angle ABC = 90 degrees). At vertex A construct a disc that's perpendicular to AB. At vertex C construct a disc that's perpendicular to BC. Consider any point D that is on the intersection line of the two discs. Then DAB = BCD = 90 degrees. If D is not coplanar with A,B,C then angle CDA must be less than 90 degrees. As D slides along the intersection line, angle CDA reaches a maximum (90 degrees), and lengths AD and CD both reach a minimum, exactly when D is on the A,B,C plane. If D is not coplanar with A,B,C then the lines CD and AD are hypotenuses of right triangles with a vertex D' where the discs' intersection line meets plane ABC. Then, DD' has nonzero length, CD must be longer than CD', AD must be longer than AD', and angle CDA must be less than the right angle CD'A. Draw the diagonal AC. Then length AC = the square root of the sum of the squares of AD' and CD'. Since AD > AD' and CD > CD', AC is less than the square root of the sum of the squares of AD and CD, so AC can't be the hypotenuse of a right triangle ADC. I think this holds true in higher dimensions, too. Last fiddled with by cheesehead on 20100708 at 17:20 Reason: Not elegantly minimized, but Oll Korrect now, I think. 

20100708, 18:55  #8  
"William"
May 2003
New Haven
2372_{10} Posts 
Quote:
Hence it is NOT sufficient to check 3 angles, but it is sufficient to check all four angles. This suggests a solution to the puzzle  calculate the pythagorean error at all four vertices, and take a. The largest or b. The sum of the absolute values d. The sum of he squared deviations A useful solution probably includes some kind of scaling to adjust for the pixel granularity. William 

20100708, 20:41  #9 
Apr 2010
3^{2}·17 Posts 
Those are interesting observations. In particular, they show some aspects of why checking a number of angles can be insufficient.
Already in 2D, even checking four angles is not sufficient. With large enough aspect ratio, you can have crossing sides (the X shape) with all four angles arbitrarily close to 90 degrees, and with opposite sides having exactly the same length. Anyway, as I mentioned in the description of the problem, doing several isolated tests (here: checking several angles) shall be considered a general nono. The idea I want to convey is that of a "fitting rectangle". If "fitting" somehow does not trigger enough neurons, think of "regression" instead. If the task were to fit sample data to a function of some given form, you would know what to do. Well, actually, yes, this is the task. I have illustrated the problem with a figure in post #3. The blue rectangle tries to fit the yellow quadrilateral. (That the blue rectangle's sides happen to be parallel to the pixel coordinate axes is not a requirement; I arranged for that in order to enable you to verify most easily that it is a rectangle.) It cannot fit perfectly because the yellow quadrilateral is not rectangular. However, the blue rectangle tries to come close to the yellow quadrilateral. There is a fairly standard criterion to define what is "close", and you know it. Use it, then questions (1), (2), (3) can be addressed in a straightforward manner. Depending on how clever you map the problem to mathematical expressions, you get the puzzle done with 1...3 sheets of paper, with remarkable results. Last fiddled with by ccorn on 20100708 at 21:01 
20100708, 20:56  #10 
Apr 2010
3^{2}×17 Posts 
example X shape
Here is the same set of four given vertices as in post #3, but and are swapped (presumably by error). Watch how the fitting rectangle changes.
Last fiddled with by ccorn on 20100708 at 21:06 
20100708, 21:00  #11 
"Richard B. Woods"
Aug 2002
Wisconsin USA
2^{2}·3·641 Posts 

Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Rectangle Packing  henryzz  Puzzles  4  20120205 16:23 