Give two squares and a two-dimensional plane. Please find a straight line that divides the two squares into two halves. Suppose the top and bottom edges of the square are parallel to the x-axis.

The data square of each square contains three values, the coordinates of the lower left vertex of the square [X,Y] = [square[0],square[1], and the side length of the square[2]. The calculated line passing through two squares will form four intersections. Please return the coordinates of the two ends of the line segment formed by the four intersections (the two endpoints are the two farthest points in the four intersections, and the line segment connected by these two points will pass through the other two intersections). The return format of two endpoint coordinates [X1,Y1] and [X2,Y2] is {X1,Y1,X2,Y2}. If X1= X2, ensure X1 < X2, otherwise ensure Y1 < = Y2.

If multiple straight lines meet the requirements at the same time, select the one with the largest slope for calculation and return (the straight line parallel to the Y axis is regarded as having infinite slope).

Input:

square1 = {-1, -1, 2}

square2 = {0, -1, 2}

Output: {- 1,0,2,0}

Explanation: the straight line y = 0 can divide two squares into two parts with equal area at the same time, and the endpoints of the two returned line segments are [- 1,0] and [2,0]

### Idea:

To bisect a square, the bisector must be the connecting line between the center points of the two squares. Therefore, the following steps are required:

- Calculate the center coordinates of two squares
- Judge whether the slope of the straight line formed by the two points exists
- If it does not exist, it means that the two points are on the same x-axis

- If so, calculate the slope k and the coefficient b
- According to the absolute value of the slope, we can know whether the straight line intersects with the square up and down or left and right.
- If the absolute value of slope is greater than 1, the upper and lower intersect

- If the absolute value of the slope is less than or equal to 1, the left and right intersect. If the slope is equal to 1, it is the diagonal, which intersects both up and down and left and right. It can be calculated on any side

- If the absolute value of slope is greater than 1, the upper and lower intersect

class Solution { public: vector<double> cutSquares(vector<int>& square1, vector<int>& square2) { // A straight line that divides a square into two halves must pass through the center of the square double square1_center_x = square1[0]+(double)square1[2]/2; double square1_center_y = square1[1]+(double)square1[2]/2; double square2_center_x = square2[0]+(double)square2[2]/2; double square2_center_y = square2[1]+(double)square2[2]/2; vector<pair<double,double>> tmp; // The slope is infinite, and the straight line parallel to the y axis is considered separately if(square1_center_x==square2_center_x){ tmp.push_back({square1_center_x,square1[1]}); tmp.push_back({square1_center_x,square1[1]+square1[2]}); tmp.push_back({square1_center_x,square2[1]}); tmp.push_back({square1_center_x,square2[1]+square2[2]}); } else{ double a = (square1_center_y-square2_center_y)/(square1_center_x-square2_center_x); double b = (square1_center_y*square2_center_x-square2_center_y*square1_center_x)/(square2_center_x-square1_center_x); // If the absolute value of slope is greater than 1, the upper and lower intersect if(a>=1||a<=-1) { double cross_x = (square1[1]-b)/a; tmp.push_back({cross_x,square1[1]}); cross_x = (square1[1]+square1[2]-b)/a; tmp.push_back({cross_x,square1[1]+square1[2]}); cross_x = (square2[1]-b)/a; tmp.push_back({cross_x,square2[1]}); cross_x = (square2[1]+square2[2]-b)/a; tmp.push_back({cross_x,square2[1]+square2[2]}); } // If the absolute value of the slope is less than or equal to 1, the left and right intersect. If the slope is equal to 1, it is the diagonal, which intersects both up and down and left and right. It can be calculated on any side else{ double cross_y = a*square1[0]+b; tmp.push_back({square1[0],cross_y}); cross_y = a*(square1[0]+square1[2])+b; tmp.push_back({square1[0]+square1[2],cross_y}); cross_y = a*(square2[0])+b; tmp.push_back({square2[0],cross_y}); cross_y = a*(square2[0]+square2[2])+b; tmp.push_back({square2[0]+square2[2],cross_y}); } } sort(tmp.begin(),tmp.end());//This place card for a while, give up writing by yourself and use ready-made ones. return {tmp.front().first,tmp.front().second,tmp.back().first,tmp.back().second}; } };