It's also computational ry, And I feel addicted to computational ry recently.
Of course, it will also be used at work, but it generally calls the boost geometry library directly at work.
The last time I wrote the smallest surrounded circle, this time is the smallest surrounded rectangle, which is more complex than the smallest surrounded circle.
The smallest enclosed rectangle may be not necessarily an upright rectangle, or it may be skewed like the same.
The solution is as follows:
1. Find out the polygon convex bag. Here, the convex bag directly calls the system function. For details, refer to here, although it was not written at that time.
2. Use the two adjacent vertex links of the convex hull as an edge of a rectangle.
3. Find the farthest point from the obtained edge on the convex hull, and draw a parallel line through the obtained edge to obtain the second side of the rectangle.
4. Place the top point of the convex hull on the obtained side of the projection, and obtain the two points with the farthest distance between the projection points. The two points are taken as a straight line and used as the other two sides of the rectangle.
5. traverse all adjacent two points of the convex hull and run the new two ~ 4. Use the rectangle with the smallest area as the result.
Normally, the rectangle goes through five random points.
The result is as follows:
The Matlab code is as follows:
Clear all; close all; clc; n = 30; P = rand (n, 2); ind = convhull (P (:, 1), P (:, 2 )); L = length (IND); hull = P (IND, :); % random dot Convex Hull area = inf; for I = 2: l p1 = hull (I-1 ,:); % two vertices on the convex hull P2 = hull (I, :); k1 = (p1 (2)-P2 (2)/(p1 (1)-P2 (1 )); % connects the line of two points, as a side of the rectangle b1 = p1 (2)-K1 * P1 (1); D = ABS (Hull (:, 1) * k1-hull (:, 2) + B1)/SQRT (K1 ^ 2 + 1); % the distance from the point on all the convex packets to the line K1 and B1 [H ind] = max (d ); % get the highest distance, that is, the height, and obtain the coordinates b2 = hull (IND, 2)-K1 * hull (IND, 1); % relative to K1, b1 is opposite to another parallel edge K1, B2; k2 =-1/K1; % to obtain the vertical line slope B = hull (:, 2) -K2 * hull (:, 1); % K2 composed of all vertices over convex hull, B linear system X1 =-(b1-b)/(K1-K2 ); % projection Y1 =-(-B * k1 + B1 * K2)/(K1-K2); x2 =-(b2-b) /(K1-K2); % projection y2 =-(-B * k1 + B2 * K2) of all points on the second side that has been obtained on the convex hull/(K1-K2 ); [junk indmax1] = max (X1); % the maximum and minimum values of the X direction on the first side [junk indmin1] = min (X1); [junk indmax2] = max (X2 ); % The maximum and minimum values projected in the X direction of the second side [junk indmin2] = min (X2); W = SQRT (x1 (indmax1)-x1 (indmin1 )) ^ 2 + (Y1 (indmax1)-y1 (indmin1) ^ 2); % width of the rectangle if area> = H * w % minimum area = H * W; pbar = [x1 (indmax1) y1 (indmax1); % four rectangle Corner Points X2 (indmax2) y2 (indmax2); X2 (indmin2) y2 (indmin2); x1 (indmin1) y1 (indmin1)]; endendpbar (5, :) = pbar (1, :); Hold on; plot (P (:, 1), P (:, 2 ),'. '); plot (pbar (:, 1), pbar (:, 2), 'R') axis equal;
MATLAB exercise program (minimum enclosed rectangle)