[Careercup] 7.5 A line Cut Two squares in half average split two square lines

Source: Internet
Author: User

7.5 Given, squares on a two-dimensional plane, find a line that would cut these the squares in half. Assume the top and the bottom sides of the square run parallel to the x-axis

This problem gives us two squares, so let's ask for a straight line that says the two squares are divided evenly into two parts, assuming that two squares are parallel to the X axis. So we first need to know what kind of straight line to divide a square evenly into two parts, the answer is any one over the center of the square line, then the two square is divided into two parts is connected to the center of the two square straight line. This problem requires us to establish a number of classes, points, lines, and squares, is a good topic for the study of object-oriented programming. Where the point class is represented by two variables of type double, the line class is represented by two point classes, and the square class is represented by the top left and bottom right vertices and side length. The more important two sub-functions extend and cut, where extend is the point from one center to the other, and the outer edge of the other center, whether the inner edge or outer edge is determined by the positive or negative of the third parameter. The cut function is the intersection of the straight lines connecting the two centers and the outer edges of the two squares, and two points to determine a straight line. Perhaps as a reader you will ask, why so troublesome, directly to the two midpoint is not straight line, why so troublesome, so loaded B. Yes, the landlord is to take you to pack B take you to fly, this solution is to prevent others to meet the test instructions line, then the line segment two endpoints just on the side of the square, yes, that is so pull wind, see the code as follows:

classPoint { Public:    Double_x, _y; Point (DoubleXDoubley): _x (x), _y (y) {};};classLine { Public: Point _start, _end; Line (point start, point end): _start (Start), _end (end) {};};/*(left, top) _________ |            |         | |                      Size |_________| (right, down)*/classSquare { Public:    Double_left, _top, _right, _down, _size; Square (DoubleLeftDoubleTopDoubleRightDoubleDownDoublesize): _left (left), _top (top), _right (right), _down (down), _size (size) {}; Point Middle () {returnPoint ((_left + _right) *0.5, (_top + _down) *0.5); }    //Return the point whrere the line connecting Mid1 and Mid2 intercepts the edge of square 1.Point extend (Point mid1, point Mid2,Doublesize) {        DoubleXdir = mid1._x < mid2._x? -1:1; DoubleYdir = mid1._y < mid2._y? -1:1; if(mid1._x = = mid2._x)returnPoint (mid1._x, mid1._y + ydir * Size *0.5); DoubleSlope = (mid1._y-mid2._y)/(Mid1._x-mid2._x); DoubleX1 =0, y1 =0; if(Fabs (slope) = =1) {x1= mid1._x + xdir * Size *0.5; Y1= mid1._y + ydir * Size *0.5; } Else if(Fabs (slope) <1) {x1= mid1._x + xdir * Size *0.5; Y1= Slope * (x1-mid1._x) +mid1._y; } Else{y1= mid1._y + ydir * Size *0.5; X1= (y1-mid1._y)/slope +mid1._x; }        returnPoint (x1, y1); }    //Calculate The line that connecting the MidsLine cut (Square all) {point P1=Extend (Middle (), Other.middle (), _size); Point P2= Extend (middle (), Other.middle (),-1*_size); Point P3=Extend (Other.middle (), Middle (), other._size); Point P4= Extend (Other.middle (), Middle (),-1*other._size); Point Start= P1, end =P1; Vector<Point> points ={p2, p3, P4};  for(inti =0; i < points.size (); ++i) {if(Points[i]._x < start._x | | (points[i]._x = = start._x && points[i]._y <start._y)) {Start=Points[i]; } Else if(points[i]._x > End._x | | (points[i]._x = = end._x && points[i]._y >end._y)) {End=Points[i]; }        }        returnLine (start, end); }};

[Careercup] 7.5 A line Cut Two squares in half average split two square lines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.